SQL Dsum基于标准

时间:2015-07-02 15:31:36

标签: ms-access

我正在尝试将列'权重'= 2的所有金额相加,并按月计算,并以美元货币格式化。以下是我到目前为止的情况:

SELECT 
Format([Final Action Date],"yyyy-mm") AS [Month], 
DSum("[Amount]","C02: Underwriting Audit Case Detail Report Record Selection"," [Weight] = '2'") AS FA_Critical, 
Sum(IIf([Weight]="2",1,0)) AS Critical_Count

FROM [C02: Underwriting Audit Case Detail Report Record Selection]
WHERE ((([C02: Underwriting Audit Case Detail Report Record Selection].[Case Type]) Not In ("**Target IUP")))
GROUP BY Format([Final Action Date],"yyyy-mm");

这是我到目前为止的结果:

Month   FA_Critical Critical_Count
2015-01 2035480     2
2015-02 2035480     2
2015-03 2035480     0
2015-04 2035480     1

这就是我想要的:

Month   FA_Critical Critical_Count
2015-01 $1,350,000  2
2015-02 $510,480    2
2015-03 $0          0
2015-04 $175,000    1

请帮忙。 谢谢。

1 个答案:

答案 0 :(得分:1)

您的问题符合您的DSum标准,它在整个记录集中查看[权重] ='2',这就是为什么您获得所有4条记录的相同值。 GROUPUM没有以与SUM相同的方式对DSUM进行分区。

使用SUM而不是DSUM,而不是使用1,0表示true和false,使用amount列的值:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6" />
    <httpRuntime targetFramework="4.6" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="Curo.curoService">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:4568/
                         Design_Time_Addresses/Curo/curoService/" />
          </baseAddresses>
        </host>
        <endpoint address ="" binding="wsHttpBinding"
             contract = "Curo.IcuroService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding"
               contract = "IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <connectionStrings>
    <add name="curoEntities" connectionString="metadata=res://*/CuroDB.csdl|res://*/CuroDB.ssdl|res://*/CuroDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=WIN-M71FFCH83PK\SQL14MOBILESERV;initial catalog=curo;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>