Cannot return correct value in SQL statement

  • I need to create a commission report. basically if you are a certain salesperson 0 commission, if you sell to certain customers, 0 commission, certain products have 0 commission, and finally if non of those conditions apply then look up the commission percentage based on the family code.

    attached is a word document with the SQL code, and an Excel file with the answer set.

    The code seems correct, but of course is not. Just wondering if someone can explain where I went wrong, thank you.

  • Maybe you should read this. Most people don't want to download files from people they don't know...

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • See if you can try posting some sample data from your table(Just a few rows should suffice) and also paste in your code.

  • The column t00.[CustNo] is never NULL, so you'll always end up with a value of 0.

    You don't need the nested CASE, you can just add more conditions as needed.

    Select t00.[InvDate]

    ,t00.[CustNo]

    ,t00.[Salesman]

    ,t00.[Quantity]

    ,t00.[ExtendedPrice]

    ,t00.[ExtendedCost]

    ,t00.[ItemNo]

    ,t00.[InvoiceNo]

    ,t00.[LineNo]

    ,t00.[Warehouse]

    ,t00.[chargecust]

    ,t01.[customerid] AS CustomerIdT01

    ,t02.[salesman] AS SalesmanT02

    ,t03.itemid AS ItemIdT03

    ,t03.[commissionperc]AS ItemComPercT03

    ,t04.[productfamilyid] AS ProductFamilyIdT04

    ,T05.[ProductFamilyID] AS ProductFamilyIdT05

    ,t05.[commissionperc]as ProdFamilyComPercT05

    ,T06.[Customerid] AS CustomerIdT06

    ,t06.[commissionperc] AS CommissionPercT06

    ,Case when [t00].[CustNo]is not null then 0

    when [t02].[salesman]is not null then 0

    when [t06].[customerid] is not null then [t06].[commissionperc]

    when [t03].[ItemID] is Not Null then [t03].[commissionperc]

    Else [t05].[commissionperc] end AS BaseCommPerc

    FROM [US02301_DWH].[dbo].[Fact_InvoiceAnalysis]t00

    left join [dbo].[manualNOcommissioncustomer]as t01 on t00.[chargecust]=t01.[customerid]

    left join [dbo].[manualNOcommissionsalesgroup] as t02 on t00.[salesman]=t02.[salesman]

    left join [dbo].[manualcommissionitem] as t03 on t00.[itemno]=t03.[itemid]

    left join [dbo].[itemmaster] as t04 on t00.[itemno] = t04.[itemid]

    left join [dbo].[ManualCommisionProductFamily]as t05 on [t04].[productfamilyid] = [t05].[productfamilyid]

    LEFT JOIN [dbo].[ManualFixedCommissionCustomer] as T06 on [t00].[custno]=[t06].[customerid]

    where YEAR([invdate])='2015' and T00.salesman='16'

    order by t00.Salesman desc

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

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

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