Solved

PIVOT Question

Posted on 2013-01-29
3
156 Views
Last Modified: 2013-03-05
I have an Application table which holds information about an application. I have a ApplicationCompany table which identifies the state that the application operates in, as well as other information such as ItemCount and UnitCount.

The Application may or may not apply to all possible states, which is held in the Company table.

With
Application table
1  Test
2  Test2

Company
AR
AK

ApplicationCompany
AppID State Items Units
1         AR     2         209
1         AK     1         100
2         AK     10         90

I am hoping to arrive at:

ApplicationName  ApplicationDescription  State  Items   Units State Items Units
Test                       Test Application             AR      2          209   AK     1        100
Test2                                                           AK      10         90
0
Comment
Question by:coldchillin
3 Comments
 
LVL 1

Expert Comment

by:DoutorApedeuta
ID: 38831996
I'm a little bit confused about the names of the tables/columns, but I guess that what you need is a LEFT JOIN, something like:

SELECT <whatever columns you want>
FROM Application a
LEFT JOIN ApplicationCompany ac ON ac.AppID = a.AppID

Maybe I'm missing something... if so, may you clarify the names of tables/columns/result set?
0
 
LVL 1

Author Comment

by:coldchillin
ID: 38832132
Application Table
AppID
AppName

Company Table
CompanyID
State

ApplicationCompany table
AppID
CompanyID
ItemCount
UnitCount

SELECT AppID,AppName
,[1] AS UnitsAR
,[2] AS unitsAK
FROM (
SELECT a.AppID,a.AppName,c.CompanyID,ac.UnitCount
FROM Application a
JOIN ApplicationCompany ac ON ac.AppID = a.AppID
JOIN Company c ON c.CompanyID = pc.CompanyID
) as SourceTable
PIVOT (MAX(UnitCount) FOR CompanyID IN ([1],[2])
as PivotTable;

This works and I get:
AppID       AppName     UnitsAR   UnitsAK
100           Test             100           50

But I would actually like to achieve:
AppID        AppName   UnitsAR ItemCountAR  UnitsAK   ItemCountAK
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 38833586
Use this:

SELECT AppName
,SUM([1]) AS UnitsAR
,SUM([-1]) AS ItemsAR
,SUM([2]) AS unitsAK
,SUM([-2]) AS ItemsAK
FROM 
(
SELECT a.AppID,a.AppName,c.CompanyID, -ac.CompanyID CompanyID1, ac.UnitCount,ac.Items
FROM Application a
JOIN ApplicationCompany ac ON ac.AppID = a.AppID
JOIN Company c ON c.CompanyID = ac.CompanyID
) as SourceTable
PIVOT (MAX(UnitCount) FOR CompanyID IN ([1],[2])) a
PIVOT (MAX(Items) FOR CompanyID1 IN ([-1],[-2])) b
GROUP BY AppName;

Open in new window


The trick here is to create a fake CompanyID1 column with negative values that are equal to CompanyID, and pivot by this values.

see SQL Fiddle for complete solution
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now