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]

結果のスピネットは次のとおりです(フィットするようにヘッダーを短くしました)。

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 に答える