【问题标题】:Combine two inner join queries into one procedure将两个内连接查询合并到一个过程中
【发布时间】:2014-03-27 14:51:26
【问题描述】:
        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

【问题讨论】:

  • 你想如何组合它们?男孩的前 15 名用户可能与女孩的前 15 名用户完全不同。或者,它们可能是相同的列表,但顺序不同。向我们展示您希望输出的样子。
  • 嗨,两种情况下的用户都是一样的,如果用户没有记录任何女性数据,我想说 0,反之亦然

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


【解决方案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 这样的 cmets 和谢谢.. ;-)
【解决方案2】:
 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 

【讨论】:

  • Msg 102, Level 15, State 1, Line 3 ')' 附近的语法不正确。消息 102,级别 15,状态 1,第 3 行“TotalMale”附近的语法不正确。消息 102,级别 15,状态 1,第 4 行“TotalFeMale”附近的语法不正确。
  • 把结束放在括号内
  • 嗨,数据仍在重复
  • 我不知道它们的关系表结构。
【解决方案3】:

您可以使用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;

【讨论】:

    【解决方案4】:

    写成:

    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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多