Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Need all activity by product code even if it's empty. Expand / Collapse
Author
Message
Posted Monday, November 4, 2013 9:25 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 8:28 AM
Points: 6, Visits: 18
Trying to show all activity (typically dollars) and eventually group by product code. there are some months when some (if not most) have no activity. in that case we should show 0.

Below is a query that works as long as I don't add the WHERE clause to filter by date - if we do, then the product codes with NULL get dropped.

SELECT
prodcode.product_code,
prodcode.description,
coitem_log.co_line,
coitem_log.co_release,
coitem_log.activity_date,
coitem_log.activity_seq,
coitem_log.item,
coitem_log.trans_amt,
(coitem_log.trans_amt / co.exch_rate) as 'Converted Ext Price'
FROM co
INNER JOIN coitem_log ON co.co_num = coitem_log.co_num
LEFT OUTER JOIN item ON coitem_log.item = item.item
RIGHT OUTER JOIN prodcode ON item.product_code = prodcode.product_code
--where (coitem_log.activity_date between '7/1/2013 00:00:00:000' and '7/31/2013 23:59:59:999')
where co.end_user_type is Null


This is a sample of what we want returned even if we filter by date(s):

product_code description co_line co_release activity_date activity_seq item trans_amt Converted Ext Price
PM&C PREVENTATIVE MAINTAINANCE 1 0 2013-03-14 09:58:34.133 1 04-01-01 397.48000000 397.4800000000000000
PM&C PREVENTATIVE MAINTAINANCE 1 0 2013-04-30 17:19:42.300 1 04-01-01 1320.00000000 1320.0000000000000000
PM&C PREVENTATIVE MAINTAINANCE 1 0 2013-09-10 14:51:05.033 1 04-01-01 1020.00000000 1020.0000000000000000
PM&C PREVENTATIVE MAINTAINANCE 1 0 2013-05-29 15:06:17.707 1 04-01-01 195.00000000 195.0000000000000000
REF REFERENCE MATERIAL NULL NULL NULL NULL NULL NULL NULL
RENTAL RENTAL NULL NULL NULL NULL NULL NULL NULL
REPAIR CUSTOMER REPAIR NULL NULL NULL NULL NULL NULL NULL
RETURNS CREDIT RETURNS REQ MATERIAL NULL NULL NULL NULL NULL NULL NULL
SWDV SOFTWARE DEVELOPMENT NULL NULL NULL NULL NULL NULL NULL


Any help is greatly appreciated.
Post #1511168
Posted Monday, November 4, 2013 10:41 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Today @ 10:42 PM
Points: 361, Visits: 1,548
To make sure any answer is correct, please read the link under the signature line on how to post questions.



Can't you just do the following to pick up the null dates:

where ((coitem_log.activity_date between '7/1/2013 00:00:00:000' and '7/31/2013 23:59:59:999') or coitem_log.activity_date is null)




__________________________________________________________________________________________________________
How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1511204
Posted Monday, November 4, 2013 10:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 8:28 AM
Points: 6, Visits: 18
There are no Null activity dates. I'm trying to match product codes that have no activity, essentially building that list through the query.
Post #1511208
Posted Monday, November 4, 2013 7:56 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
rlindstrom 38273 (11/4/2013)
There are no Null activity dates. I'm trying to match product codes that have no activity, essentially building that list through the query.


The RIGHT JOIN to the Product Codes table is generating NULL activity dates. You're even showing them in your expected output (as NULLs).

LinksUp (11/4/2013)
To make sure any answer is correct, please read the link under the signature line on how to post questions.



Can't you just do the following to pick up the null dates:

where ((coitem_log.activity_date between '7/1/2013 00:00:00:000' and '7/31/2013 23:59:59:999') or coitem_log.activity_date is null)




LinksUp's suggestion is close but I'd suggest to use the best practice form for filtering on dates, as follows:

WHERE ((coitem_log.activity_date >= '2013-07-01' AND coitem_log.activity_date < '2013-08-01') or coitem_log.activity_date IS NULL)






My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1511348
Posted Tuesday, November 5, 2013 7:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 8:28 AM
Points: 6, Visits: 18
Since I'm new to this maybe another shot at explaining.

There are no empty records in the coitem_log table - every time a line item on a order is changed an entry is created. When I run this wide open I get my list of all product codes and items that match each product code - NULLS are what I'm expecting if a specific product code didn't have activity for any of its corresponding items.

Most months there are some product codes that DON'T have corresponding items on a order but each month I need a complete list of all the product codes and activity for any items that match it's corresponding product code.

thanks for the suggestions thus far - hope this helps.

Post #1511498
Posted Tuesday, November 5, 2013 8:39 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 8:28 AM
Points: 6, Visits: 18
it worked as was suggested - I had a typo. thanks for the suggestions
Post #1511511
Posted Tuesday, November 5, 2013 8:43 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 7:24 AM
Points: 5,430, Visits: 10,099
I think what's happening is that the WHERE clause on the dates is turning your outer join into an inner join. It's difficult to visualise with no sample data, but try turning the WHERE clause into a join predicate, something like this:

FROM co
INNER JOIN coitem_log ON co.co_num = coitem_log.co_num
LEFT OUTER JOIN item ON coitem_log.item = item.item AND coitem_log.activity_date between '7/1/2013 00:00:00:000' and '7/31/2013 23:59:59:99
RIGHT OUTER JOIN prodcode ON item.product_code = prodcode.product_code
where co.end_user_type is Null

John
Post #1511513
Posted Tuesday, November 5, 2013 5:27 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
rlindstrom 38273 (11/5/2013)
Since I'm new to this maybe another shot at explaining.

There are no empty records in the coitem_log table - every time a line item on a order is changed an entry is created. When I run this wide open I get my list of all product codes and items that match each product code - NULLS are what I'm expecting if a specific product code didn't have activity for any of its corresponding items.

Most months there are some product codes that DON'T have corresponding items on a order but each month I need a complete list of all the product codes and activity for any items that match it's corresponding product code.

thanks for the suggestions thus far - hope this helps.



I believe that I understand what you are saying. For example, if you date filtering criteria spans 2 months, you'd like any product code listed with a NULL activity date to show once or twice depending on whether it was missing from either or both of those months. Is that accurate?

This can be done but it is easier to provide you a working example if you were to help us out with some DDL, consumable sample data and expected results.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1511675
Posted Wednesday, November 6, 2013 6:20 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 8:28 AM
Points: 6, Visits: 18
You are correct. I did supply some sample data with expected results in my initial post - is there something else that would help?
Post #1511848
Posted Wednesday, November 6, 2013 6:27 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 10:53 PM
Points: 3,421, Visits: 5,359
rlindstrom 38273 (11/6/2013)
You are correct. I did supply some sample data with expected results in my initial post - is there something else that would help?


Yes please. DDL that can be run to create the necessary tables. INSERT statement(s) to populate the data into the tables.

It doesn't necessary need to be all the products you show, but you do need to show at least 2 months worth of activity.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1511851
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse