Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

SSRS SQL Server 2008 Query to display all records from one table in report

I have a report based on two related tables.  one table is drivers and the other is load records.  tables are joined by driver number.  I have a report (query below) that counts loads a day per driver.  This works fine for drivers that had a load that day.  for example the driver table has 25 drivers in it, only 15 drivers had loads on 2/12 so the names of only 15 drivers show up.  The user wants ALL drivers to show up on the report and if they had no loads it will report a 0.  

I have tried a join showing all records from driver table, but still do not get all drivers listed if they had no loads.

Any ideas on how to list all drivers regardless if they had loads that day?


WITH ShippersPerDriver AS (SELECT     D.ENumber, COUNT(DISTINCT M.Shipper) AS ShipperCNT, M.ShDate
                                                               FROM         Malt AS M INNER JOIN
                                                                                     Tbl_Driver AS D ON M.Driver = D.ENumber
                                                               GROUP BY D.ENumber, M.ShDate)
    SELECT     Malt.ShDate, Malt.FrtBill, Malt.Shipper, Tbl_Driver.LastName, Tbl_Driver.FirstName, SHP.ShipperCNT, SHP.ENumber, SHP.ShDate AS Expr1
     FROM         Malt LEFT OUTER JOIN
                            ShippersPerDriver AS SHP ON Malt.Driver = SHP.ENumber AND Malt.ShDate = SHP.ShDate LEFT OUTER JOIN
                            Tbl_Driver ON Malt.Driver = Tbl_Driver.ENumber
     WHERE     (Malt.ShDate BETWEEN @StartDate AND @EndDate)
     ORDER BY Tbl_Driver.LastName
0
rtay
Asked:
rtay
  • 2
1 Solution
 
DcpKingCommented:
I think you're making things too complicated too soon in your development.

Try just linking your Driver and Load tables on driver number (using inner join) and pulling only the driver name fields and, from the Load table, the date. Then constrain it to 2/12 and you'll get those 15 records.

Then try the various other types of join (left and right outer, and full outer). One will give you your full set of drivers, and nulls in some records for the field  from the Load table. Then build it from there.

hth

Mike
0
 
rtayAuthor Commented:
I have done that in my testing.  I just sent my over all working code.  What I have been unable to get is the nulls for for drivers with no records.  I have tried inner joins and get a result of let's say 10 drivers with load records instead of 15 drivers, 5 with loads.  

Your are correct though, that query example is more complicated than than the help I am looking for.  I just need a simple example that will supply the nulls.

Thanks
0
 
DcpKingCommented:
Inner Joins won't cut it. You need to use outer joins to get the missing rows too. Try this:

select d.LastName, d.FirstName, m.ShDate
from Tbl_Driver d
left outer join Malt m
on m.Driver = d.eNumber
where m.ShDate = @dtDate

with @dtDate set to whatever date will give you known results - like 2/12, I think you said.

You should get all the drivers, but nulls in some rows in the date column.

Expand the complexity from there.

hth

Mike
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now