我想重复计数1,但它与每个记录重复1。你能建议做什么?想要一个计数,但它与每一个记录
SELECT Count(*),
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
WHERE memberid = 6)AS innerTable
GROUP BY innerTable.NAME,
innerTable.bookingdate,
innerTable.bookingid,
innerTable.duration,
innerTable.imagename,
innerTable.serviceprice,
innerTable.startdate,
innerTable.status,
innerTable.totalpassengers,
innerTable.url
这听起来像[XY问题](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。请给我们你的表与数据和你期望什么,而不是你的方法来实现它。 – 2015-02-10 07:17:12
因为所有内部查询都可以使用连接进行组合。 – 2015-02-10 07:25:06
SELECT COUNT(*),T *从(SELECT Tours.Name,Tours.Duration,Tours.DurationType,Tours.Url,TourImages.Name作为ImageName,TourBooking.CreatedDate为BookingDate, TourBooking.ID为BookingID,TourBooking。起始日期作为开始日期,作为TourBooking.ServicePrice ServicePrice 从TourBooking加入图尔上TourBooking.TourID = Tours.ID 加入TourImages上TourImages.TourID = TourBooking.TourID 其中TourBooking.MemberID = 6)为T 组由 T.BookingDate , t.BookingID, t.Duration, t.DurationType, t.ImageName, t.Name, t.ServicePrice, t.StartDate, t.Url – Mike 2015-02-10 07:45:40