Solved

reformat query

Posted on 2012-12-27
10
144 Views
Last Modified: 2012-12-27
how can i change my query so it display results like cross tab or pivot format?

SELECT [Location Code], SUM(Quantity)
FROM dbo.[CQC$Item Ledger Entry]
WHERE [Item No_] = '10438'
AND [Location Code] IN ('10', '20', '90', '95')
GROUP BY [Location Code]

Open in new window



results are:
results current
not all locations have quantities all times but ideally I would like my results to be like (using results as screen shot shows):

10        20        90        95
            2500    5050


Any help would be great.

thank you so much.
0
Comment
Question by:metropia
  • 7
  • 2
10 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 38724633
metropia,

What databse are you using?  Will your report always be using those 4 location codes?

AielloJ
0
 

Author Comment

by:metropia
ID: 38724643
Using SQL server 2009. for now the location codes will be those 4. possibly adding more in the event future.

Thank you.
0
 

Author Comment

by:metropia
ID: 38724655
SQL Server 2008 R2. I apologize for the typing error.
0
 
LVL 9

Accepted Solution

by:
sognoct earned 500 total points
ID: 38724728
select
SUM(case when  [Location Code] = 10 then Quantity else 0 end) as l10,
SUM(case when  [Location Code] = 20 then Quantity else 0 end) as l20,
SUM(case when  [Location Code] = 90 then Quantity else 0 end) as l90,
SUM(case when  [Location Code] = 95 then Quantity else 0 end) as l95
FROM dbo.[CQC$Item Ledger Entry]
WHERE [Item No_] = '10438'
0
 

Author Comment

by:metropia
ID: 38724943
thank you much. do you think this could be done using PIVOT? i would love to learn how to do it that way. if you could show me an example, if possible.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:sognoct
ID: 38725042
cannot debug query (must restart my pc ... )
so, there can be some errors ... but should be something like :

SELECT 
[10], [20], [90], [95]
FROM
(SELECT [Location Code], SUM(Quantity)
  FROM dbo.[CQC$Item Ledger Entry]) AS SourceTable
  PIVOT (
  SUM(Quantity)
  FOR [Location Code] IN ('10', '20', '90', '95') ) AS PivotTable

Open in new window

0
 

Author Comment

by:metropia
ID: 38725139
it gives an error: Incorrect syntax near '10'.
0
 

Author Comment

by:metropia
ID: 38725184
got it past that error :

SELECT '10', '20', '90', '95'
FROM
(SELECT [Location Code], Quantity
  FROM dbo.[CQC$Item Ledger Entry]) AS SourceTable
  PIVOT (
  SUM(Quantity)
  FOR [Location Code] IN ([10], [20], [90], [95]) ) AS PivotTable
0
 

Author Comment

by:metropia
ID: 38725186
how can i display the quantities below each location?
0
 

Author Comment

by:metropia
ID: 38725307
it seems nobody is willing to explain little gurther. thank you though.

i am going to have to choose the expert comment that gave me a quick and dirty solution to my problem.

thank you.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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 …

920 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

15 Experts available now in Live!

Get 1:1 Help Now