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

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP