我想重複計數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