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 2012 Forums
 Transact-SQL (2012)
 Problem getting specific record
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arslan
Starting Member

2 Posts

Posted - 05/15/2013 :  05:36:10  Show Profile  Reply with Quote
Hi Experts,

i've a problem and that is : i've two tables

            Category                        Items  
 Cat_Id(PK)        Cat_Name      Item_ID(PK)   Item_Name   Cat_Id(FK)
    1               abc             1            Item1         2
    2               Kar             2            Item2         1
    3               Isb             3            Item3         1
                                    4            Item4         3
                                    5            Item5         2 

i want to display data from "Items" on the basis of Cat_Id(FK)..
For Example:

Category 1
Item2
Item3

Category 2
Item1
Item5

Category 3
Item4

How can i do this in one Sql query/Procedure.... I've tried enough myself with different conditions and logics but in vain....

Please Help me..

Thanx in Advance...

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/15/2013 :  05:46:08  Show Profile  Reply with Quote
you can use query like below

SELECT Cat_Name,Cat_Id,CAST(0 AS int) AS Item_ID
FROM Category                        
UNION ALL
SELECT i.Item_Name,c.cat_ID,i.Item_ID
FROM Items i
INNER JOIN Category c
ON c.Cat_ID = i.Cat_ID
ORDER BY Cat_ID,Item_ID


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

arslan
Starting Member

2 Posts

Posted - 05/15/2013 :  07:44:25  Show Profile  Reply with Quote
Thank you for the help Your ans gives me an idea and i've resolve my problem thanx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 05/16/2013 :  00:23:13  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
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.08 seconds. Powered By: Snitz Forums 2000