|
我有以下SQL(SQL Server 2005),可用于计算每一列的总时间:- A( A! j) J5 B9 R0 i! C9 P- L
DECLARE @Param1 DATETIME
8 _* r- D/ V# Q+ K S7 dDECLARE @Param2 DATETIME- f) p% K, m+ W
DECLARE @Param3 DATETIME5 u( Z D" e) j& |2 w, m5 b- }
SET @Param1 = '2009-01-01'
$ F: k; g$ F2 p0 z9 G/ _- R: s. zSET @Param2 = '2009-09-09'
3 z+ g* k, q. {; J) l2 m6 F! N2 j: @SELECT SUM(IdleSec) AS TotalIdleSec,
5 O H( `: p2 L6 _+ J) h SUM(ProductionSec) AS TotalProductionSec, : s8 g( u* L9 \ s$ N* b/ G
SUM(UplineSec) AS TotalUplineSec, 5 ?" p# U/ V2 Y1 k4 r h
SUM(DownlineSec) AS TotalDownlineSec, 0 _- `) ]5 j( A% E; {! Q2 d
SUM(UserSec) AS TotalUserSec! d+ {+ p+ b/ {- X$ I S" G
FROM Job+ t2 u7 D' J- h1 o" n/ ~% G
WHERE (DateTime >= dbo.FormatDateTime(@Param1, 'yyyy-mm-dd')) + G) q) n; c' B1 w) e6 ]0 `. M" S
AND " o( B( G9 q1 T- e) l! S
(DateTime 这将为上面的项目返回一个包含 1行 的表格,并且效果很好。
q7 B" k% M8 W/ T3 y2 B" ?0 l3 s我有点不确定如何返回每天/每周/每月的SUM值表,即
5 o) V1 l4 x/ i3 c
0 B$ u0 r, D/ O9 S日期范围之间每天的所有值的总和。 e$ l0 f+ O: w% P% i* ?; F
9 c% E6 z1 m S- G& p日期范围内每个WEEK的所有值的总和。
, f" i6 r/ W# L* s4 x9 B
( F# h( m! n4 A% q; L& {3 D日期范围之间每个MONTH的所有值的总和。8 }1 D( U, u- c' N6 I
; p2 G \! f8 Y- y
& Z) H% u5 F: ]( s! f我敢肯定有一个简单的方法可以做到这一点,但我不确定。我看过一些使用DAY(date)命令的教程,但我尝试过,似乎无法获得所需的东西。# M3 I, r+ w$ P
我期待着您的出色帮助。
& O* s( Q7 Q, R. N& M- J
v2 q7 }. Y" }# w解决方案: E! W- u6 K; F2 `
/ w# r4 s/ S3 }: h1 F h
- G5 `* R+ R/ @0 K$ t
' v( D3 A$ Y% c4 F9 N8 f9 n% R 您将GROUP BY子句添加到查询中:2 g' A1 I3 ~3 [6 ]+ f
按MONTH:
/ O' ^/ E+ V3 r: H" W! {SELECT SUM(IdleSec) AS TotalIdleSec, 1 `8 B. w, N- f7 V
SUM(ProductionSec) AS TotalProductionSec, 7 R8 ?/ u* u4 T2 \6 a
SUM(UplineSec) AS TotalUplineSec, * _8 e7 d% U' U+ L* b
SUM(DownlineSec) AS TotalDownlineSec,
) `( F0 y$ e9 d0 s SUM(UserSec) AS TotalUserSec,% I) L; v! V- c3 u7 N
DATEPART(year, DateTime) as Year,
6 Q7 V' g/ Z, A1 C0 ^ DATEPART(month, DateTime) as Month
, B4 H, a# c6 gFROM Job
5 B; Z3 x+ d: q! K7 L. DWHERE (DateTime >= dbo.FormatDateTime(@Param1, 'yyyy-mm-dd')) + H) R- n/ c/ C% @# X; B
AND ( V; c1 t6 T6 T$ @5 m& H7 E
(DateTime 通过周:4 ~& f& i# y& v' N" U
SELECT SUM(IdleSec) AS TotalIdleSec,
3 n3 v5 D% X5 |" i1 f! }; z/ z3 K4 C SUM(ProductionSec) AS TotalProductionSec, - _0 u+ Q% T& k- S
SUM(UplineSec) AS TotalUplineSec,
6 d2 n, R0 Z3 E8 U7 [: `3 D SUM(DownlineSec) AS TotalDownlineSec, ; t" c% ~# f# F' Y1 L! v
SUM(UserSec) AS TotalUserSec,
4 j' B( F( j* L7 f" g) D DATEPART(year, DateTime) as Year,( g" {3 m' N" y! |
DATEPART(week, DateTime) as Week
; b X# g+ R- _. v8 G$ OFROM Job
7 \/ H5 O. P" ]+ w3 zWHERE (DateTime >= dbo.FormatDateTime(@Param1, 'yyyy-mm-dd'))
) _8 W3 K" m/ k) l AND , o6 \& X7 G' M0 I) L" H( @
(DateTime 到DAY:
9 ^$ X1 W, @/ b' p2 M SELECT SUM(IdleSec) AS TotalIdleSec,
4 m) V5 M5 }9 c# l* Z SUM(ProductionSec) AS TotalProductionSec, - M3 m/ X3 K6 b# |2 i$ D
SUM(UplineSec) AS TotalUplineSec,
! F: C( n- t2 Q+ F* y1 Z5 T SUM(DownlineSec) AS TotalDownlineSec, / b' I7 A: k* n$ A& K1 Y8 U
SUM(UserSec) AS TotalUserSec,$ ]& a9 z- g: t
DATEPART(year, DateTime) as Year,# a8 t | Z6 Q. z
DATEPART(dayofyar, DateTime) as Day
3 h8 G1 b4 a( W9 NFROM Job
, [/ Y4 y% I: K. J7 JWHERE (DateTime >= dbo.FormatDateTime(@Param1, 'yyyy-mm-dd')) 6 |0 T; O% T4 \ u" A, r
AND
7 K- g$ `5 G% c: b& g/ `6 l, E% J4 H (DateTime 对于DAY,还有其他选项,例如使用CAST提取日期部分或您似乎拥有的FormatDateTime函数。我使用DATEPART来保持一致性。 |
|