0
Ich habe den folgenden Code, und ich möchte den Union-Befehl verwenden, um sie zu kombinieren.Ich habe versucht, den Union-Befehl mit und ohne die letzte Tabelle (12) .Danke voraus.Wie verwende ich Union innerhalb while? (SQL)
DECLARE @COUNTER as int
SET @COUNTER = 0
WHILE @COUNTER < 12
BEGIN
SET @COUNTER = @COUNTER + 1
SELECT e.Month,e.Code,e.AVERAGE,f.AVERAGE
FROM (SELECT @COUNTER as Month,Clients.Clients_Code as Code, AVG(Transactions.Amount_of_indebtedness) as AVERAGE
FROM Account INNER JOIN
Clients ON Account.Account_Number = Clients.Account_Number INNER JOIN
Credit_Card ON Account.Account_Number = Credit_Card.Account_Number INNER JOIN
Transactions ON Credit_Card.Credit_Number = Transactions.Credit_Number
WHERE (YEAR(Transactions.Date_and_time_of_transaction) = '2009') AND (MONTH(Transactions.Date_and_time_of_transaction) < @COUNTER)
GROUP BY Clients.Clients_Code)e,
(SELECT @COUNTER as Month,Clients.Clients_Code as Code, AVG(Transactions.Amount_of_indebtedness) as AVERAGE
FROM Account INNER JOIN
Clients ON Account.Account_Number = Clients.Account_Number INNER JOIN
Credit_Card ON Account.Account_Number = Credit_Card.Account_Number INNER JOIN
Transactions ON Credit_Card.Credit_Number = Transactions.Credit_Number
WHERE (YEAR(Transactions.Date_and_time_of_transaction) = '2009') AND (MONTH(Transactions.Date_and_time_of_transaction) >= @COUNTER)
GROUP BY Clients.Clients_Code)f
WHERE f.Code=e.Code AND e.Month=f.Month AND f.AVERAGE>e.AVERAGE
END