dbTalk Databases Forums  

How To write An Sql Crosstable Select statment

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss How To write An Sql Crosstable Select statment in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Joe Saliba
 
Posts: n/a

Default How To write An Sql Crosstable Select statment - 05-08-2005 , 08:10 AM






Hi,

would like to know how to write a crosstable select statment in sql
server2000 where having:
- ItemNumber, ItemDescription, ItemColor, ItemSize as rows
- Stores as columns
- Qty * Netttc as value

from Table sales

thx

*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #2  
Old   
John Bell
 
Posts: n/a

Default Re: How To write An Sql Crosstable Select statment - 05-08-2005 , 09:37 AM






Hi

Posting DDL and example data and expected output helps to answer your query
more easily see http://www.aspfaq.com/etiquett*e.asp?id=5006

If you had a Items Table

CREATE TABLE Items ( ItemNumber int, Description varchar(50), Color
char(10), Size int )

CREATE TABLE SaleItems ( ItemNumber int, Store varchar(50), Qty int, Nettttc
decimal(8,3))

Then something like:

SELECT I.ItemNumber, I.Description, I.Color , I.Size
SUM(CASE WHEN S.Store = 'London' THEN S.Qty*S.Nettttc END) AS [London],
SUM(CASE WHEN S.Store = 'Paris' THEN S.Qty*S.Nettttc END) AS [Paris],
SUM(CASE WHEN S.Store = 'Madrid' THEN S.Qty*S.Nettttc END) AS [Madrid]
FROM Items I
LEFT JOIN SaleItems S ON I.ItemNumber = S.ItemNumber
GROUP BY I.ItemNumber, I.Description, I.Color , I.Size

may be something similar to what you want.


John

"Joe Saliba" <josephs73 (AT) hotmail (DOT) com> wrote

Quote:
Hi,

would like to know how to write a crosstable select statment in sql
server2000 where having:
- ItemNumber, ItemDescription, ItemColor, ItemSize as rows
- Stores as columns
- Qty * Netttc as value

from Table sales

thx

*** Sent via Developersdex http://www.developersdex.com ***



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.