0

I have three tables I need to pull data for, the left table needs to always display the rows that are called out in the "where". My problem is that since there are three tables I can't get this to work. I have tried joins like crazy, it works when only doing two tables, but as soon as I get the third in there it doesn't work, it then will only display rows that are not null across all three tables. I need to be able to display the specified IDs in the first table regardless if the other two tables are completely null for the specified IDs

I have a database where I only have read access, so changing the structure on this won't work. Below is my SQL code that works as it is to get me the data I want with the exception that there is a ID that doesn't display since it has no data for the day specified.

select
    [User].Id,
    [User].Name,
    convert(float,round(sum(SalesOrderJournalDetail.Price*SalesOrderJournalDetail.Shipped),2)) as 'Sales Yesterday', 
    convert(float,round(sum(SalesOrderJournalDetail.ActualCost*SalesOrderJournalDetail.Shipped),2)) as 'Cost Yesterday',
    count(distinct(SalesOrderJournalDetail.SalesOrderId)) as 'Orders Yesterday', 
    count(SalesOrderJournalDetail.SalesOrderId) as 'Lines Yesterday',
    convert(float,UserTotal.SalesMTD,2) as 'Sales MTD',
    round(convert(float,UserTotal.CostMTD,2),2) as 'Cost MTD'
from 
    [User], UserTotal, SalesOrderJournalDetail
where
    [User].Id in (' 725',' 150',' 239',' 225',' 209',' 227',' 222',' 232',' 241',' 215',' 214',' 722',' 134',' 201',' 238',' 721','M104',' 244',' 245',' 104')
and convert(varchar(10),SalesOrderJournalDetail.InvoiceDate,111) = '2012/04/19'
    and [User].Id=SalesOrderJournalDetail.SalesPersonUserId
    and SalesOrderJournalDetail.SalesPersonUserId=UserTotal.UserId
group by [User].Id, [User].Name, SalesOrderJournalDetail.SalesPersonUserId, UserTotal.UserId, UserTotal.SalesMTD, UserTotal.CostMTD 
order by [User].Name

To help explain the structure, the [User] table displays the user's ID number and Name, the SalesOrderJournalDetail table displays the current day's sales, and the UserTotal table displays the monthly sales data (via some calculations).

I need to display the day's sales and monthly sales from the latter two tables, and need to pull the user's name and ID from the first.

All three table's unique data are [User].Id, SalesJournalDetail.SalesPersonUserId, and UserTotal.UserId

Any ideas how I can get this to show all the ID's shown in the where cluase regardless if there are no day or monthly sales for that user?

Oh and this is using Microsoft SQL

1 Answer 1

1

Created tables with your structure. Very minor column name changes are there. Please change accordingly. ActualCost is ActualPrice in my table.

Following will be the query

SELECT 
    ISNULL(SalesJournalUserId,TotalSalesUserId) AS UserId,
    ISNULL(SalesJournalUserName,TotalSalesUserName) AS UserName,
    [Orders Yesterday],
    [Lines Yesterday],
    [Sales Yesterday],
    [Cost Yesterday],
    [Sales MTD],
    [Cost MTD]  FROM
(
    (
    SELECT 
        SalesUser.Id as SalesJournalUserId,
        SalesUser.Name as SalesJournalUserName,
        [Orders Yesterday],
        [Lines Yesterday],
        [Sales Yesterday],
        [Cost Yesterday]

    FROM
        [SalesUser]
    LEFT JOIN
    (
        select
            [User].Id,
            [User].Name,
            Count(distinct(SalesOrderJournalDetail.ID)) as 'Orders Yesterday',
            Count((SalesOrderJournalDetail.ID)) as 'Lines Yesterday',
            convert(float,round(sum(SalesOrderJournalDetail.Price*SalesOrderJournalDetail.Shipped),2)) as 'Sales Yesterday', 
            convert(float,round(sum(SalesOrderJournalDetail.ActualPrice*SalesOrderJournalDetail.Shipped),2)) as 'Cost Yesterday'  
        from 
            [SalesUser] as [User]
        LEFT JOIN SalesOrderJournalDetail
        on [User].Id = SalesOrderJournalDetail.SalesPersonUserId    
        where
        --[User].Id =1 and
        convert(varchar(10),SalesOrderJournalDetail.InvoiceDate,111) = '2012/04/19'
        group by [User].Id,[user].Name
    )  SOJD

    ON SalesUser.Id = SOJD.ID
    ) SOJDALLUSERS

INNER JOIN
    (
    SELECT 
        SalesUser.Id as TotalSalesUserId,
        SalesUser.Name as TotalSalesUserName,
        [Sales MTD],
        [Cost MTD] 
    FROM
        [SalesUser]

    LEFT JOIN
    (
        select
        [User].Id,
        [User].Name,
        convert(float,sum(UserTotal.SalesMTD),2) as 'Sales MTD',
        round(convert(float,sum(UserTotal.CostMTD),2),2) as 'Cost MTD' 
        from 
        [SalesUser] as [User]
        LEFT JOIN UserTotal
        on [User].Id = UserTotal.UserId    
        --where
        --[User].Id =1     
        group by [User].Id,[User].name
    ) AS SOUT 
    ON SalesUser.Id = SOUT.ID
    ) SOUTALLUSERS

    ON SOJDALLUsers.SalesJournalUserId = SOUTALLUSERS.TotalSalesUserId


) 

How it works Subquery SOJDALLUSERS: Joins SalesUser table with your SalesOrderJournalTable. Fetches data for every user

Subquery SOUT : Joins SalesUser table with UserTotal table. fetches data for every user. Your SalesMTD and CostMTD should be a "SUM" for every user if i am not wrong. You have missed it.

The above two results are inner joined to get the details for every user. Since the above subquery brings data for every user in sales table, an inner join for the above two subqueries will guarantee data for all users with appropriate null values whenever data is missing.

I have created the tables and checked following scenarios 1. data in both journal and salestotal table for a user 2. data in journal table but not in salestotal table for a user 3. data in salestotal table but not in journal table for a user.

IT WORKS.. ENJOY

Table schema for your referenceTable schema for your reference

Mark as answer if it gives you some direction into your problem. It was a nice challenge doing this query..

3
  • THANK YOU VERY MUCH, much thorough answer than I expected :) However it doesn't seem to work when I stick it into SQL Management Studio to test it. I get a "Invalid object name 'SalesUser'." error. I changed "ActualPrice" back to "ActualCost" to match my database but am unsure if there are others I need to change as well? Sorry, I'm very new to SQL. Thank you!
    – BlueCaret
    Apr 25, 2012 at 16:43
  • My table name is [SalesUser] and your table name is [User].. you need to make that change and if any if required to match your table schema... It should work since i have created the tables, inserted data and ran the above query.. just make sure the object names i.e. table and column names match your table..
    – ajay swamy
    Apr 26, 2012 at 4:05
  • Got it! It works! THANK you SO much! I've been trying to figure this out for a week and based on what the query is now, I was no where close :)
    – BlueCaret
    Apr 26, 2012 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.