0
J'ai le code suivant et je veux utiliser la commande union pour les combiner.J'ai essayé d'utiliser la commande union avec et sans la dernière table (12) .Merci dans avance.Comment utiliser union dans 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