从视图计算Count(SQL)

时间:2016-06-30 13:57:47

标签: sql sql-server count subquery

我有这个观点:

SELECT [ID]
      ,[PersonName]
      ,[PersonFUNCTION]
      ,[GUESTName]
      ,[Team]
      ,[sector]
      ,[MeetingCity]
      ,[GUESTCOMPANY]
      ,[TypeMeeting]
  FROM [DB_TEST].[dbo].[Meetings]

从这个角度来看,我们可以阅读例子:

名为“XXX”(PersonName)的人是首席执行官(PersonFUNCTION),他在巴黎(会员城)与“马克·扎克伯格”(GUESTName)会面,“Facebook”是(GUESTCOMPANY),最后是会议是“一对一会议”(TypeMeeting)!

PS:请注意,例如,XXX可以在不同的城市与马克·扎克伯格会面一次。

我想做的是:

添加3列:计数(一对一会议)和计数(一到几次会议)和计数(小组会议)

  • 计数(一对一会议)= [PersonName]在一对一会议中遇到[GUESTName]的次数,无论城市是否不同或其他任何不同......

类似的东西:

SELECT [ID]
      ,[PersonName]
      ,[PersonFUNCTION]

     ,Count( One to One Meeting between PersonName and GUESTName )  ?
     ,Count( One to Few Meeting between PersonName and GUESTName)  ?
     ,Count ( Group Meeting between PersonName and GUESTName) ?

      ,[GUESTName]
      ,[Team]
      ,[sector]
      ,[MeetingCity]
      ,[GUESTCOMPANY]
      ,[TypeMeeting]
  FROM [DB_TEST].[dbo].[Meetings]

谢谢

4 个答案:

答案 0 :(得分:1)

这样的事情可以帮助你获得所有列和计数。

SELECT  [ID],
        [PersonName],
        [PersonFUNCTION],
        m2.OneToOneCount, --Count( One to One Meeting between PersonName and GUESTName )
        m2.OneToFewCount, --Count( One to Few Meeting between PersonName and GUESTName)
        m2.GroupCount,    --Count ( Group Meeting between PersonName and GUESTName) 
        [GUESTName],
        [Team],
        [sector],
        [MeetingCity],
        [GUESTCOMPANY],
        [TypeMeeting]
FROM    [DB_TEST].[dbo].[Meetings] m
        CROSS APPLY (SELECT COUNT(CASE WHEN m2.[TypeMeeting] = 'OneToOne' THEN 1 END) AS OneToOneCount,
                            COUNT(CASE WHEN m2.[TypeMeeting] = 'OneToFew' THEN 1 END) AS OneToFewCount,
                            COUNT(CASE WHEN m2.[TypeMeeting] = 'Group' THEN 1 END) AS GroupCount
                     FROM   [DB_TEST].[dbo].[Meetings] m2
                     WHERE  m2.[PersonName] = m.[PersonName]
                            AND m2.[GUESTName] = m.[GUESTName]) m2

如果你不能使用CROSS APPLY,这是一个JOIN替代方案。

SELECT  [ID],
        [PersonName],
        [PersonFUNCTION],
        m2.OneToOneCount, --Count( One to One Meeting between PersonName and GUESTName )
        m2.OneToFewCount, --Count( One to Few Meeting between PersonName and GUESTName)
        m2.GroupCount,    --Count ( Group Meeting between PersonName and GUESTName) 
        [GUESTName],
        [Team],
        [sector],
        [MeetingCity],
        [GUESTCOMPANY],
        [TypeMeeting]
FROM    [DB_TEST].[dbo].[Meetings] m
        JOIN (  SELECT  [PersonName],
                        [GUESTName],
                        COUNT(CASE WHEN m2.[TypeMeeting] = 'OneToOne' THEN 1 END) AS OneToOneCount,
                        COUNT(CASE WHEN m2.[TypeMeeting] = 'OneToFew' THEN 1 END) AS OneToFewCount,
                        COUNT(CASE WHEN m2.[TypeMeeting] = 'Group' THEN 1 END) AS GroupCount
                FROM    [DB_TEST].[dbo].[Meetings] m2
                GROUP BY [PersonName],
                        [GUESTName]
         ) m2 ON m2.[PersonName] = m.[PersonName]
             AND m2.[GUESTName] = m.[GUESTName]

答案 1 :(得分:0)

您可以使用条件聚合:

select PersonName, GuestName,
       sum(case when TypeMetting = 'one-to-one' then 1 else 0 end) as Num_OneToOne,
       sum(case when TypeMetting = 'one-to-few' then 1 else 0 end) as Num_OneToFew,
       sum(case when TypeMetting = 'group' then 1 else 0 end) as Num_Group
from Meetings
group by PersonName, GuestName;

答案 2 :(得分:0)

您可以使用SQL COUNT function with Partition By子句

尝试

SELECT Distinct [ID]
      ,[PersonName]
      ,[TypeMeeting] --...
      ,COUNT([TypeMeeting]) OVER (PARTITION BY [PersonName], [TypeMeeting]) Cnt
FROM [Meetings]

结果如下

enter image description here

答案 3 :(得分:0)

BM

另一种解决方案可能是使用SQL Pivot query

以下是示例数据和SQL pivot Select语句

SELECT *
FROM (
  SELECT
    [ID],
    [PersonName],
    [TypeMeeting]
  FROM [Meetings]
) TableData
PIVOT (
  Count(ID)
  FOR [TypeMeeting] IN (
    [T1],[T2],[T3],[T4],[T5],[T6]
  )
) PivotTable

我刚用'T1'等会议类型,你需要用“[]”中的实际值替换它们

结果如下

enter image description here

如果您有多种不同的会议类型,可以使用dynamic pivot query in SQL Server,但我猜上面的解决方案就够了