简体   繁体   English

使用Cross Apply的SQL查询有条件地求和

[英]SQL Query using Cross Apply to get Sum Conditionally

Output to be produced 将产生的产出

在此处输入图片说明

using this as reference but now with different scenario SQL Server query : get the sum conditionally 使用此作为参考,但现在使用不同的方案SQL Server查询:有条件地获取总和

explanation: 说明:

Item, Sales, and remarks columns are given column from a database, New Sales column is a formulated column where in, it is getting the sum of items with the same keyword remarks except the default n/a remarks. “项目”,“销售”和“备注”列是从数据库中获得的列,“新销售”列是一个公式化的列,其中,除默认n / a备注外,它使用相同的关键字备注获取项目的总和。

(regardless the remarks is not fully identical, at least there's a common similarity like what is on the image above - item 5 has "new" in it, still it sums up with item 6 because of their similar keyword found "small") (无论备注是否完全相同,至少存在上图所示的共同点-第5项中包含“ new”,但由于发现了类似的关键字“ small”,因此仍与第6项相加)

code used 使用的代码

FIRST OPTION- using partition - This doesn't work because when the remarks is not identical to each other it will not get the sum properly (for item5 and item6) FIRST OPTION-使用分区 -这不起作用,因为当备注彼此不同时,它将无法正确获取总和(对于item5和item6)

 CASE
     WHEN ([remarks] not  like '%big%') AND ([remarks] not  like '%PAENGS%') 


     THEN sales 
     ELSE SUM(sales) OVER(PARTITION BY [remarks])
     END as 'New Sales'

SECOND OPTION -using Cross Apply - So it leave me to this, but I was lost as it is not getting the desired output. 第二种选择-使用交叉应用 -因此,我不得不这样做,但是我迷失了方向,因为它没有获得所需的输出。

 CROSS APPLY
     SELECT
        d.*, 
        NewSales = 
        CASE
        WHEN ([remarks] not like '%big%') or ([remarks] not like '%small%')
        THEN Sales 
        ELSE x.NewSales
        END


        FROM #MSRSuperFinal3  d
        CROSS APPLY(SELECT NewSales = SUM(Sales)
                    FROM #MSRSuperFinal3 
                    WHERE ([remarks] like  '%big%') or ([remarks] like  '%small%')
        )x

Any help will be highly appreciated 任何帮助将不胜感激

Using CROSS APPLY 使用CROSS APPLY

SELECT *
FROM temp t
CROSS APPLY(
    SELECT SUM(sales)
    FROM temp
    WHERE
        remarks LIKE '%' + t.remarks + '%'
        OR t.remarks LIKE '%' + remarks + '%'
)x(NewSales)
WHERE remarks <> 'n/a'

UNION ALL

SELECT *, 
    NewSales = sales
FROM temp
WHERE remarks = 'n/a'
ORDER BY item

Based on your comment, this should be your final query: 根据您的评论,这应该是您的最终查询:

SELECT * 
FROM #MSRSuperFinal3 t
CROSS APPLY( 
    SELECT 
        SUM(CurrentMonth)
    FROM #MSRSuperFinal3 
    WHERE 
        t.misc LIKE '%' + misc + '%'
        OR misc LIKE '%' + t.misc + '%'
)x(NewSales) 
WHERE 
    ([misc] LIKE '%BIGKAHUNA%') 
    or ([misc] LIKE '%PAENGS%') 

UNION ALL 
SELECT *, 
    NewSales = CurrentMonth 
FROM #MSRSuperFinal3 
WHERE 
    ([misc] not like '%BIGKAHUNA%')
    AND ([misc] not like '%PAENGS%')
    AND ([misc] not like '%ROBINSONS%')
ORDER BY location, name 

Try Left Join clause instead of Cross Apply: 尝试使用Left Join子句而不是Cross Apply:

SELECT a.item,
       a.sales,
       a.remarks,
       CASE
           WHEN a.remarks = 'n/a' 
           THEN a.sales
           ELSE SUM( b.sales )
       END AS [New Sales]
FROM query_using_cross_apply_to_get_sum_conditionally a
LEFT JOIN query_using_cross_apply_to_get_sum_conditionally b 
      ON(a.remarks LIKE '%' + b.remarks + '%'
         AND b.remarks != 'n/a')
GROUP BY a.item, a.sales, a.remarks;
CASE
    WHEN (a.[remarks] <> 'n/a') THEN a.sales 
    ELSE 
    (
        SELECT SUM(b.sales) 
        FROM #MSRSuperFinal3 b
        WHERE b.[remark] LIKE '%' + a.[remarks] + '%'
    )
END as 'New Sales'

Note that i just added a table alias so that the query knows which remark to use where. 请注意,我只是添加了一个表别名,以便查询知道在哪里使用哪个备注。 Otherwise something that might be faster is doing a Common table expression and firstly doing a sum for sales that contain each other, so for each unique remark sum all sales that contain in, and then in your last select you cna merely join on your common table expression table. 否则,可能会更快一些:创建一个通用表表达式,首先对包含彼此的销售额做一个总和,因此对于每个唯一备注总和,包含其中的所有销售额,然后在最后一次选择中,您只能加入您的通用表表达式表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM