Home Forums SQL Server 2008 T-SQL (SS2K8) Query to give sum of items with condition from multiple tables RE: Query to give sum of items with condition from multiple tables

  • You are missing some data there... or I misunderstand the request.
    is the attached table the expected output, the sample input or something else?  If it is the sample input, where are you getting the total cost from?  All I see is a cost code and a cost code ID.  If it is expected output, it is missing the total cost.  I expect that it is one of the 3 tables in your original query (likely tblCostCodes).  It is that particular join that is likely causing you greif though.  You are doing an inner join so the only rows that will be returned are the ones where there is a match.  Try changing your last inner join to a full outer join.

    But without seeing some sample data, I am really just taking stabs in the dark.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.