Solved

SQL Pivot Table

Posted on 2009-04-02
7
783 Views
Last Modified: 2012-05-06
Is this possible in SQL
TABLE

Product              |      QT     |  Client
name1                |       2      |  ClientName1
name1                |       5      | ClientName2
name2                |       3      |  ClientName2
name2                |       7      | ClientName1
...

Result

Product     ClientName1     ClientName2 ...     SUM
name1                   2                       5                 7
name2                   7                       3                10


I dont tink so but....
Tanks in advanced
0
Comment
Question by:rop65828
  • 2
  • 2
  • 2
7 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 24051507
yes it is possible

select product
    ,sum(case when client = 'ClientName1' then qt else null end) as clientname1
    ,sum(case when client = 'ClientName2' then qt else null end) as clientname2
from yourtable
group by product
order by 1
0
 
LVL 22

Expert Comment

by:dportas
ID: 24051642
This is a cross tab report and any reporting tool will do it for you. SQL obviously isn't a good language for report development because it has no features for controling the presentation of data.
0
 
LVL 22

Expert Comment

by:dportas
ID: 24051656
I should add that some SQL DBMSs have proprietary features that do this. Microsoft SQL Server has PIVOT for example.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:rop65828
ID: 24051955
I use SQL server 2005 and Lowfatspread i dont know how many clients there are. I would like an dynamic query.
0
 

Author Comment

by:rop65828
ID: 24051964
Microsoft SQL Server 2005
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 50 total points
ID: 24052726
try this then...


select Product,clientid,sum(qt) as qt

  into #temp

 from yourtable

 where ...

 group by product,clientid
 
 

Select client,identity(int,1,1) as rowno

  into #temp1

  from (

select distinct client

 from #temp as t

) as x
 

declare @cid int

declare @sql varchar(8000)
 

set @cid = 1

set @sql='Select product'
 

while exists (select rowno from #temp1 where rowno=@cid)  

do

   select @SQL=@SQL+',SUm(Case when client='''+Client+''' then qt else null end) as ['+client+']'

         ,@cid=@rowno+1

     from #temp1

     Where rowno=@cid

end
 

Set @sql=@sql+',sum(qt) as [sum] from #temp group by product order by 1'
 

Exec(@SQL)

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

910 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

25 Experts available now in Live!

Get 1:1 Help Now