Solved

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

Posted on 2012-03-19
3
456 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Hi, I have heard from my friends that it’s not possible to create Label Printing report using SSRS. I am amazed after hearing this words not possible in SSRS. I googled lot and found that it is possible to some of people know about the Report Bui…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

14 Experts available now in Live!

Get 1:1 Help Now