将选择查询与count(*),group by组合在一起

时间:2011-06-20 21:21:50

标签: sql sql-server tsql

我有3个选择查询:

查询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并将以下列作为输出

    来自Query1的
  • 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
   )

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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