在SQL Server存储过程中基于不同选择显示结果

SKJ

我目前的Microsoft SQL Server上的工作2008年我的问题是:我已经通过参数来表示5个条件@rep@name@work@year@seg如果我只填写某些条件,该如何编写查询以显示结果?然后我有一个查询,该查询将计算访问的总数。例如,

DECLARE @Rep varchar(100)
DECLARE @Name varchar(100)
DECLARE @Work varchar (100)
DECLARE @Year int
DECLARE @Seg int

SET @RepCode = ''
SET @Name = 'SYED'
SET @Work = ''
SET @Year = '2014'
SET @Seg = ''

我希望结果显示syed2014年的结果以及代表,工作和他的段。我尝试了一下,但没有成功。

if @Rep IS NOT NULL
    SELECT Id, Name, Work, YEAR, Rep 
    FROM #SalesRepVisit
    WHERE Rep = @Rep
ELSE 
   if @Name IS NOT NULL
      SELECT Id, Name, Work, YEAR, Rep 
      FROM #SalesRepVisit
      WHERE Name = @Name
   ELSE
      if @Work IS NOT NULL
         SELECT Id, Name, Work, YEAR, Rep 
         FROM #SalesRepVisit
         WHERE Work = @Work
      ELSE
          if @Year IS NOT NULL
             SELECT Id, Name, Work, YEAR, Rep
             FROM #SalesRepVisit
             WHERE YEAR = @Year
          ELSE
             if @Seg IS NOT NULL
                SELECT Id, Name, Work, YEAR, Rep 
                FROM #SalesRepVisit
                WHERE Seg = @Seg

要计算访问的总数,我有此查询。

SELECT DISTINCT
    Id as Id,
    Name as Customername,
    Rep As Repcode,
    (Select COUNT(*)  from #SalesRepVisit where Month = 1 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'January',
    (Select COUNT(*)  from #SalesRepVisit where Month = 2 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'February',
    (Select COUNT(*)  from #SalesRepVisit where Month = 3 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'March' ,
    (Select COUNT(*)  from #SalesRepVisit where Month = 4 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'April',
    (Select COUNT(*)  from #SalesRepVisit where Month = 5 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'May',
    (Select COUNT(*)  from #SalesRepVisit where Month = 6 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'June',
    (Select COUNT(*)  from #SalesRepVisit where Month = 7 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'July',
    (Select COUNT(*)  from #SalesRepVisit where Month = 8 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'August',
    (Select COUNT(*)  from #SalesRepVisit where Month = 9 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'September',
    (Select COUNT(*)  from #SalesRepVisit where Month = 10 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'October',
    (Select COUNT(*)  from #SalesRepVisit where Month = 11 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'November',
    (Select COUNT(*)  from #SalesRepVisit where Month = 12 and Rep = @Rep and Name = @Name AND Work = @Work and YEAR = @Year)as 'December',
    Work as Work,
    YEAR as Year
FROM #SalesRepVisit
WHERE 
    Rep = @Rep and Name = @Name  AND Work = @Work AND YEAR = @Year

查询不显示任何内容。我希望查询显示“ syed”的结果以及他的名字,代表,工作,段和年。

拉杰什

@Radar的改进答案

在这种Where情况下我们可以使用Case语句

SELECT Id,
       Name,
       SUM ( CASE WHEN Month =1 THEN 1 ELSE 0 END ) as 'January',
       SUM ( CASE WHEN Month =2 THEN 1 ELSE 0 END ) as 'February',
       ...
       ...
FROM   #SalesRepVisit
where  ( Rep  = Case When IsNull(@RepCode,'') ='' then Rep else @RepCode end)
   AND ( Name = Case When IsNull(@Name,'')='' then Name else @Name end)
   AND ( Work = Case When IsNull(@Work,'') ='' then Work else @Work end)
   AND ( YEAR = Case When IsNull(@Year,'') ='' then YEAR else @Year end)
GROUP BY Id, Name

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在SQL Server存储过程中基于不同选择显示结果

来自分类Dev

SQL:在存储过程中显示表的结果

来自分类Dev

如何获取sql server存储过程结果以在其他存储过程中处理?

来自分类Dev

SQL Server存储过程中的错误

来自分类Dev

SQL Server存储过程中的IF条件

来自分类Dev

SQL Server:存储过程中的IF THEN ELSE

来自分类Dev

SQL-帮我选择在存储过程中串联两个变量的结果

来自分类Dev

如何从带有PHP预准备语句的SQL Server存储过程中返回多个结果?

来自分类Dev

将查询放入存储过程中时,SQL Server将不会返回结果

来自分类Dev

如何计算 SQL Server 存储过程中选择的数量

来自分类Dev

SQL Server:在存储过程中将多个列选择到多个变量中

来自分类Dev

将存储过程中的单个值选择输出分配给变量(SQL Server)

来自分类Dev

如何在存储过程中提交事务之前在SQL Server中锁定选择的行

来自分类Dev

存储过程中的动态SQL无法获取所需的结果

来自分类Dev

SQL设置变量作为存储过程中查询的结果

来自分类Dev

SQL设置变量作为存储过程中查询的结果

来自分类Dev

在SQL Server中的存储过程中读取xml的问题

来自分类Dev

SQL Server中存储过程中的可选参数

来自分类Dev

在SQL Server中的存储过程中读取xml的问题

来自分类Dev

SQL Server:IF条件在存储过程中不起作用

来自分类Dev

SQL Server:存储过程中的并发问题

来自分类Dev

做 ; 表示SQL Server存储过程中的任何内容

来自分类Dev

SQL Server存储过程中的多个表类型参数

来自分类Dev

从SQL Server存储过程中检索特定数据

来自分类Dev

如何捕获SQL Server存储过程中的错误?

来自分类Dev

在SQL Server存储过程中插入@tablename

来自分类Dev

SQL Server:存储过程中的动态查询

来自分类Dev

在SQL Server中的存储过程中使用循环

来自分类Dev

在SQL Server存储过程中创建表类型参数

Related 相关文章

  1. 1

    在SQL Server存储过程中基于不同选择显示结果

  2. 2

    SQL:在存储过程中显示表的结果

  3. 3

    如何获取sql server存储过程结果以在其他存储过程中处理?

  4. 4

    SQL Server存储过程中的错误

  5. 5

    SQL Server存储过程中的IF条件

  6. 6

    SQL Server:存储过程中的IF THEN ELSE

  7. 7

    SQL-帮我选择在存储过程中串联两个变量的结果

  8. 8

    如何从带有PHP预准备语句的SQL Server存储过程中返回多个结果?

  9. 9

    将查询放入存储过程中时,SQL Server将不会返回结果

  10. 10

    如何计算 SQL Server 存储过程中选择的数量

  11. 11

    SQL Server:在存储过程中将多个列选择到多个变量中

  12. 12

    将存储过程中的单个值选择输出分配给变量(SQL Server)

  13. 13

    如何在存储过程中提交事务之前在SQL Server中锁定选择的行

  14. 14

    存储过程中的动态SQL无法获取所需的结果

  15. 15

    SQL设置变量作为存储过程中查询的结果

  16. 16

    SQL设置变量作为存储过程中查询的结果

  17. 17

    在SQL Server中的存储过程中读取xml的问题

  18. 18

    SQL Server中存储过程中的可选参数

  19. 19

    在SQL Server中的存储过程中读取xml的问题

  20. 20

    SQL Server:IF条件在存储过程中不起作用

  21. 21

    SQL Server:存储过程中的并发问题

  22. 22

    做 ; 表示SQL Server存储过程中的任何内容

  23. 23

    SQL Server存储过程中的多个表类型参数

  24. 24

    从SQL Server存储过程中检索特定数据

  25. 25

    如何捕获SQL Server存储过程中的错误?

  26. 26

    在SQL Server存储过程中插入@tablename

  27. 27

    SQL Server:存储过程中的动态查询

  28. 28

    在SQL Server中的存储过程中使用循环

  29. 29

    在SQL Server存储过程中创建表类型参数

热门标签

归档