25 Temmuz 2008 Cuma

TSQL de Inner Join ve Group By birlikte kullanmak

DECLARE @ay AS INT
SET @ay=1
DECLARE @yil AS INT
SET @yil=2008

SELECT h.hasta_id, h.adi+' '+h.soyadi [Adı Soyadı], s.total
FROM hastalar as h
INNER JOIN
(
  SELECT refHasta_id, count(1) AS total
  FROM seanslar AS s
  WHERE s.refKlinik_id=71 AND month(tarihi)=@ay AND year(tarihi)=@yil AND durumu=1
  GROUP BY refHasta_id
) AS s
ON s.refHasta_id=h.hasta_id
ORDER BY h.adi+' '+h.soyadi




hasta_id Adı Soyadı                                total
----------- -------------------                      -----------
25123       ABDULKADİR KARAHAN  13
27420       ABDULKADİR UÇAN             9
24097       ABDULLAH DAKMAN           8
25091       ABDULLAH TUNÇ                 14

Hiç yorum yok: