电竞比分网-中国电竞赛事及体育赛事平台

分享

sql語句:月份匯總查詢語句

 krrish 2010-08-25

sql月份匯總查詢語句

select '鉛筆' as [月份],
sum(case month(日期) when 1 then 鉛筆 else 0 end) as [1月],
sum(case month(日期) when 2 then 鉛筆 else 0 end) as [2月],
sum(case month(日期) when 3 then 鉛筆 else 0 end) as [3月],
sum(case month(日期) when 4 then 鉛筆 else 0 end) as [4月],
sum(case month(日期) when 5 then 鉛筆 else 0 end) as [5月],
sum(鉛筆) as 總價
from table

union all

select '鋼筆' as [月份],
sum(case month(日期) when 1 then 鋼筆 else 0 end) as [1月],
sum(case month(日期) when 2 then 鋼筆 else 0 end) as [2月],
sum(case month(日期) when 3 then 鋼筆 else 0 end) as [3月],
sum(case month(日期) when 4 then 鋼筆 else 0 end) as [4月],
sum(case month(日期) when 5 then 鋼筆 else 0 end) as [5月],
sum(鋼筆) as 總價
from table


結果

 鉛筆 1月 2月 3月 4月 5月 總價
  鉛筆 200 500 600 700 500 2500
  鋼筆 200 300 250 400 300 1450
  毛筆 300 400 500 300 600 2100
  總價 700 1200 1350 1400 1400 6050

本文來源網頁制作教程網www. 原文鏈接:http://www./php100/php-1024.html

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多