SQL数组变量和If语句

时间:2014-09-17 16:20:12

标签: sql sql-server arrays tsql

我有一个历史表,可以保存有关合规性问题的数据。我想创建一个图表,显示每周的每个群集的合规状态。不幸的是,查询仅显示日期的不合规状态,因此我需要创建一个查询,其中包括集群也符合的日期。

我认为需要发生的是创建一个不同日期值的数组' D'然后以某种方式将数组值与行匹配,如果该行存在则显示它,如果它没有& #39; t存在然后创建一个缺少日期和兼容值的行。

有人能帮助我吗?

编辑 SQLFiddle:http://sqlfiddle.com/#!3/5fb3d/2

查询现在显示的内容:

+-------------------------------------------------+
|    D       |   Cluster      |      value        |
+-------------------------------------------------+
| 09/05/2014 |      T1        |  Non-compliant    |
+-------------------------------------------------+
| 09/12/2014 |      T1        |  Non-compliant    |
+-------------------------------------------------+
| 09/17/2014 |      T1        |  Non-compliant    |
+-------------------------------------------------+
| 09/05/2014 |      T2        |  Non-compliant    |
+-------------------------------------------------+
| 09/12/2014 |      T2        |  Non-compliant    |
+-------------------------------------------------+
| 09/17/2014 |      T2        |  Non-compliant    |
+---------------------------------------------- --+
| 09/12/2014 |      T3        |  Non-compliant    |
+-------------------------------------------------+
| 09/17/2014 |      T3        |  Non-compliant    |
+-------------------------------------------------+

我需要它看起来像:

+-------------------------------------------------+
|    D       |   Cluster      |      value        |
+-------------------------------------------------+
| 09/05/2014 |      T1        |  Non-compliant    |
+-------------------------------------------------+
| 09/12/2014 |      T1        |  Non-compliant    |
+-------------------------------------------------+
| 09/17/2014 |      T1        |  Non-compliant    |
+-------------------------------------------------+
| 09/05/2014 |      T2        |  Non-compliant    |
+-------------------------------------------------+
| 09/12/2014 |      T2        |  Non-compliant    |
+-------------------------------------------------+
| 09/17/2014 |      T2        |  Non-compliant    |
+-------------------------------------------------+
| 09/05/2014 |      T3        |  Compliant        |
+---------------------------------------------- --+
| 09/12/2014 |      T3        |  Non-compliant    |
+-------------------------------------------------+
| 09/17/2014 |      T3        |  Non-compliant    |
+-------------------------------------------------+

表结构示例数据:

CREATE TABLE CLUSTER_INFO_HIST
    (

     CLUSTER_NAME varchar(255), 
     HA_ENABLED varchar(255),
     DATE_PULL varchar(255)
    );

INSERT INTO CLUSTER_INFO_HIST
(CLUSTER_NAME, HA_ENABLED ,DATE_PULL)
VALUES
('T1', '1','09/12/2014'),
('T1', '1','09/05/2014'),
('T1', '0','08/31/2014'),
('T2', '1','09/12/2014'),
('T2', '0','09/05/2014'),
('T2', '0','08/31/2014'),
('T3', '1','09/12/2014'),
('T3', '1','09/05/2014'),
('T3', '0','08/31/2014');

CREATE TABLE CLUSTER_INFO
    (

     CLUSTER_NAME varchar(255), 
     HA_ENABLED varchar(255),
     DATE_PULL varchar(255)
    );

INSERT INTO CLUSTER_INFO
(CLUSTER_NAME, HA_ENABLED ,DATE_PULL)
VALUES
('T1', '1','09/17/2014'),
('T2', '0','09/17/2014'),
('T3', '1','09/17/2014');

我的查询:

SELECT DATE_PULL, CLUSTER_NAME, HA_ENABLED FROM CLUSTER_INFO WHERE HA_ENABLED <> '1'
UNION ALL
SELECT DATE_PULL, CLUSTER_NAME, HA_ENABLED FROM CLUSTER_INFO_HIST WHERE HA_ENABLED <> '1'
ORDER BY DATE_PULL, CLUSTER_NAME

1 个答案:

答案 0 :(得分:1)

如果您只想显示现有的表条目,请写下:

Select DATE_PULL,CLUSTER_NAME,CASE when Coalesce(HA_Enabled,'0') =0 then 'Compliant' else 'NON-Compliant' end as value
from
( 
Select * from CLUSTER_INFO_HIST 
UNION
Select * from  CLUSTER_INFO 
) CI  
ORDER BY DATE_PULL, CLUSTER_NAME

如果您需要在日期范围内提供信息,即使并非所有群集都报告您可能会使用:

Declare @Start dateTime='20140101'
Declare @End dateTime=GetDate()


;With CTE as ( -- create date table
Select @Start as aDate
UNION ALL
Select  1 + aDate
from CTE 
where  aDate+1 <@End
)
Select aDate,ALCLUSTER.CLUSTER_NAME,Coalesce(HA_Enabled,'0') as HA_Enabled
      ,CASE when Coalesce(HA_Enabled,'0') =0 then 'Compliant' else 'NON-Compliant' end as value
from CTE c

JOIN  --join all known CLUSTER_NAMEs 
( 
Select DISTINCT CLUSTER_NAME from CLUSTER_INFO_HIST 
UNION
Select DISTINCT CLUSTER_NAME from  CLUSTER_INFO c
) ALCLUSTER on 1=1

LEFT JOIN -- add HA_Enabled info
( 
Select * from CLUSTER_INFO_HIST 
UNION
Select * from  CLUSTER_INFO 
) CI on Convert(date,DATE_PULL,101)=c.aDate and ALCLUSTER.CLUSTER_NAME=CI.CLUSTER_NAME

OPTION (MAXRECURSION 0) -- prevent recursion break for CTE at 100