當前位置:首頁 » 編程語言 » sql將表裡重復的類別放在一起
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql將表裡重復的類別放在一起

發布時間: 2023-02-04 22:46:20

① 將sql里相同ID的數據放在一起用什麼語句

create function pj(@id int)
returns varchar(200)
as
begin
declare @tfbm varchar(200)
select @tfbm isnull(@tfbm,'') + ',' from table
where id = @id
return left(@tfbm,len(@tfbm) - 1)
end
go

select k.id,dbo.pj(k.id) from (
select id from table
group by id) k

試試看行不行,具體思路就是:
1、創建函數,將一個ID的所有tfbm值拼接在一起,並返回
2、將數據按照ID分組,使每個ID都唯一,然後查詢,在查詢時,調用函數,將其當做列,傳ID即可

② SQL怎麼將2張表查詢出來的相同欄位合並顯示在一個欄位中.

條件:A表有a.1,a.2,a.3,c四個欄位;B表有b.1,b.2,b.3,c四個欄位。
要求:SQL將2張表查詢出來的相同欄位合並顯示在一個欄位中.
答案:select a.* from A as a,b.* from B as b where a.c=b.c

③ SQL 兩張表合並 (兩張表的列都相同)

可以參考下面的方法:

1、第一種,用一張臨時表,把所有數據都放在一張表裡

INSERT INTO QunList SELECT * FROM QunList89,90,91,92;

再在QunList中查詢

2、第二種,用 UNION ALL方式查詢

SELECT * FROM QunList89

UNION ALL

SELECT * FROM QunList90;

(3)sql將表裡重復的類別放在一起擴展閱讀:

SQL參考語句

刪除表

drop table tabname--這是將表連同表中信息一起刪除但是日誌文件中會有記錄

刪除信息

delete from table_name-這是將表中信息刪除但是會保留這個表

增加列

Alter table table_name add column_name column_type [default 默認值]--在表中增加一列,[]內的內容為可選項

④ 如何利用SQL語句將多個工作表中具有相同屬性的數據匯總起來

將多個表中的數據匯總最常用的辦法是使用union all,具體的要視表結構、「相同屬性」的具體定義、匯總方式等因素而定,可能還要用到連接、篩選、子查詢、分組等手段,當然聚合函數sum是少不了的。

下面例子實現將t1,t2,t3中的各個產品匯總起來,顯示每種產品的總量:

select t.proct ,sum(t.qty) from
(select proct,qty from t1
union all
select proct,qty from t2
union all
select proct,qty from t3) t
group by t.proct;

⑤ 我想將sql server數據表裡相同ID的值都放在一起,要怎麼寫代碼

可以這樣做:
select
*
into
tableclass0
from
table
where
班級=0
select
*
into
tableclass1
from
table
where
班級=1
select
*
into
tableclass2
from
table
where
班級=2
select
*
into
tableclass3
from
table
where
班級=3
...
select
*
into
tableclass9
from
table
where
班級=9
在把原先的表刪除,就實現了1張表分成10張的,只要條件對就可以了,tableclass0-9是新表的名稱,按你的表結構,可以不用導入id

⑥ SQL連接查詢兩表後需要知道其中一個欄位重新的重復次數並將重復的行放在一起

你的state是放在一個欄位,還是要放在很多欄位?如果是放在一個欄位,如果你的資料庫是oracle的話,那麼就比較容易了。
select A.name,A.id,C.state from A,
(select id,wmsys.wm_concat(state) state from B group by id) C where a.id=C.id
如果不是oracle,那麼我就不清楚了,sqlserver可能要用到stuff函數來做。

⑦ SQL查詢,如何把具有相同類別號的幾個數據合並起來呢

select max(欄位1) as 欄位1,max(欄位2) as 欄位2,類別 from table group by 類別
這樣不行嗎? 親

⑧ SQL 如何將一個表中的兩條或多條擁有相同ID的記錄合並為一條

一、創建表:

  • create table stuUnion

    (

    sid int identity primary key,

    cid int,

    id varchar(500)

    )

二、添加數據:

  • insert into stuUnion

    elect 1,'a' union

    select 1,'b' union

    select 2,'c' union

    select 2,'d' union

    select 3,'e' union

    select 3,'f' union

    select 3,'g'

三、用標量函數查詢:

  1. 創建標量函數:

    create function b(@cid int)

    returns varchar(500)

    as

    begin

    declare @s varchar(500)

    select @s=isnull(@s+'','')+rtrim(id)+',' from stuUnion where cid=@cid

    return @s

    end;

  2. 用標量函數查詢:

    select cid,dbo.b(cid) as id from stuUnion group by cid

  3. 用sqlserver的xml:

    select cid,ID=STUFF((select ' '+rtrim(id)+',' from stuUnion where st.cid=cid order by id for XML path('')),1,1,'') from stuUnion st group by cid

⑨ sql如何在多個表中的數據分類匯總到一個表裡

create proc Mypro
as
begin
declare @t table(個人編號 varchar(10),工資 money,養老保險 money,年金 money,住房公積金 money)
insert into @t(個人編號,工資,養老保險,年金) select * from 表一
insert into @t select * from 表二
insert into @t select * from 表三
delete from @t where
exists(select * from @t where exists(select 個人編號,工資,養老保險,年金 from @t group by 個人編號,工資,養老保險,年金 having Count(*)>1) and 住房公積金 is null)
declare @t1 table(個人編號 varchar(10),工資 money,養老保險 money,年金 money,住房公積金 money)
insert into @t1 select distinct * from @t
select 個人編號,
sum(工資) as工資總和,avg(工資) as工資平均值,
sum(養老保險) as 養老保險總和,avg(養老保險) as 養老保險平均值,
sum(年金) as 年金總和,avg(年金) as 年金平均值,
sum(住房公積金) as 住房公積金總和,avg(住房公積金) as 住房公積金平均值
from @t1 group by 個人編號
end

--存儲過程直接查詢出你想要的數據,因為不知道你要總匯總和還是總匯平均值,你自己改下
--然後執行 insert into 表四 exec Mypro 插入你的表四吧