線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2160
推到 Plurk!
推到 Facebook!

請教一個營業交易統計的SQL語法問題

答題得分者是:yubad2000
wwwbbs
初階會員


發表:41
回覆:59
積分:25
註冊:2003-05-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-12-19 02:55:30 IP:220.229.xxx.xxx 訂閱
請問各位前輩在SQL Server 2000有個Tabel其中的欄位是建檔日期,DataType為DateTime。
請問要如何透過這個欄位統計出某月份或某季或某期間的星期一至星期六的交易筆數及某時段的交易筆數?
報表結果:
星期一:68筆
星期二:162筆
...
...
...
星期六:24筆
08:00~09:59:XX筆
10:00~12:0:XX筆
...
...
...
22:00~24:XX筆

christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-12-19 11:35:03 IP:203.73.xxx.xxx 未訂閱
給你一個方向
SELECT
convert(char(13),X_02,20) TIM , SUM(X_01) SUM
FROM MSTAB
GROUP BY convert(char(13),X_02,20)
------
What do we live for if not to make life less difficult for each other?
yubad2000
中階會員


發表:0
回覆:44
積分:78
註冊:2007-09-30

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-12-20 16:31:13 IP:66.171.xxx.xxx 未訂閱
Try this:
Select Datepart(weekday, date_created) as wday, (Datepart(hour, date_created) /2) as hours_2, count(*) as num_of_trans
from transaction_table
where date_created between @start_date and @end_date
group by wday, hours_2

The problem you might have with this SQL is that you have to find some way to display the wday and hours_2 fields
because Datepart() function returns integer.

===================引 用 wwwbbs 文 章===================
請問各位前輩在SQL Server 2000有個Tabel其中的欄位是建檔日期,DataType為DateTime。
請問要如何透過這個欄位統計出某月份或某季或某期間的星期一至星期六的交易筆數及某時段的交易筆數?
報表結果:
星期一:68筆
星期二:162筆
...
...
...
星期六:24筆
08:00~09:59:XX筆
10:00~12:0:XX筆
...
...
...
22:00~24:XX筆

------
===波士頓夜未眠===
What a wonderful world!!
Jazz up the world with jazz!!
==================

When I am not programming...
you can find me here:
http://www.holy-war.de/EN/World3/bin/?advertiser=63190
wwwbbs
初階會員


發表:41
回覆:59
積分:25
註冊:2003-05-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-12-25 18:17:16 IP:220.229.xxx.xxx 訂閱
謝謝二位大大,你們提供的方法都不行,目前的思路是覺得只能用分批查詢才能解決,不知有無更Smart的方法?
yubad2000
中階會員


發表:0
回覆:44
積分:78
註冊:2007-09-30

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-12-26 02:36:22 IP:66.171.xxx.xxx 未訂閱
Then, maybe I misunderstood your output format.
If you need to count the total record of each weekday and the total record of each 2-hour period separately, and just want to use one query, then you need "Union"
Try again with this:

Select Datepart(weekday, date_created) as period, count(*) as num_of_trans
from transaction_table
where date_created between @start_date and @end_date
group period
union
Select (Datepart(hour, date_created) /2) as period, count(*) as num_of_trans
from transaction_table
where date_created between @start_date and @end_date
group by period

Hope this is what you want.

===================引 用 wwwbbs 文 章===================
謝謝二位大大,你們提供的方法都不行,目前的思路是覺得只能用分批查詢才能解決,不知有無更Smart的方法?
------
===波士頓夜未眠===
What a wonderful world!!
Jazz up the world with jazz!!
==================

When I am not programming...
you can find me here:
http://www.holy-war.de/EN/World3/bin/?advertiser=63190
pillar62
資深會員


發表:9
回覆:324
積分:271
註冊:2002-04-15

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-04-08 11:31:47 IP:219.87.xxx.xxx 訂閱
您好
您的需求可以使用下列語法處理。
SELECT { fn DAYOFWEEK(DT_CREATE) } AS dayweek
FROM TABLE
WHERE SUBSTRING(CONVERT(CHAR(10), DT_CREATE, 112), 1, 6) = '200801'
其中 { fn DAYOFWEEK(DT_CREATE) } 是用來取星期幾
CONVERT(CHAR(10), DT_CREATE, 112) 是用來將日期型態轉換成字串型態
試試看吧!
------
Pillar Wang
pillar62
資深會員


發表:9
回覆:324
積分:271
註冊:2002-04-15

發送簡訊給我
#7 引用回覆 回覆 發表時間:2008-05-30 14:39:55 IP:220.229.xxx.xxx 訂閱
您好
請問大大是否有測試過可行嗎?
------
Pillar Wang
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#8 引用回覆 回覆 發表時間:2008-05-31 11:44:48 IP:61.67.xxx.xxx 未訂閱
基本上樓上各位前輩都提供了很多SQL中使用各類FUNCTION的技巧, 不過因為我不是用M$ SQL, 這些FUNCTION根本不管用, 所以我都會用很基本的寫法(假設今天我們是用Delphi來寫程式, 而不是直接在SQL ANSLY...下操作)
SELECT * FROM TABLE1 WHERE DATE1>= :DATE1 AND DATE2<= DATE2
然後由程式提供區間的DATE1, DATE2 兩個值送入 SQL 內不就可以判斷?
編輯記錄
P.D. 重新編輯於 2008-05-31 11:45:38, 註解 無‧
dap570
一般會員


發表:0
回覆:2
積分:0
註冊:2008-04-18

發送簡訊給我
#9 引用回覆 回覆 發表時間:2008-06-13 09:43:36 IP:218.64.xxx.xxx 訂閱
用DELPHI把参数 送进存储过程的做...
------
After three days without programming, life becomes meaningless.
系統時間:2024-04-30 7:04:49
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!