zoukankan      html  css  js  c++  java
  • sql if else 写法,和 多个case when用法

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO
    /*
    * create by huang qing
    * modify by chaoxueling
    * modify date 2013-05-15
    */
    ALTER PROCEDURE [dbo].[sp_GetPropsTop_ByPropsCount]
     @PageIndex  int,
     @PageSize  int,
     @OrderType  int
    AS
    BEGIN

    if(@OrderType=1)
    begin
    with Mem_TbUserPropsTop as
     (
      select A.*,b.PropsCountB,ROW_NUMBER() over (order by PropsCountA desc) as rowNumber from (
      select  QdUserId,PropsId,propscnt as PropsCountA from tbuserpropscnt where   propsid=153
       ) as A
      full  join(
      select  QdUserId,PropsId,propscnt as PropsCountB from tbuserpropscnt where  propsid=154
      ) as B
      on a.qduserid=b.qduserid
        )
     select qduserid,PropsId,ISNULL(PropsCountA,0) as PropsCountA,ISNULL(PropsCountB,0) as PropsCountB, rowNumber from Mem_TbUserPropsTop
     where rowNumber>@PageSize*(@PageIndex-1) and RowNumber<=@PageSize*@PageIndex
     order by   PropsCountA  desc 
    end
    else if(@OrderType=0)
    begin
    with Mem_TbUserPropsTop as
     (
      select A.*,b.PropsCountA,ROW_NUMBER() over (order by PropsCountB desc) as rowNumber from (
      select  QdUserId,PropsId,propscnt as PropsCountB from tbuserpropscnt where   propsid=154
       ) as A
      full  join(
      select  QdUserId,PropsId,propscnt as PropsCountA from tbuserpropscnt where  propsid=153
      ) as B
      on a.qduserid=b.qduserid
        )
     select qduserid,PropsId,ISNULL(PropsCountA,0) as PropsCountA,ISNULL(PropsCountB,0) as PropsCountB, rowNumber from Mem_TbUserPropsTop
     where rowNumber>@PageSize*(@PageIndex-1) and RowNumber<=@PageSize*@PageIndex
     order by   PropsCountB desc 
    end
    else
    begin
    with Mem_TbUserPropsTop as
     (
      select A.*,b.PropsCountA,ROW_NUMBER() over (order by PropsCountB desc) as rowNumber from (
      select  QdUserId,PropsId,propscnt as PropsCountB from tbuserpropscnt where   propsid=154
       ) as A
      full  join(
      select  QdUserId,PropsId,propscnt as PropsCountA from tbuserpropscnt where  propsid=153
      ) as B
      on a.qduserid=b.qduserid
        )
     select qduserid,PropsId,ISNULL(PropsCountA,0) as PropsCountA,ISNULL(PropsCountB,0) as PropsCountB, rowNumber from Mem_TbUserPropsTop
     where rowNumber>@PageSize*(@PageIndex-1) and RowNumber<=@PageSize*@PageIndex
     order by   PropsCountB desc 
    end


    END

     和

     order by   case when @OrderType=1  then PropsCountA when @OrderType=0 then PropsCountB when @OrderType=2  then PropsCountC else PropsCountD end  desc 

  • 相关阅读:
    事务处理多数据库的方法
    微信定位js
    linux命令大全
    git团队协作
    springboot入门的一点基础
    Spring Boot Actutaur + Telegraf + InFluxDB + Grafana 构建监控平台
    Hadoop+HBase+ZooKeeper三者关系与安装配置
    Centos7网络配置-转载
    【Java EE 学习 21 下】【 使用易宝支付接口实现java网上支付功能】
    linux 中 iptables关于ping的问题
  • 原文地址:https://www.cnblogs.com/cxlings/p/3077762.html
Copyright © 2011-2022 走看看