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

  • 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_codedescriptionco_lineco_releaseactivity_dateactivity_seqitemtrans_amtConverted Ext Price

    PM&CPREVENTATIVE MAINTAINANCE102013-03-14 09:58:34.133104-01-01397.48000000397.4800000000000000

    PM&CPREVENTATIVE MAINTAINANCE102013-04-30 17:19:42.300104-01-011320.000000001320.0000000000000000

    PM&CPREVENTATIVE MAINTAINANCE102013-09-10 14:51:05.033104-01-011020.000000001020.0000000000000000

    PM&CPREVENTATIVE MAINTAINANCE102013-05-29 15:06:17.707104-01-01195.00000000195.0000000000000000

    REFREFERENCE MATERIALNULLNULLNULLNULLNULLNULLNULL

    RENTALRENTALNULLNULLNULLNULLNULLNULLNULL

    REPAIRCUSTOMER REPAIRNULLNULLNULLNULLNULLNULLNULL

    RETURNSCREDIT RETURNS REQ MATERIALNULLNULLNULLNULLNULLNULLNULL

    SWDVSOFTWARE DEVELOPMENTNULLNULLNULLNULLNULLNULLNULL

    Any help is greatly appreciated.

  • 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/

  • There are no Null activity dates. I'm trying to match product codes that have no activity, essentially building that list through the query.

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • 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.

  • it worked as was suggested - I had a typo. thanks for the suggestions 🙂

  • 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

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • You are correct. I did supply some sample data with expected results in my initial post - is there something else that would help?

  • 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![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • I also included the select query that works as needed until I use the WHERE clause with dates. Excuse my ignorance but not sure what else you mean.

  • rlindstrom 38273 (11/6/2013)


    I also included the select query that works as needed until I use the WHERE clause with dates. Excuse my ignorance but not sure what else you mean.

    Click on the link under my signature line on the correct way to post questions. That article will explain DDL, Insert Statements, etc . . .

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Hi -

    I think I am right in saying that you are trying to display the Product Class and Months of sales - but you may not have sales in that period - so wish to show a ZERO.

    You may require to create a Year/Month Reference Table with the product class - using this as the LEFT table - Join to the details table

    Select ProductClass, Year, Month, SUM( ProductClass_SalesValue )

    from DateProductClassTable as aa

    Left outer join ProductClassDetails as bb

    on aa.productClass = bb.productClass

    group by ProductClass, Year, Month

    Does this help or understand the problem ?

    ________________________________________________________________________________________________
    Regards
    Steve
    SQL 2008 DBA/DBD - MCTS/MCITP

    Please don't trust me, test the solutions I give you before using them.

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply