2015-05-13 12 views
5

Tôi muốn nhóm dữ liệu dựa trên khoảng thời gian, chúng ta hãy nói nhóm 3 giờ. Làm thế nào tôi có thể nhóm dữ liệu trong một khung thời gian của dữ liệu.Máy chủ SQL: Cách nhóm theo cột ngày giờ dựa trên khoảng thời gian (Chẳng hạn như trong vòng 2 giờ)

dữ liệu của tôi là như

DocId, UserCode,  ProcessCode, ProcessDone 
1   1    10  21/11/2015 11:04:00 
2   1    10  21/11/2015 12:14:00 
3   1    20  21/11/2015 11:04:00 
4   1    20  21/11/2015 11:54:00 
5   1    30  21/11/2015 13:04:00 

Ví dụ trong dữ liệu trên, chúng tôi muốn nhóm các dữ liệu dựa trên UserCode quá trình sử dụng trong khuôn khổ một thời gian chúng ta hãy nói 10-12.

như

UserCode, Process, Total 
    1  10  1 
    1  20  2 

Như đang tổng số này được thực hiện dựa trên Thời gian giữa 10-12 và nhóm của UserCodeProcessCode.

Trả lời

1

Hãy thử cách này:

select UserCode, ProcessCode, count(1) Total 
from tab 
where convert(time,ProcessDone) between '10:00' and '12:00' 
group by UserCode, ProcessCode 

Sql Fiddle Demo

hoặc

select UserCode, ProcessCode, count(1) Total 
from tab 
where DATEPART(hh,ProcessDone) > 10 and DATEPART(hh,ProcessDone) < 12 
group by UserCode, ProcessCode 

Sql Fiddle Demo

hoặc bao gồm date trong group by

select UserCode, ProcessCode, count(1) Total 
from tab 
where convert(time,ProcessDone) between '10:00' and '12:00' 
group by UserCode, ProcessCode, convert(date,ProcessDone) 

Sql Fiddle Demo

+0

Cảm ơn @Parado nhưng có thể bao gồm các hồ sơ có ngày trước ngày tôi yêu cầu vì nó chỉ kiểm tra thời gian không phải ngày. chẳng hạn như 20/11/2015 11:00 – Mohit

+0

@Mohit Hãy thử giải pháp thứ ba của tôi. – Parado

0

Bạn nên nhóm bởi phần ngày phân tích cú pháp

select 
    DATEPART(YEAR,ProcessDone), 
    DATEPART(MONTH,ProcessDone), 
    DATEPART(DAY,ProcessDone), 
    DATEPART(HH,ProcessDone), 
    UserCode, 
    ProcessCode, 
    count(1) Total 
from tab 
group by UserCode, 
    ProcessCode, 
    DATEPART(YEAR,ProcessDone), 
    DATEPART(MONTH,ProcessDone), 
    DATEPART(DAY,ProcessDone), 
    DATEPART(HH,ProcessDone) 

Và sau đó kết hợp các phần cập nhật cho visualization

0

tôi sử dụng cách này :

SELECT 
    UserCode, ProcessCode, COUNT(*) As [Total] 
FROM 
    yourTable 
GROUP BY 
    UserCode, ProcessCode, 
    DATEPART(HOUR, ProcessDone) - (DATEPART(HOUR, ProcessDone) + 0) % 2 

Đó thực sự bạn có thể có hai biến như:

.... 
    DATEPART(HOUR, ProcessDone) - (DATEPART(HOUR, ProcessDone) + @start) % @Step 
0

Something như thế này nên làm việc:

select 
    UserCode, 
    ProcessCode, 
    count(*) Total 
from tab 
group by UserCode, 
    ProcessCode, 
    DATEPART(YEAR,ProcessDone), 
    DATEPART(MONTH,ProcessDone), 
    DATEPART(DAY,ProcessDone), 
    DATEPART(HH,ProcessDone)/3 
; 
0

Tôi nghĩ rằng tất cả các bạn đang tìm kiếm là một đơn giản mệnh đề where

SELECT UserCode, 
     ProcessCode AS Process, 
     COUNT(*) AS Total 
FROM @yourTable 
WHERE DATEPART(HOUR,ProcessDone) BETWEEN 10 AND 12 
GROUP BY UserCode,ProcessCode 
Các vấn đề liên quan