SQL:找到最接近的浮点值

时间:2017-08-08 06:50:11

标签: mysql sql

没有窗口功能支持的MariaDB / MySQL

<ClaimsProvider>
  <Domain>salesforce</Domain>
  <DisplayName>Salesforce</DisplayName>
  <TechnicalProfiles>
    <TechnicalProfile Id="salesforce">
      <DisplayName>Salesforce</DisplayName>
      <Description>Login with your Salesforce account</Description>
      <Protocol Name="SAML2"/>
      <Metadata>
    <Item Key="RequestsSigned">false</Item>
    <Item Key="WantsEncryptedAssertions">false</Item>
    <Item Key="WantsSignedAssertions">false</Item>
    <Item Key="PartnerEntity">https://contoso-dev-ed.my.salesforce.com/.well-known/samlidp.xml</Item>
      </Metadata>
      <CryptographicKeys>
    <Key Id="SamlAssertionSigning" StorageReferenceId="B2C_1A_SAMLSigningCert"/>
    <Key Id="SamlMessageSigning" StorageReferenceId="B2C_1A_SAMLSigningCert"/>
      </CryptographicKeys>
      <OutputClaims>
    <OutputClaim ClaimTypeReferenceId="socialIdpUserId" PartnerClaimType="userId"/>
    <OutputClaim ClaimTypeReferenceId="givenName" PartnerClaimType="given_name"/>
    <OutputClaim ClaimTypeReferenceId="surname" PartnerClaimType="family_name"/>
    <OutputClaim ClaimTypeReferenceId="email" PartnerClaimType="email"/>
    <OutputClaim ClaimTypeReferenceId="displayName" PartnerClaimType="username"/>
    <OutputClaim ClaimTypeReferenceId="authenticationSource" DefaultValue="externalIdp"/>
    <OutputClaim ClaimTypeReferenceId="identityProvider" DefaultValue="SAMLIdp" />
      </OutputClaims>
      <OutputClaimsTransformations>
    <OutputClaimsTransformation ReferenceId="CreateRandomUPNUserName"/>
    <OutputClaimsTransformation ReferenceId="CreateUserPrincipalName"/>
    <OutputClaimsTransformation ReferenceId="CreateAlternativeSecurityId"/>
    <OutputClaimsTransformation ReferenceId="CreateSubjectClaimFromAlternativeSecurityId"/>
      </OutputClaimsTransformations>
      <UseTechnicalProfileForSessionManagement ReferenceId="SM-Noop"/>
    </TechnicalProfile>
  </TechnicalProfiles>
</ClaimsProvider>

对于每个设备,DATA表中都有许多记录

Table DEVICE {
 id int,   <— Primary key
 name  varchar(10)  
}

用户提供了一些target_value。

任务:在每个设备的DATA表中找到最接近的值。

Table DATA (
  device_id   int,    <— Foreign key to DEVICE.id
  value float
)

对于输入target_value = 5.0,预期输出为:

DEVICE
-----------
id |  name 
-----------
1  |  A
2  |  B
3  |  C


       DATA
------------------------
device_id      |  value 
------------------------
 1             |  5.3
 1             |  5.9
 2             |  4.0
 2             |  4.5
 3             |  5.1

我知道如何为单个设备找到最接近的值(例如device_id = 2):

--------------------
id |  value 
---------------------
 1 |  5.3
 2 |  4.5
 3 |  5.1

...但我不知道如何扩展它以返回几个device_id的结果

0 个答案:

没有答案