SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 SQL Server - GROUP BY clause
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cms9651
Starting Member

26 Posts

Posted - 08/11/2012 :  11:22:04  Show Profile  Reply with Quote
Hi there, I need your help.
Here is my problem.

I tried this query in dbms SQL Server 2008 and I have this output:

Q	MAT	NUMBER
1F	MOL	103623
1F	MOL	103623
1D	LIP	69119
1D	LIP	69119

I need this:

Q	MAT	NUMBER
1F	MOL	103623
1D	LIP	69119
	Tot	172742

Can you help me?
Thanks in advance.
SELECT
	strDTZZ AS Q,
        COALESCE ([MAT], 'Tot') AS [MAT],
        NUMBER
FROM
        (
		SELECT
			LEFT (DTZZ, 2) AS strDTZZ,
			CASE
		WHEN LEFT (TZZ, 2) = '1D' THEN
			'LIP'
		WHEN LEFT (TZZ, 2) = '1F' THEN
			'MOL'
		WHEN LEFT (TZZ, 2) = '1G' THEN
			'IRT'
		WHEN LEFT (TZZ, 2) = '1H' THEN
			'MRE'
		WHEN LEFT (TZZ, 2) = '1I' THEN
			'UOT'
		WHEN LEFT (TZZ, 2) = '1M' THEN
			'MAL'
		WHEN LEFT (TZZ, 2) = '1S' THEN
			'RAS'
		WHEN LEFT (TZZ, 2) = '1O' THEN
			'PMC'
		WHEN LEFT (TZZ, 2) = '1P' THEN
			'BUP'
		WHEN LEFT (TZZ, 2) = '1Q' THEN
			'LAC'
		ELSE
			'CIS'
		END AS MAT,
                COUNT (*) AS NUMBER,
                FROM
                        dbo_40
                GROUP BY 
                ROLLUP ( LEFT (DTZZ, 2),			
			CASE
		WHEN LEFT (TZZ, 2) = '1D' THEN
			'LIP'
		WHEN LEFT (TZZ, 2) = '1F' THEN
			'MOL'
		WHEN LEFT (TZZ, 2) = '1G' THEN
			'IRT'
		WHEN LEFT (TZZ, 2) = '1H' THEN
			'MRE'
		WHEN LEFT (TZZ, 2) = '1I' THEN
			'UOT'
		WHEN LEFT (TZZ, 2) = '1M' THEN
			'MAL'
		WHEN LEFT (TZZ, 2) = '1S' THEN
			'RAS'
		WHEN LEFT (TZZ, 2) = '1O' THEN
			'PMC'
		WHEN LEFT (TZZ, 2) = '1P' THEN
			'BUP'
		WHEN LEFT (TZZ, 2) = '1Q' THEN
			'LAC'
		ELSE
			'CIS'
		END
                )
        ) AS SubQ
WHERE
        1 = 1 
AND (strDTZZ = '1D' OR strDTZZ = '1F');

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 08/11/2012 :  11:25:34  Show Profile  Reply with Quote
isnt it enough to add a DISTINCT?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cms9651
Starting Member

26 Posts

Posted - 08/11/2012 :  11:40:20  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

isnt it enough to add a DISTINCT?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





thank you for reply.
if add the DISTINCT in query I have this output:
Q 	MAT	NUMBER
1F	MOL	103623
1D	LIP	69119

I don't have the Total...
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 08/11/2012 :  16:43:58  Show Profile  Reply with Quote
isnt total returned by ROLLUP?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

cms9651
Starting Member

26 Posts

Posted - 08/12/2012 :  16:23:16  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

isnt total returned by ROLLUP?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/




Not, nothing total in my query last version...
Go to Top of Page

vijayan.vinu3
Starting Member

19 Posts

Posted - 08/18/2012 :  07:41:56  Show Profile  Reply with Quote
Try this Query:


Select IsNULL(Q, '') As Q, MAX(Case When GroupingId = 1 Then 'Total' Else MAT End) As MAT, SUM(NUMBER) As NUMBER From
	(
		Select Q, MAX(MAT) As MAT, MAX(NUMBER) As NUMBER, GROUPING(Q) As GroupingId From Ex
		Group By Q
	) As a
Group By IsNULL(Q, '')
With RollUp
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000