2017-06-14 78 views
0

對我的查詢有一些小問題。我正在努力爲項目獲得款項。我知道我可能錯過了一些超級簡單的東西,但我陷入了困境。如果有人可以看看,並給我一個關於如何解決這個問題的建議。總計函數/分組條款

select 
    (select count(distinct(o1.orderno)) 
     from mck_hvs.orderheader o1 with(nolock) 
     where o1.orderno = od.orderno and o1.refrigerate = 'Y' 
     ) as TotalColdOrders, 

    (select count(distinct(o2.orderno)) 
     from mck_hvs.orderdetails o2 with(nolock) 
     where o2.orderno = od.orderno and o2.drugclass 
     not in ('null', 'Rx') 
     ) as ControlledOrders, 

    (select count(distinct(o3.orderno)) 
     from mck_hvs.orderheader o3 with(nolock) 
     where o3.orderno = od.orderno and o3.pucksideinorder = 'Y' and 
     o3.totesideinorder = 'N' and o3.numitems > 4 
     ) as RobotOrders, 

    (select count(distinct(o4.orderno)) 
     from mck_hvs.orderheader o4 with(nolock) 
     where o4.orderno = od.orderno and o4.pucksideinorder = 'Y' and 
     o4.totesideinorder = 'Y' 
     ) as ComboOrders, 

    (select count(distinct(o5.rxnum)) 
     from mck_hvs.orderdetails o5 with(nolock) 
     where o5.refrigerate = 'Y' 
     ) as TotalColdScripts, 

    (select count(distinct(o6.rxnum)) 
     from mck_hvs.orderdetails o6 with(nolock) 
     where o6.orderno = od.orderno and o6.drugclass 
     not in ('null', 'RX') 
     ) as ControlledScripts, 

    (select sum(o7.numscripts) 
     from mck_hvs.orderheader o7 with(nolock) 
     where o7.orderno = od.orderno and o7.pucksideinorder = 'Y' and 
     o7.totesideinorder = 'N' and o7.numitems > 4 
     ) as RobotScripts, 

    (select sum(o8.numscripts) 
     from mck_hvs.orderheader o8 with(nolock) 
     where o8.orderno = od.orderno and o8.pucksideinorder = 'Y' and 
     o8.totesideinorder = 'Y' 
     ) as ComboOrderScripts, 

    count(distinct(od.orderno)) as TotalOrders, 
    count(distinct(od.rxnum)) as TotalScripts 
from 
    mck_hvs.orderdetails od with(nolock) 
+0

一個LEFT JOIN ON ORDERHEADER和ORDERDETAILS並在SELECT超過GROUP申請案件或IIF BY – maSTAShuFu

+0

你有什麼問題?錯誤的結果或SQL錯誤?預期的結果或錯誤信息是什麼? –

+0

我得到「消息8120,級別16,狀態1,行1列'Mck_hvs.orderdetails.orderno」在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中。 –

回答

0

你應該試試這個:

SELECT 
    od.orderno, 
    COUNT(DISTINCT 
     CASE WHEN oh.refrigerate = 'Y' THEN od.orderno END 
    ) AS totalcoldorders, 
    COUNT(DISTINCT 
     CASE WHEN od.drugclass NOT IN ('null', 'Rx') THEN od.orderno END 
    ) AS controlledorders, 
    COUNT(DISTINCT 
     CASE 
      WHEN oh.pucksideinorder = 'Y' 
       AND oh.totesideinorder = 'N' AND oh.numitems > 4 
      THEN od.orderno 
     END 
    ) AS robotorders, 
    COUNT(DISTINCT 
     CASE 
      WHEN oh.pucksideinorder = 'Y' AND oh.totesideinorder = 'Y' 
      THEN oh.orderno 
     END 
    ) AS comboorders, 
    COUNT(DISTINCT 
     CASE WHEN od.refrigerate = 'Y' THEN od.rxnum END 
    ) AS totalcoldscripts, 
    COUNT(DISTINCT 
     CASE WHEN od.drugclass NOT IN ('null', 'RX') THEN od.rxnum END 
    ) AS controlledscripts, 
    SUM(
     CASE 
      WHEN oh.pucksideinorder = 'Y' 
       AND oh.totesideinorder = 'N' AND oh.numitems > 4 
      THEN oh.numscripts 
     END 
    ) AS robotscripts, 
    SUM(
     CASE 
      WHEN oh.pucksideinorder = 'Y' AND oh.totesideinorder = 'Y' 
      THEN oh.numscripts 
     END 
    ) AS comboorderscripts, 
    (
     SELECT COUNT(DISTINCT orderno) 
     FROM mck_hvs.orderdetails WITH (nolock) 
    ) AS totalorders, 
    (
     SELECT COUNT(DISTINCT rxnum) 
     FROM mck_hvs.orderdetails WITH (nolock) 
    ) AS totalscripts 
FROM mck_hvs.orderdetails od WITH (nolock) 
LEFT JOIN mck_hvs.orderheader oh WITH (nolock) 
ON oh.orderno = od.orderno 
GROUP BY od.orderno 
ORDER BY od.orderno;