當前位置:首頁 » 編程語言 » sql看數據欄位分布
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql看數據欄位分布

發布時間: 2023-07-17 15:34:10

A. 怎樣sql統計數據區間分布

select
count(case when time>=1 and time <=2 then muid end),
count(case when time>=2.1 and time <=3 then muid end),
count(case when time>=3.1 and time <=4 then muid end)
from table

B. 查詢一個資料庫表有多少欄位的sql

這個要看你用的什麼資料庫,要用到系統表,不同的資料庫系統表不一樣。比如MS SQL可以這樣寫:
select count(*) from syscolumns where id = object_id('表名')
syscolumns 就是系統表

C. sql 將一個欄位的數據分列顯示

SQL2000不支持開窗函數row_number() ,實現這種效果可以藉助存儲過程。

CREATE PROCEDURE 存儲過程2
AS
set nocount on
/* 創建一個臨時表,利用identity 添加一個從1開始的連續標識列 */
/* x欄位我設置為變長字元串型,請更改為與原始欄位類型相同 */
create table #temp (id int identity,X varchar(50))
/* 將原始表中數據插入臨時表 */
insert into #temp(x) select colName from tableName order by colName
/* 輸出希望得到列表效果 */
/* 思路:id除4求模數根據結果將X值分別放到欄位a,b,c */
/* 用(case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end)將每4條記錄標識為一組 */
/* 最後通過求分組最大值得辦法得到最終列表 */
select
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 1 then x else null end) as a,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 2 then x else null end) as b,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 3 then x else null end) as c,
max(case (case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end) when 0 then x else null end) as d
from #temp
group by (case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end)
set nocount off

上面代碼源自下面存儲過程,相對容易理解,但是由於過程里使用多一次操作查詢(update)其效率也許會慢一些(但是本人未證實)

CREATE PROCEDURE 存儲過程1
AS
set nocount on
create table #temp (id int identity,X nvarchar(50),idd int, flag smallint)
insert into #temp(x) select colName from tableName order by colName
update #temp set flag=case id%4 when 1 then 1 when 2 then 2 when 3 then 3 else 0 end,
idd=case id%4 when 1 then id + 3 when 2 then id +2 when 3 then id +1 else id end
select
max(case flag when 1 then x else null end) as a,
max(case flag when 2 then x else null end) as b,
max(case flag when 3 then x else null end) as c,
max(case flag when 0 then x else null end) as d
from #temp group by idd
set nocount off

D. sql server 怎麼查詢數據欄位說明

SQL Server的注釋屬於擴展屬性,可以從sys.extended_properties視圖中查看
SELECT * FROM sys.extended_properties WHERE major_id=OBJECT_ID('WMSBASCUS')

結果是WMSBASCUS表所有列的注釋信息
minor_id是列的Id,可以從sys.columns中查看錶的列id
通過sys.extended_properties和sys.columns進行關聯可以查看某個具體列的信息

SELECT c.name,p.value

FROM sys.extended_properties p ,sys.columns c

WHERE p.major_id=OBJECT_ID('WMSBASCUS') and c.name='CUSTNO'
and p.major_id=c.object_id and p.minor_id=c.column_id

E. SQL 單表查詢按欄位分類統計如何橫排顯示

嵌入寫:即簡單,又易懂
select name ,
case type when '白班' then '1' else '' end as dayjob ,
case type when '夜班' then '1' else '' end as nightjob
from work
把上面這句做為整體,嵌入到下面。
select a.name ,count(a.dayjob),count(a.nightjob) from
(
select name ,
case type when '白班' then '1' else '' end as dayjob ,
case type when '夜班' then '1' else '' end as nightjob
from work
) as a group by a.name

括弧裡面就是一個子查詢,這樣就可以達到你的要求。