SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 To retrive a row even it has no data
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

archana23
Starting Member

37 Posts

Posted - 06/04/2012 :  10:09:22  Show Profile  Reply with Quote
Hi,

i have written one simple query to retrive items information from each location.

SELECT DISTINCT
CASE Location
WHEN 'Tx' THEN 'TEXAS'
WHEN 'NJ' THEN 'New Jersy'
WHEN 'NC' THEN 'North Carolina'
ELSE 'OTHER' END AS Location,
Items
FROM
ItemsByLocation where date= @date


From above query i am getting data for each location.
but For example if Location NJ has no rows for that particular date
then i am getting like

Location Items

TEXAS 120
North Carolina 75

Its not retruning any row for Location New Jercy bcoz it has no items but according to my requirement it should return with items as Zero values

Location Items

TEXAS 120

North Carolina 75

New Jercy 0


Can you please help me on this..

Archana

visakh16
Very Important crosS Applying yaK Herder

India
47157 Posts

Posted - 06/04/2012 :  10:23:32  Show Profile  Reply with Quote
you need to have a master table for locations like

Location                LocCode
TEXAS                   Tx
NewJersey               NJ
North Carolina          NC
....

and then use like 

SELECT lm.Location, 
COALESCE(i.Items ,0) AS Items
FROM LocationMaster lm
LEFT JOIN ItemsByLocation i
ON i.Location = lm.LocCode
AND i.date= @date


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

archana23
Starting Member

37 Posts

Posted - 06/04/2012 :  10:30:05  Show Profile  Reply with Quote
Thanks for reply.

I dont want to get zero's for other locations. I need to show zero for only NJ location.

Thanks..

Archana
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000