将两个内部联接查询合并到一个过程中

时间:2014-02-25 11:44:04

标签: sql sql-server stored-procedures inner-join

        SELECT TOP 15
      us.MxitId AS TransactionCreatedBy, COUNT(t.CreatedBy) Total
FROM [User] us
INNER JOIN [Transaction] t ON t.CreatedBy = us.UserId
Where ChildGender = 'Male'
GROUP BY us.MxitId, t.ChildGender 
ORDER BY 2 desc





SELECT TOP 15
      us.MxitId AS TransactionCreatedBy, COUNT(t.CreatedBy) Total
FROM [User] us
INNER JOIN [Transaction] t ON t.CreatedBy = us.UserId
Where ChildGender = 'Female'
GROUP BY us.MxitId, t.ChildGender 
ORDER BY 2 desc

我正在尝试将上述两个程序合并为一个。

请任何人帮助我,我会获得us.MxitId列的重复值。

 Select   us.MxitId AS TransactionCreatedBy,
(SELECT TOP 15
         COUNT(t.CreatedBy) TotalMale where ChildGender = 'Male') ,

          ( Select top 15 COUNT(t.CreatedBy) TotalFemale where ChildGender = 'Female')


    FROM [User] us
    INNER JOIN [Transaction] t ON t.CreatedBy = us.UserId

    GROUP BY us.MxitId, t.ChildGender 
    ORDER BY 2 desc

4 个答案:

答案 0 :(得分:1)

此查询应该可以在没有重复的MxitId的情况下获得性别计数。

SELECT TOP 15
      us.MxitId AS TransactionCreatedBy
, COUNT(t.CreatedBy) AS Total
, SUM(CASE WHEN ChildGender = 'Female' THEN 1 ELSE 0 END) AS FemaleCount
, SUM(CASE WHEN ChildGender = 'Male' THEN 1 ELSE 0 END) AS MaleCount
FROM [User] us
INNER JOIN [Transaction] t ON t.CreatedBy = us.UserId
GROUP BY us.MxitId
ORDER BY 2 desc

答案 1 :(得分:0)

 SELECT TOP 15
      us.MxitId AS TransactionCreatedBy, 
    sum(case when ChildGender = 'Male' then 1 else 0 end) TotalMale
      ,sum(case when ChildGender = 'feMale' then 1 else 0 end) TotalFeMale
FROM [User] us
INNER JOIN [Transaction] t ON t.CreatedBy = us.UserId

GROUP BY us.MxitId, t.ChildGender 

答案 2 :(得分:0)

您可以使用row_number()

执行此操作
SELECT TransactionCreatedBy, ChildGender, Total
FROM (SELECT us.MxitId as TransactionCreatedBy, t.ChildGender, COUNT(t.CreatedBy) as Total,
             row_number() over (partition by t.ChildGender order by COUNT(t.CreatedBy) desc
                               ) as seqnum
      FROM [User] us INNER JOIN
           [Transaction] t
           ON t.CreatedBy = us.UserId
      GROUP BY us.MxitId, t.ChildGender 
     ) t
WHERE seqnum <= 15
ORDER BY Total desc;

我还在select列表中添加了性别,以便您可以查看哪些行来自哪个表。

如果您想将两个计数放在同一个表中,然后按某种方式取得前15个:

      SELECT us.MxitId as TransactionCreatedBy, COUNT(t.CreatedBy) as Total,
             sum(case when t.ChildGender = 'Male' then 1 else 0 end) as TotalMales,
             sum(case when t.ChildGender = 'Female' then 1 else 0 end) as TotalFemales
      FROM [User] us INNER JOIN
           [Transaction] t
           ON t.CreatedBy = us.UserId
      GROUP BY us.MxitId, t.ChildGender 
      ORDER BY COUNT(t.CreatedBy) desc
      LIMIT 15;

答案 3 :(得分:0)

写为:

select top 15 us.MxitId AS TransactionCreatedBy,
        count(case ChildGender when 'Male' then 1 else null end) as MaleCount,
        count(case ChildGender when 'Female' then 1 else null end) as FemaleCount
from [User] us
INNER JOIN [Transaction] t ON t.CreatedBy = us.UserId
GROUP BY us.MxitId