优化这段代码SELECT t2.GoodsID ,t2.ShopID ,t2.sales_value/SUM(t2.sales_value) ,@total_sales := @total_sales + t2.sales_value ,@total_sales/SUM(t2.sales_value) ,case when @total_sales/SUM(t2.sales_value)<=0.6 then 'A' when @total_sales/SUM(t2.sales_value)>0.8 then 'C' ELSE 'B' END from ( select t1.GoodsID ,t1.ShopID ,sum(t1.SaleValue) as sales_value from demo.OrderItem as t1 where t1.SDate between 20160101 and 20160131 -- 选择一个月 and t1.ShopID = 'WDGC' -- 选择一家门店 group by t1.GoodsID,t1.ShopID order by sum(t1.SaleValue) desc)t2 join(select @total_sales := 0) as x group by t2.GoodsID ,t2.ShopID;
时间: 2023-04-04 18:02:05 浏览: 146
这段代码是用来优化一个 SQL 查询语句的,它的作用是计算某个门店在一个月内每个商品的销售占比,并且根据销售占比的大小将其分为 A、B、C 三类。具体实现方式是通过对订单数据进行聚合计算,然后使用 MySQL 的变量来记录总销售额,最后根据总销售额和每个商品的销售额计算出销售占比并分类。
相关问题
优化以下代码:select a.goodsid, b.goodsname, b.goodsunit, b.goodstype, k.factoryname, h.placepointid, h.placepointname, --b.accflag, sum(a.goodsqty)sl, sum(g.unitprice*a.goodsqty) cb from bms_st_qty_lst a, pub_goods b, gpcs_placepoint h, bms_batch_def g, pub_factory k where a.goodsid=b.goodsid and a.storageid=h.storageid and b.factoryid=k.factoryid and a.batchid=g.batchid and b.accflag=1 and h.retailcenterid=34 and not exists(select 1 from gresa_sa_dtl , gresa_sa_doc where gresa_sa_dtl.rsaid=gresa_sa_doc.rsaid and a.goodsid=gresa_sa_dtl.goodsid and h.placepointid=gresa_sa_doc.placepointid and to_char(gresa_sa_doc.credate,'yyyy-mm-dd')>=${条件.前推日期}) and (h.placepointid in (${条件.门店ID}) or decode('${条件.门店ID}','to_number(null)',null,'-') is null) and (a.goodsid in (${条件.货品ID}) or decode('${条件.货品ID}','to_number(null)',null,'-') is null) group by a.goodsid, b.goodsname, b.goodsunit, b.goodstype, k.factoryname, h.placepointid, h.placepointname --b.accflag order by h.placepointid,a.goodsid
请注意,由于我不知道你的数据结构和表关系,因此我无法完全优化你的代码。但是,以下是一些潜在的优化建议:
1.使用 ANSI SQL join 语法代替旧的 where 子句连接。这将使代码更清晰,易于维护。
2.尝试使用索引来优化查询性能。根据你的表结构和使用情况,可能需要对某些列添加索引。
3.考虑将一些 where 子句移动到 join 条件中。这可能会改善查询性能。
4.使用 with 子句创建临时表,以便在查询中重复使用某些数据。这可以减少查询时间并提高性能。
下面是重写后的代码,但请注意,这只是一个示例,你需要根据你的实际情况进行调整和修改。
```
WITH sales AS (
SELECT DISTINCT
rsaid,
goodsid,
placepointid
FROM gresa_sa_dtl
JOIN gresa_sa_doc ON gresa_sa_dtl.rsaid = gresa_sa_doc.rsaid
WHERE to_char(gresa_sa_doc.credate, 'yyyy-mm-dd') >= ${条件.前推日期}
),
filtered AS (
SELECT
a.goodsid,
b.goodsname,
b.goodsunit,
b.goodstype,
k.factoryname,
h.placepointid,
h.placepointname,
SUM(a.goodsqty) AS sl,
SUM(g.unitprice * a.goodsqty) AS cb
FROM bms_st_qty_lst a
JOIN pub_goods b ON a.goodsid = b.goodsid
JOIN gpcs_placepoint h ON a.storageid = h.storageid
JOIN bms_batch_def g ON a.batchid = g.batchid
JOIN pub_factory k ON b.factoryid = k.factoryid
LEFT JOIN sales s ON a.goodsid = s.goodsid AND h.placepointid = s.placepointid
WHERE b.accflag = 1
AND h.retailcenterid = 34
AND (h.placepointid IN (${条件.门店ID}) OR DECODE('${条件.门店ID}', 'to_number(null)', NULL, '-') IS NULL)
AND (a.goodsid IN (${条件.货品ID}) OR DECODE('${条件.货品ID}', 'to_number(null)', NULL, '-') IS NULL)
AND s.rsaid IS NULL
GROUP BY
a.goodsid,
b.goodsname,
b.goodsunit,
b.goodstype,
k.factoryname,
h.placepointid,
h.placepointname
ORDER BY
h.placepointid,
a.goodsid
)
SELECT *
FROM filtered;
```
解释代码:<script> import navBar from '@/components/NavBar' import swiper from '@/components/Swiper' import { getHome } from '../service/home' import { getUserInfo } from '../service/user' import { getLocal } from '@/common/js/utils' import { Toast } from 'vant' import axios from '../utils/axios' export default { name: 'home', data() { return { swiperList: [], isLogin: false, headerScroll: false, hots: [], newGoodses: [], recommends: [], categoryList: [ { name: '楼楼交友', imgUrl: '//s.yezgea02.com/1598679532890/jiaoyou.png', categoryId: 100001 }, { name: '楼楼家政', imgUrl: '//s.yezgea02.com/1598679532890/jiazheng.png', categoryId: 100003 }, { name: '楼楼水产', imgUrl: '//s.yezgea02.com/1598679734813/shuichan1.png', categoryId: 100002 }, { name: '楼楼租车', imgUrl: '//s.yezgea02.com/1598679532890/zuche.png', categoryId: 100004 }, { name: '楼楼招聘', imgUrl: '//s.yezgea02.com/1598679792590/pin.png', categoryId: 100005 }, { name: '楼楼二手', imgUrl: '//s.yezgea02.com/1598679853415/ershou.png', categoryId: 100006 }, { name: '楼楼宠物', imgUrl: '//s.yezgea02.com/1598679532890/chongwu.png', categoryId: 100007 }, { name: '楼楼外卖', imgUrl: '//s.yezgea02.com/1598679905787/wm.png', categoryId: 100008 }, { name: '楼楼电器', imgUrl: '//s.yezgea02.com/1598680199443/dianqi.png', categoryId: 100009 }, { name: '楼楼充值', imgUrl: '//s.yezgea02.com/1598680264015/czcz.png', categoryId: 1000010 } ], } }, components: { navBar, swiper }, async mounted() { const token = getLocal('token') if (token) { this.isLogin = true } window.addEventListener('scroll', this.pageScroll) Toast.loading({ message: '加载中...', forbidClick: true }); const { data } = await getHome() this.swiperList = data.carousels this.newGoodses = data.newGoodses this.hots = data.hotGoodses this.recommends = data.recommendGoodses Toast.clear() }, methods: { pageScroll() { let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop scrollTop > 100 ? this.headerScroll = true : this.headerScroll = false }, goToDetail(item) { this.$router.push({ path: `product/${item.goodsId}` }) } } } </script>
这是一个 Vue.js 组件的代码,其中包含了导入组件、导入服务、导入工具函数、导入第三方库等等。组件的名称是 home,其中包含了一些数据和方法,如轮播图列表、是否登录、是否滚动等等。在 mounted 生命周期中,会调用 getHome() 函数获取首页数据,并将获取到的数据赋值给组件的数据。在 methods 中,还包含了一个 goToDetail() 方法,用于跳转到商品详情页。
阅读全文