MAX(Case When)多个Where语句

时间:2018-07-24 04:45:14

标签: sql-server

我试图在下面的语句中添加一个辅助条件,以便仅在“应用程序计数”列中显示非空的记录。我假设自从创建别名以来,这就是无法正常工作的原因,但是即使我输入了实际名称,我仍然会看到所有Null。

我要去哪里错了?

这是可以工作的代码,但在“列应用程序计数”和“创建日期”中显示了大量的空字段,在每一行中填充名称。

SELECT Row_number() over(order by data_point_group_key) 'data_id', 
       report_id, section_group, section_name, data_point_group_key,
       MAX(CASE WHEN data_point = 'Application Count' THEN data_point_value END) as 'ApplicationCount',
       MAX(CASE WHEN data_point = 'Create Date' THEN data_point_value END) as 'CreateDate',
       MAX(CASE WHEN data_point = 'Name' THEN data_point_value END) as 'SolutionName'
FROM tblReportData 
WHERE report_id = (select max(report_id) from tblReportData)
group by report_id,section_group, section_name,data_point_group_key

当前输出:

+----------------------+------------------+------------+--------------+
| data_point_group_key | ApplicationCount | CreateDate | SolutionName |
+----------------------+------------------+------------+--------------+
| 1234564646464        |      NULL        |   NULL     | Sol Name     |
+----------------------+------------------+------------+--------------+
| 1234564646464        |      8           |   01-01-18 | New Name     |
+----------------------+------------------+------------+--------------+
| 132512512            |      NULL        |   NULL     | Old Name     |
+----------------------+------------------+------------+--------------+

在这里添加第二个Where过滤器,并且仍然返回所有Null

SELECT Row_number() over(order by data_point_group_key) 'data_id', 
       report_id, section_group, section_name, data_point_group_key,
       MAX(CASE WHEN data_point = 'Application Count' THEN data_point_value END)as 'ApplicationCount',
       MAX(CASE WHEN data_point = 'Create Date' THEN data_point_value END)as 'CreateDate',
       MAX(CASE WHEN data_point = 'Name' THEN data_point_value END)as 'SolutionName'
FROM tblReportData 
WHERE report_id = (select max(report_id) from tblReportData) AND ('ApplicationCount' IS NOT NULL OR 'CreateDate' IS NOT NULL)
group by report_id,section_group, section_name,data_point_group_key

希望的输出:

+----------------------+------------------+------------+--------------+
| data_point_group_key | ApplicationCount | CreateDate | SolutionName |
+----------------------+------------------+------------+--------------+
| 1234564646464        |      8           |   01-01-18 | New Name     |
+----------------------+------------------+------------+--------------+

当它返回值时我没有收到错误,因此我认为问题是由于别名而我选择了错误的字段。

2 个答案:

答案 0 :(得分:1)

如果我正确地理解了您的请求,那么您只想忽略结果中具有NULL的行。您可以将其设为CTE:

WITH REPORT_CTE AS (
    SELECT Row_number() over(order by data_point_group_key) 'data_id', 
           report_id, section_group, section_name, data_point_group_key,
           MAX(CASE WHEN data_point = 'Application Count' THEN data_point_value END)as 'ApplicationCount',
           MAX(CASE WHEN data_point = 'Create Date' THEN data_point_value END)as 'CreateDate',
           MAX(CASE WHEN data_point = 'Name' THEN data_point_value END)as 'SolutionName'
    FROM tblReportData 
    WHERE report_id = (select max(report_id) from tblReportData)
    group by report_id,section_group, section_name,data_point_group_key
)
SELECT * FROM REPORT_CTE
WHERE ApplicationCount is not null and CreateDate is not null and SolutionName is not null

另一种选择是使用内部联接将tblReportData包含三遍,但是由于您使用的分组依据列不是问题,因此我不确定如何表述或您的分组是否如此您可以使用CASE和MAX来选择字段。

另一种选择可能是使用pivot

答案 1 :(得分:0)

更新

虽然我的观察是正确的,但提出的解决方案却不正确。 您将SolutionName列设为空是因为where子句已经过滤掉了所有data_point'Application Count'以外的'Create Date'值。

一个简单的解决方法是将原始查询包装在一个通用表表达式中,然后从中进行选择,并使用基于其列的where子句:

;WITH cte AS
(
    SELECT Row_number() over(order by data_point_group_key) 'data_id', 
           report_id, section_group, section_name, data_point_group_key,
           MAX(CASE WHEN data_point = 'Application Count' THEN data_point_value END) as 'ApplicationCount',
           MAX(CASE WHEN data_point = 'Create Date' THEN data_point_value END) as 'CreateDate',
           MAX(CASE WHEN data_point = 'Name' THEN data_point_value END) as 'SolutionName'
    FROM tblReportData 
    WHERE report_id = (select max(report_id) from tblReportData) 
    AND (data_point = 'Application Count' OR data_point = 'Create Date')
    GROUP BY report_id,section_group, section_name,data_point_group_key
)

SELECT *
FROM cte
WHERE ApplicationCount IS NOT NULL
OR CreateDate IS NOT NULL

第一版

'ApplicationCount''CreateDate'是字符串文字,不能为null

您需要检查data_point列是否包含以下值:

SELECT Row_number() over(order by data_point_group_key) 'data_id', 
       report_id, section_group, section_name, data_point_group_key,
       MAX(CASE WHEN data_point = 'Application Count' THEN data_point_value END)as 'ApplicationCount',
       MAX(CASE WHEN data_point = 'Create Date' THEN data_point_value END)as 'CreateDate',
       MAX(CASE WHEN data_point = 'Name' THEN data_point_value END)as 'SolutionName'
FROM tblReportData 
WHERE report_id = (select max(report_id) from tblReportData) 
AND (data_point = 'Application Count' OR data_point = 'Create Date')
group by report_id,section_group, section_name,data_point_group_key