Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Create Summary Data from multiple tables

Author  Topic 

katethegreat
Starting Member

13 Posts

Posted - 2013-08-13 : 12:22:48
Hello all,

I've been struggling with this for about 2 weeks now and can't seem to get any further.

I have two tables: orders and orders_extended. They can be joined by the common orderid field (example SELECT * FROM orders JOIN orders_extended ON orders.order = orders_extended.orderid WHERE 1=1)

I need to create a report that sums the following fields:

SELECT

CONVERT(VARCHAR(12), orderdate, 101) As orderdate
, COALESCE (
CASE WHEN orders_extended.productprice < 0 THEN 'DISCOUNT' ELSE orders_extended.productnumber END
, CASE WHEN orders_extended.productnumber LIKE '%AB%' THEN 'PRODUCTGROUPAB' ELSE orders_extended.productnumber END
, CASE WHEN orders_extended.productnumber LIKE '%CD%' THEN 'PRODUCTGROUPCD' ELSE NULL END
, CASE WHEN orders.shippingcost > 0 AND orders.shippingtype NOT LIKE '10%' THEN 'SHIPPING' ELSE NULL END
, CASE WHEN orders.shippingcost > '5.5' AND orders.shippingtype LIKE '10%' THEN 'SHIPPING' ELSE NULL END
, CASE WHEN orders.shippingcost > '5.5' AND orders.shippingtype LIKE '10%' THEN 'HANDLING' ELSE NULL END
, CASE WHEN orders.salestax > '0' THEN 'SalesTax' ELSE NULL END

) As productgroup

, COALESCE (
orders_extended.productprice
, CASE WHEN orders.shippingcost > 0 AND orders.shippingtype NOT LIKE '10%' THEN orders.shippingcost ELSE 0 END
, CASE WHEN orders.shippingcost > 5.50 AND orders.shippingtype LIKE '10%' THEN orders.shippingcost - 5.50 ELSE 0 END
) As totalsales
---i need this data to sum below but not as a column in the results

, (CASE WHEN orders.paymentmethodid LIKE '4' or orders.paymentmethodid LIKE '5' or orders.paymentmethodid LIKE '6' THEN SUM(totalsales) ELSE 0 END) As 'Visa-MC-Disc'
, (CASE WHEN orders.paymentmethodid LIKE '6' THEN SUM(totalsales) ELSE 0 END) As 'Amex'
, (CASE WHEN orders.paymentmethodid LIKE '7' THEN SUM(totalsales) ELSE 0 END) As 'PayPal'

FROM orders
LEFT JOIN orders_extended
ON orders.orderid = orders_extended.orderid
WHERE 1=1
AND orders.orderdate > '8/1/2013'
;

What I'm trying to accomplish is to get the total dolloar amount of sales for each day for each payment type on one line per productgroup. I need the output to look like this:




Please, if anyone can assist, let me know. I'm going to have a bruise on my forehead from banging it against the wall if I don't figure this out soon!

Thanks in advance,

-Kate

Kate the Great

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-08-13 : 12:41:28
What does your input look like?
Go to Top of Page

katethegreat
Starting Member

13 Posts

Posted - 2013-08-13 : 14:14:31
quote:
Originally posted by MuMu88

What does your input look like?



Forgive my ignorance, but do you mean what do my tables/data look like? If so, here is a snapshot the data from the orders table:




and here is a snapshopt of the orders_extended table:




By the way - if you're wondering, the reasoning for the CASE WHEN orders.shippingcost > '5.5' and shippingtype LIKE '10%' etc. is because my company charges shipping and handling in the same line item, but I need to know how much of the total shippingcost is handling and how much is shipping in the summary report I'm trying to create here.

Thanks again!


Kate the Great
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-08-13 : 15:38:59
Try this. Since I don't have consumable data, I am simply guessing what you need. If this does not work, if you can post consumable data that someone can copy and paste to run in an SSMS window. This page describes how to create consumable data: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

SELECT  *
FROM ( SELECT CONVERT(VARCHAR(12), orderdate, 101) AS orderdate ,
COALESCE(CASE WHEN orders_extended.productprice < 0
THEN 'DISCOUNT'
ELSE orders_extended.productnumber
END,
CASE WHEN orders_extended.productnumber LIKE '%AB%'
THEN 'PRODUCTGROUPAB'
ELSE orders_extended.productnumber
END,
CASE WHEN orders_extended.productnumber LIKE '%CD%'
THEN 'PRODUCTGROUPCD'
ELSE NULL
END,
CASE WHEN orders.shippingcost > 0
AND orders.shippingtype NOT LIKE '10%'
THEN 'SHIPPING'
ELSE NULL
END,
CASE WHEN orders.shippingcost > '5.5'
AND orders.shippingtype LIKE '10%'
THEN 'SHIPPING'
ELSE NULL
END,
CASE WHEN orders.shippingcost > '5.5'
AND orders.shippingtype LIKE '10%'
THEN 'HANDLING'
ELSE NULL
END,
CASE WHEN orders.salestax > '0' THEN 'SalesTax'
ELSE NULL
END) AS productgroup ,
COALESCE(orders_extended.productprice,
CASE WHEN orders.shippingcost > 0
AND orders.shippingtype NOT LIKE '10%'
THEN orders.shippingcost
ELSE 0
END,
CASE WHEN orders.shippingcost > 5.50
AND orders.shippingtype LIKE '10%'
THEN orders.shippingcost - 5.50
ELSE 0
END) AS totalsales
---i need this data to sum below but not as a column in the results
,
CASE WHEN orders.paymentmethodid LIKE '4'
OR orders.paymentmethodid LIKE '5'
OR orders.paymentmethodid LIKE '6'
THEN 'Visa-MC-Disc'
WHEN orders.paymentmethodid LIKE '6' THEN 'Amex'
WHEN orders.paymentmethodid LIKE '7' THEN 'PayPal'
ELSE 'Unknown'
END AS PaymentMethod
FROM orders
LEFT JOIN orders_extended ON orders.orderid = orders_extended.orderid
WHERE 1 = 1
AND orders.orderdate > '8/1/2013'
) s PIVOT( SUM(totalsales) FOR PaymentMethod IN ( [Visa-MC-Disc],
[Amex], [PayPal] ) ) P
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-08-13 : 15:41:17
I don't have time to look into your actual question, but it appears you are going a lot of implicit conversions that you might want to avoid. For example, you comparring a string vlaue '5.5' to the shippingcost column, which I assume is money/decimal datatype.
Go to Top of Page

katethegreat
Starting Member

13 Posts

Posted - 2013-08-13 : 16:11:46
quote:
Originally posted by Lamprey

I don't have time to look into your actual question, but it appears you are going a lot of implicit conversions that you might want to avoid. For example, you comparring a string vlaue '5.5' to the shippingcost column, which I assume is money/decimal datatype.



the reasoning for the CASE WHEN orders.shippingcost > '5.5' and shippingtype LIKE '10%' etc. is because my company charges shipping and handling in the same line item, but I need to know how much of the total shippingcost is handling (handling is a flat fee of $5.50) and how much is shipping in the summary report I'm trying to create here. So if orders.shippingcost > 5.5 and shipping type starts with 10, then I know that order was charged shipping AND handling.


Kate the Great
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-08-13 : 18:23:15
So you store your shippingcost as a string?
Go to Top of Page

katethegreat
Starting Member

13 Posts

Posted - 2013-08-13 : 21:24:48
quote:
Originally posted by Lamprey

So you store your shippingcost as a string?



Hi, it's CURRENCY - so does that mean I should be using '5.50' in my CASE instead of '5.5' ? I didn't think the trailing 0 made a difference.

Thanks,

Kate the Great
Go to Top of Page

katethegreat
Starting Member

13 Posts

Posted - 2013-08-13 : 22:58:01
quote:
Originally posted by James K

Try this. Since I don't have consumable data, I am simply guessing what you need. If this does not work, if you can post consumable data that someone can copy and paste to run in an SSMS window. This page describes how to create consumable data: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

SELECT  *
FROM ( SELECT CONVERT(VARCHAR(12), orderdate, 101) AS orderdate ,
COALESCE(CASE WHEN orders_extended.productprice < 0
THEN 'DISCOUNT'
ELSE orders_extended.productnumber
END,
CASE WHEN orders_extended.productnumber LIKE '%AB%'
THEN 'PRODUCTGROUPAB'
ELSE orders_extended.productnumber
END,
CASE WHEN orders_extended.productnumber LIKE '%CD%'
THEN 'PRODUCTGROUPCD'
ELSE NULL
END,
CASE WHEN orders.shippingcost > 0
AND orders.shippingtype NOT LIKE '10%'
THEN 'SHIPPING'
ELSE NULL
END,
CASE WHEN orders.shippingcost > '5.5'
AND orders.shippingtype LIKE '10%'
THEN 'SHIPPING'
ELSE NULL
END,
CASE WHEN orders.shippingcost > '5.5'
AND orders.shippingtype LIKE '10%'
THEN 'HANDLING'
ELSE NULL
END,
CASE WHEN orders.salestax > '0' THEN 'SalesTax'
ELSE NULL
END) AS productgroup ,
COALESCE(orders_extended.productprice,
CASE WHEN orders.shippingcost > 0
AND orders.shippingtype NOT LIKE '10%'
THEN orders.shippingcost
ELSE 0
END,
CASE WHEN orders.shippingcost > 5.50
AND orders.shippingtype LIKE '10%'
THEN orders.shippingcost - 5.50
ELSE 0
END) AS totalsales
---i need this data to sum below but not as a column in the results
,
CASE WHEN orders.paymentmethodid LIKE '4'
OR orders.paymentmethodid LIKE '5'
OR orders.paymentmethodid LIKE '6'
THEN 'Visa-MC-Disc'
WHEN orders.paymentmethodid LIKE '6' THEN 'Amex'
WHEN orders.paymentmethodid LIKE '7' THEN 'PayPal'
ELSE 'Unknown'
END AS PaymentMethod
FROM orders
LEFT JOIN orders_extended ON orders.orderid = orders_extended.orderid
WHERE 1 = 1
AND orders.orderdate > '8/1/2013'
) s PIVOT( SUM(totalsales) FOR PaymentMethod IN ( [Visa-MC-Disc],
[Amex], [PayPal] ) ) P




Beautiful! The format of the summary report (output) is perfect, had to change:

, CASE WHEN orders_extended.productnumber LIKE '%AB%' THEN 'PRODUCTGROUPAB' ELSE orders_extended.productnumber END

to
, CASE WHEN orders_extended.productnumber LIKE '%AB%' THEN 'PRODUCTGROUPAB' ELSE NULL END

and add ELSE orders_extended.productnumber END to the last CASE line in AS productgroup - I'm looking for the uniform product names like 'PRODUCTGROUPAB' rather than the actual productnumber unless the productnumber doesn't fall into one of the categories, then I need the productnumber itself, or even better, an 'OTHER' category/alias for productnumbers that don't fit into my other CASE lines (not sure how to do that).


The only hangup now is that the 'SHIPPING' and 'HANDLING' as productgroup records are not being created. I'm guessing because there are not actual items/records in the orders_extended table, rather I'm trying to create records based on comparisons in the orders table - but I don't know how to get them to get inserted.

I don't have access to create a DDL as it says in the link you provided because I'm working within an online shopping cart that only gives me EXPORT access based on SELECT query. But here's the column types:

table: orders
orderid (LONG)
orderdate (DATETIME)
shippingcost (CURRENCY)
paymentmethod (LONG)
salestax (CURRENCY)
shippingtype (TEXT:100)

table: orders_extended
orderid (LONG)
productnumber (TEXT:30)
quantity (LONG)
productprice (CURRENCY)


And here's some comma delimited consumable sample data from the orders table:

orderid orderdate shippingcost paymentmethod salestax shippingtype
12345,8/1/2013,7.72,4,0,151
12346,8/1/2013,0,7,0,200
12347,8/1/2013,45.51,5,0,10157
12348,8/1/2013,0,4,0,200
12349,8/1/2013,0,4,0,200
12350,8/1/2013,0,5,0,200
12351,8/1/2013,15.15,4,0,10602
12352,8/1/2013,0,4,0,200
12353,8/1/2013,0,4,0,200
12354,8/1/2013,10.2,4,0,612
12355,8/1/2013,0,5,0,200
12356,8/1/2013,0,5,6.71,200
12357,8/1/2013,0,5,0,200
12358,8/1/2013,0,5,0,200
12359,8/1/2013,0,4,0,200
12360,8/1/2013,0,4,0,200
12361,8/1/2013,0,4,0,200
12362,8/1/2013,0,4,0,200
12363,8/1/2013,0,4,0,200
12364,8/1/2013,0,5,0,200
12365,8/1/2013,36.73,4,0,154
12366,8/1/2013,0,5,6.71,200
12367,8/1/2013,0,4,0,200
12368,8/1/2013,22.09,4,0,152
12369,8/2/2013,16.69,4,0,152
12370,8/2/2013,0,5,0,200
12371,8/2/2013,0,4,0,200
12372,8/2/2013,0,4,0,200
12373,8/2/2013,12.91,4,0,152
12374,8/2/2013,11.12,5,0,151
12375,8/2/2013,0,4,0,200
12376,8/2/2013,18.32,4,0,10151
12377,8/2/2013,0,5,0,200
12378,8/2/2013,10.2,4,0,602
12379,8/2/2013,0,4,0,200
12380,8/2/2013,0,4,0,200
12381,8/2/2013,0,4,0,200
12382,8/2/2013,0,4,0,200
12383,8/2/2013,0,4,0,200

And here is orders_extended in comma delimited:

12345,CS,1,-30
12345,474-AB-8,1,99.99
12346,LBL,1,7.5
12347,474-CD-10,1,119.99
12348,479-AB-1,1,109.99
12349,DISC,1,-35
12349,474-CD-10,1,119.99
12350,DISC,1,-30
12350,474-CD-10,1,119.99
12350,479-CD-2,1,139.99
12351,DISC,1,-32.5
12351,474-AB-8,1,99.99
12352,DISC,1,-30
12352,479-CD-2,1,139.99
12353,474-CD-10,1,119.99
12354,DISC,1,-35
12354,479-CD-2,1,139.99
12355,DISC,1,-27.5
12355,479-CD-2,2,139.99
12356,DISC,1,-30
12356,474-CD-10,1,119.99
12357,474-AB-8,1,99.99
12358,DISC,1,-30
12358,479-AB-1,1,109.99
12359,LBL,1,7.5
12360,DISC,1,-32.5
12360,474-CD-10,1,119.99
12361,DISC,1,-30
12361,479-CD-2,1,139.99
12362,DISC,1,-27.5
12362,474-AB-8,1,99.99
12363,DISC,1,-30
12363,474-CD-10,1,119.99
12364,DISC,1,-30
12364,479-AB-1,1,109.99
12365,474-CD-10,1,119.99
12366,DISC,1,-35
12366,474-AB-8,1,99.99
12367,DISC,1,-35
12367,474-CD-10,1,119.99
12368,DISC,1,-30
12368,479-CD-2,1,139.99
12369,DISC,1,-27.5
12369,479-CD-2,1,139.99
12370,DISC,1,-32.5
12370,474-AB-8,1,99.99
12371,474-CD-10,1,119.99
12372,DISC,1,-30
12372,479-CD-2,1,139.99
12373,474-AB-8,1,99.99
12374,DISC,1,-25
12374,479-AB-1,1,109.99
12375,DISC,1,-30
12375,474-CD-10,2,119.99
12376,LBL,1,7.5
12377,DISC,1,-25
12377,474-AB-8,1,99.99
12378,DISC,1,-35
12378,479-CD-2,1,139.99
12379,DISC,1,-40
12379,474-CD-10,1,119.99
12380,474-AB-8,1,99.99
12381,DISC,1,-25
12381,479-AB-1,1,109.99
12382,DISC,1,-30
12382,474-AB-8,1,99.99
12383,DISC,1,-35
12383,474-CD-10,1,119.99


The output should be (comma delimited)
orderdate,productgroup,visa-mc-disc,amex,paypal
8/10/2013,PRODUCTGROUPAB,"(sum visa/mc/disc sales of products with ""AB"" in the productnumber)","(sum amex sales of products with ""AB"" in the productnumber)","(sum paypal sales of products with ""AB"" in the productnumber)"
8/10/2013,PRODUCTGROUPCD,"(sum visa/mc/disc sales of products with ""CD"" in the productnumber)","(sum amex sales of products with ""CD"" in the productnumber)","(sum paypal sales of products with ""CD"" in the productnumber)"
8/10/2013,DISCOUNT,(sum visa/mc/disc sales of products where productprice < 0),(sum amex sales of products where productprice < 0),(sum paypal sales of products where productprice < 0)
8/10/2013,SHIPPING,(sum visa/mc/disc where shippingcost > 0 and shippingtype CASEs),(sum amex where shippingcost > 0 and shippingtype CASEs),(sum paypal where shippingcost > 0 and shippingtype CASEs)
8/10/2013,HANDLING,(sum visa/mc/disc where shippingcost > 0 and shippingtype CASEs),(sum amex where shippingcost > 0 and shippingtype CASEs),(sum paypal where shippingcost > 0 and shippingtype CASEs)
8/10/2013,SALES TAX,(sum visa/mc/disc where salestax > 0 ),(sum amex where salestax > 0 ),(sum paypal where salestax > 0 )
8/10/2013,OTHER,(sum visa/mc/disc sales of products that don’t match previously stated criteria),(sum amex sales of products that don’t match previously stated criteria),(sum paypal sales of products that don’t match previously stated criteria)
8/11/2013,PRODUCTGROUPAB,"(sum visa/mc/disc sales of products with ""AB"" in the productnumber)","(sum amex sales of products with ""AB"" in the productnumber)","(sum paypal sales of products with ""AB"" in the productnumber)"
8/11/2013,PRODUCTGROUPCD,"(sum visa/mc/disc sales of products with ""CD"" in the productnumber)","(sum amex sales of products with ""CD"" in the productnumber)","(sum paypal sales of products with ""CD"" in the productnumber)"
8/11/2013,DISCOUNT,(sum visa/mc/disc sales of products where productprice < 0),(sum amex sales of products where productprice < 0),(sum paypal sales of products where productprice < 0)
8/11/2013,SHIPPING,(sum visa/mc/disc where shippingcost > 0 and shippingtype CASEs),(sum amex where shippingcost > 0 and shippingtype CASEs),(sum paypal where shippingcost > 0 and shippingtype CASEs)
8/11/2013,HANDLING,(sum visa/mc/disc where shippingcost > 0 and shippingtype CASEs),(sum amex where shippingcost > 0 and shippingtype CASEs),(sum paypal where shippingcost > 0 and shippingtype CASEs)
8/11/2013,SALES TAX,(sum visa/mc/disc where salestax > 0 ),(sum amex where salestax > 0 ),(sum paypal where salestax > 0 )
8/11/2013,OTHER,(sum visa/mc/disc sales of products that don’t match previously stated criteria),(sum amex sales of products that don’t match previously stated criteria),(sum paypal sales of products that don’t match previously stated criteria)


Again, thank you in advance for you help, I nearly never ask for help but I just can't figure this one out!





Kate the Great
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-08-14 : 16:06:20
quote:
Originally posted by katethegreat

quote:
Originally posted by Lamprey

So you store your shippingcost as a string?



Hi, it's CURRENCY - so does that mean I should be using '5.50' in my CASE instead of '5.5' ? I didn't think the trailing 0 made a difference.

Thanks,

Kate the Great

Neither. Putting a value in a single quotes denotes a string. So what is happening is that the STRING value is being IMPLICITLY converted to a CURRENCY value since the currency datatype has a higher precedence that a string. If you want to denote a value as money/currency you should be using the dollar-sign character: $5.50

Perhaps, more importantly is that, in my opinion, you have a bad achitecture on your hands. Having to check to see if a dollar portion of a currency value is equal to 10 in order to tell what type of transaction it is seems like a bad idea. Rather, I'd suggest that you should have line items to represent charges and fees like sales tax, Shipping, handeling, etc.. Then those can be classified more easily in your database and, more importantly, constrainted.
Go to Top of Page

katethegreat
Starting Member

13 Posts

Posted - 2013-08-14 : 16:58:06
quote:
Originally posted by Lamprey

quote:
Originally posted by katethegreat

quote:
Originally posted by Lamprey

So you store your shippingcost as a string?



Hi, it's CURRENCY - so does that mean I should be using '5.50' in my CASE instead of '5.5' ? I didn't think the trailing 0 made a difference.

Thanks,

Kate the Great

Neither. Putting a value in a single quotes denotes a string. So what is happening is that the STRING value is being IMPLICITLY converted to a CURRENCY value since the currency datatype has a higher precedence that a string. If you want to denote a value as money/currency you should be using the dollar-sign character: $5.50

Perhaps, more importantly is that, in my opinion, you have a bad achitecture on your hands. Having to check to see if a dollar portion of a currency value is equal to 10 in order to tell what type of transaction it is seems like a bad idea. Rather, I'd suggest that you should have line items to represent charges and fees like sales tax, Shipping, handeling, etc.. Then those can be classified more easily in your database and, more importantly, constrainted.



I'm confused. I have two fields that have to meet certain criteria to be classified as either SHIPPING or HANDLING:

shippingtype (TEXT:100)
shippingcost (CURRENCY)

if the shippingcost (currency) line item is more than 0, then there was a shipping and/or handling charge. If the shippingtype field starts with 10, then handling was charged. The tricky part is that there may be both shipping and handling charged in the same line item, but it's denoted in the shippingtype field by a leading "10", not a value of 10, but the actual characters. Is that not a string?







As to your opinion about bad architecture, I completely agree. However I do not have access to change the architecture. I can only work with what I have to work with.

Kate the Great
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2013-08-14 : 18:04:21
Opps, I miss read something above about checking the shipping type and though it was the shipping cost. But, glad you got it sorted out. :)
Go to Top of Page

katethegreat
Starting Member

13 Posts

Posted - 2013-08-14 : 23:29:38
quote:
Originally posted by Lamprey

Opps, I miss read something above about checking the shipping type and though it was the shipping cost. But, glad you got it sorted out. :)



No problem Lamprey, there was a lot to read

I have yet to completely sort out the summary report/export though, so if you would like to help me solve this puzzle, I'd be very grateful.

Kate the Great
Go to Top of Page
   

- Advertisement -