Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8979

SQL Query Help

$
0
0

I need help figuring out this query. We are taking the latitude and longitude from a table and finding the distance from a set location to convert to miles from location. Then we want to break it down by other categories as in my code. I need help with the case statements.

Select

(

3959*acos(

cos(radians(43.7779))

*cos( radians(fc.Address1_Latitude))

*cos( radians(fc.Address1_Longitude)- radians(-88.4215))

+sin(radians(43.7779))

*sin( radians(fc.Address1_Latitude))

))

AsMiles

fromfilteredcontactfc

selectcase

WHEN'Miles'< 25  THEN'0-24'

WHEN'Miles'<= 25 and'Miles'<49THEN'25-49'

WHEN'Miles'>= 50 and'Miles'< 74THEN'50-74'

WHEN'Miles'>= 75 and'Miles'< 99THEN'75-99'

WHEN'Miles'>= 100 and'Miles'< 125THEN'100-125'

WHEN'Miles'>= 126 and'Miles'< 151THEN'126-151'

WHEN'Miles'>= 152 and'Miles'< 177THEN'152-177'

WHEN'Miles'>= 178 and'Miles'< 203THEN'178-203'

WHEN'Miles'>= 204 and'Miles'< 229THEN'204-229'

WHEN'Miles'>= 230 and'Miles'< 255THEN'230-255'

WHEN'Miles'>= 256 and'Miles'< 281THEN'256-281'

WHEN'Miles'>= 282 and'Miles'< 307THEN'282-307'

WHEN'Miles'>= 308 and'Miles'< 333THEN'308-333'

WHEN'Miles'>= 334 and'Miles'< 359THEN'334-359'

WHEN'Miles'>= 360 and'Miles'< 385THEN'360-385'

WHEN'Miles'>= 386 and'Miles'< 411THEN'386-411'

WHEN'Miles'>= 412 and'Miles'< 437THEN'412-437'

WHEN'Miles'>= 438 and'Miles'< 463THEN'438-463'

WHEN'Miles'>= 464 and'Miles'< 489THEN'464-489'

WHEN'Miles'>= 490 and'Miles'< 500THEN'490-500'

ELSE'over 500'

END

select

sum(casewhenfc.datatel_prospectstatusnamein('Prospect','Inquiry','Applicant', 'Admit',

'Confirmed','Enrolled', 'Application Started', 'Application Submitted',

'Application Completed','Application Moved to ERP')then 1 else NULLEnd) as"INQ",

sum(casewhenfc.datatel_prospectstatusnamein('Applicant','Admit','Confirmed', 'Enrolled',

'Application Submitted','Application Completed',

'Application Moved to ERP')then 1 elseNULL End)as"APP",

(selectcast(sum(casewhen  fc.datatel_prospectstatusnamein('Applicant','Admit','Confirmed',

'Enrolled','Application Submitted','Application Completed',

'Application Moved to ERP')then 1 else NULLEnd)asfloat)/

(sum(casewhenfc.datatel_prospectstatusnamein('Prospect','Inquiry','Applicant', 'Admit',

'Confirmed','Enrolled', 'Application Started', 'Application Submitted',

'Application Completed','Application Moved to ERP')then 1 else NULLEnd)))"APP/INQ",

sum(casewhenfc.datatel_prospectstatusnamein('Admit','Confirmed',

'Enrolled')then 1 else NULLEnd) as"ADM",

(selectcast(sum(casewhenfc.datatel_prospectstatusnamein('Admit','Confirmed',

'Enrolled')then 1 else NULLEnd)asfloat)/

(sum(casewhenfc.datatel_prospectstatusnamein('Applicant','Admit','Confirmed', 'Enrolled',

'Application Submitted','Application Completed',

'Application Moved to ERP')then 1 else NULLEnd)))"ADM/APP",

sum(casewhenfc.datatel_prospectstatusnamein('Confirmed','Enrolled') then 1 elseNULLEnd) as"DEP",

(selectcast(sum(casewhenfc.datatel_prospectstatusnamein('Confirmed','Enrolled') then 1 elseNULLEnd)asfloat)/

(sum(casewhenfc.datatel_prospectstatusnamein('Admit','Confirmed',

'Enrolled')then 1 else NULLEnd)))"DEP/ADM",

sum(casewhenfc.elucnsrv_currentprimaryappstatusnameLike'%Canceled%' then 1 elseNULLEnd) as"CAN",

(selectcast(sum(casewhenfc.elucnsrv_currentprimaryappstatusnameLike'%Canceled%' then 1 elseNULLEnd)asfloat)/

(sum(casewhenfc.datatel_prospectstatusnamein('Confirmed','Enrolled') then 1 elseNULLEnd)))"CAN/DEP",

sum(casewhenfc.datatel_prospectstatusname='Enrolled' then 1 elseNULLend) as"ENR",

(selectcast(sum(casewhenfc.datatel_prospectstatusname='Enrolled' then 1 elseNULLEnd)asfloat)/

(sum(casewhenfc.datatel_prospectstatusnamein('Admit','Confirmed', 'Enrolled')then 1elseNULL End)))"ENR/ADM"

FROM FilteredContactfc

wherefc.Address1_Latitudeisnot null

andfc.Address1_Longitudeisnot null

andfc.FirstNameisnot null

andfc.LastNameisnot null

andfc.datatel_academiclevelofinterestidnamein('Pre Professional','Undergraduate' ,'Special')

and(fc.statecodename='Active' andfc.customertypecodename='Prospective Student')

groupbyfc.address1_latitude,fc.address1_longitude


Viewing all articles
Browse latest Browse all 8979

Latest Images

Trending Articles



Latest Images

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