Database 板


LINE

create table #TABLE_A( id nvarchar(3), name nvarchar(2), eve_d nvarchar(4), event nvarchar(3) ) create table #TABLE_B( id nvarchar(3), date nvarchar(4), score numeric(28,2) ) -- 建立测资 insert into #TABLE_A select '001','小华','0616','事件A' insert into #TABLE_A select '002','小明','0618','事件B' insert into #TABLE_A select '002','小明','0619','事件C' insert into #TABLE_B select '001','0615',11.11 insert into #TABLE_B select '002','0617',9.00 insert into #TABLE_B select '001','0617',22.22 insert into #TABLE_B select '002','0618',7.00 insert into #TABLE_B select '002','0619',5.00 -- 取得「小於等於事件日之前」所有记录(依 TABLE_B.date 由大到小做排名) ;with tb1 as ( select t1.id as id1,t1.eve_d,t2.id as id2,t2.date,t2.score, row_number() over (partition by t1.id,t1.eve_d order by t2.date desc) num1 from #TABLE_A t1 inner join #TABLE_B t2 on t1.id = t2.id and t1.eve_d >= t2.date ), -- 将最接近「第一日」的记录排在第一位(num2 = 1) tb2 as ( select *,row_number() over (partition by id1,eve_d order by num1 desc) num2 from tb1 where 1 >= num1 ), -- 将最接近「第二日」的记录排在第一位(num2 = 1) tb3 as ( select *,row_number() over (partition by id1,eve_d order by num1 desc) num2 from tb1 where 2 >= num1 ) -- 最後结果 select t1.id,t1.name,t1.eve_d,t1.event, t2.date,t2.score, t3.date as pre_date,t3.score as pre_score from #TABLE_A t1 inner join tb2 t2 on t2.num2 = 1 and t2.id1 = t1.id and t2.eve_d = t1.eve_d inner join tb3 t3 on t3.num2 = 1 and t3.id1 = t1.id and t3.eve_d = t1.eve_d /* id name eve_d event date score pre_date pre_score 001 小华 0616 事件A 0615 11.11 0615 11.11 002 小明 0618 事件B 0618 7.00 0617 9.00 002 小明 0619 事件C 0619 5.00 0618 7.00 */ ※ 引述《kalt (甘党)》之铭言: : 资料库名称:MSSQL 资料库版本:2008 R2 EXPRESS : 新手求指导QQ 有两个TABLE资料,其中A记录玩家遭遇事件日和描述 : B则是记录当日的得分情形,长得像这样 : TABLE_A TABLE_B : id name eve_d event id date score : ----------------------------- ----------------------- : 001 小华 0616 事件A 001 0615 11.11 : 002 小明 0618 事件B 002 0617 9.00 : 002 小明 0619 事件C 001 0617 22.22 : 002 0618 7.00 : 002 0619 5.00 : 其中TABLE_B虽然不是每天都有分数记录,但每个id一定在事件日之前有分数记录。 : 而需要TABLE_A为主,去取得该事件发生日之前「最接近两日的得分和日期」, : 组成一张新的TABLE。(若是仅有前一笔分数记录,则两次都以前一笔为主) : 也就是说希望得到下述的结果: : id name eve_d event date score pre_date pre_score : ------------------------------------------------------------------------ : 001 小华 0616 事件A 0615 11.11 0615 11.11 : 002 小明 0618 事件B 0618 7.00 0617 9.00 : 002 小明 0619 事件C 0619 5.00 0618 7.00 : 目前自己尝试的想法是先将TABLE_B整理成这样 : id date score pre_date pre_score : ---------------------------------------------- : 001 0615 11.11 0615 11.11 : 001 0617 22.22 0615 11.11 : 002 0617 9.00 0617 9.00 : 002 0618 7.00 0617 9.00 : 002 0619 5.00 0618 7.00 : 於是先把TABLE_B join自己,用很暴力的方法串出来... : SELECT id, date, score, pre_date, pre_score : FROM( : SELECT Row_Number() over (PARTITION BY a.id, b.date order by a.id) as RowNum, : a.id : ,a.date : ,a.score : ,b.date as pre_date : ,b.score as pre_score : FROM TABLE_B a : LEFT JOIN TABLE_B b : ON a.id = b.id : WHERE b.date < a.date ) a : WHERE a.RowNum = '1' --这个是JOIN自己後把前一日丢到Pre_date和pre_score的情形 : UNION : SELECT a.id, min(a.date) as date, a.score : ,min(a.date) as pre_date, a.score as pre_score : FROM TABLE_B a JOIN : (SELECT : id, : date = min(date) : FROM TABLE_B : GROUP BY id) AS b : ON a.id = b.id AND a.date = b.date : GROUP BY a.id, a.score --这个是当date是最早一笔的时候,直接存进pre的情况 : 嗯,然後再拿这个新的TABLE C和TABLE_A JOIN...再限定事件日等於最接近的date : .........然後我就卡关了Q__Q : 请教各位先进有什麽方法可以解决卡关的地方, : 或者是整个观念太差应该砍掉重练,也请各方高手不吝指教m(_ _)m -- ※ 编辑: cutekid (61.221.80.36), 06/22/2016 14:52:44







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灯, 水草

请输入看板名称,例如:e-shopping站内搜寻

TOP