Counting regardless if match exists or not

  • I've tried hunting for this, but maybe I'm just wording it wrong.

    Can anyone help with trying to count records, regardless of whether a match exists or not?

    Basics:

    PRODUCT Table:

    PRODUCT_ID

    PRODUCT NAME

    MANUFACTURER table:

    MANU_ID

    MANU_NAME

    RETURN_TYPE table:

    RETURN_TYPE_ID

    RETURN_TYPE

    RETURNS Table:

    RETURNS_ID

    RETURN_DATE

    RETURN_TYPE_ID

    PRODUCT_ID

    MANU_NAME

    In this, I have a table showing returns over a given period.

    I have a pre-defined listing of products and manufacturers (any manufacturer can produce any of the products given)

    (Example, Hard Drive can be produced by Samsung, Maxtor, etc...)

    What I am looking to do is get an output that shows ALL products, by all potential manufacturers that MAY have been returned under a given type (if there were no returns, I still want to see that combination as a "0" element).

    Output sample:

    (RETURN TYPES)

    EXCHANGE UPGRADE BUYER REMORSE

    Hard Drive SAMSUNG 0 5 1

    Hard Drive MAXTOR 3 0 0

    Hard Drive SEAGATE 0 0 0

    As it sits, I am running this in a looping process where I first build a "SUM(CASE WHEN" block for the "return types".

    I then get the "product types" and looping through that,

    Get each "manufacturer" and then run a 1 line at a time SQL that looks like:

    SELECT PRODUCT, MANUFACTURER, SUM(CASE WHEN PRODUCT=x aND MANU=y THEN 1 else 0 END) as "EXCHANGE"

    This seems extremely ineffecient in that if I have 5 products and 5 manufacturers, I am running 25 different SQL statemetns to build my results (results are output througha web page, so I build one line at a time, tack it onto the previous results, then continue).

    My thinking is that there must be a way to do something like left outer join on the products->Manufacturers->Returns to get this same info (get ALL products, regardless of whether there is a manufacturer, regardless of whether there is a return or not).

    The "sum case" is the easiest part, but I'm totally lost on getting the left-side items.

    HELP!!!!! (and if this info is out there, please let me know what conditions you used to search - I've been hunting for the last 2 days and my brain is frazzled).

  • Hello,

    It looks as if it should be a fairly easy solution, but without some table structures and some sample data it is difficult to help! See http://www.sqlservercentral.com/articles/Best+Practices/61537/ on how to post ddls and sample data.

    That said, it looks as if you are over-complicating the sum case part, you should only need to do one sum case per column in you output, in fact because you are applying no other criteria than manufacturer and product, you probably only need a count(*) function, but I don't think this is the whole story as you have specified "EXCHANGE UPGRADE BUYER REMORSE" in the output — however, there are seemingly only 3 calculated columns in the output... what criteria are these other columns being calculated on?

    For just the straight count you will need something like (note this is TOTALLY untested and probably won't run as-is! need sample data to test)

    SELECT

    Product.ProductName,

    Returns.Manu_Name, -- Why manufacturer name in returns table - not normalised?

    COALESCE(COUNT(DISTINCT Returns.Returns_ID), 0) AS Exchange

    FROM Product

    LEFT JOIN Returns ON Product.Product_ID = Returns.Product_ID

    GROUP BY Product.ProductName, Returns.Manu_Name

    [/CODE]

    Apologies if this response is unclear, please provide create table code, and sample data in readily consumable format (see link above) and someone will be be able to help!

    Hope this gets you a bit further along.

    Allister

    /// edit added disclaimer on untested code

  • THanks for the info - I'll get a better structure put together.

    Th very basics of this was only shown - The "returns" tables actualyl consists of much more, but I have a view that consolidates it into the basic details I need to see.

    Waht we are looking for in the end is to do a monthly report showing the products that COULD have been returned (differnet receivers, with different types of returns based on the customer type).

    A generic cross-tab would typically be sufficient, however, based ont eh nature of the report, they want to know for a fact that we did NOT get a product/manufacturer in by showing the value as a 0 count.

    (seems a bit backwards to me - wold be easier to do a cross-tab and then direct export it to a master Excel file with vlookups, but this is not what they bosses want to do.

    As stated, I'll put together the stures as we are dealing with it and some raw data to give a better idea of what we are needing to see (and yes, I know the details I posted here are differnet from the display I was showing, but it's the same concept - just different "categories").

Viewing 3 posts - 1 through 2 (of 2 total)

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