Solved

PIVOT Question

Posted on 2013-01-29
3
158 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

813 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

18 Experts available now in Live!

Get 1:1 Help Now