Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Problem getting specific record

Author  Topic 

arslan
Starting Member

2 Posts

Posted - 2013-05-15 : 05:36:10
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

52326 Posts

Posted - 2013-05-15 : 05:46:08
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 - 2013-05-15 : 07:44:25
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

52326 Posts

Posted - 2013-05-16 : 00:23:13
welcome

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

- Advertisement -