Click here to Skip to main content
15,867,838 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have two query as follows

C#
select top 1 Total_Seats  from batch_seats where cbm_batch_id  ='B5176 '
  When i run the above query output as follows

  Total_Seats
      24


C#
select top 1 Avil_seats from batch_seats where cbm_batch_id  = 'B5176'
 When i run the above query output as follows

 Avil_seats
     6


Using the above two query i want output as follows
C#
Booked Seats
       18  (Total_Seats 24 - Avil_seats 6)

for getting output 18 how can i write the query.

Please help me.
Regards,
Narasiman P.
Posted
Updated 26-Nov-14 23:03pm
v2
Comments
Marcin Kozub 27-Nov-14 4:42am    
Really?
Ankur\m/ 27-Nov-14 4:47am    
That's way too basic. You should get a book and start learning instead of solving problems by asking here.
Shweta N Mishra 27-Nov-14 4:48am    
why you are using top 1?
/\jmot 27-Nov-14 13:20pm    
try..
select (Total_Seats-Avil_seats ) as BookedSeat from batch_seats where cbm_batch_id ='B5176 '

SQL
SELECT TOP 1 Total_Seats, Avil_Seats, (Total_Seats - Avil_Seats) AS Booked_Seats FROM batch_seats WHERE cbm_batch_id ='B5176 '
 
Share this answer
 
That's difficult to answer with any precision, because your initial queries are flawed. Using TOP in SQL without specifying an explicit ORDER BY is a recipe for horrible problems, because SQL does not have to return rows in any particular order unless ORDER BY is specified. The actual row returned can be any row at all, and can change from query to query with no obviously related change in the data if SQL has moved data around internally (which it is at liberty to do).

So the first thing you need to do is decide exactly what value you are trying to access, and modify your existing queries to explicitly return that.
Then you can do something like

SQL
SELECT MAX(Total_Seats) - MAX(Avil_seats) FROM batch_seats WHERE cbm_batch_id ='B5176 '


But the first thing you need to do is sort out what data you are trying to use!
 
Share this answer
 
Comments
Thanks7872 27-Nov-14 5:06am    
As i remember, this user has atleast got 6 accounts banned till now. He was used to post just list of requirements. Some of the members including me commented him over and over again regarding efforts he made.

He is clever. Now, he is posting some unclear (might be irrelevant)code so that it seems like he has tried something.

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