我正在使用SQL Server 2012,并且有一个声明可以返回当前周的这些记录:
Employee , Sun , Mon , Tues, Wed, Thurs, Fri, Sat
--------------------------------------------------
Smith 5 8 5 4 8 4 0
Burt 7 1 8 5 4 4 7
这是我的SQL,本周从Sun开始。如何更改,以便显示前一周?
SELECT
ReproofMidLineBy AS Employee,
COUNT( CASE WHEN DATENAME( dw, ReproofMidLineDate )= 'Sunday' THEN 1 END ) AS Sun,
COUNT( CASE WHEN DATENAME( dw, ReproofMidLineDate )= 'Monday' THEN 1 END ) AS Mon,
COUNT( CASE WHEN DATENAME( dw, ReproofMidLineDate )= 'Tuesday' THEN 1 END ) AS Tues,
COUNT( CASE WHEN DATENAME( dw, ReproofMidLineDate )= 'Wednesday' THEN 1 END ) AS Wed,
COUNT( CASE WHEN DATENAME( dw, ReproofMidLineDate )= 'Thursday' THEN 1 END ) AS Thurs,
COUNT( CASE WHEN DATENAME( dw, ReproofMidLineDate )= 'Friday' THEN 1 END ) AS Fri,
COUNT( CASE WHEN DATENAME( dw, ReproofMidLineDate )= 'Saturday' THEN 1 END ) AS Sat,
COUNT(*) AS TOTAL
FROM
dbo.tblJobLog
WHERE
(ReproofMidLineDate BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) / 7 * 7, 0)
AND DATEADD(DAY, DATEDIFF(DAY, 6, GETDATE()- 1) / 7 * 7 + 7, 6)
)
GROUP BY
ReproofMidLineBy
答案 0 :(得分:1)
用以下内容替换你的where子句:
WHERE
(
ReproofMidLineDate BETWEEN DATEADD(WEEK, -1, DATEADD(
DAY,
DATEDIFF(
DAY,
0,
GETDATE()
)/ 7 * 7,
0))
AND DATEADD(WEEK, -1, DATEADD(
DAY,
DATEDIFF(
DAY,
6,
GETDATE()- 1
)/ 7 * 7 + 7,
6
))
)
答案 1 :(得分:0)
一种非常简单的方法是为比较添加一周:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
var data = [
['idea', 'a very good one'],
['beer', 'not when driving'],
['guitar', 'yes please']
];
function download_csv() {
var csv = 'Name,Title\n';
data.forEach(function(row) {
csv += row.join(',');
csv += "\n";
});
console.log(csv);
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'test.csv';
document.getElementById('container').appendChild(hiddenElement);
hiddenElement.click();
}
</script>
<button onclick="download_csv()">Download CSV</button>
<div id="container" style="display:none;"></div>
</body>
</html>
您还可以从WHERE DATEADD(week, 1, ReproofMidLineDate) BETWEEN . . .
的范围中减去一周(或7天)。