So I am not even sure if this is possible but it really would be awesome to learn and to be able to do this.
What I am trying to do is run 3 seperate queries that return a single row of data and create a single table that can then be used in a gridview in asp.net
query 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
query 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
query 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 db开发者_StackOverflow中文版o.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
all thre queries return a single row with 3 columns so i figure this could work the only other difficult part is i want to add a new column
query1 userid total1 bookingcount
query2 userid total2 bookingcount
query3 userid total3 memberships
You can do this using a UNION statement, all you have to do is get the 3 select statements to return the same amount of fields and in the same order
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
this looks like a standard UNION - albeit you need your columns in the same order. Here comes the science (this'll be long)
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
精彩评论