如何使用sql server计算预订的座位数 [英] How to calculate the booked seats using sql server

查看:91
本文介绍了如何使用sql server计算预订的座位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个查询



i have two query as follows

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







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

Booked Seats
       18  (Total_Seats 24 - Avil_seats 6)



获取输出18如何编写查询。



请帮助我。

问候,

Narasiman P.


for getting output 18 how can i write the query.

Please help me.
Regards,
Narasiman P.

推荐答案

SELECT TOP 1 Total_Seats, Avil_Seats, (Total_Seats - Avil_Seats) AS Booked_Seats FROM batch_seats WHERE cbm_batch_id ='B5176 '


这很难以任何精确度回答,因为你初始查询存在缺陷。在SQL中使用TOP而不指定显式ORDER BY是一个可怕问题的处方,因为除非指定了ORDER BY,否则SQL不必以任何特定顺序返回行。返回的实际行可以是任何行,并且可以从查询更改为查询,如果SQL已在内部移动数据(可以随意执行),则数据中没有明显相关的更改。



因此,您需要做的第一件事就是确切地确定您要访问的值,并修改现有查询以明确返回该值。

那么你可以做类似的事情



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

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!


这篇关于如何使用sql server计算预订的座位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆