2011-06-20 62 views
0

我有3個選擇查詢:結合了COUNT(*),組選擇查詢由

查詢1:

select MMBID, MMBStartDate, MMBEnddate from MMBT1, MMBT2 
where MMBT1.Profile_ID = MMBT2.Profile_ID 

問題2:

(SELECT Profile_ID, COUNT(vp.Viewed_ProfileId) AS viewed FROM dbo.Viewed_Profiles vp 
    GROUP BY vp.Profile_Id), 

問題3:

SELECT Profile_ID, COUNT(fp.Favorite_ProfileId) AS favorites FROM dbo.Favorite_Profiles fp 
    GROUP BY fp.Profile_Id 

現在我如何將所有這些3選擇sts和g等從查詢1的follwing列作爲輸出

  • MMBID, MMBStartDate, MMBEnddate
  • 從QUERY2:COUNT(vp.Viewed_ProfileId) AS viewed
  • 從QUERY3:COUNT(fp.Favorite_ProfileId) AS favorites

感謝烏拉圭回合的幫助 孫

(SELECT Profile_ID, COUNT(vp.Viewed_ProfileId) AS viewed FROM dbo.Viewed_Profiles vp 
    GROUP BY vp.Profile_Id), 
    (SELECT Profile_ID, COUNT(fp.Favorite_ProfileId) AS favorites FROM dbo.Favorite_Profiles fp 
    GROUP BY fp.Profile_Id 
    ) 

回答

1
with one as 
(select MMBID, MMBStartDate, MMBEnddate, MMBT1.Profile_ID 
from MMBT1, MMBT2 where MMBT1.Profile_ID = MMBT2.Profile_ID), 
two as 
(SELECT Profile_ID, COUNT(vp.Viewed_ProfileId) AS viewed 
FROM dbo.Viewed_Profiles vp GROUP BY vp.Profile_Id), 
three as 
(SELECT Profile_ID, COUNT(fp.Favorite_ProfileId) AS favorites 
FROM dbo.Favorite_Profiles fp GROUP BY fp.Profile_Id) 

select one.MMBID, one.MMBStartDate, one.MMBEndDate, two.viewed, three.favorites 
from one inner join two on one.Profile_ID = two.Profile_ID 
inner join three on two.Profile_ID=three.profile_ID 
+0

現貨..謝謝你們的幫助。現在我學習了一個新的子句「With」 – Sun

0

嘗試像這樣,如果我正確理解你的問題:

select t1.Profile_ID, t1.MMBID, t1.MMBStartDate, t1.MMBEndDate, 
t2.viewed as ProfileViews, t3.viewed as FavoriteViews 
FROM 
(
Select MMBT1.Profile_ID, MMBID, MMBStartDate, MMBEndDate from MMBT1, MMBT2 where MMBT1.Profile_ID = MMBT2.Profile_ID 
)t1 
inner join 
(
SELECT Profile_ID, COUNT(vp.Viewed_ProfileId) AS viewed FROM dbo.Viewed_Profiles vp GROUP BY vp.Profile_Id 
)t2 on t1.Profile_ID = t2.Profile_ID 
inner join 
(
SELECT Profile_ID, COUNT(fp.Favorite_ProfileId) AS favorites FROM dbo.Favorite_Profiles fp 
GROUP BY fp.Profile_Id 
)t3 on t1.Profile_ID = t3.Profile_ID 
+0

'** ** FROM(...)t1'可能。 –

+0

謝謝Andriy M!當你在瀏覽器中編寫SQL時,容易忽略一些東西。 :)修正。 – mtazva

1
SELECT 
    MMBID, 
    MMBStartDate, 
    MMBEnddate, 
    vp.viewed, 
    fp.favorites 
FROM MMBT1 
    INNER JOIN MMBT2 ON MMBT1.Profile_ID = MMBT2.Profile_ID 

    INNER JOIN (
    SELECT 
     Profile_ID, 
     COUNT(Viewed_ProfileId) AS viewed 
    FROM dbo.Viewed_Profiles 
    GROUP BY Profile_ID 
) vp ON MMBT1.Profile_ID = vp.Profile_ID 

    INNER JOIN (
    SELECT 
     Profile_ID, 
     COUNT(Favorite_ProfileId) AS favorites 
    FROM dbo.Favorite_Profiles 
    GROUP BY Profile_ID 
) fp ON MMBT1.Profile_ID = fp.Profile_ID 

如果某些MMBT1.Profile_ID值沒有在任何vpfp子查詢匹配,相應的行不會被包含在結果集。如果這是不可取的,你想都來自加盟MMBT1 & MMBT2保存行,可使用LEFT JOIN的:

SELECT 
    MMBID, 
    MMBStartDate, 
    MMBEnddate, 
    COALESCE(vp.viewed, 0) AS viewed, 
    COALESCE(fp.favorites, 0) AS favorites 
FROM MMBT1 
    INNER JOIN MMBT2 ON MMBT1.Profile_ID = MMBT2.Profile_ID 

    LEFT JOIN (
    SELECT 
     Profile_ID, 
     COUNT(Viewed_ProfileId) AS viewed 
    FROM dbo.Viewed_Profiles 
    GROUP BY Profile_ID 
) vp ON MMBT1.Profile_ID = vp.Profile_ID 

    LEFT JOIN (
    SELECT 
     Profile_ID, 
     COUNT(Favorite_ProfileId) AS favorites 
    FROM dbo.Favorite_Profiles 
    GROUP BY Profile_ID 
) fp ON MMBT1.Profile_ID = fp.Profile_ID