我有一个如下表格视图。
我们如何显示昨天的销售额(在DATATYPE中标记为S),但是需要显示标记为“我”的所有其他DATATYPE?
所需的输出显示在最后一个
上
+----------+-----------+-----------------+
| DATATYPE | SALESDATE | SERIALNO |
+----------+-----------+-----------------+
| I | 20160730 | 355274073159321 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158927 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073166888 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073157333 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158851 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073159784 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158828 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158950 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073157960 |
+----------+-----------+-----------------+
| S | 20160731 | 355274073159230 |
+----------+-----------+-----------------+
| S | 20160730 | 355274073166904 |
+----------+-----------+-----------------+
| S | 20160730 | 355274073190128 |
+----------+-----------+-----------------+
| S | 20160730 | 355274073158679 |
+----------+-----------+-----------------+
| S | 20160730 | 355274073159255 |
+----------+-----------+-----------------+
| S | 20160730 | 355274073159925 |
+----------+-----------+-----------------+
I need the output like this. Only show yesterday sale (say today is 01-Aug-2016) and all item marked as I on DATATYPE column
+----------+-----------+-----------------+
| DATATYPE | SALESDATE | SERIALNO |
+----------+-----------+-----------------+
| I | 20160730 | 355274073159321 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158927 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073166888 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073157333 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158851 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073159784 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158828 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073158950 |
+----------+-----------+-----------------+
| I | 20160730 | 355274073157960 |
+----------+-----------+-----------------+
| S | 20160731 | 355274073159230 |
+----------+-----------+-----------------+
我的完整选择是
SELECT bi.btLatinAbbrev, 'C710' AS SUBS, CASE WHEN bi.btLatinAbbrev = 'Sales' THEN 'S' ELSE 'I' END AS DATATYPE, REPLACE(CONVERT(CHAR(10), bi.buDate, 112), '/', '') AS SALESDATE,
'C000079967' AS FROMSITE, CASE WHEN snc.Qty = 1 THEN '' ELSE 'EU' END AS TOSITE, snc.SN AS SERIALNO
FROM dbo.vwExtended_bi AS bi INNER JOIN
dbo.snt000 AS snt ON snt.biGUID = bi.biGUID INNER JOIN
dbo.snc000 AS snc ON snc.GUID = snt.ParentGUID
WHERE (bi.btLatinAbbrev = 'Sales') OR
(snc.Qty = 1)
答案 0 :(得分:1)
SELECT DATATYPE,CONVERT(VARCHAR(8),SALESDATE,112) SALESDATE,SERIALNO FROM table
WHERE
( DATATYPE = 'S' AND
SALESDATE = convert(date,GETDATE()-1)
) OR
DATATYPE = 'I'
答案 1 :(得分:0)
您可以使用CONVERT尝试此操作:
SELECT * FROM
(
SELECT DATATYPE, SALESDATE, SERIALNO FROM TABLE WHERE
SALESDATE = CONVERT(NVARCHAR,GETDATE()-1,112)
AND DATATYPE = 'S' --sales yesterday
UNION ALL
SELECT DATATYPE, SALESDATE, SERIALNO FROM TABLE WHERE
CONVERT(DATETIME,SALESDATE,112) <= CONVERT(DATETIME,GETDATE()-1,112)
AND DATATYPE = 'I' --I yesterday to back
) AS T
ORDER BY T.DATATYPE DESC, CONVERT(DATETIME,T.SALESDATE,112) DESC, T.SERIALNO DESC
将SERIALNO称为数字
答案 2 :(得分:0)
试试这个
SELECT * FROM Tbl
WHERE
( DATATYPE = 'S' AND
SALESDATE = CONVERT(NVARCHAR(11), GETDATE() - 1, 112) -- 20160731
) OR
DATATYPE = 'I'