sql語(yǔ)句例一:求某一字段在某一時(shí)間段內(nèi)數(shù)量的總和
表名 銷(xiāo)售 字段有:編號(hào) 金額 統(tǒng)計(jì)這個(gè)表在某一時(shí)間段內(nèi)金額的總和
select sum(金額) from 表 where 條件
sql語(yǔ)句例二:sql讀出記錄并統(tǒng)計(jì)出某一字段的總和
select * ,sum(字段) from 表名 where 條件 group by 字段
sql語(yǔ)句例三:sql語(yǔ)句按每天、每月、每年統(tǒng)計(jì)出銷(xiāo)售總額
表名: 訂單 字段有ordertime(訂單時(shí)間),money(金額)
根據(jù)訂單時(shí)間統(tǒng)計(jì)每天、每月、每年的銷(xiāo)售總額
1、每年
select year(ordertime) 年,
sum(money) 銷(xiāo)售合計(jì)
from 訂單
group by year(ordertime)
2、每月
select year(ordertime) 年,
month(ordertime) 月,
sum(money) 銷(xiāo)售合計(jì)
from 訂單
group by year(ordertime),
month(ordertime)
3、每日
select year(ordertime) 年,
month(ordertime) 月,
day(ordertime) 日,
sum(total) 銷(xiāo)售合計(jì)
from 訂單
group by year(ordertime),
month(ordertime),
day(ordertime)
sql語(yǔ)句例四:統(tǒng)計(jì)某月銷(xiāo)售量前10
表名: 銷(xiāo)售
字段: 編號(hào), 商品名稱(chēng) 價(jià)格 日期
統(tǒng)計(jì)某月商品銷(xiāo)量最大的商品前10個(gè)商品及銷(xiāo)量。
select distinct top 10 商品名稱(chēng), sum(價(jià)格) as 產(chǎn)品總價(jià), count(*) as 銷(xiāo)量 from 銷(xiāo)售 group by title
加上月份:
select distinct top 10 商品名稱(chēng), sum(價(jià)格) as 產(chǎn)品總價(jià), count(*) as 銷(xiāo)量 from 銷(xiāo)售 where (month(sdate) = '你要查的月份數(shù)字') group by 商品名稱(chēng)
統(tǒng)計(jì)某月商品銷(xiāo)量最大的商品前10個(gè)商品及銷(xiāo)量并按銷(xiāo)量排序
select distinct top 10 商品名稱(chēng), sum(價(jià)格) as 產(chǎn)品總價(jià), count(*) as 銷(xiāo)量 from 銷(xiāo)售 where (month(日期) = '你要查的月份數(shù)字') group by 商品名稱(chēng) order by 銷(xiāo)量 desc
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄