Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PIVOT Question

Posted on 2013-01-29
3
Medium Priority
?
167 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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