SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need all activity by product code even if it's empty.


Need all activity by product code even if it's empty.

Author
Message
rlindstrom 38273
rlindstrom 38273
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 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.
LinksUp
LinksUp
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1712 Visits: 4640
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/
rlindstrom 38273
rlindstrom 38273
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 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.
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17333 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
rlindstrom 38273
rlindstrom 38273
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 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.
rlindstrom 38273
rlindstrom 38273
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 18
it worked as was suggested - I had a typo. thanks for the suggestions :-)
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33795 Visits: 16649
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
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17333 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
rlindstrom 38273
rlindstrom 38273
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 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?
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17333 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search