Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL中大小写表达式中的子查询_Postgresql - Fatal编程技术网

PostgreSQL中大小写表达式中的子查询

PostgreSQL中大小写表达式中的子查询,postgresql,Postgresql,我是新来的,在PostgreSQL中并不是很出名 我有两个表(每月汇总的表用法和显示历史所有客户催款水平和催款日期的表customer_dunnings)。我需要向过去3个月内违反_用法的客户展示(我可以解决这个问题)。但我还需要显示客户是否有催款单级别,如果是,他当月的催款单级别 在表usages中,我有一列datetime,它的日期类似于“2019-09-01”,因为它总是显示每月的第一天。在表customer_dunnings中,我有一列dunning_date,如“2019-09-15”

我是新来的,在PostgreSQL中并不是很出名

我有两个表(每月汇总的表用法和显示历史所有客户催款水平和催款日期的表customer_dunnings)。我需要向过去3个月内违反_用法的客户展示(我可以解决这个问题)。但我还需要显示客户是否有催款单级别,如果是,他当月的催款单级别

在表usages中,我有一列datetime,它的日期类似于“2019-09-01”,因为它总是显示每月的第一天。在表customer_dunnings中,我有一列dunning_date,如“2019-09-15”所示,显示客户达到催款水平的确切日期以及催款水平

我想显示dunning_级别,以防客户在usages.datetime上有dunning_级别

我想用一个CASE表达式我就能做到

这是我的问题。希望你能帮我

SELECT  
u.id,  
u.datetime,  
to_char(u.datetime, 'YYYYMM') "month",  
u.customer_id,  
u.customer_number CID,   
case when u.customer_number in (select c.customer_number 
                                from v_reporting_customer_dunnings d 
                                left join v_reporting_cids c on c.customer_id=d.customer
                                left join usages u on u.customer_number=c.customer_number and to_char(u.datetime, 'YYYYMM')=to_char(d.dunning_date, 'YYYYMM')) then 'yes' else null end as dunning  
FROM usages u

表上客户使用示例:

ID          Datetime    Month   Customer_ID   CID        Dunning  
11306415573 2019-09-01  201909  2039145295    80035006   yes
8208810439  2018-12-01  201812  2039145295    80035006   yes
7926569969  2018-11-01  201811  2039145295    80035006   yes
7654669868  2018-10-01  201810  2039145295    80035006   yes
表v_报告_客户_dunnings上的客户示例:

Customer    INVOICE     DUNNING_LV  INVOICE_DT  DUNNING_DT  
2039145295  90354914    LEVEL 10    2018-09-30  2018-11-16  
2039145295  90226540    LEVEL 10    2018-04-30  2018-06-08  
表v_报告_CID中的客户示例:

Customer_ID Customer_Number    
2039145295  80035006      
2039145295  80035006      
该客户的催款级别为2倍。1x它与表格用法中的月份相同。在这种情况下,我的查询中应该有一列,给出匹配年份月份的催款单级别

我期望/想要的是:

ID          Datetime    Month   Customer_ID   CID       Dunning   Dunning_Level
11306415573 2019-09-01  201909  2039145295    80035006  NULL      NULL
8208810439  2018-12-01  201812  2039145295    80035006  NULL      NULL
7926569969  2018-11-01  201811  2039145295    80035006  yes       LEVEL 10
7654669868  2018-10-01  201810  2039145295    80035006  NULL      NULL

请添加一些最小化的示例数据和预期输出!你的问题是什么?你得到了错误的答案吗?错误消息?请提供详细信息。没有错误消息,只是不是我所期望的。对于最后一个例子,当使用子查询时,我想知道usages.datetime客户是否有催款级别。实际上,当客户在表v_reporting_customer_dunnings中时,他总是这样表示“是”。它不检查日期是否匹配。请阅读“帮助”部分。以此作为你问题的模板,大大提高了你获得满意答案的机会。在这种情况下,尤其困难,因为您无法描述表,并且提供的数据与查询中使用的列不对应。在用户表中,customer_number和customer_id(均已使用)之间的区别是什么。也没有为表v_报告CID提供说明或数据。并提供您从usages表中选择的几列,其中没有数据。请提供至少所有引用列的示例数据。@Belayer感谢您的回复和时间。我已经更正了我的文本,我希望现在它更清楚。