如何通过条件和每行的总和与另一个条件获取数据?

时间:2015-05-08 19:09:26

标签: sql-server

我有一个包含5列ReportId, Date, Area, BuildingName, Amount的表格。

示例数据如下所示:

-------------------------------------------------------
ReportId |    Date    | Area | BuildingName | Amount
-------------------------------------------------------
   1     | 01/01/2013 |  S1  |    A1-01     |   5
   2     | 01/01/2013 |  S1  |    A1-03     |   5
   3     | 01/01/2013 |  S2  |    A1-05     |   4
   4     | 02/01/2013 |  S2  |    A1-05     |   7
   5     | 02/01/2013 |  S2  |    A1-03     |   9
   6     | 03/01/2013 |  S1  |    A1-03     |   2
   7     | 04/01/2013 |  S2  |    A1-02     |   6
   8     | 05/01/2013 |  S1  |    A1-01     |   7
   9     | 06/01/2013 |  S1  |    A1-02     |   5
   10    | 06/01/2013 |  S1  |    A1-05     |   8
   11    | 06/01/2013 |  S1  |    A1-07     |   5

我需要编写一个查询来获得如下结果:

-----------------------------------------------------
    Date    | Area   | BuildingName | Amount | Sum
-----------------------------------------------------
 01/01/2013 |    S1  |    A1-01     |   5    |  12
 01/01/2013 |    S1  |    A1-03     |   5    |  7
 01/01/2013 |    S2  |    A1-05     |   4    |  11

Date值作为参数传递给查询。

“Area”,“BuildingName”,“Amount”是具有相同“Date”的记录。

“Sum”,是表中所有“Amount”的总和,在查询结果中具有相同的“Area”和“BuildingName”。

我搜索得很多,但我对此无能为力......

5 个答案:

答案 0 :(得分:0)

尝试此查询

    SELECT @Date AS 'Date'
        ,t.Area
        ,t.BuildingName
        ,t.Amount
        ,temp.SumAmount
 FROM TABLE t INNER JOIN (SELECT Area, 
                                 BuildingName,
                                 SUM(Amount) 'SumAmount'
                          FROM TABLE t
                          GROUP BY Area, BuildingName) temp 
        ON temp.Area=t.Area AND 
          temp.BuildingName=t.BuildingName
Where t.Date= @Date

答案 1 :(得分:0)

这应该有效:

;with filter as (
    select Date, Area, BuildingName, Amount
    from data
    where data.Date = @date
)
select
     filter.Date
    ,filter.Area
    ,filter.BuildingName
    ,filter.Amoount
    ,sum(data.Amount) as [Sum]
from data
join filter
   on filter.Area         = data.Area
  and filter.BuildingName = date.BuildingName
group by
     filter.Date
    ,filter.Area
    ,filter.BuildingName
;

答案 2 :(得分:0)

给这一点。

public String generateNumber()
{
    int number = 1;

    for(Map.Entry<String, Forsikringkunde> entry : this.entrySet())
    {
        if(entry.getValue().getNumber().equals(String.valueOf(number)))
        {
            number++;
        } 
    }return String.valueOf(number);
}

答案 3 :(得分:0)

您可以使用SUM的窗口版本来获取每个Area, BuildingName分区的总和。然后,在外部查询中只按日期过滤,以获得所需的结果集:

SELECT [Date], Area, BuildingName, Amount, [Sum]
FROM (
   SELECT [Date], Area, BuildingName, Amount,
          SUM(Amount) OVER (PARTITION BY Area, BuildingName) AS [Sum]
   FROM mytable ) t
WHERE [Date] = '01/01/2013'

SQL Fiddle Demo

答案 4 :(得分:0)

我们可以通过在CTE中使用Row_number和Not条件来实现相同的结果,并且在进行金额总和时使用#01; A1-01&#39;得到17,但在你的输出中你提到了12 ...如果你用相同的数据执行所有的答案,你将得到的总和= 17仅适用于&#39; AI-01&#39; .SO请检查您的数据

DECLARE @tbl1 TABLE  (ReportID int, [date] date, [area] varchar(2), BuildingName varchar(5), [Amount] int)
INSERT INTO @tbl1 VALUES(1, '20130101', 'S1', 'A1-01', 5)
INSERT INTO @tbl1 VALUES(2, '20130101', 'S1', 'A1-03', 5)
INSERT INTO @tbl1 VALUES(3, '20130101', 'S2', 'A1-05', 4)
INSERT INTO @tbl1 VALUES(4, '20130201', 'S2', 'A1-05', 7)
INSERT INTO @tbl1 VALUES(5, '20130201', 'S2', 'A1-03', 9)
INSERT INTO @tbl1 VALUES(6, '20130301', 'S1', 'A1-03', 2)
INSERT INTO @tbl1 VALUES(7, '20130401', 'S2', 'A1-02', 6)
INSERT INTO @tbl1 VALUES(8, '20130501', 'S1', 'A1-01', 7)
INSERT INTO @tbl1 VALUES(9, '20130601', 'S1', 'A1-02', 5)
INSERT INTO @tbl1 VALUES(10, '20130601', 'S1', 'A1-05', 8)
INSERT INTO @tbl1 VALUES(11, '20130601', 'S1', 'A1-01', 0)

;WITH CTE AS (
select 
DATE,
area,
buildingname,
amount,RN,R 
       from (
select 
    DATE,
    area,
    buildingname,
    amount,
ROW_NUMBER()OVER(PARTITION BY Area ORDER BY date,buildingname)RN,
SUM(AMOUNT)over(PARTITION by buildingname,area  )R from @tbl1)A

WHERE RN in( 1,2) )

select  
            DATE,
            Area,
            Buildingname,
            Amount,R  
   FROM CTE WHERE DATE NOT  IN (SELECT MAX(DATE)D FROM CTE C)