Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Ranking

$
0
0

Trying to rank data in one select (ie...one pass of the data).  I am looking to get data output as the “Want” column shows. Would like the ‘BUS’ DayType count to carry over to the ‘W/E’ Day (3,8,13 in dataset below). 

[edit] adding image...

with t as (
select cast('5/1/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 1 "Want" union all
select cast('5/2/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 2 "Want" union all
select cast('5/3/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 3 "Want" union all
select cast('5/4/2013' as date) "Day", 1568 "MonthSeq", 'W/E' "DayType", 3 "Want" union all
select cast('5/5/2013' as date) "Day", 1568 "MonthSeq", 'W/E' "DayType", 3 "Want" union all
select cast('5/6/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 4 "Want" union all
select cast('5/7/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 5 "Want" union all
select cast('5/8/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 6 "Want" union all
select cast('5/9/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 7 "Want" union all
select cast('5/10/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 8 "Want" union all
select cast('5/11/2013' as date) "Day", 1568 "MonthSeq", 'W/E' "DayType", 8 "Want" union all
select cast('5/12/2013' as date) "Day", 1568 "MonthSeq", 'W/E' "DayType", 8 "Want" union all
select cast('5/13/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 9 "Want" union all
select cast('5/14/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 10 "Want" union all
select cast('5/15/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 11 "Want" union all
select cast('5/16/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 12 "Want" union all
select cast('5/17/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 13 "Want" union all
select cast('5/18/2013' as date) "Day", 1568 "MonthSeq", 'W/E' "DayType", 13 "Want" union all
select cast('5/19/2013' as date) "Day", 1568 "MonthSeq", 'W/E' "DayType", 13 "Want" union all
select cast('5/20/2013' as date) "Day", 1568 "MonthSeq", 'BUS' "DayType", 14 "Want"
)
select 
  Day
  ,MonthSeq
  ,DayType
  ,row_number() over (partition by DayType order by Day) Have /*row number*/
  ,Want
from 
  t
order by Day  

We are using 2008. I don’t believe “Lead/Lag” is available.

Thanks




Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>