在临时表中完成计算-SQL Server

时间:2018-09-24 13:30:57

标签: sql sql-server aggregate

提琴-http://sqlfiddle.com/#!18/c2b80/17

表格:

CREATE TABLE [OrderTable] 
(
    [id] int,
    [OrderGroupID] int,
    [Total] int,
    [fkPerson] int,
    [fkitem] int
    PRIMARY KEY (id)
) 

INSERT INTO [OrderTable] (id, OrderGroupID, Total ,[fkPerson], [fkItem]) 
VALUES
  ('1', '1', '20', '1', '1'),
  ('2', '1', '45', '2', '2'),
  ('3', '2', '32', '1', '1'),
  ('4', '2', '30', '2', '2');

CREATE TABLE [Person] 
(
    [id] int,
    [Name] varchar(32)
    PRIMARY KEY (id)
) 

INSERT INTO [Person] (id, Name) 
VALUES ('1', 'Fred'),
       ('2', 'Sam');

CREATE TABLE [Item] 
(
    [id] int,
    [ItemNo] varchar(32),
    [Price] int
    PRIMARY KEY (id)
) 

INSERT INTO [Item] (id, ItemNo, Price) 
VALUES ('1', '453', '23'),
       ('2', '657', '34');

原始查询:

WITH TABLE1 AS 
(
    SELECT 
        -- P.ID AS [PersonID], 
        -- P.Name, 
        SUM(OT.[Total]) AS [Total], 
        i.[id] AS [ItemID],
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rownum,
        ot.fkperson
    FROM 
        OrderTable OT
    -- INNER JOIN Person P ON P.ID = OT.fkperson
    INNER JOIN 
        Item I ON I.[id] = OT.[fkItem]
    GROUP BY 
        -- P.ID, P.Name,
        i.id, ot.fkperson
)
SELECT 
    t1.fkperson,
    t1.[itemid],
    t1.[total],
    t1.[rownum]
    -- Totalrows = (SELECT MAX(rownum) FROM TABLE1)
FROM 
    TABLE1 T1
INNER JOIN 
    Person P ON P.ID = T1.fkperson

我试图在临时表中的列上完成sum函数,并将其联接回CTE。它要么是错误,要么是我得到了不正确的列。这样做的想法是在临时表中执行计算以提高查询性能。将临时表总和返回到查询的最佳方法是什么?任何建议都很好

当前查询:

CREATE TABLE #ot
(
     fkperson int, 
     Total int
)

INSERT INTO #ot
    SELECT 
        fkperson, 
        SUM(total) AS [Total]
    FROM 
        OrderTable
    GROUP BY 
        [fkperson]

WITH TABLE1 AS 
(
    SELECT 
        ot.[Total], 
        i.[id] AS [ItemID],
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rownum,
        ot.fkperson
    FROM 
        #ot OT
    INNER JOIN 
        Item I ON I.[id] = OT.[fkItem]
    GROUP BY 
        i.id, ot.fkperson
)
SELECT 
    t1.fkperson,
    t1.[itemid],
    t1.[total],
    t1.[rownum],
    p.[Name],
    Totalrows = (SELECT MAX(rownum) FROM TABLE1),
    totalrows = @@ROWCOUNT
FROM 
    TABLE1 T1
INNER JOIN 
    Person P ON P.ID = T1.fkperson 

2 个答案:

答案 0 :(得分:0)

我可能是错的,但我认为,您正在尝试建立#ot表中的总行数,然后将其归为最后一行select语句中输出的每一行。因此,每一行在表中都有最大的行数。

如果正确,那么我认为对原始代码所做的以下更改可以做到这一点:

CREATE TABLE #ot
(
 fkperson int, 
 [fkItem] int,  --LS23 added to make code run
 Total int
)

INSERT INTO #ot
SELECT 
    fkperson, 
    [fkItem], --LS23 added to make code run
    SUM(total) AS [Total]
FROM 
    [#OrderTable]
GROUP BY 
    [fkperson],
    [fkitem]; --LS23 added to make code run

然后使用以下方法在最终的select语句中填充TotalRows

Totalrows = (SELECT MAX(rownum) FROM TABLE1)

有帮助吗?

LS23

答案 1 :(得分:0)

如果我正确理解(基于评论),则可以:

WITH TABLE1 AS (
      SELECT P.ID AS [PersonID], P.Name, 
             SUM(OT.[Total]) AS [Total], 
             i.[id] AS [ItemID],
             ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS rownum,
             COUNT(*) OVER () as cnt
             ot.fkperson
      FROM OrderTable OT INNER JOIN
           Person P
           ON P.ID = OT.fkperson INNER JOIN 
           Item I
           ON I.[id] = OT.[fkItem]
      GROUP BY P.ID, P.Name, i.id, ot.fkperson
     )

除非您想在查询之间保留结果,否则我认为临时表没有用。