منتخب بازارچه

ادغام کوئری

mostafa2

Registered User
سلام
دوستان یه مشکلی برای من پیش اومده. می خواستم بدونم چطوری میشه دو کوئری زیر رو باهم ادغام کرد:

Select
, A.ID
Sum(B.Amount) As TotalAmount
From tb1 As A
Inner Join tb2 As B
On (A.ID = B.ID)
where A.Type = 1
Group By
A.ID



Query2:


Select
, A.ID
Sum(C.Amount) As TotalAmount
From tb1 As A
Inner Join tb3 As C
On (A.ID = C.ID)
where A.Type = 2
and C.Date < Today
Group By
A.ID​
 

ahadabasi

Registered User
HTML:
Select A.ID , Sum(B.Amount) As TotalAmount
From tb1 As A
Inner Join tb2 As B
On (A.ID = B.ID)
Inner Join tb3 As C
On (A.ID = C.ID)
where A.Type = 1 and C.Date < Today
Group By A.ID
 

ahadabasi

Registered User
ممنون از جوابتون اما شرط join با جدول tb3 متفاوت هست (A.Type=2) و join بین 3 جدول جواب نمی ده.
join‌ بین هر تعداد جدولی جواب میده مشکلی نیست، برای شرط هم یه or‌ اضاف کنید
کد:
Select A.ID , Sum(B.Amount) As TotalAmount
From tb1 As A
Inner Join tb2 As B
On (A.ID = B.ID)
Inner Join tb3 As C
On (A.ID = C.ID)
where (A.Type = 1 OR A.Type = 2) AND C.Date < Today
Group By A.ID
یا اینکه از in برای چند تا or استفاده کنید:
کد:
SELECT A.ID , SUM(B.Amount) AS TotalAmount
FROM tb1 As A
INNER JOIN tb2 As B
ON (A.ID = B.ID)
INNER JOIN tb3 As C
ON (A.ID = C.ID)
WHERE A.Type IN (1,2) AND C.Date < Today
GROUP BY A.ID
 
Last edited:
بالا