sql中的分段列表

2021-05-18 01:01:24 字數 2584 閱讀 5991

表:使用者號碼,登入時間

顯示 :每日登入各時間段的登入人數,和每天登入人數

if isnull(object_id('#tb'),'')=''

drop table #tb

create table #tb(列名1 varchar(12),時間 datetime)

insert into #tb

select '03174190188','2009-11-01 07:17:39.217' union all

select '015224486575','2009-11-01 08:01:17.153' union all

select '013593006926','2009-11-12 08:04:46.560' union all

select '013593006926','2009-11-12 07:05:46.560' union all

select '013599584239','2009-11-22 08:53:27.763' union all

select '013911693526','2009-11-23 08:53:51.683' union all

select '013846472440','2009-11-23 08:54:57.233' union all

select '013990353697','2009-11-24 08:55:25.077' union all

select '013990353697','2009-11-25 08:56:01.327' union all

select '013945594843','2009-11-26 08:57:02.233' union all

select '013990353697','2009-11-27 08:57:29.700' union all

select '013916597421','2009-11-28 08:59:49.390' union all

select '03916995857','2009-11-29 09:11:05.607' union all

select '015097712001','2009-11-30 09:13:50.293'

--sql查詢如下:

declare @mindate datetime,@maxdate datetime;

select @mindate = '2009-11-1',@maxdate = '2009-12-01';

select convert(char(10),dateadd(d,number,@mindate),120),

sum(case when convert(char(8),時間,108) between '00:00' and '01:00' then 1 else 0 end) as '00:00~01:00',

sum(case when convert(char(8),時間,108) between '01:00' and '02:00' then 1 else 0 end) as '01:00~02:00',

sum(case when convert(char(8),時間,108) between '02:00' and '03:00' then 1 else 0 end) as '02:00~03:00',

sum(case when convert(char(8),時間,108) between '03:00' and '04:00' then 1 else 0 end) as '03:00~04:00',

sum(case when convert(char(8),時間,108) between '04:00' and '05:00' then 1 else 0 end) as '04:00~05:00',

sum(case when convert(char(8),時間,108) between '05:00' and '06:00' then 1 else 0 end) as '05:00~06:00',

sum(case when convert(char(8),時間,108) between '06:00' and '07:00' then 1 else 0 end) as '06:00~07:00',

sum(case when convert(char(8),時間,108) between '07:00' and '08:00' then 1 else 0 end) as '07:00~08:00',

sum(case when convert(char(8),時間,108) between '08:00' and '09:00' then 1 else 0 end) as '08:00~09:00',count(a.列名1) as 'sum'

from #tb a right join

master..spt_values b on datediff(d,時間,dateadd(d,number,@mindate)) = 0

where dateadd(d,number,@mindate) <= @maxdate and b.type = 'p' and b.number >= 0

group by convert(char(10),dateadd(d,number,@mindate),120)

order by 1

結果圖:

Vertica系列 表的分段和分割槽

vertica 有兩個資料分布的概念,segmentation 和 partition,至少有下面幾個區別 1.目的方面 segmentation 解決各節點資料傾斜問題,適用於木桶原理,資料量大的那個節點將會拖慢整個查詢.partition 主要解決的是資料刪除和空間 問題.列式資料庫刪除資料的代...

sql實現分段功能的函式

簡單分段語句 declare b varchar 20 select b a b c d e 處理 declare sql varchar 4000 select sql select replace b,union all select exec sql 實現 split 分段功能的函式 crea...

LaTex分行分段分頁與專案列表

1.分行。強制分行可以使用 或者 或者 newline 或者 linebreak 相反,mbox 可以使文字內容保持在同一行 2.分段。par 或者連續兩個回車 就是在兩段之間加乙個空行 3.分頁。newpage 4.專案列表 itemize 帶相同標籤的專案列表 enumerate 專案標籤自動編...