吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 506|回复: 4
收起左侧

[经验求助] sql server 重复率

[复制链接]
huang0817 发表于 2024-4-23 20:18
25吾爱币
本帖最后由 huang0817 于 2024-4-23 20:20 编辑

大佬们,求助个问题
假设现在有个数据库表:orderform
有2列字段,分别是订单号:dd   商品编号:sp
然后现在要用sql server 查订单中商品对比其他订单商品的重复率

例如订单D01有3样商品,跟D22对比重复率100%
反过来订单D22有4样商品,跟D01订单比重复率75%

这样的效果能实现吗

最佳答案

查看完整内容

感觉应该是笛卡积后,然后再判断,思路如下请参考:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

YUYELOVE 发表于 2024-4-23 20:18
感觉应该是笛卡积后,然后再判断,思路如下请参考:

[SQL] 纯文本查看 复制代码
SELECT t1dd, t2dd, SUM(same) / COUNT(*)  
FROM (
    SELECT t1.dd AS t1dd, t1.sp as t1sp, t2.dd AS t2dd, t2.sp as t2sp, 
    CASE WHEN t1.sp = t2.sp THEN 1 ELSE 0 END AS same
    FROM (SELECT * FROM orderform) as t1, orderform as t2
    WHERE t1.dd != t2.dd) tmp
GROUP BY t1dd, t2dd;
Allansoft 发表于 2024-4-23 23:29
如果订单D22中有2个商品,且全是订单D01中有的,这种情况重复率应该是多少
cshadow 发表于 2024-4-24 03:18
这样?

[SQL] 纯文本查看 复制代码
SELECT COUNT(t1.sp) AS 重复数, (SELECT COUNT(sp) FROM orderform WHERE dd = 'D01' ) AS t1合计,
COUNT(t1.sp)*1.0 / (SELECT COUNT(sp) FROM orderform WHERE dd = 'D01' ) * 100
FROM 
(
SELECT sp 
FROM orderform
WHERE dd = 'D01') AS t1 JOIN
(
SELECT sp 
FROM orderform
WHERE dd = 'D22'
)  AS t2 ON t1.sp = t2.sp;

SELECT COUNT(t2.sp) AS 重复数, (SELECT COUNT(sp) FROM orderform WHERE dd = 'D22' ) AS t2合计,
COUNT(t2.sp)*1.0 / (SELECT COUNT(sp) FROM orderform WHERE dd = 'D22' ) * 100
FROM 
(
SELECT sp 
FROM orderform
WHERE dd = 'D01') AS t1 JOIN
(
SELECT sp 
FROM orderform
WHERE dd = 'D22'
)  AS t2 ON t1.sp = t2.sp
cshadow 发表于 2024-4-24 03:22
本帖最后由 cshadow 于 2024-4-24 03:23 编辑
cshadow 发表于 2024-4-24 03:18
这样?

[SQL] 纯文本查看 复制代码
[/quote]SELECT COUNT(t1.sp) AS 重复数, (SELECT COUNT(sp) FROM orderform WHERE dd = 'D01' ) AS D01合计,
COUNT(t1.sp)*1.0 / (SELECT COUNT(sp) FROM orderform WHERE dd = 'D01' ) * 100 占D01重复率 ,
(SELECT COUNT(sp) FROM orderform WHERE dd = 'D22' ) AS D22合计,
COUNT(t2.sp)*1.0 / (SELECT COUNT(sp) FROM orderform WHERE dd = 'D22' ) * 100 占D22重复率
FROM 
(
SELECT sp 
FROM orderform
WHERE dd = 'D01') AS t1 JOIN
(
SELECT sp 
FROM orderform
WHERE dd = 'D22'
)  AS t2 ON t1.sp = t2.sp;
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-13 18:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表