select t0.[人員],sum(t0.m1) as '1月',sum(t0.m2) as '2月',sum(t0.m3) as '3月'
from
(
SELECT [人員]
,year([RECEIVEDATE]) as yy
, case when month([RECEIVEDATE])=1 then [維修費] else 0 end as m1
, case when month([RECEIVEDATE])=2 then [維修費] else 0 end as m2
, case when month([RECEIVEDATE])=3 then [維修費] else 0 end as m3
FROM [MyLab].[dbo].[費用]
) t0
where t0.yy=2022
group by t0.[人員]
感謝
請問
select MECHANIC_NAME 維修人員, DATEPART(MONTH, RECEIVEDATE) 月, TOTALAMOUNT 維修金 from WO
其中DATEPART(MONTH, RECEIVEDATE) 月
如何取出1月,2月,3月,.........
維修費加總?
DATEPART(MONTH, RECEIVEDATE)=1 就是1月
DATEPART(MONTH, RECEIVEDATE)=2 就是2月
...餘類推
select t0.[人員],sum(t0.m1) as '1月小計',sum(t0.m2) as '2月小計',sum(t0.m3) as '3月小計'
from
(
SELECT [人員]
,year([RECEIVEDATE]) as yy
, case when DATEPART(MONTH, RECEIVEDATE)=1 then [維修費] else 0 end as m1
, case when DATEPART(MONTH, RECEIVEDATE)=2 then [維修費] else 0 end as m2
, case when DATEPART(MONTH, RECEIVEDATE)=3 then [維修費] else 0 end as m3
FROM [MyLab].[dbo].[費用]
) t0
where t0.yy=2022
group by t0.[人員]
兄弟,請再多指引方向
一堆紅蚯蚓
請新增程式碼的方式,貼sql語法上來
請再多給點方向!
select t0.MECHANIC_NAME 維修人員, sum(t0.m1) as '1月',sum(t0.m2) as '2月',sum(t0.m3) as '3月'
from
(
SELECT MECHANIC_NAME
,year([RECEIVEDATE]) as yy
, case when month([RECEIVEDATE])=1 then [維修費] else 0 end as m1
, case when month([RECEIVEDATE])=2 then [維修費] else 0 end as m2
, case when month([RECEIVEDATE])=3 then [維修費] else 0 end as m3
FROM [MyLab].[dbo].[費用]
) t0
where t0.yy=2022
group by MECHANIC_NAME
以下是可以執行的
select t0.MECHANIC_NAME 維修人員, sum(t0.m1) as '1月',sum(t0.m2) as '2月',sum(t0.m3) as '3月'
from
(
SELECT MECHANIC_NAME
,year([RECEIVEDATE]) as yy
, case when month([RECEIVEDATE])=1 then [維修費] else 0 end as m1
, case when month([RECEIVEDATE])=2 then [維修費] else 0 end as m2
, case when month([RECEIVEDATE])=3 then [維修費] else 0 end as m3
FROM [MyLab].[dbo].[費用]
) t0
where t0.yy=2022
group by t0.MECHANIC_NAME
不好意思請問我的源碼
select MECHANIC_NAME 維修人員, DATEPART(MONTH, RECEIVEDATE) 月, TOTALAMOUNT 維修費 from WO
1.維修費的欄位要加在哪裡select?
2.FROM [t0].[wo].[費用這欄位要填哪一個]
請把table schema 貼上來
您好請參考table schema
select t0.MECHANIC_NAME 維修人員, sum(t0.m1) as '1月',sum(t0.m2) as '2月',sum(t0.m3) as '3月'
from
(
SELECT MECHANIC_NAME //<--這裡改成你的維修人員欄位名稱
,year([RECEIVEDATE]) as yy
, case when month([RECEIVEDATE])=1 then [維修費] else 0 end as m1
, case when month([RECEIVEDATE])=2 then [維修費] else 0 end as m2
, case when month([RECEIVEDATE])=3 then [維修費] else 0 end as m3
FROM WO //<--這裡改成你的TableName
) t0
where t0.yy=2022
group by t0.MECHANIC_NAME
感恩大大!讚
select t0.MECHANIC_NAME 維修人員, sum(t0.m1) as '1月',sum(t0.m2) as '2月',sum(t0.m3) as '3月'
from
(
SELECT MECHANIC_NAME
,year([RECEIVEDATE]) as yy
, case when month([RECEIVEDATE])=1 then (TOTALAMOUNT) else 0 end as m1
, case when month([RECEIVEDATE])=2 then (TOTALAMOUNT) else 0 end as m2
, case when month([RECEIVEDATE])=3 then (TOTALAMOUNT) else 0 end as m3
FROM wo
) t0
where t0.yy=2022
group by t0.MECHANIC_NAME
ROW TO COLUMN
還需要看你的row data是如何才能決定。
感謝您的回覆sql語法還不熟悉請幫忙指引方向也行麻煩您了!
以下是撈出的資料我該如何才能呈現上面的欄位資料
select MECHANIC_NAME 維修人員, RECEIVEDATE 收件日期, TOTALAMOUNT 維修金 from WO
selsct 人員 ,sum(1月) 至 sum(12月) form 費用
where 年='2022' and 費用='維修費'
group by 人員
匯出成Excel後..加總各月份(偷懶一下...)
建議使用程式及語法,貼出來。或是SQL Fiddle
@bassabnick
啊啊啊.....?
兄弟您這程度與我不相上下啊
有無更像AI人工智慧程式碼的SQL語言想法?
該怎麼寫...
像這種多層階式的表格,我就不會用單一查詢句來產生報表,而是用C# + LINQ + EXCEL輸出來解決,如【外勞出勤記錄表】
(點到為止,我只是想說,不要單用一個SQL就想解決問題,可以的話,活用多個語言或平台,最好)
以下演示用 C# + LINQ to SQL + IIS + 輸出EXCEL(我慣用epplus你也可以找其他好用的)