Solved

SQL Query problem

Posted on 2012-03-12
3
270 Views
Last Modified: 2012-08-14
Hello EE,

I have a query results like this:

Name       Level      Key      Description          Jannuary    February
 
Peter          3            P           Peter's Sales        500               200
Isabel         3            P           Isa's Sales            200               450


How could I do a new query the results be like this :

Key              Peter Jan   Peter Feb         Isa Jan    Isa Feb
 
 P                    500               200                200        450

(since both lines on first query was P)   there is only 1 row on second query result and Vendors are horizontal. If there were 5 months they would have 5 months each...

is it possible ? ... thanks sql experts !!
0
Comment
Question by:PhilippeRenaud
[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
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 500 total points
ID: 37711217
i will Take the result of you first query as table1 then the new query would be

select distinct Peter.Key, peter.Jan as Peter Jan  ,peter.feb as Peter Feb , Isa.jan as Isa Jan,  isa.feb as Isa Feb
FROM table1 as peter JOIN table1 as Isa ON peter.Level=Isa.Level
AND peter.Key=Isa.Key
AND peter.Name <> Isa.Name

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 37711235
Ok but is there a way to to this without hardcoding the names ?
because tomorrow it could have 4 name (peter isa john mike) and the query wouldnt work anymore.. ?
0
 
LVL 11

Expert Comment

by:SANDY_SK
ID: 37711381
Well if that is the case what you could do is use a stored procedure to construct the query in the same way. but then there will involve too many self joins, not too sure if that's a very efficient way.

There is one option of pivot tables but i don't thing it can be used in this scenario.

Exploring it, if i am able to get it will post it .
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

623 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