如何在SQL Server 2014中使用if语句

时间:2016-09-28 14:23:15

标签: sql-server-2012

这是我的表格

INSERT INTO Paiment (PaimentDate, Champs1, Champs2, Champs3)
VALUES (2013, 90, 70, 60), (2011, 20, 30, 40), (2009, 50, 0, 60), 
       (2008, 60, 16, 60), (2007, 0, 36, 20), (2006, 30, 25, 63),
       (2017, 10, 70, 40), (2019, 20, 30, 32), (2030, 50, 0, 63), 
       (2020, 60, 16, 22), (2018, 0, 36, 33), (2005, 30, 25, 17)

entrydate在2005年之前时,将sums champs1和champs2与paimentDate降低到2009年。

否则entrydate大于或等于2005,将paimentDate低于2003的champs1的数量加上与paimentDate低于2001的champs2数量

这是我的代码,请任何人都可以帮助我。

 select  
     (Case 
         when DateofEntry < 2011 AND PaimentDate < 2005

            Then (IsNull(Champs1, 0) + IsNull(Champs2, 0) 
                                                    Else (case when DateofEntry >= 2001
               Then (
                    (CASE WHEN PaimentDate < 2009 THEN ISNULL(Champs1.E1, 0) ELSE 0 END 

                                        +

                                         CASE WHEN PaimentDate < 2011 THEN ISNULL(Champs2.E3, 0) ELSE 0 END)
                  )
          end)

    End)

这是我的代码。问题是当paiementDate低于2005时,它必须使用champs1 + champs2中的所有元素的总和,使用低于2005的服装。但是在这个代码中,它使得所有的champs1 + champs2的总和,即使该paiement高于2005年并且这不是我想要的。

请帮助

1 个答案:

答案 0 :(得分:0)

重新格式化您的SELECT语句

if (project.getPlugins().hasPlugin('com.android.application') ||
          project.getPlugins().hasPlugin('com.android.library')) {
    task install(type: Upload, dependsOn: assemble) {
      repositories.mavenInstaller {
        configuration = configurations.archives

        pom.groupId = GROUP
        pom.artifactId = POM_ARTIFACT_ID
        pom.version = VERSION_NAME

        pom.project {
          name POM_NAME
          packaging POM_PACKAGING
          description POM_DESCRIPTION
          url POM_URL

          scm {
            url POM_SCM_URL
            connection POM_SCM_CONNECTION
            developerConnection POM_SCM_DEV_CONNECTION
          }

          licenses {
            license {
              name POM_LICENCE_NAME
              url POM_LICENCE_URL
              distribution POM_LICENCE_DIST
            }
          }

          developers {
            developer {
              id pom_developer_id
              name pom_developer_name
            }
          }

        }

        //Updated part which adds dependencies in pom
        pom.withXml {
          def dependenciesNode = asNode().appendNode('dependencies')

          //Iterate over the compile dependencies (we don't want the test ones), adding a <dependency> node for each
          configurations.compile.allDependencies.each {
            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', it.group)
            dependencyNode.appendNode('artifactId', it.name)
            dependencyNode.appendNode('version', it.version)
          }
        }
      }
    }