2014-06-26 9 views
0

我有一个SQL查询,从表中取经纬度,并将其与我的位置设置latitiude和经度比较。我希望能够有一个案例声明,将英里分成我的其他专栏。我不能得到这个SQL查询来把我的距离,并在案件陈述中放入类别

*在这里,我走的纬度和经度来自人民,从一组点发现距离,使之成为一个名为万里

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)) 
)) 
As Miles 
from filteredcontact fc 

**以下字段是哪里出了问题,我想采取我发现的里程,并试图在下一个案例陈述中列入类别。举例来说,任何距离校园0-24英里的人都希望它能够提出多少个询问,申请人,确认等等。我想为每一组距离做这件事。

select case 
WHEN 'Miles' < 25 THEN '0-24' 
WHEN 'Miles' <= 25 and 'Miles' <49 THEN '25-49' 
WHEN 'Miles' >= 50 and 'Miles' < 74 THEN '50-74' 
WHEN 'Miles' >= 75 and 'Miles' < 99 THEN '75-99' 
WHEN 'Miles' >= 100 and 'Miles' < 125 THEN '100-125' 
WHEN 'Miles' >= 126 and 'Miles' < 151 THEN '126-151' 
WHEN 'Miles' >= 152 and 'Miles' < 177 THEN '152-177' 
WHEN 'Miles' >= 178 and 'Miles' < 203 THEN '178-203' 
WHEN 'Miles' >= 204 and 'Miles' < 229 THEN '204-229' 
WHEN 'Miles' >= 230 and 'Miles' < 255 THEN '230-255' 
WHEN 'Miles' >= 256 and 'Miles' < 281 THEN '256-281' 
WHEN 'Miles' >= 282 and 'Miles' < 307 THEN '282-307' 
WHEN 'Miles' >= 308 and 'Miles' < 333 THEN '308-333' 
WHEN 'Miles' >= 334 and 'Miles' < 359 THEN '334-359' 
WHEN 'Miles' >= 360 and 'Miles' < 385 THEN '360-385' 
WHEN 'Miles' >= 386 and 'Miles' < 411 THEN '386-411' 
WHEN 'Miles' >= 412 and 'Miles' < 437 THEN '412-437' 
WHEN 'Miles' >= 438 and 'Miles' < 463 THEN '438-463' 
    WHEN 'Miles' >= 464 and 'Miles' < 489 THEN '464-489' 
WHEN 'Miles' >= 490 and 'Miles' < 500 THEN '490-500' 
ELSE 'over 500' 
END 

**这里是它需要的地方,并把上面的英里放到列中。

sum (case when fc.datatel_prospectstatusname in ('Prospect','Inquiry', 'Applicant', 'Admit', 
'Confirmed', 'Enrolled', 'Application Started', 'Application Submitted', 
'Application Completed', 'Application Moved to ERP') then 1 else NULL End) as "INQ", 
    sum (case when fc.datatel_prospectstatusname in ('Applicant','Admit', 'Confirmed', 'Enrolled', 
    'Application Submitted', 'Application Completed', 
    'Application Moved to ERP')then 1 else NULL End) as "APP", 
    (select cast (sum(case when fc.datatel_prospectstatusname in   ('Applicant','Admit', 'Confirmed', 
'Enrolled', 'Application Submitted', 'Application Completed', 
'Application Moved to ERP') then 1 else NULL End)as float)/ 
(sum (case when fc.datatel_prospectstatusname in ('Prospect','Inquiry', 'Applicant', 'Admit', 
'Confirmed', 'Enrolled', 'Application Started', 'Application Submitted', 
'Application Completed', 'Application Moved to ERP') then 1 else NULL End))) "APP/INQ", 
    sum (case when fc.datatel_prospectstatusname in ('Admit', 'Confirmed', 
    'Enrolled') then 1 else NULL End) as "ADM", 
    (select cast (sum(case when fc.datatel_prospectstatusname in ('Admit', 'Confirmed', 
    'Enrolled') then 1 else NULL End)as float)/ 
    (sum (case when fc.datatel_prospectstatusname in ('Applicant','Admit', 'Confirmed', 'Enrolled', 
    'Application Submitted', 'Application Completed', 
    'Application Moved to ERP') then 1 else NULL End))) "ADM/APP", 
    sum (case when fc.datatel_prospectstatusname in ('Confirmed', 'Enrolled') then 1 else NULL End) as "DEP", 
    (select cast (sum(case when fc.datatel_prospectstatusname in ('Confirmed', 'Enrolled') then 1 else NULL End)as float)/ 
    (sum (case when fc.datatel_prospectstatusname in ('Admit', 'Confirmed', 
    'Enrolled') then 1 else NULL End))) "DEP/ADM", 
    sum (case when fc.elucnsrv_currentprimaryappstatusname Like '%Canceled%' then 1 else NULL  End) as "CAN", 
    (select cast (sum(case when fc.elucnsrv_currentprimaryappstatusname Like '%Canceled%' then 1 else NULL End)as float)/ 
    (sum (case when fc.datatel_prospectstatusname in ('Confirmed', 'Enrolled') then 1 else NULL End))) "CAN/DEP" 
    sum (case when fc.datatel_prospectstatusname ='Enrolled' then 1 else NULL end) as "ENR", 
    (select cast (sum(case when fc.datatel_prospectstatusname ='Enrolled' then 1 else NULL End)as float)/ 
    (sum (case when fc.datatel_prospectstatusname in ('Admit', 'Confirmed', 'Enrolled') then 1 else NULL End))) "ENR/ADM" 




FROM FilteredContact fc 
    where fc.Address1_Latitude is not null 
    and fc.Address1_Longitude is not null 
    and fc.FirstName is not null 
and fc.LastName is not null 
and fc.datatel_academiclevelofinterestidname in ('PreProfessional' , 'Undergraduate' , 'Special') 
    and (fc.statecodename = 'Active' and fc.customertypecodename = 'Prospective Student') 
    group by fc.address1_latitude, fc.address1_longitude 
+0

您能否提供一个您希望输出看起来像什么的例子?一个简单的输入矩阵可以帮助人们理解你的问题。 – SlimsGhost

+0

这是'Case'声明中的问题吗?因为它将'String''Miles'与'int'进行比较,并且它总是错误的。 – Caveman42

+0

看下面的打印屏幕 – user3629893

回答

0

enter image description here不知道究竟是什么问题就在这里,但我注意到几件事情:

  1. 在你的第二个代码块,你必须:

    select case 
    WHEN 'Miles' < 25 THEN '0-24' 
    WHEN 'Miles' **<=** 25 and 'Miles' <49 THEN '25-49' 
    ... 
    

    ,它看起来像这样应该是

    select case 
    WHEN 'Miles' < 25 THEN '0-24' 
    WHEN 'Miles' **>=** 25 and 'Miles' <49 THEN '25-49' 
    ... 
    
  2. 在您的第二个代码块中,编译器会将“Miles”视为字符串文字,而不是指向列的指针(这似乎是预期的行为)。这将导致转换错误

此外,你是否收到任何错误(即不编译),或者是数据不正确?如果数据不正确,诸如您如何知道数据不正确或您知道数据错误等情况将对我们有所帮助。

有一两件事你可以做的(这是不漂亮)是这样的:

declare @MilesTable TABLE (Miles INT) 
    INSERT INTO @MilesTable (Miles) 
    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)) 
    )) 
    from filteredcontact fc 

    select case 
    WHEN Miles < 25 THEN '0-24' 
    WHEN Miles <= 25 and 'Miles' <49 THEN '25-49' 
    WHEN Miles >= 50 and 'Miles' < 74 THEN '50-74' 
    WHEN Miles >= 75 and 'Miles' < 99 THEN '75-99' 
    WHEN Miles >= 100 and 'Miles' < 125 THEN '100-125' 
    WHEN Miles >= 126 and 'Miles' < 151 THEN '126-151' 
    WHEN Miles >= 152 and 'Miles' < 177 THEN '152-177' 
    WHEN Miles >= 178 and 'Miles' < 203 THEN '178-203' 
    WHEN Miles >= 204 and 'Miles' < 229 THEN '204-229' 
    WHEN Miles >= 230 and 'Miles' < 255 THEN '230-255' 
    WHEN Miles >= 256 and 'Miles' < 281 THEN '256-281' 
    WHEN Miles >= 282 and 'Miles' < 307 THEN '282-307' 
    WHEN Miles >= 308 and 'Miles' < 333 THEN '308-333' 
    WHEN Miles >= 334 and 'Miles' < 359 THEN '334-359' 
    WHEN Miles >= 360 and 'Miles' < 385 THEN '360-385' 
    WHEN Miles >= 386 and 'Miles' < 411 THEN '386-411' 
    WHEN Miles >= 412 and 'Miles' < 437 THEN '412-437' 
    WHEN Miles >= 438 and 'Miles' < 463 THEN '438-463' 
     WHEN Miles >= 464 and 'Miles' < 489 THEN '464-489' 
    WHEN Miles >= 490 and 'Miles' < 500 THEN '490-500' 
    ELSE 'over 500' 
    END 
    FROM @MilesTable 

就像我说的,这是不漂亮,但它会在最低限度....希望缓解转换错误这让你开始朝着正确的方向发展!

+0

但是,我们没有使用里程表,这是我们正在报告中的一列。我们把它放在离校园不远的地方,而不用使用这个选择语句。在转换英里数后,我们希望它将数字放入coluhms中。例如,我会在数学后离校园20英里远的地方居住。现在我想把我的青年放到一个类别中,我将属于这个类别,并且距离校园不到24英里。我们正试图获得每个类别的累计总数。 – user3629893

+0

我认为你要做的然后是把: ( 3959 * acos( cos(弧度(43。7779)) * COS(弧度(fc.Address1_Latitude)) * COS(弧度(fc.Address1_Longitude) - 弧度(-88.4215)) +罪(弧度(43.7779)) *罪(弧度(fc.Address1_Latitude) ) )) 你的时间。如果您试图针对您在运行时创建的列运行case语句,则它不会像原文中所写的那样工作,因为尽管您和我知道该列被称为“Miles”,但SQL不知道。这将是巨大而令人讨厌的,因此@MilesTable –