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)
一個LEFT JOIN ON ORDERHEADER和ORDERDETAILS並在SELECT超過GROUP申請案件或IIF BY – maSTAShuFu
你有什麼問題?錯誤的結果或SQL錯誤?預期的結果或錯誤信息是什麼? –
我得到「消息8120,級別16,狀態1,行1列'Mck_hvs.orderdetails.orderno」在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中。 –