Solved

PIVOT Question

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
T-SQL - How to use like '%%' operator to search an integer 2 38
Help  needed 3 36
export sql results to csv 6 39
SQL Insert parts by customer 12 42
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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, …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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