久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何查詢以獲取過去 7 天的總數(shù)?

How to query to get totals for last seven days?(如何查詢以獲取過去 7 天的總數(shù)?)
本文介紹了如何查詢以獲取過去 7 天的總數(shù)?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我使用的是 SQL Server 2008.

I am using SQL Server 2008.

我想編寫一個(gè)查詢,提供給定天數(shù)的總活動(dòng)量.具體來說,我想統(tǒng)計(jì)過去 7 天每天的總票數(shù).

I want to write a query that gives me total activity for a number of given days. Specifically, I want to count total votes per day for the last seven days.

我的桌子是這樣的:

 VoteID --- VoteDate --------------  Vote --- BikeID

 1          2012-01-01 08:24:25      1        1234 
 2          2012-01-01 08:24:25      0        5678
 3          2012-01-02 08:24:25      1        1289
 4          2012-01-03 08:24:25      0        1234
 5          2012-01-04 08:24:25      1        5645
 6          2012-01-05 08:24:25      0        1213
 7          2012-01-06 08:24:25      1        1234
 8          2012-01-07 08:24:25      0        1125

我需要我的結(jié)果看起來像這樣

I need my results to look like this

VoteDate ---- Total
2012-01-01    5
2012-01-02    6
2012-01-03    7
2012-01-04    1
2012-01-05    3

我的想法是我必須做這樣的事情:

My thought is that I have to do something like this:

SELECT    SUM(CASE WHEN Vote = 1 THEN 1 ELSE 0 END) AS Total
FROM      Votes
GROUP BY  VoteDate

此查詢不起作用,因?yàn)樗鼉H計(jì)算(幾乎完全相同)同時(shí)發(fā)生的投票.當(dāng)然,我只想看特定的一天.我該如何實(shí)現(xiàn)?

This query doesn't work because it counts only votes that occurred (almost exactly) at the same time. Of course, I want to look only at a specific day. How do I make this happen?

推薦答案

Cast it as a date:

Cast it as a date:

SELECT    
     cast(VoteDate as date) as VoteDate, 
     SUM(CASE WHEN Vote = 1 THEN 1 ELSE 0 END) AS Total
FROM      Votes
WHERE VoteDate between dateadd(day, -7, GETDATE()) and GETDATE()
GROUP BY  cast(VoteDate as date)

您的 VoteDate 列是一個(gè) datetime,但您只需要其中的 date 部分.最簡單的方法是將其轉(zhuǎn)換為 date 類型.您可以在此處閱讀有關(guān) SQL Server 日期類型的更多信息.

Your VoteDate column is a datetime, but you just want the date part of it. The easiest way to do that is to cast it as a date type. You can read more about SQL Server date types here.

如果你的 Vote 列是 1 或 0,你可以只做 sum(vote) as Total 而不是做 case聲明.

And if your Vote column is either 1 or 0, you can just do sum(vote) as Total instead of doing the case statement.

這篇關(guān)于如何查詢以獲取過去 7 天的總數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产精品一区二区三级 | 高清一区二区三区 | 日本不卡免费新一二三区 | 久久国内精品 | 国家aaa的一级看片 h片在线看 | 国产成人免费视频网站视频社区 | 欧美精品在欧美一区二区少妇 | 欧美精品一区二区三区在线播放 | 免费在线观看成年人视频 | 日本黄色免费视频 | 欧美性久久 | 国产区在线观看 | 中文字幕91 | 91在线精品视频 | 国产精品成人一区二区三区夜夜夜 | 中文字幕不卡在线88 | 激情六月丁香婷婷 | 国产精品免费一区二区三区四区 | 少妇久久久久 | 一区二区三区精品视频 | 91精品国产一区二区三区 | 亚洲精品乱码久久久久v最新版 | 日韩人体在线 | 久久国产激情视频 | 91精品中文字幕一区二区三区 | 97av在线 | 婷婷免费视频 | 亚洲精品黄 | 免费看黄视频网站 | 精品国产精品 | 日韩欧美专区 | 日日夜夜精品 | 午夜性视频 | 日韩三片| 欧美视频在线播放 | 成人午夜免费视频 | 久久人人爽人人爽人人片av免费 | 日日干夜夜操 | 日韩伦理一区二区 | 日韩国产中文字幕 | 久久久久免费观看 |