DECLARE @fr varchar(50)
DECLARE @to varchar(50)
SELECT *
FROM test1.dbo.duplicate
WHERE dbavailability >= -1
IF (@fr='2014-05-01' AND @to='2014-05-04')
BEGIN
ALTER TABLE test1.dbo.duplicate ADD DEFAULT ((5)) FOR dbavailability
END
Database data:
id | tpoi | date | dbavailability
--------------------------------------------
1 | A | 2014-05-01 | 5
1 | A | 2014-05-02 | 4
1 | A | 2014-05-03 | 3
1 | A | 2014-05-04 | -1
Expected result:
id | tpoi | date | dbavailability
--------------------------------------------
1 | A | 2014-05-01 | -1
1 | A | 2014-05-02 | -1
1 | A | 2014-05-03 | -1
1 | A | 2014-05-04 | -1
我在date列中有这个表数据,1到4 ok,dbavailability包含5,4,3,-1 ok现在我正在搜索记录,如从日期开始(2014-05-01)到目前为止(2014-05 -04)记录。在该记录中,dbavailability包含-1,那么对于特定id,整个列的dbavailability应为-1,因此如何将重复值添加到" dbavailability"柱?并且在我的查询中我使用if条件,但它没有得到正确的结果。
答案 0 :(得分:0)
SELECT
id, tpoi, "date", CASE WHEN id IN
(SELECT DISTINCT
id
FROM
test1.dbo.duplicate
WHERE
dbavailability = -1) THEN '-1' ELSE dbavailability END AS dbAvailaility
FROM test1.dbo.duplicate
WHERE
date BETWEEN '2014-05-01' AND '2014-05-04'