Solved

Simple left join not working as expected

Posted on 2004-04-21
2
541 Views
Last Modified: 2008-02-20
Hello All.  I have a fairly simple LEFT JOIN in which I'm trying to get a list of counts and amounts grouped by a dimension.  I want a list of every row of my dimension, with the corresponding count and amount (from two separate tables), listing 0 if there isn't anything.  The following does not work properly:

SELECT
      a.seizure_disp_desc, count(Sub_Asset_Disp_Cd) AS Number, sum(Disp_amt) AS Amount
      FROM seizure_disp_cd_dim a
      LEFT JOIN t_sub_asset b ON seizure_disp_cd = sub_asset_disp_cd
      LEFT JOIN t_disposition c ON b.sub_asset_id = c.sub_asset_id
      WHERE Disp_Dt BETWEEN '1/1/01' AND '12/31/03'
      GROUP BY a.seizure_disp_desc

However, if I remove the WHERE clause, it does work, giving my 0's where I expect it.  

Can anyone explain to me what I am missing here and what I can do to make this work?  Thanks!

J
0
Comment
Question by:keithjd
2 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 10882198
Assuming that Disp_Dt is datetime, try changing:

     WHERE Disp_Dt BETWEEN '1/1/01' AND '12/31/03'
To
     WHERE Disp_Dt BETWEEN '2001-01-01' AND '2003-12-31'
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 50 total points
ID: 10882228
which table contains the Disp_DT

if it isn't seizure_disp_cd_dim
then the where clause will be excluding all non joined rows

so if disp_dt belongs on t_disposition
re-write as
SELECT
     a.seizure_disp_desc, coalesce(count(Sub_Asset_Disp_Cd),0) AS Number, coalesce(sum(Disp_amt),0.00) AS Amount
     FROM seizure_disp_cd_dim a
     LEFT JOIN t_sub_asset b
        ON seizure_disp_cd = sub_asset_disp_cd
     LEFT JOIN ( select *
                        from t_disposition
                       WHERE Disp_Dt BETWEEN '1/1/01' AND '12/31/03') as c
      ON b.sub_asset_id = c.sub_asset_id
     GROUP BY a.seizure_disp_desc

     hth
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 66
SQL Server tables join on parse list 6 22
How to share SSIS Package? 6 36
Attaching Database Failed ? 3 39
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

786 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