Solved

Simple left join not working as expected

Posted on 2004-04-21
2
540 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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

762 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

9 Experts available now in Live!

Get 1:1 Help Now