Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have one query if i execute i need multiple result fields based on condition.
Ex:
Suppose one table has ID and status

Query : select id,status from table
SQL
Result: 1 Result set
28	Pending
28	Pending
28	Pending
28	Pending
26	Pending
26	Pending
26	Pending
26	Pending

But i want 2 result sets like below..

SQL
1 result set 
28	Pending
28	Pending
28	Pending
28	Pending


SQL
2nd result set
26	Pending
26	Pending
26	Pending
26	Pending

based on ID i need to create tables.


Please help this.Thanks for advance.

Thanks,
Aravind G
Posted
Updated 2-Jun-15 21:09pm
v4
Comments
Maciej Los 3-Jun-15 3:32am    
What have you tried, where are you stuck?
Tomas Takac 3-Jun-15 3:36am    
There need to be two selects. Show your code and we can start from there.

Try this:

SQL
DECLARE @tmp TABLE(ID INT, [Status] VARCHAR(50))

INSERT INTO @tmp(ID, [Status])
VALUES(1, 'Pending'),(1, 'Pending'),
(1, 'Pending'),(9, 'Pending'),
(9, 'Pending'),(18, 'Pending'),
(18, 'Pending'),(18, 'Pending'),
(18, 'Pending'),(25, 'Pending'),
(25, 'Pending'),(25, 'Pending')

DECLARE @curid INT = 1
DECLARE @maxid INT = 0
SELECT @maxid = MAX(ID) FROM @tmp
WHILE (@curid<=@maxid )
BEGIN
    IF EXISTS(SELECT * FROM @tmp WHERE ID = @curid)
    BEGIN
        SELECT * FROM @tmp WHERE ID = @curid
    END
    SET @curid +=1
END



Returns result sets as expected. But i need to warn you: above code executes lots of queries in short period of time. It might cause several issues. I'd suggest to get entire data at ones and then - on client side - split data to smaller portions.
 
Share this answer
 
You need a condition based on the value of id, check out the SQL WHERE Clause[^]
Of course, then you have to execute the sql twice each with different id.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900