包含开始和结束日期的查询

时间:2014-03-25 18:09:44

标签: tsql

有人可以帮我查询。我有桌子dbo.table。我写了这个查询:

select Name, Service, number, Dates, Country, Price
from dbo.table
where country is not NULL and Service in ('Incoming', 'Outgoing') and years =2014 and months=3 

我得到了结果:

User1   Incoming    1111    03.07.2014  Belarus  5,5
User1   Incoming    1111    03.09.2014  Belarus  1,5
User1   Incoming    1111    03.10.2014  Belarus  1,5
User1   Outgoing    1111    03.10.2014  Belarus  2
User1   Outgoing    1111    03.11.2014  Belarus  3
User1   Outgoing    1111    03.11.2014  Belarus  4
User1   Incoming    1111    03.07.2014  France   4,3
User1   Incoming    1111    03.07.2014  France   2,7
User1   Incoming    1111    03.08.2014  France   1
User1   Outgoing    1111    03.15.2014  France   2
User1   Outgoing    1111    03.15.2014  France   3
User1   Outgoing    1111    03.15.2014  France   6

如果想获得此结果,我应该使用该查询:

User1   Incoming    03.07.2014  03.10.2014  Belarus 8,5
User1   Outgoing    03.10.2014  03.11.2014  Belarus 9
User1   Incoming    03.07.2014  03.08.2014  France  8
User1   Outgoing    03.15.2014  03.15.2014  France  11

1 个答案:

答案 0 :(得分:1)

select Name, Service,  MIN(Dates) as MinDate, MAX(Dates) as MaxDate, Country, SUM(Price) as Price
from dbo.table
where country is not NULL and Service in ('Incoming', 'Outgoing') and years =2014 and months=3
GROUP BY Name, Service, Country