我不知道正确的语法是什么,但希望您了解我想要实现的目标:
SELECT Count(BookCategory) AS TotalBookAC
FROM Passenger
WHERE BookCategory='AC';
SELECT Count(BookCategory) AS TotalBookGen
FROM Passenger
WHERE BookCategory='General';
...
加入两个或多个sql语句需要keyword
一个?
乘客的表格:
TicketID - PK
TrainNumber - FK
BookDate - As Date
PassengerName - varchar
Age - num
Sex - varchar
Address - varchar
ReservationStatus - 'waiting' OR 'confirmed'
BookCategory - 'CP' OR 'General'
TrainList 的表格:
TrainNumber - PK
TrainName - varchar
Source - varchar (place)
Destination - varchar (place)
FareAC - num (currency)
FareGeneral - num (currency)
DayAvailable - varchar (weekdays)
现在我正在努力学习如何构建一个SQL语句来查询 TrainList.TrainNumber 以及 AC 的Count
字段 Passenger.BookCategory 中的常规。
这是我想要实现的示例输出
+-------------+---------------+--------------+--------------+--------------+
| TrainNumber | TotalSeatsGen | TotalSeatsAC | TotalBookGen | TotalBookAC |
+-------------+---------------+--------------+--------------+--------------+
| 1 | 10 | 12 | 11 | 13 |
+-------------+---------------+--------------+--------------+--------------+
^ . . .
DISTINCT <-- specific records for this TrainNumber only -->
答案 0 :(得分:1)
您想要从表Passenger中选择所有记录并计算出现次数。所以你必须扫描整个表并有条件地计数。在标准SQL中,您将使用CASE WHEN;在MS Access中,您使用IIF。计数用SUM完成;你每场比赛增加一个,所以你实际上在计算。
SELECT
TrainNumber,
SUM( IIF(BookCategory = 'General' and ReservationStatus = 'confirmed', 1, 0) ) AS TotalSeatGen,
SUM( IIF(BookCategory = 'AC' and ReservationStatus = 'confirmed', 1, 0) ) AS TotalSeatAC,
SUM( IIF(BookCategory = 'General', 1, 0) ) AS TotalBookGen,
SUM( IIF(BookCategory = 'AC', 1, 0) ) AS TotalBookAC
FROM Passenger
GROUP BY TrainNumber
ORDER BY TrainNumber;
答案 1 :(得分:0)
简单的答案,
声明@Passenger表(id int,BookCategory varchar(50))
插入@Passenger 值(1,&#39; AC&#39;),(2,&#39; AC&#39;),(3,&#39;一般&#39;),(4,&#39; AC&#39;) ;),(5,&#39;通用&#39;),(6,&#39; AC&#39;),(7,&#39; AC&#39), (8,&#39;通用&#39;),(9,&#39; AC&#39)
从@Passenger中选择*
SELECT BookCategory, - Count(BookCategory)totalcount
BookCategory =&#39; AC&#39;然后Count(BookCategory)结束AS TotalBookAC,
BookCategory =&#39; General&#39;然后Count(BookCategory)结束AS TotalBookGen
FROM @Passenger
在哪里
BookCategory in(&#39; AC&#39;,&#39; General&#39;) 按BookCategory分组
选择*来自 (
SELECT BookCategory,Count(BookCategory)totalcount
- BookCategory =&#39; AC&#39;然后Count(BookCategory)结束AS TotalBookAC,
- BookCategory =&#39; General&#39;然后Count(BookCategory)结束AS TotalBookGen
FROM @Passenger
在哪里
BookCategory in(&#39; AC&#39;,&#39; General&#39;) 按BookCategory分组 ) 一个
答案 2 :(得分:0)
SELECT BookCategory, Count(*)
FROM Passenger
GROUP BY BookCategory;
然后在应用程序的表示层中完成剩下的工作
答案 3 :(得分:0)
为您需要的每个计数创建单独的查询。
我们假设他们将被称为qryTotalSeatsGen,qryTotalSeatsAC,qryTotalBookGen,qryTotalBookAC。
每个查询都有两个字段:TrainNumber
和Total
现在使用上述所有查询创建最终查询。将他们全部加入TrainNumber
,并在该字段中包含每个人的总数。
SELECT qryTotalSeatsGen.Total AS TotalSeatsGen,
qryTotalSeatsAC.Total AS TotalSeatsAC,
qryTotalBookGen.Total AS TotalBookGen,
qryTotalBookAC.Total AS TotalBookAC
FROM (qryTotalSeatsGen INNER JOIN qryTotalSeatsAC ON qryTotalSeatsGen.TrainNumer = qryTotalSeatsAC.TrainNumer)
INNERJOIN ....
使用Ms Access查询构建器(就像上面我用手写的查询只是为了给你一个想法,它可能不准确)。
答案 4 :(得分:-1)
CREATE TABLE #FinalResult
(
Tno INT,
TotSeatGen INT,
TotSeatAC INT,
TotBookGen INT,
TotBookAC INT
)
INSERT INTO #FinalResult(Tno) VALUES (1)
UPDATE #FinalResult SET TotBookAC = (SELECT Count(BookCategory) AS TotalBookAC FROM Passenger WHERE BookCategory='AC') WHERE Tno =1
UPDATE #FinalResult SET TotBookGen = (SELECT Count(BookCategory) AS TotalBookGen FROM Passenger WHERE BookCategory='General') WHERE Tno =1
............
............
............
SELECT * FROM #FinalResult