當前位置:首頁 » 編程語言 » sql如何取不同的值
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql如何取不同的值

發布時間: 2022-09-03 11:39:50

sql如何獲取一個表某三個列不一樣的數據

SELECTMJBH,CPBH,JOBNOFROMTABGROUPBYMJBH,CPBH,JOBNO;
或者
SELECTDISTINCTMJBH,CPBH,JOBNOFROMT;

㈡ 一條sql語句如何選取出多個不同的運算結果值,內詳。

select (select count(*) from table_name where 年齡 between 18 and 30),(select count(*) from table_name where 年齡 between 3000 and 5000) from al;

㈢ SQL按條件取值,根據不同的條件來取不同的結果

select case when DATEPART(hour,采樣時間)<9 then 10
when DATEPART(hour,采樣時間)>=9 and DATEPART(hour,采樣時間)<10 then 11
else 11:30 end as time from table

㈣ sql查詢某欄位多少種不同值,怎麼寫

select class from table group by class輸出的結果為class123這個結果為class里有哪些不同的數字 select count(class) from table group by class輸出結果為class3這個結果為class里有多少種不同的數字(共3種)

㈤ (資料庫查詢)你好,我想請問一下SQL,怎樣從一張表取2種不同條件的列值,讓他們並列在同一張表。

使用 join 連接 :

select
k.class,
sum( decode(k.abc,'100003',1,0)),
sum( decode(k.efg,'100004',1,0)),
sum( decode(k.hij,'100005',1,0)),
sum( decode(k.klm,'100006',1,0)),
cc.dept_code,
cc.fenshu份數,
cc.zongshu 項目總數
from
DETAIL k
where k.ggg='100001' and score='0' group by k.class:

join
( select
c.ggg,
c.dept_code,
count(distinct c.aaa) fenshu,
count(c.bbb) zongshu
from
DETAIL c
where c.ggg='100001'group by c.class) cc

on c.ggg =k.ggg

㈥ SQL中怎樣取多個值

select 後面跟的是查詢內容(欄位名),查詢一個欄位或多個欄位,如果只從一個表中查詢,比如說a、b、c、d、e同屬字母這個欄位那就,select zimu from table where....,如果屬於不同的欄位,那就select 欄位1、欄位2、... from table1 where ......

㈦ SQL語句 只取不同的數據

select後加上distinct,另再把需要值(不為空或空串)的條件寫入where子句

㈧ sql語句要select某欄位不重復的數據應該如何寫

sql語句要select某欄位不重復的數據使用distinct關鍵字,例如從 Company" 列中僅選取唯一不同的值使用以下SQL:

SELECT DISTINCT Company FROM Order;

題主的問題B、C欄位數據都相同,可以使用select distinct A,B from table_name 來實現。

(8)sql如何取不同的值擴展閱讀

在表中,可能會包含重復值,有時希望僅僅列出不同(distinct)的值,可以使用關鍵詞 DISTINCT 用於返回唯一不同的值。

語法:

SELECT DISTINCT 列名稱 FROM 表名稱

用法注意:

1、distinct【查詢欄位】,必須放在要查詢欄位的開頭,即放在第一個參數;

2、只能在SELECT 語句中使用,不能在 INSERT, DELETE, UPDATE 中使用;

3、DISTINCT 表示對後面的所有參數的拼接取 不重復的記錄,即查出的參數拼接每行記錄都是唯一的;

4、不能與all同時使用,默認情況下,查詢時返回的就是所有的結果。

㈨ sql 怎麼取不重復的數據的所有數據

SQL數據重復分幾種情況,一種是原數據重復,第二種是粒度重復,第三種是分布重復。
原數據重復的情況,你直接可以distinct掉,例如,學生表當中有兩個重復的學號,你想取出不重復的,直接可以寫:select
distinct
學號
from
學生表
第二種是查詢粒度重復,比如你有一張表是存儲區域的,分別為省、市、縣三列。而你需要的是只查找不同的省市,則也可以使用distinct:select
distinct
省,市
from
區域
第三種則是分布重復,比如在join
的時候,左右兩個表格存在一對多的關系,造成的重復,或者在聚合之後出現了維度重復,則這種相對來說比較麻煩,你需要在子查詢中統計或查找出唯一值,然後再去關聯,或者是按照一定的數據需求的取數規則,在查詢結果後再進行聚合,取到唯一值。
不過不管怎麼樣,都是要看實際需求是什麼樣子的。大多可以用子查詢和關聯聯合解決。

㈩ SQL 如何取出每一行欄位的值不同的行

先列轉行在查詢
select* from tablea_3
NO A B C D E F G
a 1 0 0 0 1 0 0
c 0 0 0 0 0 0 0
b 3 2 0 0 1 3 2

declare @s varchar(8000)------------------------------------------先列轉行
set @s = 'create table tablea_32 (no varchar(20)'
select @s = @s + ',' + no + ' varchar(10)' from tablea_3
set @s = @s + ')'
exec(@s)
print @s
--藉助中間表實現行列轉換
declare @name varchar(20)

declare t_cursor cursor for
select name from syscolumns
where id=object_id('tablea_3') and colid > 1 order by colid

open t_cursor

fetch next from t_cursor into @name

while @@fetch_status = 0
begin
exec('select ' + @name + ' as t into tablea_33 from tablea_3')
set @s='insert into tablea_32 select ''' + @name + ''''
select @s = @s + ',''' + rtrim(t) + '''' from tablea_33
exec(@s)
exec('drop table tablea_33')
fetch next from t_cursor into @name
end
close t_cursor
deallocate t_cursor

--查看行列互換處理結果

select * from tablea_32
declare @a int
declare @b int
declare @c char(10)

select @a= min( ascii(no)) from tablea_32

select @b= max( ascii(no)) from tablea_32

while (@a<=@b)
begin

set @c=char(@a)

insert into #A
select distinct a,no
from tablea_32 where no=@c and a<>0

insert into #B
select distinct B,no
from tablea_32 where no=@c and B<>0

insert into #C
select distinct C,no
from tablea_32 where no=@c and C<>0
set @a=@a+1
end

if exists ( select count(distinct A) from #a having count(distinct a)>1)
PRINT 'A'

if exists ( select count(distinct B) from #B having count(distinct B )>1)
print 'B'

if exists ( select count(distinct C) from #C having count(distinct C )>1)
print 'C'