久久久久久久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)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 欧美精品一区二区在线观看 | 久久精品欧美一区二区三区不卡 | 天天搞天天操 | 国产成人精品午夜 | 日韩在线免费播放 | 亚洲一区在线播放 | 欧美一级观看 | 国产精品久久一区 | 国产高清视频一区二区 | 影音av | 日韩三级在线观看 | 99久久久国产精品 | 日韩一级黄色片 | 亚洲成人动漫在线观看 | 午夜爱爱毛片xxxx视频免费看 | 国产精品久久久久久久久久久久久 | 国产精品久久久久久久免费大片 | 欧美激情在线播放 | 午夜国产一级 | 欧美日韩国产一区二区三区不卡 | 国产欧美精品在线 | 日韩精品免费在线观看 | 久久亚洲一区二区三区四区 | 国产99免费视频 | 香蕉视频一区二区 | 在线精品亚洲欧美日韩国产 | 免费视频一区二区 | 日韩欧美成人一区二区三区 | 久久精品国产亚洲一区二区 | 欧美在线一区二区三区 | 欧美日韩一区二区三区不卡视频 | 亚洲人a| 日韩av免费在线电影 | 色男人天堂av | 欧美成人手机在线 | 午夜精品久久久久久久久久久久 | 色www精品视频在线观看 | 欧美成人视屏 | 国产精品高潮呻吟 | 一级毛片免费视频 | 91最新入口 |