Sql Query要细分

时间:2014-06-04 05:04:27

标签: sql sql-server sql-server-2008 stored-procedures

我在SQL Server中有以下表结构

交易

 ---------------------------------------------------------------------
 **ID | FromEmployeeID | ToEmployeeID | OutgoingTotal | IncomingTotal**|
 ---------------------------------------------------------------------
   1  |      10        |     20       |       10      |       50
   2  |      11        |     10       |       20      |       40    

我需要通过Summary将此数据转换为另一个表employeeID,就好像从员工开始到员工的事务我必须在摘要中登记为每个员工的两个记录(1从传出和传入一个

摘要

 ---------------------------------------------------------------------
 **ID | EmployeeID | outgoingcount| incomingcount | OutgoingTotal| IncomingTotal**|
 ---------------------------------------------------------------------
   1  |      10    |     1        |       1       |   10         |  50
   2  |      20    |     0        |       1       |   0          |  50
   3  |      11    |     1        |       0       |   0          |  40   

我还有员工表,其中有员工ID和员工的其他信息。

如何实现这一目标?

3 个答案:

答案 0 :(得分:1)

我使用临时表并首先插入您的员工ID,然后在单独的查询中更新计数和总和。 Haven没有测试过这个,但你可以试试这个:

CREATE TABLE #Summary (
  ID INT IDENTITY(1,1) NOT NULL,
  EmployeeID INT NOT NULL,
  OutgoingCount INT NOT NULL,
  IncomingCount INT NOT NULL,
  OutgoingTotal INT NOT NULL,
  IncomingTotal INT NOT NULL
)

--Get all the distinct 'from' employee IDs
INSERT INTO #Summary( EmployeeID )
SELECT DISTINCT FromEmployeeID FROM Transaction;

--Get the rest of the employee ids, except for those that have already been added to the table
INSERT INTO #Summary( EmployeeID )
SELECT DISTINCT ToEmployeeID FROM Transaction t
LEFT JOIN #Summary s ON s.EmployeeID = t.ToEmployeeID
WHERE s.EmployeeID IS NULL

--Update the summary table with the outgoing results
UPDATE s
SET OutgoingCount = e.OutgoingCount,
    OutgoingTotal = e.OutgoingTotal
FROM
  #Summary s
  JOIN (SELECT FromEmployeeID AS EmployeeID, COUNT(OutgoingTotal) AS OutgoingCount, SUM(OutgoingTotal) AS OutgoingTotal FROM Transaction t WHERE t.FromEmployeeID = s.EmployeeID GROUP BY t.FromEmployeeID) e ON e.EmployeeID = s.EmployeeID

--Repeat the process for incoming results
UPDATE s
SET IncomingCount = e.IncomingCount,
    IncomingTotal = e.IncomingTotal
FROM
  #Summary s
  JOIN (SELECT ToEmployeeID AS EmployeeID, COUNT(IncomingTotal) AS IncomingCount, SUM(IncomingTotal) AS IncomingTotal FROM Transaction t WHERE t.ToEmployeeID = s.EmployeeID GROUP BY t.ToEmployeeID) e ON e.EmployeeID = s.EmployeeID

--End the end, select your results
SELECT
  *
FROM
  #Summary

答案 1 :(得分:0)

<@> AS @Nithin说,总结在你的例子中并没有总结。

WITH AllEmployees AS (
    SELECT DISTINCT EmployeeId
    FROM [Transaction]
    CROSS APPLY (
        VALUES (FromEmployeeID)
              ,(ToEmployeeID)
    ) AS T(EmployeeId)
)
SELECT EmployeeId
      ,Outgoing.Records AS outgoingcount
      ,Incoming.Records AS incomingcount 
      ,COALESCE(Outgoing.Total, 0) AS OutgoingTotal
      ,COALESCE(Incoming.Total, 0) AS IncomingTotal
FROM AllEmployees
     CROSS APPLY (
          SELECT COUNT(1) AS Records
                ,SUM(OutgoingTotal) As Total
          FROM [Transaction]
          WHERE [Transaction].FromEmployeeID = AllEmployees.EmployeeId
     ) AS Outgoing
     CROSS APPLY (
          SELECT COUNT(1) AS Records
                ,SUM(IncomingTotal) AS Total
          FROM [Transaction]
          WHERE [Transaction].ToEmployeeID = AllEmployees.EmployeeId
     ) AS Incoming

答案 2 :(得分:0)

根据我的理解,我写了一个查询:

SELECT ISNULL(OutGoing.FromEmployeeID,Incoming.ToEmployeeID) EmployeeID,
       ISNULL(OutgoingCount,0) OutgoingCount,
       ISNULL(IncomingCount,0) IncomingCount,
       ISNULL(OutgoingTotal,0) OutgoingTotal,
       ISNULL(IncomingTotal,0) IncomingTotal
FROM (
        SELECT FromEmployeeID,
               COUNT(*) OutgoingCount,
               SUM(OutgoingTotal) OutgoingTotal
        FROM CallLogs
        GROUP BY FromEmployeeID
      ) OutGoing
FULL OUTER JOIN 
      (
        SELECT ToEmployeeID,
               COUNT(*) IncomingCount ,
               SUM(IncomingTotal) IncomingTotal
        FROM CallLogs
        GROUP BY ToEmployeeID
      ) Incoming
ON OutGoing.FromEmployeeID = Incoming.ToEmployeeID