Skip to content
Advertisement

Different conditions When Sum in same query

I have 6 querys that do the same but have differents conditions in WHERE statements. Is it possible to get all the criterias for each query and get 6 differents results? I think that I can use UNION ALL but the queries are so big. Below are the queries:

query 1(Dv1)

SELECT
  *Dv1* AS Scaled,
   SUM(h.weight_x1000000/1000000) AS Total,
   SUM(CASE when h.windows >0 then h.weight_x1000000/1000000 end) AS Ra,
   SUM(case when h.doors > 0 then h.weight_x1000000/1000000 end) AS Re

FROM store h INNER JOIN 
     sales p ON
     p.id = h.id 

WHERE h.date Between 2018 AND 2020
    AND p.status = 'O';

query 2(Dv2)

SELECT
   *Dv2* AS Scaled,
   SUM(h.weight_x1000000/1000000) AS Total,
   SUM(CASE when h.windows >0 then h.weight_x1000000/1000000 end) AS Ra,
   SUM(case when h.doors > 0 then h.weight_x1000000/1000000 end) AS Re

FROM store h INNER JOIN 
     sales p ON
     p.id = h.id 

WHERE h.date Between 2018 AND 2020
   AND p.status = 'O'
   AND h.location = 'AM';

query 3(Dv3)

SELECT
   *Dv3* AS Scaled,
   SUM(h.weight_x1000000/1000000) AS Total,
   SUM(CASE when h.windows >0 then h.weight_x1000000/1000000 end) AS Ra,
   SUM(case when h.doors > 0 then h.weight_x1000000/1000000 end) AS Re

FROM store h INNER JOIN 
   sales p ON
   p.id = h.id 

WHERE h.date Between 2018 AND 2020
   AND p.status = 'O'
   AND h.location = 'AM'
   AND h.reg > 2;

. . . query 6(Dv6)

SELECT
   *Dv6* AS Scaled,
   SUM(h.weight_x1000000/1000000) AS Total,
   SUM(CASE when h.windows >0 then h.weight_x1000000/1000000 end) AS Ra,
   SUM(case when h.doors > 0 then h.weight_x1000000/1000000 end) AS Re

FROM store h INNER JOIN 
   sales p ON
   p.id = h.id 

WHERE h.date Between 2018 AND 2020
   AND p.status = 'O'
   AND h.location = 'AM'
   AND h.reg > 2
   AND h.rec = 'R'
   AND h.ver= 'EM'
   AND p.regx > 2;

Output expected: enter image description here

Advertisement

Answer

Create Table #Cond (Scaled Varchar(Max), Conditn varchar(max))

Insert Into #Cond (Scaled, Conditn)
Select 'Dv1', '' Union
Select 'Dv2', ' AND h.location = ''AM'' ' Union 
Select 'Dv3', ' AND h.reg > 2 '  Union 
Select 'Dv4', ' AND h.rec = ''R'' ' Union 
Select 'Dv5', ' AND h.status = ''EM'' ' Union 
Select 'Dv6', ' AND p.regx > 2 '



Declare @QryTmpl varchar(Max) =
'SELECT
   ''@Scaled'' AS Scaled,
   SUM(h.weight_x1000000/1000000) AS Total,
   SUM(CASE when h.windows >0 then h.weight_x1000000/1000000 end) AS Ra,
   SUM(case when h.doors > 0 then h.weight_x1000000/1000000 end) AS Re
FROM store h INNER JOIN 
   sales p ON
   p.id = h.id 
WHERE h.date Between 2018 AND 2020 AND p.status = ''O''
   @COND '  


DECLARE  @NL varchar(1) = char(13), @SQL varchar(max) = '', @MultCond varchar(Max) = ''

Select @MultCond = @MultCond + Conditn + @NL, 
    @SQL = @SQL + Replace(Replace(@QryTmpl,'@Scaled', Scaled),'@COND', @MultCond) + ' Union All ' + @NL
from #Cond order by Scaled

Select @SQL = @SQL + '~'
Select @SQL = Replace(@SQL,' Union All ' + @NL + '~','')

-- For Testing (View the Results as Text)
Select @SQL

Exec(@SQL)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement