0

我正在使用 SQL 上的 Join 和 CASE 查询编写查询,但它没有给出预期的结果。

如果我尝试运行 count 命令,它会单独运行,如果我尝试另一个命令,它会单独运行,但如果我将它们结合起来,它们就不能正常工作。

在这种情况下,如果我要删除COUNT(CCD_METHOD_OPTION_DESC.OPTION_ID),它可以工作,但按如下方式添加它是行不通的。

SELECT COUNT(CCD_METHOD_OPTION_DESC.OPTION_ID) CASE
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '1' then 'Option1'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '2' then 'Option2'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '3' then 'Option3'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '4' then 'Option4'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '5' then 'Option5'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '6' then 'Option6'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '7' then 'Option7'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '8' then 'Option8'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '9' then 'Option9'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '0' then 'Option0'
  else 'NoNumber'
  END AS Option_ID
  , METHOD_ID, CCD_METHOD_OPTION_DESC.CLIENT_ID, CCD_CALLS.DNIS, CCD_CALLS.ACCOUNT_NO
  , CCD_CALLS.LANG, CCD_CALLS.CCD_LAST_METHOD, CCD_CALLS.PERSON_ID
  , CCD_CALLS.ROUTE_CODE, CCD_CALLS.TRANSFER_TO, CCD_CALLS.CCD_END_CODE
  , CCD_CALLS.STARTDATETIME, CONVERT(char(10),CCD_CALLS.STARTDATETIME,101) as Date
  , CONVERT(char(20),CCD_CALLS.STARTDATETIME,108) as Interval_Time
  , CCD_CALLS.ENDDATETIME
from CCD_METHOD_OPTION_DESC
RIGHT JOIN CCD_CALLS ON CCD_CALLS.CLIENT_ID = CD_METHOD_OPTION_DESC.CLIENT_ID)

本质上,我试图计算每个选项的数量,例如每个选项 0-9 与其他信息一起被选中的次数

4

2 回答 2

0

使用 WITH 子句的解决方案...

WITH count_options (count, OPTION_ID)  
AS  
(  
SELECT COUNT(*) as count
       CCD_METHOD_OPTION_DESC.OPTION_ID as OPTION_ID
  from CCD_METHOD_OPTION_DESC
 group by CCD_METHOD_OPTION_DESC.OPTION_ID
)  


SELECT count_options.COUNT, CASE
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '1' then 'Option1'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '2' then 'Option2'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '3' then 'Option3'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '4' then 'Option4'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '5' then 'Option5'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '6' then 'Option6'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '7' then 'Option7'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '8' then 'Option8'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '9' then 'Option9'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '0' then 'Option0'
  else 'NoNumber'
  END AS Option_ID
  , METHOD_ID, CCD_METHOD_OPTION_DESC.CLIENT_ID, CCD_CALLS.DNIS, CCD_CALLS.ACCOUNT_NO
  , CCD_CALLS.LANG, CCD_CALLS.CCD_LAST_METHOD, CCD_CALLS.PERSON_ID
  , CCD_CALLS.ROUTE_CODE, CCD_CALLS.TRANSFER_TO, CCD_CALLS.CCD_END_CODE
  , CCD_CALLS.STARTDATETIME, CONVERT(char(10),CCD_CALLS.STARTDATETIME,101) as Date
  , CONVERT(char(20),CCD_CALLS.STARTDATETIME,108) as Interval_Time
  , CCD_CALLS.ENDDATETIME
from CCD_METHOD_OPTION_DESC
RIGHT JOIN CCD_CALLS ON CCD_CALLS.CLIENT_ID = CD_METHOD_OPTION_DESC.CLIENT_ID
INNER JOIN count_options ON count_options.OPTION_ID = CCD_METHOD_OPTION_DESC.OPTION_ID;
于 2019-07-29T04:31:10.460 回答
0

尝试这个 :

SELECT COUNT(CCD_METHOD_OPTION_DESC.OPTION_ID) over(partition by CASE
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '1' then 'Option1'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '2' then 'Option2'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '3' then 'Option3'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '4' then 'Option4'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '5' then 'Option5'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '6' then 'Option6'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '7' then 'Option7'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '8' then 'Option8'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '9' then 'Option9'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '0' then 'Option0'
  else 'NoNumber'
  END ) AS Option_ID_ct
  ,CASE
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '1' then 'Option1'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '2' then 'Option2'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '3' then 'Option3'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '4' then 'Option4'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '5' then 'Option5'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '6' then 'Option6'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '7' then 'Option7'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '8' then 'Option8'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '9' then 'Option9'
  when CCD_METHOD_OPTION_DESC.OPTION_ID = '0' then 'Option0'
  else 'NoNumber'
  END AS Option_ID
  , METHOD_ID, CCD_METHOD_OPTION_DESC.CLIENT_ID, CCD_CALLS.DNIS, CCD_CALLS.ACCOUNT_NO
  , CCD_CALLS.LANG, CCD_CALLS.CCD_LAST_METHOD, CCD_CALLS.PERSON_ID
  , CCD_CALLS.ROUTE_CODE, CCD_CALLS.TRANSFER_TO, CCD_CALLS.CCD_END_CODE
  , CCD_CALLS.STARTDATETIME, CONVERT(char(10),CCD_CALLS.STARTDATETIME,101) as Date
  , CONVERT(char(20),CCD_CALLS.STARTDATETIME,108) as Interval_Time
  , CCD_CALLS.ENDDATETIME
from CCD_METHOD_OPTION_DESC
RIGHT JOIN CCD_CALLS ON CCD_CALLS.CLIENT_ID = CD_METHOD_OPTION_DESC.CLIENT_ID
于 2019-07-29T03:06:53.480 回答