从查询中获取计数

时间:2015-02-09 13:47:25

标签: sql subquery correlated-subquery

Select Count(*),
(select Name from Tours Where Tours.ID = TourBooking.TourID) as Name,
(select Url from Tours Where Tours.ID = TourBooking.TourID) as Url,
(select Top 1 Name from TourImages Where TourImages.TourID = TourBooking.TourID Order By ID Asc) as ImageName,
(Select Duration +' '+ CASE WHEN  DurationType = 'd' THEN 'Day(s)' WHEN  DurationType = 'h' THEN 'Hour(s)' END from Tours Where Tours.ID = TourBooking.TourID)as Duration,
(select replace(replace('<a> Adult(s) - <c> Children','<a>', sum(case when [Type] = 1 then 1 else 0 end)                      ),
'<c>', sum(case when [Type] = 2 then 1 else 0 end)) from TourPerson Where TourPerson.BookingId = TourBooking.ID) as TotalPassengers,
StartDate,CreatedDate as BookingDate,ID as BookingID,[Status],ServicePrice
from TourBooking

如何从此查询中获取计数

1 个答案:

答案 0 :(得分:0)

  

如何从此查询中获取计数

我认为您尝试使用您的第一个COUNT(*)语句执行此操作。相反,您可以尝试嵌套整个查询并以此方式获得计数。

SELECT COUNT(innerTable.*), innerTable.* FROM (
SELECT
(select Name from Tours Where Tours.ID = TourBooking.TourID) as Name,
(select Url from Tours Where Tours.ID = TourBooking.TourID) as Url,
(select Top 1 Name from TourImages Where TourImages.TourID = TourBooking.TourID Order By ID Asc) as ImageName,
(Select Duration +' '+ CASE WHEN  DurationType = 'd' THEN 'Day(s)' WHEN  DurationType = 'h' THEN 'Hour(s)' END from Tours Where Tours.ID = TourBooking.TourID)as Duration,
(select replace(replace('<a> Adult(s) - <c> Children','<a>', sum(case when [Type] = 1 then 1 else 0 end)                      ),
'<c>', sum(case when [Type] = 2 then 1 else 0 end)) from TourPerson Where TourPerson.BookingId = TourBooking.ID) as TotalPassengers,
StartDate,CreatedDate as BookingDate,ID as BookingID,[Status],ServicePrice
from TourBooking ) as innerTable