Solved

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

Posted on 2012-03-19
3
450 Views
Last Modified: 2012-03-21
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
Comment
Question by:rtay
  • 2
3 Comments
 
LVL 16

Expert Comment

by:DcpKing
ID: 37740590
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
 
LVL 5

Author Comment

by:rtay
ID: 37740664
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
 
LVL 16

Accepted Solution

by:
DcpKing earned 500 total points
ID: 37741573
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Written by Valentino Vranken. Introduction: The first step of creating a SQL Server Reporting Services (SSRS) report involves setting up a connection to the data source and programming a dataset to retrieve data from that data source.  The data‚Ķ
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now