Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 2nd, 2008, 10:14 AM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default table join

hello

I have created the following sql statement which works fine

SELECT I.[ItemisedGrantID], I.[ProjectID], CASE
                       WHEN I.SubCostType is NULL
                       THEN I.COSTTYPE ELSE I.COSTTYPE + ' - ' + I.SubCostType
                       END
                       AS Display,I.[total], g.Issued
, CASE WHEN G.Issued > I.Total then G.Issued - I.Total
    WHEN G.Issued < I.Total then G.Issued - I.Total
    --else 0
    end
    as 'xxx'
from V_ItemisedGrant I,
    (SELECT ItemisedGrantID, sum(Amount) as Issued
    from PROJECTITEMISEDOUTPUTCOSTS
    GROUP BY ItemisedGrantID
    )AS G
WHERE i.ItemisedGrantID = g.ItemisedGrantID


This will give me results like this

ItemisedGrantID/ProjectID/Display/Total/Issued/xxx
74/10000/Cost3/1456/1456/null
75/10000/Cost2/800/800/null
76/10001/Cost3/3244/3218/-6
77/10001/Cost2/1563/1562/-1
78/10001/cost1/2306/2314/8


the results are fine IF both tables are populated with the same ItemisedGrantID , However, if say no costs are registered in the
PROJECTITEMISEDOUTPUTCOSTS table for '10000' i want it to say 0

ie i want the query to look at all ItemisedGrant Items and only
PROJECTITEMISEDOUTPUTCOSTS if there are any, I know its a join, but I am not sure how to do it.

Would appreciate any help

Thank you


 
Old July 2nd, 2008, 10:38 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Without looking too carefully it sounds like you need a LEFT OUTER JOIN:
Code:
from V_ItemisedGrant I LEFT OUTER JOIN
    (SELECT ItemisedGrantID, sum(Amount) as Issued 
    from PROJECTITEMISEDOUTPUTCOSTS 
    GROUP BY ItemisedGrantID
    )AS G
 ON i.ItemisedGrantID = g.ItemisedGrantID
--

Joe (Microsoft MVP - XML)
 
Old July 2nd, 2008, 01:39 PM
Authorized User
 
Join Date: Oct 2007
Posts: 46
Thanks: 1
Thanked 0 Times in 0 Posts
Send a message via MSN to debbiecoates
Default

Brilliant, Thank you very much

Debbie x






Similar Threads
Thread Thread Starter Forum Replies Last Post
Excluding values from a table join dzitam SQL Language 2 April 30th, 2007 09:10 AM
can i make self join table seco MySQL 1 April 18th, 2007 11:17 PM
Join table puzzle... vanjamier SQL Language 1 November 26th, 2004 04:43 AM
could i join function(return table) with a table alyeng2000 SQL Server 2000 6 September 30th, 2004 07:23 AM
outer join on same table roog SQL Language 4 September 30th, 2004 05:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.