我一直在问问题,以获取公司所要求的SQL查询的最终结果。我终于得到了最初要求我提供的最终结果,但他们想要更多。
我的公司希望我让查询仅显示变动(已售,已购买或已调整)的值。我不知道如何进行这项工作或在哪里看。
SET ARITHABORT OFF
SET ANSI_WARNINGS OFF
SELECT
[UPC]=t1.F01,
COALESCE((SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and (X.F1034 = 3) AND X.F01 = T1.F01 Group by X.F01,X.F1034),0) AS [Total QTY Sold],
COALESCE((SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and (X.F1034 = 3) AND X.F01 = T1.F01 Group by X.F01,X.F1034),0) AS [Total Amount Sold],
COALESCE((SELECT SUM (COALESCE (X.F1301, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and (X.F1034 = 3) AND X.F01 = T1.F01 Group by X.F01,X.F1034),0) AS [Total Cost Sold],
[Margin]=COALESCE(((1-(COALESCE((SELECT SUM (COALESCE (X.F1301, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and (X.F1034 = 3) AND X.F01 = T1.F01 Group by X.F01,X.F1034),0)/COALESCE((SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and (X.F1034 = 3) AND X.F01 = T1.F01 Group by X.F01,X.F1034),0)))*100),0),
COALESCE((SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and X.F1034 = 8201 AND X.F01 = T1.F01 Group by X.F01,X.F1034),0) AS [QTY Purchased],
COALESCE((SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and X.F1034 = 8201 AND X.F01 = T1.F01 group by X.F01,X.F1034),0) AS [Amount Purchased],
COALESCE((SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and X.F1034 = 8801 AND X.F01 = T1.F01 Group by X.F01,X.F1034),0) AS [QTY Adjusted],
COALESCE((SELECT SUM (COALESCE (X.F65, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2011-10-1' and X.F254<='2011-11-11') and X.F1034 = 8801 AND X.F01 = T1.F01 Group by X.F01,X.F1034),0) AS [Amount Adjusted],
((COALESCE((SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2010-1-1' and X.F254<='2011-11-11') and X.F1034 = 8201 AND X.F01 = T1.F01 Group by X.F01,X.F1034),0)) - (COALESCE((SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2010-1-1' and X.F254<='2011-11-11') and X.F1034 = 3 AND X.F01 = T1.F01 Group by X.F01,X.F1034),0)) + (COALESCE((SELECT SUM (COALESCE (X.F64, 0)) FROM [STORESQL].[dbo].[RPT_ITM_D] X WHERE (X.F254>='2010-1-1' and X.F254<='2011-11-11') and X.F1034 = 8801 AND X.F01 = T1.F01 Group by X.F01,X.F1034),0))) AS [Current Inventory]
FROM [STORESQL].[dbo].[COST_TAB] t1
where F27='119828' and (F90 is null or F90=1)
group by T1.F01
order by t1.[F01]
结果的结果如下(缩短标题以使其适合):
UPC QTY_S AMT_S Cost_S Margin QTY_P AMT_P QTY_A AMT_A Inv
123 1 61.19 54 11.75 0 0 0 0 0
456 0 0 0 0 0 0 0 0 5
789 39 38.61 19.5 49.49 2600 1300 0 0 2559
012 0 0 0 0 0 0 0 0 0
我需要的结果如下:
UPC QTY_S AMT_S Cost_S Margin QTY_P AMT_P QTY_A AMT_A Inv
123 1 61.19 54 11.75 0 0 0 0 0
789 39 38.61 19.5 49.49 2600 1300 0 0 2559
您可能已经注意到,无论UPC和Inv的价值如何,我都需要查询以仅导致移动项。
先感谢您
最佳答案
做这个:
SELECT
*
FROM (
--your query here (remove order by)
) dt
WHERE dt.QTY_S!=0
ORDER BY dt.UPC