我正在使用此查询进行组合和计数。有时它有效,有时它不起作用。个别查询有时彼此不相等。这里有什么问题吗?有没有更好/更简单的方法来做到这一点?提前谢谢。
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