/****** Object:  StoredProcedure [dbo].[GetCommonGroupByRegion]    Script Date: 03/23/2017 17:31:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO ALTER proc [dbo].[GetCommonGroupByRegion]
@CarCategory varchar(50)=null,---车型
@CreateTimeStart datetime=null, --入会开始时间
@CreateTimeEnd datetime=null, --入会结束时间
@AuthenticationTimeStart datetime=null, --认证开始时间
@AuthenticationTimeEnd datetime=null, --认证结束时间
@BuyTimeStart datetime=null, --购车开始时间
@BuyTimeEnd datetime=null --购车结束时间 as
begin DECLARE @strPC VARCHAR(2000), @strAPP VARCHAR(2000), @strWX VARCHAR(2000), @strWAP VARCHAR(2000)
---------------------pc start
SET @strPC = ( SELECT DISTINCT STUFF(
( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK )
WHERE qtype = 'pc' ORDER BY qudao
FOR XML PATH('')), 1,4, '') AS jzhw
FROM CreatedType t)
set @strPC= left(@strPC,len(@strPC)-1)
-------------------------pc end***********
------------------------app start
SET @strAPP = ( SELECT DISTINCT STUFF(
( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK )
WHERE qtype = 'app' ORDER BY qudao
FOR XML PATH('')), 1,4, '') AS jzhw
FROM CreatedType t)
set @strAPP= left(@strAPP,len(@strAPP)-1)
------------------------- app end *******
------------------------wx start
SET @strWX = ( SELECT DISTINCT STUFF(
( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK )
WHERE qtype = 'wx' ORDER BY qudao
FOR XML PATH('')), 1,4, '') AS jzhw
FROM CreatedType t)
set @strWX= left(@strWX,len(@strWX)-1)
------------------------- wx end *******
------------------------wap start
SET @strWAP = ( SELECT DISTINCT STUFF(
( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK )
WHERE qtype = 'wap' ORDER BY qudao
FOR XML PATH('')), 1,4, '') AS jzhw
FROM CreatedType t)
set @strWAP= left(@strWAP,len(@strWAP)-1)
------------------------- wap end *******
select
c.CarCategory as '车型',
d.Region as '区域',
COUNT(case when a.CreatedPerson like '%D%' and len(a.CreatedPerson)=5 then a.Id else null end ) as '经销商',
COUNT(case when a.CreatedPerson in( @strPC) then a.Id else null end ) as '网站', COUNT(case when a.CreatedPerson in(@strAPP) then a.Id else null end ) as 'APP', COUNT(case when a.CreatedPerson in(@strWX) then a.Id else null end ) as '微信', COUNT(case when a.CreatedPerson in( @strWAP) then a.Id else null end ) as 'wap'
from
Membership a
left join IF_Customer b on a.IdentityNumber=b.IdentityNumber
left join IF_Car c on b.CustId=c.CustId
left join CS_CarDealerShip d on c.DealerId=d.DealerId
where
a.IsDel=0 --case when @CreateTimeStart is not null then and a.CreateTime>@CreateTimeStart else null end
--and ( (@CreateTimeStart is not null and @CreateTimeStart!='') and (a.CreateTime>@CreateTimeStart) )
--and ( (@CreateTimeEnd is not null and @CreateTimeEnd!='') and (a.CreateTime<=@CreateTimeEnd) ) and (a.CreateTime>=@CreateTimeStart or @CreateTimeStart is null)
and (a.CreateTime<=@CreateTimeEnd or @CreateTimeEnd is null) --and ( (@AuthenticationTimeStart is not null and @AuthenticationTimeStart!='') and (a.AuthenticationTime>@AuthenticationTimeStart) )
--and ( (@AuthenticationTimeEnd is not null and @AuthenticationTimeEnd!='') and (a.AuthenticationTime<=@AuthenticationTimeEnd) ) and (a.AuthenticationTime>=@AuthenticationTimeStart or @AuthenticationTimeStart is null)
and (a.AuthenticationTime<=@AuthenticationTimeEnd or @AuthenticationTimeEnd is null) --and ( (@CarCategory is not null and @CarCategory!='') and (c.CarCategory=@CarCategory) )
and (c.CarCategory=@CarCategory or @CarCategory is null ) --and ( (@BuyTimeStart is not null and @BuyTimeStart!='') and ( c.BuyTime>@BuyTimeStart) )
--and ( (@BuyTimeEnd is not null and @BuyTimeEnd!='') and ( c.BuyTime<=@BuyTimeEnd) ) and (c.BuyTime>=@BuyTimeStart or @BuyTimeStart is null)
and (c.BuyTime<=@BuyTimeEnd or @BuyTimeEnd is null) and d.Region<>''
and d.Region<>'-'
group by c.CarCategory, d.Region
; end

解析:   and (c.BuyTime<=@BuyTimeEnd or @BuyTimeEnd is null)

首先 and 条件后面是括号,那就说明括号 是一个整体,  括号里面 用的 or 语法,也就说明只要任意一个条件满足就ok, 当参数为空的时候,假如作为条件的字段是时间格式的,如果做大小比较肯定会失败,这个时候后面的 or的条件满足了,就把前面的给忽略了,其实这个时候就相当于  and 1=1 了,

sql 存储过程参数为空则不作为条件的更多相关文章

  1. sql 存储过程参数是表类型,数据库中如何调用

    DECLARE @NEW_STUDENT as [CancelLendersContent] INSERT @NEW_STUDENT VALUES (0,0,0,'12345678912','张三', ...

  2. 六、SQL语句进行多条件查询,并解决参数为空的情况

    一.SQL语句进行多条件查询,并解决参数为空的情况 QueryEntity query; var whereSql = new StringBuilder("Where 1=1") ...

  3. SQL Server 存储过程中处理多个查询条件的几种常见写法分析,我们该用那种写法

    本文出处: http://www.cnblogs.com/wy123/p/5958047.html 最近发现还有不少做开发的小伙伴,在写存储过程的时候,在参考已有的不同的写法时,往往很迷茫,不知道各种 ...

  4. 在论坛中出现的比较难的sql问题:14(存储过程问题 存储过程参数、存储过程内的参数)

    原文:在论坛中出现的比较难的sql问题:14(存储过程问题 存储过程参数.存储过程内的参数) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的 ...

  5. 在SQL存储过程中给条件变量加上单引号

    在SQL存储过程中给条件变量加上单引号,不加语句就会出问题,以下就是在存储过程中将条件where设置成了动态变化的,给where赋完值再和前面的语句拼接,再execute(SQL) ), )), )+ ...

  6. FineReport: 参数为空选出全部值(按条件查询,空条件时直接过滤,不进行查询。。)

    在Java报表软件FineReport中,选择特定的参数(如下图中的姓名.身份证号等)后,会返回我们要查询的数据,然而假如没有输入参数值,我们却仍需要返回数据时该怎样处理呢?应该过滤掉这个条件,不按这 ...

  7. sql 解析字符串添加到临时表中 sql存储过程in 参数输入

    sql 解析字符串添加到临时表中  sql存储过程in 参数输入 解决方法 把字符串解析 添加到 临时表中 SELECT * into #临时表   FROM dbo.Func_SplitOneCol ...

  8. SQL 存储过程 多条件 分页查询 性能优化

    最优化查询代码 -- 注意:此处可能会出现 字符串过长问题,所以 必要的情况下请分段处理 set @sql1 =' SELECT * FROM ( select ROW_NUMBER() OVER(O ...

  9. PL/SQL存储过程编程

    PL/SQL存储过程编程 /**author huangchaobiao *Email:huangchaobiao111@163.com */ PL/SQL存储过程编程(上) 1. Oracle应用编 ...

随机推荐

  1. C#项目”XXXXX”针对的是”.NETFramework,Version=v4.7.1”但此计算机没有安装它

    遇到这样一个问题:C#项目”XXXXX”针对的是”.NETFramework,Version=v4.7.1”但此计算机没有安装它 就是我在打开别人的项目,发现别人的项目.Net Framework的版 ...

  2. pyCharm最新激活码(2018激活码)

    首先输入新的License sever address 首先尝试处理方法是,针对过期会弹出激活框: 选择 Activate new license with License server (用lice ...

  3. mac 互传文件

    搭建HTTP服务,然后局域网访问就行 PHP方式: php -S 172.21.205.xxx:9999 Python python -m SimpleHTTPServer 8001 在浏览器访问:h ...

  4. ASP.NET Core MVC+EF Core从开发到部署

    笔记本电脑装了双系统(Windows 10和Ubuntu16.04)快半年了,平时有时间就喜欢切换到Ubuntu系统下耍耍Linux,熟悉熟悉Linux命令.Shell脚本以及Linux下的各种应用的 ...

  5. hibernate05--list和iterator

    package cn.bdqn.test; import java.util.Iterator; import java.util.List; import org.hibernate.Query; ...

  6. js 判断当前操作系统是ios还是android还是电脑端

    js判断客户端是否是IOS或者是Android //如果返回true 则说明是Android function is_weixin() { var ua = window.navigator.user ...

  7. 21、bootstrap框架

    http://www.bootcss.com/ Bootstrap 插件==全部依赖 jQuery== 请注意,Bootstrap 的所有 JavaScript 插件都依赖 jQuery,因此 jQu ...

  8. 浅谈编码Base64、Hex、UTF-8、Unicode、GBK等

    网络上大多精彩的回答,该随笔用作自我总结: 首先计算机只认得二进制,0和1,所以我们现在看到的字都是经过二进制数据编码后的:计算机能针对0和1的组合做很多事情,这些规则都是人定义的:然后有了字节的概念 ...

  9. php 腾讯地图和百度地图的相互转换

    关于百度和腾讯地图坐标转换的问题,网上大多给的是JS实现的,而JS又是异步实现的,无法返回值,在有些应用场景下不好使用,这里我从腾讯论坛上找到了一个PHP转换的方法,贴出来分享给大家.经测试十分有效! ...

  10. td使用word-break: break-all;强制换行无效的解决

    今天发现表格内容不换行,加了word-break: break-all;也没有效果,后来检查发现用户编辑的html内容包含了CSS,其中有一项: td {padding-top:1px;padding ...