sql server - SQL derived column call number -
i have query below:
select top 8 (orderth_strmoviename) top8hotfilms, convert(char(8), orderth_dtmsessiondatetime, 112) dayofcount, count( orderth_strmoviename)as filmoccurence [movies].[dbo].[tblordertickethistory] convert(char(8), orderth_dtmsessiondatetime, 112) >= (select dateadd(dd, 0, datediff(dd, 0, getdate()))) group orderth_strmoviename , convert(char(8), orderth_dtmsessiondatetime, 112) order filmoccurence desc
the result below:
movies session day occurence .tix *san andreas 20150531 2156 *masss 20150531 1954 *tomorrowland 20150531 990 spy 20150531 825 pitch perfect 2 20150531 374 mad max fury road 20150531 302 *masss 20150601 268 *san andreas 20150601 257
qns now, how derive column called number, end result below?
number movies session day occurence .tix 1 *san andreas 20150531 2156 2 *masss 20150531 1954 3 *tomorrowland 20150531 990 4 spy 20150531 825 5 pitch perfect 2 20150531 374 6 mad max fury road 20150531 302 7 *masss 20150601 268 8 *san andreas 20150601 257
you can try below
select top 8 rank() on (order count( orderth_strmoviename) desc) number, (orderth_strmoviename) top8hotfilms, convert(char(8), orderth_dtmsessiondatetime, 112) dayofcount, count( orderth_strmoviename)as filmoccurence [movies].[dbo].[tblordertickethistory] convert(char(8), orderth_dtmsessiondatetime, 112) >= (select dateadd(dd, 0, datediff(dd, 0, getdate()))) group orderth_strmoviename , convert(char(8), orderth_dtmsessiondatetime, 112)
eg:
create table test (name varchar(10), age numeric) insert test values ('john',10); insert test values('happy',20); insert test values ('mary',35); insert test values ('mary',35); insert test values ('john',10); select top 2 row_number() over(order sum(age)) number, name n, sum(age) age_sum
from test group name order name
number n age_sum 1 happy 20 2 john 20
Comments
Post a Comment