all groups > sql server programming > november 2005 >
You're in the

sql server programming

group:

Urgent SQL query help, please !


Re: Urgent SQL query help, please ! Dan Guzman
11/2/2005 8:49:12 PM
sql server programming:
One method below, assuming SQL Server 2000.

In the future, please post DDL (create statements) and data (insert
statements). This will help ensure a more timely and accurate response to
your problem.

SELECT
COALESCE(a.Store, b.Store, c.Store, d.Store) AS Store,
COALESCE(a.Category, b.Category, c.Category, d.Category) AS Category,
a.Amount AS SaleLY,
b.Amount AS SaleTY,
c.Amount AS ReturnsLY,
d.Amount AS ReturnsTY
FROM Transactions a
FULL JOIN Transactions b ON
a.Store = b.Store AND
a.Category = b.Category AND
a.Subcategory = b.Subcategory
FULL JOIN Transactions c ON
a.Store = c.Store AND
a.Category = c.Category AND
a.Subcategory = c.Subcategory
FULL JOIN Transactions d ON
a.Store = d.Store AND
a.Category = d.Category AND
a.Subcategory = d.Subcategory
WHERE
a.TYLY = '2004' AND
a.TransactionType = 'Sale' AND
b.TYLY = '2005' AND
b.TransactionType = 'Sale' AND
c.TYLY = '2004' AND
c.TransactionType = 'Return' AND
d.TYLY = '2005' AND
d.TransactionType = 'Return'


--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Urgent SQL query help, please ! Alex Nitulescu
11/2/2005 9:17:38 PM
I have a table which is as follows: (Grouped by TY/LY, TransactionType,
Store, Category and Subcategory) - (TY/LY stands for ThisYear/LastYear)

TYLY TransactionType Store Category Subcategory Amount
-----------------------------------------------------------------------
2005 Sale A 1 11
110
2005 Return A 1 11
115
2005 Sale A 1 12
120
2005 Return A 1 12
125
2005 Sale B 2 21
210
2005 Return B 2 21
215
2005 Sale B 2 22
220
2005 Return B 2 22
225
--------------- and the same for last year:
2004 Sale A 1 11
310
2004 Return A 1 11
315
2004 Sale A 1 12
320
2004 Return A 1 12
325
2004 Sale B 2 21
410
2004 Return B 2 21
415
2004 Sale B 2 22
420
2004 Return B 2 22
425


I would need to transpose this table so that I obtain the following table
structure:

Store Category Subcategory SaleTY SaleLY ReturnsTY
ReturnsLY
----------------------------------------------------------------------
A 1 11 110 310
115 315
A 1 12 120 320
125 325
.............................
B 2 21 210 410
215 415
B 2 22 220 420
225 425

and so on, where TY stands for This Year and LY stands for LY.

How do I do that with queries, please (essentially, Year and TransactionType
becoming columns from rows) ?

Re: Urgent SQL query help, please ! Alex Nitulescu
11/2/2005 10:35:45 PM
Thanks a lot - I will try that out ASAP, and let you know how it's working
out.

Alex

[quoted text, click to view]

Re: Urgent SQL query help, please ! Alex Nitulescu
11/2/2005 11:15:36 PM
Dan, I have a slight problem - it works ALMOST great, but what If I don't
have a record of type
TYLY = '2004' AND TransactionType = 'Sale' for a specific
Store/Categ/Subcateg ?

Currently, for instance, for Store X I had NO Sales whatsoever in year 2004
for a specific Categ and Subcateg, so I have that record only for 2005. When
I run this query, the record for 2005 is excluded !

Thanks a lot for your help, really !

Alex.


[quoted text, click to view]

Re: Urgent SQL query help, please ! Dan Guzman
11/2/2005 11:42:09 PM
Below is another technique using correlated subqueries and a derived table.
This ought to do the job, assuming that Store, Category, Subcategory, TYLY
and TransactionType uniquely identify a row.

SELECT
Store,
Category,
SubCategory,
(SELECT Amount FROM Transactions b
WHERE a.Store = b.Store AND
a.Category = b.Category AND
a.SubCategory = b.SubCategory AND
b.TYLY = '2004' AND b.
TransactionType = 'Sale') AS SalesLY,
(SELECT Amount FROM Transactions b
WHERE a.Store = b.Store AND
a.Category = b.Category AND
a.SubCategory = b.SubCategory AND
b.TYLY = '2005' AND
b.TransactionType = 'Sale') AS SalesTY,
(SELECT Amount FROM Transactions b
WHERE a.Store = b.Store AND
a.Category = b.Category AND
a.SubCategory = b.SubCategory AND
b.TYLY = '2004' AND
b.TransactionType = 'Return') AS ReturnsLY,
(SELECT Amount FROM Transactions b
WHERE a.Store = b.Store AND
a.Category = b.Category AND
a.SubCategory = b.SubCategory AND
b.TYLY = '2005' AND
b.TransactionType = 'Return') AS ReturnsTY
FROM (SELECT
a.Store,
a.Category,
a.SubCategory
FROM Transactions a
GROUP BY
a.Store,
a.Category,
a.SubCategory) AS a


--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Re: Urgent SQL query help, please ! Radu
11/3/2005 10:35:40 AM
Thanks a lot, Dan, you really saved me last night ! I was desperate for
not obtaining the data I needed, and the thing was due today, and it
was already almost midnight !

Thank you again.
Alex Nitulescu


PS. Right now I am at my day job, so I cannot test your suggestion yet
- I have no access to my computer. But I'll try it out as soon as I get
home.

PS. Last night I stood up up to 5:30 AM, and I solved the problem by
manipulating the data in the table (in the Insert used to populate that
table) - I had some records with NULL in them so obviously conditions
like (a.category = b.category) (or the condition (a.subcategory =
b.subcategory)) were not working properly because both a.category and
b.category were NULL.
So I defined a default in those fields instead and I used the newly
found keyword "COALESCE" :-)))) to insert data in the table, so now I'm
not missing any more records.
AddThis Social Bookmark Button