SQL 2012 PIVOT table with total column

Hello Experts,

I need help with a pivot table.

For testing, I have already created a SQLFiddle.

SQLFiddle

Here is the result ...

TYPE      | Gebunden  | CD  | PDF
JQuery   | 1                   | 1     | 1
PHP        | 1                   | 1     | 0
VBA        | 1                   | 0      | 0

But I want to show afterType column a Sum of each Row (Gebunden + CD + PDF) and one column to the "ZEILE" field ...

So the result should look like ...

TYPE     | SUM |  LINE |  Gebunden |  CD |  PDF
JQuery  | 3       | 15      | 1                  | 1      | 1
PHP       | 2      | 20       | 1                 |  1      |  0
VBA       | 1      | 31       |  1                 | 0       | 0

I hope someone can help me.

Thanks in advance

Talki
Klaus TalkenbergerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Deepak ChauhanSQL Server DBACommented:
select [TYPE], [Gebunden]+[CD]+[PDF] as [SUM] , line , [Gebunden], [CD], [PDF] from (SELECT [TYPE], line,[Gebunden], [CD], [PDF]

FROM 

( SELECT [TYPE],PUNKTE as line, [Kriterium], [Krit_Flag] 
	FROM TEST
) SRC
PIVOT 
(
  sum(Krit_Flag)
  for Kriterium in ([Gebunden], [CD], [PDF])
) pvt) T

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
E.g.

DECLARE @Sample TABLE
    (
      [TYPE] VARCHAR(10) NULL ,
      ZEILE INT NULL ,
      PUNKTE INT NULL ,
      Kriterium NVARCHAR(8) NULL ,
      Krit_Flag INT NULL
    );

INSERT  INTO @Sample
VALUES  ( 'PHP', 1, 15, 'Gebunden', 1 ),
        ( 'PHP', 1, 15, 'CD', 1 ),
        ( 'PHP', 1, 15, 'PDF', 0 ),
        ( 'VBA', 1, 20, 'Gebunden', 1 ),
        ( 'VBA', 1, 20, 'CD', 0 ),
        ( 'VBA', 1, 20, 'PDF', 0 ),
        ( 'JQuery', 1, 31, 'Gebunden', 1 ),
        ( 'JQuery', 1, 31, 'CD', 1 ),
        ( 'JQuery', 1, 31, 'PDF', 1 );

WITH    Data
          AS ( SELECT   [TYPE] ,
                        Kriterium ,
                        Krit_Flag ,
                        SUM(Krit_Flag) OVER ( PARTITION BY [TYPE] ) AS Summe
               FROM     @Sample
             )
    SELECT  *
    FROM    Data D PIVOT ( SUM(D.Krit_Flag) FOR D.Kriterium IN ( Gebunden, CD, PDF ) ) P;

Open in new window

Klaus TalkenbergerAuthor Commented:
Hi deepakChauhan

Your SQL Statement works fatastic  ...

Thank you for your solution ...

Greetings

Talki
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Klaus TalkenbergerAuthor Commented:
This code works fine ;-)
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
similar ver, which tend to be simplified:

SELECT [TYPE], [PUNKTE],
[Gebunden]+[CD]+[PDF] as [sum], [Gebunden], [CD], [PDF]
FROM 
(
	SELECT [TYPE], [PUNKTE], [Kriterium], [Krit_Flag] 
	FROM TEST
) SRC
PIVOT 
(
  sum(Krit_Flag)
  for Kriterium in ([Gebunden], [CD], [PDF])	
) pvt
Order By 1

Open in new window

Prakash SamariyaIT ProfessionalCommented:
Just Add "([Gebunden]+ [CD]+ [PDF]) as TOTAL" in top Select statement, for Line column you didn't mentioned!   However You may get its values by just adding "PUNKTE as line" in inner select statement and "Line" in the top select statement!

Example:
SELECT [TYPE],line, [Gebunden], [CD], [PDF], ([Gebunden]+ [CD]+ [PDF]) as TOTAL
FROM (
      SELECT [TYPE],PUNKTE as line, [Kriterium], [Krit_Flag]
      FROM TEST
) SRC
PIVOT
(
  sum(Krit_Flag)
  for Kriterium in ([Gebunden], [CD], [PDF])
) pvt;
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.