このクエリを使用して、組み合わせとカウントを行っています。うまくいくこともあれば、うまくいかないこともあります。個々のクエリが互いに等しくない場合があります。ここで何か問題がありますか?これを行うためのより良い/より簡単な方法はありますか?前もって感謝します。
SELECT CONVERT(VarChar(8), End_Date, 112)as End_Date,count(*) as Qty, 'LPFG' as Wc_Code
FROM
(
(SELECT dimdbmc.dbo.t_Allocation_Event.Demand_Order_Nbr, Demand_Order_Line_Nbr,
CONVERT(VARCHAR(8), min(dimdbmc.dbo.t_Allocation_Event.Row_Insert_Datetime), 112) as End_Date
FROM dimdbmc.dbo.t_Allocation_Event INNER JOIN dimdbmc.dbo.t_Demand_Order ON (dimdbmc.dbo.t_Allocation_Event.MMM_Facility_Code = dimdbmc.dbo.t_Demand_Order.MMM_Facility_Code) AND (dimdbmc.dbo.t_Allocation_Event.Demand_Order_Nbr = dimdbmc.dbo.t_Demand_Order.Demand_Order_Nbr)
WHERE (((dimdbmc.dbo.t_Allocation_Event.MMM_Facility_Code)='MC')
AND ((dimdbmc.dbo.t_Allocation_Event.Event_Type_Code)='Allocate')
AND dimdbmc.dbo.t_demand_order.demand_order_type_code IN ( 'COMS-SMPNO')
AND ((dimdbmc.dbo.t_Allocation_Event.Row_Insert_Datetime)>= '8/01/2012'))
Group by
dimdbmc.dbo.t_Allocation_Event.Demand_Order_Nbr, dimdbmc.dbo.t_Allocation_Event.Demand_Order_Line_Nbr)
UNION ALL
(SELECT dimdbmc.dbo.t_Allocation_Event.Demand_Order_Nbr, Demand_Order_Line_Nbr,
CONVERT(VARCHAR(8), min(dimdbmc.dbo.t_Allocation_Event.Row_Insert_Datetime), 112) as End_Date
FROM dimdbmc.dbo.t_Allocation_Event INNER JOIN dimdbmc.dbo.t_Demand_Order ON (dimdbmc.dbo.t_Allocation_Event.MMM_Facility_Code = dimdbmc.dbo.t_Demand_Order.MMM_Facility_Code) AND (dimdbmc.dbo.t_Allocation_Event.Demand_Order_Nbr = dimdbmc.dbo.t_Demand_Order.Demand_Order_Nbr)
WHERE (((dimdbmc.dbo.t_Allocation_Event.MMM_Facility_Code)='MC')
AND ((dimdbmc.dbo.t_Allocation_Event.Demand_Order_Nbr) Is Not Null)
AND ((dimdbmc.dbo.t_Allocation_Event.Event_Type_Code)='Allocate')
AND substring(dimdbmc.dbo.t_Demand_Order.Demand_Order_Nbr, 8, 1) <>'_'
AND dimdbmc.dbo.t_demand_order.demand_order_type_code IN ( 'COMS-CUST','COMS- GOVT','COMS-INTL')
AND ((dimdbmc.dbo.t_Demand_Order.Charge_To_Customer_Nbr)='WHP4101')
AND ((dimdbmc.dbo.t_Allocation_Event.Row_Insert_Datetime)>= '8/01/2012'))
Group by
dimdbmc.dbo.t_Allocation_Event.Demand_Order_Nbr,dimdbmc.dbo.t_Allocation_Event.Demand_Order_Line_Nbr
)
)as a
group by End_Date
order by End_Date