Tsql 包含开始日期和结束日期的查询
谁能帮我提个问题吗。我有桌子dbo.table。我写下了这个问题:Tsql 包含开始日期和结束日期的查询,tsql,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 Bel
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
如果有效,则将其标记为已回答,以便其他寻找类似答案的人可以使用此选项:)
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