當前位置:首頁 » 編程語言 » sql多表聯查求和分組
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql多表聯查求和分組

發布時間: 2022-12-06 01:26:32

1. sql 多表查詢求和

建表a(插入數據過程略)

create table a

(mingzi varchar2(50),

id int);

2. 求個 SQL 多表、分組匯總合計的 語句

select a.專業級別,a.專業名稱,b.計劃人數,sum(case when 學生編號='X' then 1 else 0 end) 報名人數,sum(case when 學生編號<>'X' then 1 else 0 end) 審核人數,
sum(case when 住址='長沙' then 1 else 0 end) 長沙,
sum(case when 住址='望城' then 1 else 0 end) 望城,
sum(case when 住址='寧鄉' then 1 else 0 end) 寧鄉,
sum(case when 住址='瀏陽' then 1 else 0 end) 瀏陽,
sum(case when 性別='男' then 1 else 0 end) 男,
sum(case when 性別='女' then 1 else 0 end) 女,
from
TB_Student a,TB_Profession b
where a.專業級別=b.專業級別 and a.專業名稱=b.專業名稱
group by a.專業級別,a.專業名稱,b.計劃人數

3. sql多表查詢分組問題

據我所知,group by子句應該與聚合函數如:sum()搭配使用才有效果。
結果集中出現的欄位如果不參與聚合運算,就應當寫入group by 子句。比如:select a1,a2,a3,sum(a4) from t group by a1,a2,a3.
如果就是不想用聚合函數,如:select a1,a2,a3,a4 from t group by a1,a2,a3,a4。這樣也是可以的,效果等價於:
select distinct a1,a2,a3,a4 from t

4. Sql語句,分組統計多表聯查

selecta.lqr,
(selectsum(sl)fromt2wherea.lqr=t2.lqr)kc,
(selectsum(sl)fromt1wherea.lqr=t1.lqr)lq
from
(
selectlqr
fromt1
union
selectlqr
fromt2
)a

5. sql 多表關聯 求和語句 怎麼寫

你結果顯示有問題吧,最後id=2怎麼來的?

創建表:

createtabletable1
(idint,
類別varchar(10),
貨號varchar(3))

insertintotable1values(1,'電子','011')
insertintotable1values(2,'零件','022')
insertintotable1values(3,'主板','033')

createtabletable2
(idint,
貨號varchar(3),
數量int)

insertintotable2values(1,'011',5)
insertintotable2values(2,'022',6)
insertintotable2values(3,'033',-8)
insertintotable2values(4,'011',22)
insertintotable2values(5,'022',65)
insertintotable2values(6,'033',81)

查詢:

selecta.id,a.類別,a.貨號,SUM(b.數量)
fromtable1a,table2bwherea.貨號=b.貨號anda.貨號='011'
groupbya.id,a.類別,a.貨號

結果:

6. SQL多表關聯查詢再拼接和分組的問題

第二個問題的意思是:列出每個學校的交費人數,和總費用select 所屬學校,count(),sum(收費金額)from T1,T2 where T1.IDCard=T2.IDCard group by 所屬學校問題已解決,我是把之前的所有子查詢語句當做條件,這樣就是代碼量多了點。。。where (select count(*) from tb1 as t1 whree t1.orgid=org.id) >0 or (select count(*) from tb2 as t2 whree t2.orgid=org.id) >0 or 。。。這樣就查詢出了我想要的數據怎麼限制b,像這樣嗎要這個結果,對巴?GROUP BY A, B 不行么?理論是絕對可以的哦。如果這個不型,可以考慮這個數據這設計的願因是 一個人對應多條應有考勤, 一條實際應考勤就是代表一個周期(如圖2 B.jfjs 應是這個周期應有的考勤 中存的3就是 這個周期應該有相應的三個應際考勤記錄) 如果 關聯 人的話 要怎麼才能 分別查詢滿足條件的周期下的應際考勤數你的意思是 一條應有考勤對應一個周期,一條實際考勤代表一個周期中的一個點?例如一個實際考勤是一周,就要對應5條實際考勤?這樣的話也是一樣都對到人上去,然後應有考勤是什麼周期(幾號-幾號)應該對應幾次實際考勤,實際考勤就直接記錄打考勤的時間。這樣在統計的時候取出人、應有考勤數、考勤周期里實際考勤數應該就滿足需求了吧。 應有考勤和實際考勤同人關聯,實際考勤通過時間周期和應有考勤關聯然後用count()取出應有考勤數。b.考勤周期 = 輸入參數(或者不要這個條件就是所有的有考勤時間的數據),如果需要人員信息再去關聯a表就好。就是實際考勤需要取考勤的周期,這個周期可以放在實際考勤表裡面那就不用時間關聯直接周期關聯就行。用人員的id和應有考勤關聯起來就好。

7. sql 多表聯接查詢(超過有五張表,但是又分多種情況,子查詢語句有十幾條)統計然後分組

select (select orgname from org as og where org.id=og.id),
(select count(*) from tb1 as t1 whree t1.orgid=org.id) as number1,
(select count(*) from tb2 as t2 whree t2.orgid=org.id) as number2,
(select count(*) from tb3 as t3 whree t3.orgid=org.id) as number3,
(select count(*) from tb4 as t4 whree t4.orgid=org.id) as number4,
(select count(*) from tb5 as t5 whree t5.orgid=org.id) as number5 ,
。。。。。。。。。。。。。。。。。。。。。。。。number18
into #table from org as org,tb as tb where org.id=tborg.id
group by org.name having count(*)>0
go
select * from #table where number1<>0 and number2<>0 and number3<>0 and number4<>0 and number5 <>0
go

文字太多看到頭暈,也不知道你要的結果是不是這個意思。希望能幫到你吧!

8. SQL多表分組查詢

selectt1.*,t2.English
from
(selecta.id,a.name,a.address,MAX(b.Chinese)Chinesefromstudenta,Scoreb
wherea.id=b.idgroupbya.id,a.name,a.address)t1,
scoret2
wheret1.id=t2.idandt1.Chinese=t2.Chinese

9. 怎麼用sql語句來實現分組求和

第二列第一行是不是寫錯了? 應該是5 ?

select 名稱,min(金額) 類型1,max(金額) 類型2,sum(金額) 金額合計 from 表名
group by 名稱 order by 1