sql - Prevent duplicate COUNT in SELECT query -


i have following query can see multiple count(competitorid) calls. performance issue, or sql server 2008 'cache' count? if performance issue, possible store count prevent multiple lookups?

select eventid,count(competitorid) numberrunners,     case when count(competitorid)<5 1          when count(competitorid)>=5 , count(competitorid)<=7 2          else 3 end numberplacings comps group eventid order eventid; 

its better practice value once , use subsequently whenever possible. in case, can use inner query count once , compute other (derived) columns off value shown below:

select  eventid, numberrunners,         case when numberrunners <5 1              when numberrunners >=5 , numberrunners <=7 2              else 3          end numberplacings    (             select  eventid,                     numberrunners = count(competitorid)                comps             group eventid         ) t order eventid; 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -