Database 板


LINE

以下为(Sales, 同一天最多重复时有几种product)的解法: create table #t ( Sales char(10), Product char(1), StartDate char(8), EndDate char(8) ) -- 建立测资 insert into #t select 'Amy', 'A', '20000101', '20000105' insert into #t select 'Amy', 'B', '20000101', '20000103' insert into #t select 'Amy', 'C', '20000104', '20000108' insert into #t select 'Amy', 'D', '20000103', '20000104' insert into #t select 'Amy', 'A', '20000110', '20000111' insert into #t select 'Jack','A', '20000201', '20000202' insert into #t select 'Jack','B', '20000203', '20000204' insert into #t select 'Jack','C', '20000208', '20000209' insert into #t select 'Jack','D', '20000204', '20000207' insert into #t select 'Jack','B', '20000204', '20000204' -- 解法 select Sales,max(cnt) cnt from ( select t2.Sales,count(distinct t1.Product) cnt from #t t1 inner join #t t2 on t1.Sales = t2.Sales and t2.StartDate >= t1.StartDate and t2.StartDate <= t1.EndDate group by t2.Sales,t2.StartDate,t2.EndDate ) t1 group by Sales 说明: 绿色黄色两个条件(交集)可创出 7 种 case 如下: A1------B1--------C1 case 1 A2------B2---C2---D2 case 2 A3---B3---C3 case 3 A4--------B4---C4 case 4 A5--------B5 case 5 A6------B6--------C6----D6 case 6 A7------B7--------C7 case 7 注: 可看出不管哪种 case 它们至少有一天的交集(绿色箭头指向的部份) 所以就可以把这些 group by 起来做不同种 product 的计算 各 case 中 An,Bn,Cn,Dn 所代表的意义: A1 = t1.StartDate B1 = t1.EndDate,t2.StartDate C1 = t2.EndDate A2 = t1.StartDate B2 = t2.StartDate C2 = t1.EndDate D2 = t2.EndDate A3 = t1.StartDate,t2.StartDate B3 = t1.EndDate C3 = t2.EndDate A4 = t1.StartDate,t2.StartDate B4 = t2.EndDate C4 = t1.EndDate A5 = t1.StartDate,t2,StartDate B5 = t1.EndDate,t2,EndDate A6 = t1.StartDate B6 = t2.StartDate C6 = t2.EndDate D6 = t1.EndDate A7 = t1.StartDate B7 = t2.StartDate C7 = t1.EndDate,t2,EndDate ※ 引述《Mutex (Mutex)》之铭言: : 资料库名称:MSSQL : 资料库版本:2008 : 内容/问题描述:挑选时间区间内 重复的日期和最多种类 :         也可以计算出每一天的Product种类有几种,再取MAX : 资料: : Sales, Product, StartDate, EndDate : Amy, A, 20000101, 20000105 : Amy, B, 20000101, 20000103 : Amy, C, 20000104, 20000108 : Amy, D, 20000103, 20000104 : Amy, A, 20000110, 20000111 : Jack, A, 20000201, 20000202 : Jack, B, 20000203, 20000204 : Jack, C, 20000208, 20000209 : Jack, D, 20000204, 20000207 : Jack, B, 20000204, 20000204 : 希望可以汇整出来的答案为(Sales, 最多重复时有几种product,最多重复的那一天) : Amy, 3, 20000104 : Jack, 2, 20000204 : 或是(Sales, 同一天最多重复时有几种product) : Amy, 3 : Jack, 2 : 忽然想到这个情境,但是却思索不出个好解法 : 只好来向请高手请益了 : 谢谢 --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.221.80.36
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1462517666.A.669.html ※ 编辑: cutekid (61.221.80.36), 05/06/2016 15:05:08
1F:推 Mutex: 再次感谢大大 获益良多 05/06 19:52







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP