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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 help me Query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

niyaz4872
Starting Member

Iran
40 Posts

Posted - 01/10/2013 :  03:13:19  Show Profile  Reply with Quote
following is my table structure

id count
01 2
04 1
09 5

following is sample results i wan't

id count
01 2
02 0
03 0
04 1
05 0
06 0
07 0
08 0
09 5
10 0

mahdi87_gh
Yak Posting Veteran

Iran
72 Posts

Posted - 01/10/2013 :  03:19:36  Show Profile  Reply with Quote
use Right Join Or LefJoin in your query
could u put your entire query?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  03:20:37  Show Profile  Reply with Quote
does id values come from a master table?

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

Go to Top of Page

niyaz4872
Starting Member

Iran
40 Posts

Posted - 01/10/2013 :  03:26:56  Show Profile  Reply with Quote
yes .and max id is ten
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  03:31:12  Show Profile  Reply with Quote
then you can do like

SELECT m.id,COALESCE(t.[count],0) AS [count]
FROM MasterTable m
LEFT JOIN YourTable t
ON t.id = m.id


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

Go to Top of Page

niyaz4872
Starting Member

Iran
40 Posts

Posted - 01/10/2013 :  03:40:01  Show Profile  Reply with Quote
tnx.
if id does not values come from a master table?
results i wan't
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  03:40:58  Show Profile  Reply with Quote
Are you telling that you need to generate id on the fly?

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

Go to Top of Page

niyaz4872
Starting Member

Iran
40 Posts

Posted - 01/10/2013 :  03:43:04  Show Profile  Reply with Quote
yes
Go to Top of Page

niyaz4872
Starting Member

Iran
40 Posts

Posted - 01/10/2013 :  03:44:37  Show Profile  Reply with Quote
i don't master table
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  03:46:22  Show Profile  Reply with Quote
then how will you decide on upper limit? will it be always 10?

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

Go to Top of Page

niyaz4872
Starting Member

Iran
40 Posts

Posted - 01/10/2013 :  03:48:16  Show Profile  Reply with Quote
yes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  03:53:32  Show Profile  Reply with Quote

;With NumberTable(N)
AS
(
SELECT 1
UNION ALL
SELECT N + 1
FROM NumberTable
WHERE N + 1 <= 10
)

SELECT n.N,COALESCE(t.[count],0) AS [count]
FROM NUmberTable n
LEFT JOIN YourTable t
ON t.id = n.N


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

Go to Top of Page

niyaz4872
Starting Member

Iran
40 Posts

Posted - 01/10/2013 :  04:09:11  Show Profile  Reply with Quote
tnx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/10/2013 :  04:13:06  Show Profile  Reply with Quote
wlcm

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

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.12 seconds. Powered By: Snitz Forums 2000