Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在查询中插入固定值_Sql_Sql Server - Fatal编程技术网

Sql 在查询中插入固定值

Sql 在查询中插入固定值,sql,sql-server,Sql,Sql Server,有这个问题,我想找人帮我解决。我原以为用程序代码解决这个问题会更容易,但现在我开始重新思考这个想法 使用PIVOT的SQL查询从SELECT语句中提取数据,其中一个字段[Cat_Entries]是一个硬编码值,用作单元格标签。Cat_条目的值是使用COUNTEventGUID和/或CASTSUMDuration作为varchar10作为同一个句子中的TotalRepairTime计算得出的 从下面的SELECT语句中,您可以看到三个查询的并集用于生成输出。但是,如果子查询没有返回任何值作为计算结

有这个问题,我想找人帮我解决。我原以为用程序代码解决这个问题会更容易,但现在我开始重新思考这个想法

使用PIVOT的SQL查询从SELECT语句中提取数据,其中一个字段[Cat_Entries]是一个硬编码值,用作单元格标签。Cat_条目的值是使用COUNTEventGUID和/或CASTSUMDuration作为varchar10作为同一个句子中的TotalRepairTime计算得出的

从下面的SELECT语句中,您可以看到三个查询的并集用于生成输出。但是,如果子查询没有返回任何值作为计算结果。然而,要求显示标签[Cat_Entries],而不考虑其值

下面是数据现在的样子:

Cat     Cat Entries           6/1/2012  7/1/2012  8/1/2012  9/1/2012
------  --------------------  --------  --------  --------  --------
00000A  Critical Down Time       1       
00000A  Critical Outage          1       
00000A  Total Repair Time        65         
00000B  Critical Down Time                                     6
00000B  Total Repair Time                                      90
00000C  Critical Down Time       1          5    
00000C  Critical Outage          1          5    
00000C  Total Repair Time        30         240    
00000D  Critical Down Time                                     2     
00000E  Critical Down Time                          1    
00000G  Critical Down Time                                     1    
00000M  Critical Down Time        1                            3    
00000M  Critical Outage           1                 3    
00000M  Total Repair Time         60                180   
。。。这是所需输出的外观**指示插入的空行:

    Cat     Cat Entrie s          6/1/2012  7/1/2012  8/1/2012  9/1/2012
    ------  --------------------  --------  --------  --------  --------
    00000A  Critical Down Time       1       
    00000A  Critical Outage          1       
    00000A  Total Repair Time        65         
    00000B  Critical Down Time                                     6
**  00000B  Critical Outage          
    00000B  Total Repair Time                                      90
    00000C  Critical Down Time       1          5    
    00000C  Critical Outage          1          5    
    00000C  Total Repair Time        30         240 
    00000D  Critical Down Time                                     2   
**  00000D  Critical Outage          
**  00000D  Total Repair Time 
以下是使用PIVOT的存储过程:

。。。以下是存储过程使用的视图:

SELECT SiteGUID, Cat_Entries, EntriesCount AS Value, 
       CONVERT(date, CAST(Month AS varchar(2)) + '-1-' + CAST(Year AS varchar(4))) AS MonthYear, 
       Month, Year, OrderBy, Cat, EventId
FROM (SELECT SiteGUID, 'Critical Down Time' AS Cat_Entries, COUNT(EventGUID) AS EntriesCount,
             Month, Year, 1 AS OrderBy, Cat, EventId
FROM dbo.vCatCountTotalEntries
GROUP BY SiteGUID, Month, Year, Cat, EventId

UNION
      SELECT SiteGUID, 'Critical Outage' AS Cat_Entries, COUNT(EventGUID) AS EntriesCount,
             Month, Year, 2 AS OrderBy, Cat, EventId
      FROM   dbo.vCatCountEqpEntries
      GROUP BY SiteGUID, Cat, Month, Year, Cat, EventId
UNION
      SELECT SiteGUID, 'Total Repair Time' AS Cat_Entries, 
             CAST(SUM(Duration) AS varchar(10)) AS TotalRepairTime, Month, Year, 3 AS OrderBy,
             Cat, EventId
      FROM   dbo.vCatCountOnEqpRprTm
      GROUP BY SiteGUID, Cat, Month, Year, Cat, EventId) AS W
一如既往,我感谢你的时间和努力!
R.

因此,为了获得额外的行,您的主select查询必须能够计算它们,这意味着您不能将它们过滤掉

您当前正在startdate和stopdate之间执行[Year],因此您的初始结果集将只包含日期之间的值。与其限制它,不如让你的轴心为你做那个限制

所以你唯一需要做的改变就是把你的年度筛选从你的主要选择中去掉,我相信你会看到你想要的结果

下面是一个例子来说明我的意思:

    CREATE TABLE #years(year_name VARCHAR(20), year_actual int)
            --Insert three years
    INSERT INTO #years
            ( year_name, year_actual )
    SELECT 'rat', -- year_name - varchar(20)
              1924  -- year_actual - int
    UNION ALL
    SELECT 'ox',
              1925
    UNION ALL
    SELECT 'tiger',
              1926

            --I only care about two of those years
    SELECT year_name, [1924], [1925]
    FROM
            --I need to select all the years so that all three rows show
    (SELECT year_name, year_actual FROM #years) yr
    PIVOT
            --Here I select the years I actually care to count
    (COUNT(year_actual) FOR year_actual IN ([1924], [1925])) pvt
结果:

     --Now I pulled all three rows, with 'dragon' having zero rows
    ox  0   1
    rat 1   0
    tiger   0   0

@JohnSaunders,想从sql方面试试吗?谢谢谢谢你的参与!年份将成为列标题,所以我需要选择这些,不是吗?以前从未使用过pivot-了解它的功能,但不是100%确定how@Risho你仍然在做你的cols变量,是的。您可以删除的部分是主查询变量中“+CHAR39+StartDate+CHAR39+”和“+CHAR39+StopDate+CHAR39+”之间的[Year]。
     --Now I pulled all three rows, with 'dragon' having zero rows
    ox  0   1
    rat 1   0
    tiger   0   0