T-sql:如何根据类别动态创建价格范围?
这是针对电子商务网站的。目前,当客户点击某个类别时,应用程序会查找该类别内产品的最高价格和最低价格,将差值除以 5(仅需要 5 个价格范围),然后根据定义的价格范围对产品进行分组。
问题是,当 90% 的产品在 50 美元左右,但很少有产品在 3000 美元左右时,那么大多数产品将被分组在第一个范围中,而后续范围只显示很少的商品。这将违背设定价格范围的目的。
理想的情况是,应用程序根据产品群体显示价格范围,例如:
- 第一个价格范围中的前 10% 产品
- 第二个价格范围中的后续 25
- % 第三个价格范围中的
- 后续 25% 第四个价格范围中
- 的最后 15%价格范围
,但每个价格范围的最小值和最大值因类别而异。
这可以在 1 个存储过程中完成吗 (SQL Express 2008)?有什么想法吗?
提前致谢。
This is for ecommerce site. Currently, when customer click on a category, application will find the maximum price and minimum price from products within this category, divide the difference by 5(only 5 price ranges needed), then group the products according to defined price ranges.
The problem is when 90% of products are around $50, but few products are around $3000, then most products will be grouped in the first range, and subsequent ranges only shows few items. This will defeat the purpose of having price ranges.
Ideal situation is, application display price ranges according to products population, like:
- First 10% of products in 1st price range
- Subsequent 25% in 2nd price range
- Subsequent 25% in 3nd price range
- Subsequent 25% in 4nd price range
- Last 15% in 5th price range
but min and max value of each price range is vary by category.
Can this be done in 1 store procedure (SQL Express 2008)? Any ideas?
Thanks in advanced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将最高百分比与之前找到的范围混合将产生五个价格范围。
SQL语句
测试数据
Mixing TOP PERCENT and previously found ranges would yield the five price ranges.
SQL Statement
Test data
一种解决方案是动态生成价格范围。您可以这样做:
假设您有 100 种产品。
等等。
One solution is to generate price ranges dynamically. This is how you can do it:
Lets say you have 100 products.
And so on.
您可以使用ntile窗口功能根据需要将产品分组到价格范围内。一种方法是根据价格将产品分成20类(每类5%)。然后桶 1-2 将是第一个 10%,3-7 接下来是 25%,依此类推。
请参阅下面基于 AVDW 数据库产品表的 SQL 示例
You can use the ntile windowing function to group the product into the price ranges as required. One approach is to break the products into 20 buckets based on price(each bucket with 5%). Then then buckets 1-2 will be first 10%,3-7 next 25% and so on.
See below a sample SQL based on AVDW database products table