sql - Count issue when introducing a new column -
fairly new sql. i'm trying count total number of booking id's in query holidays in 2 regions.
this want need..
id count region name 427139 1 france 427776 2 spain 427776 2 france
but seem deliver this..
id count region name 427139 1 france 427776 1 spain 427776 1 france
bookings id's unique split 2 rows when introduce region region table (via quotes , properties tables.)
here's sql..
select count(bo.id) count, bo.id 'booking id', re.name 'region name' booking bo (nolock) left join quote qu (nolock) on qu.id = bo.quoteid left join property pr (nolock) on pr.code = qu.code left join region re (nolock) on re.id = pr.regionid bo.id = '427776' or bo.id = '427139' group bo.id,re.name order bo.id
can help?
thanks looking!
in sqlserver2005+ can use over() clause aggregate function count()
select count(*) over(partition bo.id) [count], bo.id 'booking id', re.name 'region name' booking bo (nolock) left join quote qu (nolock) on qu.id = bo.quoteid left join property pr (nolock) on pr.code = qu.code left join region re (nolock) on re.id = pr.regionid bo.id = '427776' or bo.id = '427139' group bo.id, re.name order bo.id
i think request in new format can used without group clause
select count(*) over(partition bo.id) [count], bo.id 'booking id', re.name 'region name' booking bo (nolock) left join quote qu (nolock) on qu.id = bo.quoteid left join property pr (nolock) on pr.code = qu.code left join region re (nolock) on re.id = pr.regionid bo.id = '427776' or bo.id = '427139' order bo.id
Comments
Post a Comment