Solved

PIVOT Question

Posted on 2013-01-29
3
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 25

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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