Advertisement
TDCustmerSupportJP

Untitled

Aug 6th, 2017
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.38 KB | None | 0 0
  1. SELECT goods_id1, goods_id2, cnt, total_cnt1, total_cnt2,
  2.   1.0*cnt/IF(total_cnt1<total_cnt2,total_cnt1,total_cnt2) AS simpson_coeff,
  3.   1.0*cnt/SQRT(total_cnt1*total_cnt2) AS cos_coeff,
  4.   1.0*cnt/(total_cnt1+total_cnt2) AS dice_coeff
  5. FROM
  6. (
  7.   SELECT t1.goods_id AS goods_id1, t2.goods_id AS goods_id2, COUNT(1) AS cnt
  8.   FROM
  9.   (
  10.     SELECT member_id, goods_id, time
  11.     FROM sales_slip
  12.     WHERE TD_TIME_RANGE(time, '2013-12-17', '2013-12-18', 'JST')
  13.     GROUP BY member_id, goods_id, time -- 同member_id,同timeの重複購入を排除 --
  14.   ) t1,
  15.   (
  16.     SELECT member_id, goods_id, time
  17.     FROM sales_slip
  18.     WHERE TD_TIME_RANGE(time, '2013-12-17', '2013-12-18', 'JST')
  19.     GROUP BY member_id, goods_id, time -- 同member_id,同timeの重複購入を排除 --
  20.   ) t2
  21.   WHERE t1.member_id = t2.member_id
  22.   AND t1.time = t2.time
  23.   AND t1.goods_id < t2.goods_id
  24.   GROUP BY t1.goods_id, t2.goods_id
  25. ) basket
  26. JOIN
  27. (
  28.   SELECT goods_id, COUNT(DISTINCT(member_id, time)) AS total_cnt1
  29.   FROM sales_slip
  30.   WHERE TD_TIME_RANGE(time, '2013-12-17', '2013-12-18', 'JST')
  31.   GROUP BY goods_id
  32. ) item1
  33. ON basket.goods_id1 = item1.goods_id
  34. JOIN
  35. (
  36.   SELECT goods_id, COUNT(DISTINCT(member_id, time)) AS total_cnt2
  37.   FROM sales_slip
  38.   WHERE TD_TIME_RANGE(time, '2013-12-17', '2013-12-18', 'JST')
  39.   GROUP BY goods_id
  40. ) item2
  41. ON basket.goods_id2 = item2.goods_id
  42. ORDER BY cnt DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement