2

我一直在提问以获得我公司要求的 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]

结果的spinet如下(缩短标题以使其适合):

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 中的值如何,我都需要查询以仅生成有移动的项目。

先感谢您

4

2 回答 2

8

做这个:

SELECT
    *
    FROM (
            --your query here (remove order by)
         ) dt
    WHERE dt.QTY_S!=0
    ORDER BY dt.UPC
于 2011-11-11T16:19:15.173 回答
1

基本上你需要得到你的结果,然后应用你的过滤器。

; with cte as (
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
)
Select * from cte 
where [Total Amount Sold]> 0 or [QTY Purchased] > 0 or [QTY Adjusted] > 0
order by UPC
于 2011-11-11T18:45:54.397 回答