【写SQL语句】按照用户统计对应订单数和订单总金额

【写SQL语句】按照用户统计对应订单数和订单总金额,第1张

SELECTUIDas用户,COUNT(ORDER_SN)as订单总数,SUM(TOTAL)as合计总金额FROM订单表groupbyuid。

selectname,totalamountfrom(

selecta。name,sum(xxfje)astotalamountfrombookasa

innerjoinxfjlasxonaid=xkhid

groupbyaname

orderbytotalamount;

常见语句

更新:update table1 set field1=value1 where 范围

查找:select from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)

排序:select from table1 order by field1,field2 [desc]

求和:select sum(field1) as sumvalue from table1

百度百科-语句

本月的直接加,本日的用case when来加,脚本实例如下,以oracle为例:

select  sum(金额)  本月的金额总数,

        sum(case when 日期字段>=trunc(sysdate) then 金额 else 0 end) 今日的金额总数

        from 表名

        where 日期字段>=trunc(sysdate,'mm')

在sqlserver中case when 语法与上面一致:

select  sum(金额)  本月的金额总数,

        sum(case when 日期字段>=cast(convert(varchar,getdate(),112) as smalldatetime) then 金额 else 0 end) 今日的金额总数

        from 表名

        where 日期字段>=cast(convert(varchar(6),getdate(),112)+'01' as smalldatetime)

以上就是关于【写SQL语句】按照用户统计对应订单数和订单总金额全部的内容,包括:【写SQL语句】按照用户统计对应订单数和订单总金额、能否用一句SQL同时查询本日和本月金额总值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10152149.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存