使用外连接加入3个查询

时间:2010-07-30 14:03:39

标签: sql-server join

我创建了一个表tblOperationLog并编写了触发器来填充它,因为用户在我的主表中删除,更新或插入一行。 这是删除并插入一个:

CREATE TRIGGER FILL_TABLE  
ON Person FOR INSERT, DELETE
AS  

  INSERT INTO tblOperationLog  
 SELECT  SYSTEM_USER, 
         'user has inserted a row with ID = ' + Convert(nvarchar, inserted.id), 
          'Insert', CURRENT_TIMESTAMP, getdate()
  FROM inserted 


  INSERT INTO tblOperationLog  
 SELECT  SYSTEM_USER, 
         'user has deleted a row with ID = ' + Convert(nvarchar, deleted.id), 
          'Insert', CURRENT_TIMESTAMP, getdate() 
  FROM deleted 

现在我想编写一个程序来显示每个用户每天所做的操作的平均值。我希望在单独的列中获得每个操作(删除,插入和更新)的平均值。为了达到这个目标,我写了这些问题:

select A.Users , avg(A.[Number Of Inserts])as 'Number Of Inserts' from
(select  Users,[Time],COUNT(*) as 'Number Of Inserts' from tblOperationLog where Opertion='Insert' group by Users, [Date]) A  group by Users

go
select B.Users , avg(B.[Number Of Updates])as 'Number Of Updates' from
(select  Users,[Date],COUNT(*) as 'Number Of Updates' from tblOperationLog where Operation='Update' group by Users, [Date]) B  group by Users

go

select C.Users , avg(C.[Number Of Deletes])as 'Number Of Deletes' from
(select  Users,[Date],COUNT(*) as 'Number Of Deletes' from tblOperationLog where Operation='Delete' group by Users, [Date]) C  group by Users

Go

通过上述过程,我将每个操作显示在一个单独的表中。但是我希望将它们全部放在一个表中,并且我希望为没有删除的用户或...平均操作0。你能帮我加入吗?我想我必须使用全外连接。但每次我的最终查询都有错误。我使用SQL Server。

3 个答案:

答案 0 :(得分:0)

查看“UNION”SQL运算符

答案 1 :(得分:0)

这是一个可能有用的查询。

WITH OpLogCTE AS (
    SELECT Users, Date, Operation, AVG(COUNT(*)) OVER (PARTITION BY Users, Date, Operation) AS OpCount
    FROM tblOperationLog
    GROUP BY Users, Date, Operation
    )

SELECT Users, Date,
    SUM(CASE Operation WHEN 'Insert' THEN OpCount ELSE 0 END) AS InsertCount,
    SUM(CASE Operation WHEN 'Update' THEN OpCount ELSE 0 END) AS UpdateCount,
    SUM(CASE Operation WHEN 'Delete' THEN OpCount ELSE 0 END) AS DeleteCount
FROM OpLogCTE
GROUP BY Users, Date
ORDER BY Users, Date

顶部是公用表表达式(CTE)。它包含一个查询,用于获取每个用户,日期和操作的平均值。

下半部分获取CTE的结果并对数据进行分组,以便为​​每个用户和日期获取一行。

答案 2 :(得分:0)

这是你需要的吗? (您是否尝试计算每个用户每次操作的平均每日数量)

SELECT * 
INTO #tblOperationLog
FROM (
SELECT 'Alice' AS Users, CAST('2010-07-29' AS DATE) AS [DATE],
'Delete' AS OPERATION UNION ALL
SELECT 'Bob' AS Users, CAST('2010-07-30' AS DATE) AS [DATE],
'Insert' AS OPERATION UNION ALL
SELECT 'Bob' AS Users, CAST('2010-07-30' AS DATE) AS [DATE],
'Insert' AS OPERATION
) DemoData


DECLARE @DateCount float 
SELECT @DateCount= COUNT(DISTINCT [DATE]) FROM #tblOperationLog

SELECT Users,
COUNT(CASE WHEN OPERATION = 'Delete' THEN 1 END)/@DateCount AS DelAverage,
COUNT(CASE WHEN OPERATION = 'Insert' THEN 1 END)/@DateCount AS UpdateAverage,
COUNT(CASE WHEN OPERATION = 'Update' THEN 1 END)/@DateCount AS InsertAverage
FROM #tblOperationLog
GROUP BY Users

输出

Users DelAverage             UpdateAverage          InsertAverage
----- ---------------------- ---------------------- ----------------------
Alice 0.5                    0                      0
Bob   0                      1                      0