所以我甚至不确定这是否可行但是学习并且能够做到这一点真的很棒。
我要做的是运行3个单独的查询,这些查询返回单行数据并创建一个表,然后可以在asp.net的gridview中使用
查询1
SELECT dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT
(dbo.BOOKINGS.USERID) AS bookingcount
FROM dbo.BOOKINGS INNER JOIN
dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR
WHERE (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.MAJOR.SDESCR = 'Cruises') AND
(dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A')
GROUP BY dbo.BOOKINGS.USERID
查询2
SELECT dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT
(dbo.BOOKINGS.USERID) AS bookingcount
FROM dbo.BOOKINGS INNER JOIN
dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR
WHERE (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.MAJOR.SDESCR <> 'Cruises') AND
(dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A')
GROUP BY dbo.BOOKINGS.USERID
查询3
SELECT SUM(dbo.SUBS.AMOUNT) AS total, COUNT(dbo.SUBS.AMOUNT) AS Memberships,
dbo.HOMES.USERID
FROM dbo.HOMES INNER JOIN
dbo.SUBS ON dbo.HOMES.HOME = dbo.SUBS.HOME AND dbo.HOMES.JOINED
= dbo.SUBS.PAIDON
WHERE (dbo.HOMES.JOINED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.HOMES.USERID =
@USER)
GROUP BY dbo.HOMES.USERID
所有thre查询都返回包含3列的单行 所以我认为这可能是唯一困难的部分是我想添加一个新列
query1 userid total1 bookingcount
query2 userid total2 bookingcount
query3 userid total3 memberships
答案 0 :(得分:2)
您可以使用UNION语句执行此操作,您只需获取3个select语句以返回相同数量的字段并按相同顺序
SELECT 'QUERY1' AS QUERYNAME, dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT(dbo.BOOKINGS.USERID) AS TOTAL2
FROM dbo.BOOKINGS INNER JOIN
dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR
WHERE (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.MAJOR.SDESCR = 'Cruises') AND
(dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A')
GROUP BY dbo.BOOKINGS.USERID
UNION ALL
SELECT 'QUERY2' AS QUERYNAME, dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT(dbo.BOOKINGS.USERID) AS TOTAL2
FROM dbo.BOOKINGS INNER JOIN
dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR
WHERE (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.MAJOR.SDESCR <> 'Cruises') AND
(dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A')
GROUP BY dbo.BOOKINGS.USERID
UNION ALL
SELECT 'QUERY3' AS QUERYNAME, dbo.HOMES.USERID, SUM(dbo.SUBS.AMOUNT) AS total, COUNT(dbo.SUBS.AMOUNT) AS TOTAL2
FROM dbo.HOMES INNER JOIN
dbo.SUBS ON dbo.HOMES.HOME = dbo.SUBS.HOME AND dbo.HOMES.JOINED
= dbo.SUBS.PAIDON
WHERE (dbo.HOMES.JOINED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.HOMES.USERID =
@USER)
GROUP BY dbo.HOMES.USERID
答案 1 :(得分:1)
这看起来像一个标准的UNION - 虽然你需要你的列以相同的顺序。这是科学(这将是很长的)
SELECT 'query1', dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT
(dbo.BOOKINGS.USERID) AS bookingcount
FROM dbo.BOOKINGS INNER JOIN
dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR
WHERE (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.MAJOR.SDESCR = 'Cruises') AND
(dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A')
GROUP BY dbo.BOOKINGS.USERID
UNION
SELECT 'query2', dbo.BOOKINGS.USERID, SUM(dbo.BOOKINGS.APRICE) AS total, COUNT
(dbo.BOOKINGS.USERID) AS bookingcount
FROM dbo.BOOKINGS INNER JOIN
dbo.TOURS ON dbo.BOOKINGS.TOUR = dbo.TOURS.TOUR INNER JOIN
dbo.MAJOR ON dbo.TOURS.MAJOR = dbo.MAJOR.MAJOR
WHERE (dbo.BOOKINGS.BOOKED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.MAJOR.SDESCR <> 'Cruises') AND
(dbo.BOOKINGS.USERID = @USER) AND (dbo.MAJOR.DIVISION = 'A')
GROUP BY dbo.BOOKINGS.USERID
UNION
SELECT 'query3',dbo.HOMES.USERID, SUM(dbo.SUBS.AMOUNT) AS total,
COUNT(dbo.SUBS.AMOUNT) AS Memberships
FROM dbo.HOMES INNER JOIN
dbo.SUBS ON dbo.HOMES.HOME = dbo.SUBS.HOME AND dbo.HOMES.JOINED
= dbo.SUBS.PAIDON
WHERE (dbo.HOMES.JOINED BETWEEN CONVERT(int, @startdate) AND CONVERT(int,
@enddate)) AND (dbo.HOMES.USERID =
@USER)
GROUP BY dbo.HOMES.USERID