Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
I have one table which contain SelBuy flag. My table looks like,
IF OBJECT_ID(N'tempdb..#tmpList', N'U') IS NOT NULL 
Begin
	DROP TABLE #tmpList;
End
Create Table #tmpList
(
	SelSRNO Bigint,
	SelRate Float,
	SelQty Float,
	BuySrno Bigint,
	BuyRate Float,
	BuyQty Float,
	SelBuy varchar(20),
	CustName varchar(50)
)

INSERT Into #tmpList(SelSRNO,SelRate,SelQty,BuySrno,BuyRate,BuyQty,SelBuy,CustName)
Select 1 SelSRNO,10 SelRate,10 SelQty,0 BuySrno,0 BuyRate,0 BuyQty,'S' SelBuy,'Manoj' CustName
Union 
Select 2 SelSRNO,12 SelRate,12 SelQty,0 BuySrno,0 BuyRate,0 BuyQty,'S' SelBuy,'Manoj' CustName
Union 
Select 0 SelSRNO,0 SelRate,0 SelQty,7 BuySrno,40 BuyRate,30 BuyQty,'B' SelBuy,'Manoj' CustName
Union 
Select 3 SelSRNO,22 SelRate,24 SelQty,0 BuySrno,0 BuyRate,0 BuyQty,'S' SelBuy,'MANOJ-1' CustName
Union 
Select 0 SelSRNO,0 SelRate,0 SelQty,7 BuySrno,110 BuyRate,120 BuyQty,'B' SelBuy,'MANOJ-1' CustName
Union 
Select 0 SelSRNO,0 SelRate,0 SelQty,9 BuySrno,40 BuyRate,40 BuyQty,'B' SelBuy,'MANOJ-1' CustName
Select * from #tmpList
Order By CustName,SelBuy Desc


I want to select record Sel and Buy based on SelBuy Flag.
Selectd record like,

SelSRNO	SelRate	SelQty	BuySrno	   BuyRate	BuyQty	CustName
1	    10	     10	      7	       40	    30	  Manoj
2	    12	     12	      0	        0	     0	  Manoj
3	    22	     24	      7	      110	   120	  MANOJ-1
0	     0	      0	      9	       40	    40	  MANOJ-1

Please help me for select record in this format.
Posted
Comments
jaket-cp 30-Oct-15 5:08am    
A simple select search should suffice.
https://msdn.microsoft.com/en-us/library/ms189499.aspx

But it depends on the search criteria.
Can you specify how the 4 records are derived from the data in #tmpList.
John C Rayan 30-Oct-15 5:32am    
Can you tell us what is the criteria to get select output you have given. It looks to me you are missing some other column to link up the records.

1 solution

A select statement is so basic to SQL - perhaps the most basic of all. For that reason, I recommend you to this website:  [click][^]

Because of how the language is structured, if you can master SELECT you can get through perhaps 90% of what you'd need to do. The remainder comes with time.

Good Luck

 
Share this answer
 

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