1樓:匿名使用者
create table test_b (name char(10),a char(10),b int,c nvarchar(200))
insert test_b select '張三','語文',98,'超長髮揮'
insert test_b select '張三','數學',78,'一般'
insert test_b select '張三','英語',88,'發揮失常'
create function functionname_testb (@name char(10))
returns nvarchar(2000)
as begin
declare @s nvarchar(2000)
set @s=''
select @s=@s+','+c from test_b where name=@name
return (stuff(@s,1,1,'')) end
select name, sum(b) ,dbo.functionname_testb(name) from test_b group by name
2樓:
首先t2中科目就有問題,如果t2中沒有科目這一列的話,sql語句應該是這個樣子的:
select t.姓名,sum(成績) as 總成績,(select 評語 from t1 as t,t1 as t2 where t.姓名 = t2.
姓名 and t.科目 = 語文)||、||(select 評語 from t1 as t,t1 as t2 where t.姓名 = t2.
姓名 and t.科目 = 數學)||、||(select 評語 from t1 as t,t1 as t2 where t.姓名 = t2.
姓名 and t.科目 = 英語)as 總評語 from t1 as t ,t1 as t2 where t.姓名 =t2.
姓名;給你排下版吧!看起來有點繁瑣:
select t.姓名,sum(成績) as 總成績,
(select 評語 from t1 as t,t1 as t2 where t.姓名 = t2.姓名 and t.科目 = 語文)||、||
(select 評語 from t1 as t,t1 as t2 where t.姓名 = t2.姓名 and t.科目 = 數學)||、||
(select 評語 from t1 as t,t1 as t2 where t.姓名 = t2.姓名 and t.科目 = 英語)as 總評語
from t1 as t ,t1 as t2
where t.姓名 =t2.姓名;
希望有幫到你,正確望採納!!!!
3樓:雲天英雄
檢查一下select 語句,可以指定t1表,
4樓:匿名使用者
你這個關聯就有問題,科目那一列
sql 分類彙總查詢語句
5樓:匿名使用者
醉含笑的很牛,不過sum(pay)有點需要改動最終完美版:
select min(id) as 序號,max(dept) as 部門,
sum(case when zt='01' or zt='02' then pay else 0 end) as 合計,
sum(case zt when 01 then 1 else 0 end) as 個數01狀態,
sum(case zt when 02 then 1 else 0 end) as 個數02狀態,
count(zt) as 總數
from aac
group by dept
6樓:醉含笑
select min(id) as 序號,
max(dept) as 部門,
sum(pay) as 合計,
sum(case zt when '01' then 1 else 0 end) as 個數01狀態,
sum(case zt when '02' then 1 else 0 end) as 個數02狀態,
count(zt) as 總數
from 表名
group by dept
這段**是sqlserver和oracel通用,其中“表名”的地方,換成你的表名
喔看掉了這個條件:我現在想統計01、02兩種狀態的數量和pay合計
還是 zjwssg提醒,但最後兩個sum中when後面,建議還是加單引號吧
把上面的**改為這樣應該沒問題了
select min(id) as 序號,
max(dept) as 部門,
sum(case when zt='01' or zt='02' then pay else 0 end) as 合計,
sum(case zt when '01' then 1 else 0 end) as 個數01狀態,
sum(case zt when '02' then 1 else 0 end) as 個數02狀態,
count(zt) as 總數
from 表名
group by dept
7樓:我tm不管
select dept as 部門,sum(pay) as 合計,sum(case zt when '01'then 1 else 0 end) as 個數(01狀態),
sum(case zt when '02'then 1 else 0 end) as 個數(02狀態),count(*) as 總數
from 表 group by dept
以上,希望對你有所幫助
8樓:
select
row_number() over(order by a.dept) 序號,
a.dept 部門,
a.合計,
b.個數01,
c.個數02,
d.總數
from
(select dept,sum(pay) 合計 from t where zt='01' or zt='02' group by dept) a,
(select dept,count(pay) 個數01 from t where zt='01' group by dept) b,
(select dept,count(pay) 個數02 from t where zt='02' group by dept) c,
(select dept,count(pay) 總數 from t group by dept) d
where a.dept=b.dept and b.dept=c.dept and c.dept=d.dept
參照樓上的寫法,改進一下有:
select
row_number() over(order by dept) 序號,
dept as 部門,
sum(case when zt='01' or zt='02' then pay else 0 end) 合計,
sum(case when zt='01' then 1 else 0 end) as 個數01狀態,
sum(case when zt='02' then 1 else 0 end) as 個數02狀態,
count(*) as 總數
from t
group by dept
9樓:匿名使用者
如果你用的是sql server可以:
select 序號=identity(int,1,1),dept as 部門,sun(pay) as 合計,sum(case when zt='01' then 1 else 0 end) as 個數01狀態,sum(case when zt='02' then 1 else 0 end) as 個數02狀態,count(*) as 總數 into #tmp_total from yourtablename group by dept
select * from #tmp_total 就得到你要的效果了你要說是在什麼資料庫下,資料庫不同寫法也是有一定差別的
10樓:世界大同喵
create table tb (id int,dept varchar(10),pay int,zt int)
insert tb select 1,'辦公室',20,1
union all select 2,'局領導',10,2
union all select 3,'辦公室',40,3
union all select 4,'局領導',10,1
union all select 5,'辦公室',50,1
union all select 6,'局領導',10,2
union all select 7,'辦公室',20,2
union all select 8,'局領導',10,2
select identity(int,1,1) as 序號,
dept as 部門,
sum(case when zt='01' or zt='02' then pay else 0 end) 合計,
sum(case when zt='1' then 1 else 0 end) 個數01狀態,
sum(case when zt='2' then 1 else 0 end) 個數02狀態,
count(*) as 總數 into #temp from tb group by dept
select * from #temp
11樓:匿名使用者
select a.dept,a.pay,c.[01],c.[02],b.ztnum
from
(select dept,sum(pay) as pay from table_1 where zt in(01,02) group by dept
)aleft join
(select dept,count(zt) as ztnum from table_1 group by dept
) bon a.dept=b.dept
left join
(select *
from
(select dept,zt,count(zt) as ztnum from table_1 where zt in(01,02) group by dept,zt)a
pivot
(sum(a.ztnum)
for a.zt in ([01],[02])
) as tpivot
) con b.dept=c.dept
sql按分類級別彙總,sql 按分類級別彙總
按分類級別的話,用group by進行分組,如果彙總的話,用sum求和,用count計數。sql select count 1 sum sorce from tablename group by class no 解釋 以上是假設等級欄位為class no,之後求出每個等級的總條數和對應的 sorc...
Excel中分類彙總中如何進行多項彙總且顯示彙總名稱
飄逸幽谷 建議用透視表,處理資料理強大 excel中如何實現多項分類彙總 選中需要分類匯來總的列 選擇源 選單的 bai資料 單擊升序排du序。2分類彙總 zhi 選擇選單dao的 資料 單擊 分類彙總 3在分類彙總對話方塊選擇相應的項 分類欄位選擇要分類彙總的列名,這裡選擇 班級 選定彙總項勾選 ...
在Excel中怎樣進行分類彙總
雪v歌 分類彙總就是按類別對資料進行分類彙總 求和 均值 極值等 以下圖為例,計算每個人的銷售額合計。操作方法 選定資料區域,資料 分類彙總,分類欄位 選擇 姓名 彙總方式 選擇 求和 選定彙總項 勾選 銷售額 再單擊 確定 按鈕,如下圖 結果如下圖,每個人都自動插入了彙總行,並且有總計行。知識擴充...