My table data is as follows:
declare @t table (name varchar(10), amt int, dt datetime)
insert into @t (name, amt, dt)
values
('meeseva',100,'06-17-2015'), ('meeseva',200,'06-17-2015'),
('meeseva',200,'06-17-2015'), ('meeseva',100,'06-16-2015'),
('meeseva',100,'06-15-2015'), ('meeseva',100,'06-14-2015'),
('fish',100,'06-17-2015'), ('fish',200,'06-17-2015'),
('fish',100,'06-16-2015'), ('fish',200,'06-16-2015'),
('fish',100,'06-15-2015'), ('fish',100,'06-14-2015'),
('raju',100,'06-17-2015'), ('raju',200,'06-17-2015'),
('raju',100,'06-16-2015'), ('raju',100,'06-15-2015'),
('raju',100,'06-14-2015'), ('raju',500,'06-14-2015')
So far i tried
select
name,
SUM(amt),
dt,
ROW_NUMBER() OVER (PARTITION BY name order by dt)
from
@t
where
dt >= (SELECT CONVERT (VARCHAR(10), Getdate() - 4, 101))
and dt <= (SELECT CONVERT (VARCHAR(10), Getdate(), 101))
GROUP BY
name, dt
ORDER BY
name, dt desc
I have data where I need to get data based on the sum of the sums, and the highest two total values should be returned per day if the amounts are the same based on the record identifier, and the date of the 2 records should be returned.
My desired result:
name sum dt
-----------------------------------
fish 300 2015-06-17 00:00:00.000
meeseva 500 2015-06-17 00:00:00.000
fish 300 2015-06-16 00:00:00.000
raju 200 2015-06-16 00:00:00.000
fish 100 2015-06-15 00:00:00.000
meeseva 100 2015-06-15 00:00:00.000
raju 600 2015-06-14 00:00:00.000
meeseva 100 2015-06-14 00:00:00.000