简体   繁体   中英

Select and group by - calculated field

I'm having some issues to complete a SQL statement in SQL Server 2008.

My 'query1' is the following:

SELECT [Vc_MONTH],
       [Vc_STATE],
       [Vc_PRODUCT],
       SUM ([TOTAL]) as Total_Units,
       SUM ([OPEN]) as Open_Units

FROM [test].[dbo].[Tbl_Summary]

GROUP BY [Vc_MONTH],
         [Vc_REGION],
         [Vc_PRODUCT],

This query selects Month, Region, Product, Sum of Total Units and Sum of Open Units.

I already group by Month, Region and Product. (I have plenty more lines)

This query works.

What I need is another 'query2' that groups by (ALL) the months listed on the table and then an union of this two selects.

At the end I need something like this

query1

|MONTH | STATE | PRODUCT | TOTAL | OPEN |
|:-----|:------|:--------|:------|:-----|
|JAN   | CA    | PENCIL  | 200   | 160 |
|JAN   | FL    | BOOK    | 300   | 280 |
|FEB   | CA    | PENCIL  | 180   | 150 |
|FEB   | FL    | PENCIL  | 250   | 100 |
|MAR   | CA    | BOOK    | 250   | 100 |
|MAR   | FL    | BOOK    | 100   | 50  |

query2 - This is what I need

|MONTH | STATE | PRODUCT | TOTAL | OPEN |
|:-----|:------|:--------|:------|:-----|
|JAN   | CA    | PENCIL  | 200   | 160 |
|JAN   | FL    | BOOK    | 300   | 280 |
|FEB   | CA    | PENCIL  | 180   | 150 |
|FEB   | FL    | PENCIL  | 250   | 100 |
|MAR   | CA    | BOOK    | 250   | 100 |
|MAR   | FL    | BOOK    | 100   | 50  |

UNION

|ALL   | CA    | PENCIL  | 380   | 310 |
|ALL   | CA    | BOOK    | 250   | 100 |
|ALL   | FL    | PENCIL  | 250   | 100 |
|ALL   | FL    | BOOK    | 400   | 330 |

Thanks in advance, Luis

I think you should just use grouping sets . Much simpler query and no union :

SELECT (CASE WHEN GROUPING([Vc_MONTH]) = 1 THEN 'ALL' ELSE [Vc_MONTH] END) as [Vc_MONTH],
       [Vc_STATE], [Vc_PRODUCT],
       SUM ([TOTAL]) as Total_Units,
       SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY GROUPING SETS (([Vc_MONTH], [Vc_REGION], [Vc_PRODUCT]),
                        ([Vc_REGION], [Vc_PRODUCT])
                       );

Not a SQL Server Guru by any stretch, but I think it has a with clause:

with monthly as (
  SELECT
    [Vc_MONTH], [Vc_STATE], [Vc_PRODUCT],
    SUM ([TOTAL]) as Total_Units,
    SUM ([OPEN]) as Open_Units
  FROM [test].[dbo].[Tbl_Summary]
  GROUP BY
    [Vc_MONTH], [Vc_STATE], [Vc_PRODUCT]
)
select
  [Vc_MONTH], [Vc_STATE], [Vc_PRODUCT],
  Total_Units, Open_Units
from monthly
union all
select
  '*ALL', [Vc_STATE], [Vc_PRODUCT],
  sum (Total_Units), sum (Open_Units)
from monthly
group by [Vc_STATE], [Vc_PRODUCT]

so you already have query 1:

SELECT [Vc_MONTH],
   [Vc_STATE],
   [Vc_PRODUCT],
   SUM ([TOTAL]) as Total_Units,
   SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY [Vc_MONTH],
     [Vc_STATE],
     [Vc_PRODUCT]

next you need to GROUP BY Month and Product correct? However, you need to specify a value in the 'Vc_STATE' column so that result sets from the two queries return the same columns.

UNION
SELECT [Vc_MONTH],
   'ALL STATES',
   [Vc_PRODUCT],
   SUM ([TOTAL]) as Total_Units,
   SUM ([OPEN]) as Open_Units
FROM [test].[dbo].[Tbl_Summary]
GROUP BY [Vc_MONTH],
     [Vc_PRODUCT]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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