0

我有一个名为时间表的表。其中我有以下行

  ID Status ProjId ActId  Date
    1  pending  1     1     2014-aug-07
    2  denied   1     2     2014-aug-08
    3  saved    1     3     2014-aug-09
    4  approved 1     4     2014-aug-10

我已使用以下查询来获得所需的结果。

SELECT * from tblTimesheet 
WHERE DATEPART(wk,spentDate) = 32 AND ((timesheetstatus = 'Pending' or timesheetstatus = 'Approved') and (timesheetstatus <> 'Saved' or timesheetstatus <> 'Denied'))

我对上述查询的预期结果是 0。但我得到的结果是 1。

 5  pending  1     1     2014-aug-11
    6  pending  1     2     2014-aug-12
    7  approved 1     3     2014-aug-13
    8  approved 1     4     2014-aug-14

这种情况下我的查询有效。

SELECT * from tblTimesheet 
WHERE DATEPART(wk,spentDate) = 32 AND ((timesheetstatus = 'Pending' or timesheetstatus = 'Approved') and (timesheetstatus <> 'Saved' or timesheetstatus <> 'Denied'))

我对上述查询的预期结果是 1。我得到的结果是 1。

如何在 mssql 中编写上述要求的查询。方案 1 有效。但方案 2 无效。如何达到预期的结果。任何帮助将不胜感激!!!

4

3 回答 3

1

请注意,这是 mysql 的解决方案,因为它也被标记了。它使用 MySQL 的“一周”标准定义 - 这似乎与您自己的定义不同。如果更符合您的定义,您可以使用 WEEK(date,3) 或类似的东西。

 DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Status VARCHAR(20) NOT NULL
,ProjId INT NOT NULL
,ActId INT NOT NULL
,Date DATE NOT NULL
);

INSERT INTO my_table VALUES
(1  ,'pending',1     ,1     ,'2014-08-07'),
(2  ,'denied',   1     ,2     ,'2014-08-08'),
(3  ,'saved',    1     ,3     ,'2014-08-09'),
(4  ,'approved', 1     ,4     ,'2014-08-10');

SELECT *,WEEK(date) FROM my_table;
+----+----------+--------+-------+------------+------------+
| ID | Status   | ProjId | ActId | Date       | WEEK(date) |
+----+----------+--------+-------+------------+------------+
|  1 | pending  |      1 |     1 | 2014-08-07 |         31 |
|  2 | denied   |      1 |     2 | 2014-08-08 |         31 |
|  3 | saved    |      1 |     3 | 2014-08-09 |         31 |
|  4 | approved |      1 |     4 | 2014-08-10 |         32 |
+----+----------+--------+-------+------------+------------+

SELECT x.* 
  FROM my_table x 
  LEFT 
  JOIN my_table y 
    ON WEEK(y.date) = WEEK(x.date) 
   AND y.status NOT IN ('approved','pending') 
 WHERE y.id IS NULL;
+----+----------+--------+-------+------------+
| ID | Status   | ProjId | ActId | Date       |
+----+----------+--------+-------+------------+
|  4 | approved |      1 |     4 | 2014-08-10 |
+----+----------+--------+-------+------------+

编辑:结果与 WEEK(date,3)...

SELECT x.* 
  FROM my_table x 
  LEFT 
  JOIN my_table y 
    ON WEEK(y.date,3) = WEEK(x.date,3) 
   AND y.status NOT IN ('approved','pending') 
 WHERE y.id IS NULL;
Empty set (0.00 sec)
于 2014-12-16T12:33:12.370 回答
0

尝试这个

SELECT * 
FROM tblTimesheet 
WHERE YEARWEEK(STR_TO_DATE(Date,"%Y-%M-%d"))=YEARWEEK(CURDATE()) AND (status='pending' or status='approved');
于 2014-12-16T12:13:37.363 回答
0

尝试这个,

Select * from 
(
SELECT * from tblTimesheet 
WHERE DATEPART(wk,spentDate) = 32 
AND (timesheetstatus = 'Pending' or timesheetstatus = 'Approved')
)tbl 
where
 (timesheetstatus <> 'Saved' or timesheetstatus <> 'Denied'
于 2014-12-16T12:49:27.747 回答