Database 板


LINE

(針對 SQL 語言的問題,用這個標題。請用 Ctrl+Y 砍掉這行) 資料庫名稱:oracle sql 資料庫版本: 內容/問題描述: 主要問題有二 一 欄位一 時間區間以 當日每小時 做間隔,不知道有沒有時間函數可以處理? 目前想到 以 CASE 作條件輸出 ; WITH 做虛擬表格 查詢 ; 製作 VIEW 觀視表 (和WITH 一樣) 二 達成率希望做個別統計 EX: 07:00 產量/100 ; 08:00 產量/50 主要卡在問題一的時間區隔處理, 沒有其他資料表有相關欄位可以做 JOINT 或 子查詢 請大家幫幫忙 資料表 G_SN_TRAVEL 欄位 OUT_PROCESS_TIME (DATE 機台過站時間戳記) 希望輸出查詢統計表如下 欄位一 欄位二 欄位三 CLOCK_TIME QTY(產量) RATE(達成率) 07:00 XXX XX % 08:00 XXX XX % 09:00 XXX XX % 已有 方案 都可以完成 問題一 方案一 SELECT (case to_char(A.OUT_PROCESS_TIME,'HH24') when '07' then '07:00~07:59' when '08' then '08:00~08:59' when '09' then '09:00~09:59' when '10' then '10:00~10:59' when '11' then '11:00~11:59' when '12' then '12:00~12:59' when '13' then '13:00~13:59' when '14' then '14:00~14:59' when '15' then '15:00~15:59' when '16' then '16:00~16:59' when '17' then '17:00~17:59' when '18' then '18:00~18:59' when '19' then '19:00~19:59' when '20' then '20:00~20:59' end) AS TIME_CLOCK, COUNT (A.OUT_PROCESS_TIME) AS QTY, TO_CHAR ((COUNT (A.OUT_PROCESS_TIME) /120),'0.000') AS RATE FROM SAJET.G_SN_TRAVEL A WHERE A.PROCESS_ID = '100032' AND to_char(A.OUT_PROCESS_TIME,'YYYYMMDD') = TO_CHAR(SYSDATE,'YYYYMMDD') GROUP BY (case to_char(A.OUT_PROCESS_TIME,'HH24') when '07' then '07:00~07:59' when '08' then '08:00~08:59' when '09' then '09:00~09:59' when '10' then '10:00~10:59' when '11' then '11:00~11:59' when '12' then '12:00~12:59' when '13' then '13:00~13:59' when '14' then '14:00~14:59' when '15' then '15:00~15:59' when '16' then '16:00~16:59' when '17' then '17:00~17:59' when '18' then '18:00~18:59' when '19' then '19:00~19:59' when '20' then '20:00~20:59' end) ORDER BY (case to_char(A.OUT_PROCESS_TIME,'HH24') when '07' then '07:00~07:59' when '08' then '08:00~08:59' when '09' then '09:00~09:59' when '10' then '10:00~10:59' when '11' then '11:00~11:59' when '12' then '12:00~12:59' when '13' then '13:00~13:59' when '14' then '14:00~14:59' when '15' then '15:00~15:59' when '16' then '16:00~16:59' when '17' then '17:00~17:59' when '18' then '18:00~18:59' when '19' then '19:00~19:59' when '20' then '20:00~20:59' end) ASC =============================== 方案二 with v_today(vday) as ( SELECT to_char(sysdate,'YYYYMMDD')FROM dual ), --select * from v_today v_G_SN_TRAVEL(v_WORK_ORDER,v_SERIAL_NUMBER,v_OUT_PROCESS_TIME,v_vc_time) as ( select a.WORK_ORDER ,a.SERIAL_NUMBER ,a.OUT_PROCESS_TIME ,to_char(a.OUT_PROCESS_TIME,'YYYYMMDDHH24MISS') as vc_time from SAJET.G_SN_TRAVEL a ,v_today b where 0=0 --and a.WORK_ORDER = 'MO20050015' and a.PROCESS_ID = '100032' and to_char(a.OUT_PROCESS_TIME,'YYYYMMDD') BETWEEN b.vday and b.vday ), --select * from v_G_SN_TRAVEL v_clock_07 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '070000' and '079999' ), v_clock_08 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '080000' and '089999' ), v_clock_09 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '090000' and '099999' ), v_clock_10 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '100000' and '109999' ), v_clock_11 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '110000' and '119999' ), v_clock_12 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '120000' and '129999' ), v_clock_13 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '130000' and '139999' ), v_clock_14 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '140000' and '149999' ), v_clock_15 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '150000' and '159999' ), v_clock_16 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '160000' and '169999' ), v_clock_17 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '170000' and '179999' ), v_clock_18 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '180000' and '189999' ), v_clock_19 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '190000' and '199999' ), v_clock_20 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '200000' and '209999' ), v_clock_21 (v_count_time) as ( select COUNT(v_vc_time) from v_G_SN_TRAVEL where to_char(v_OUT_PROCESS_TIME,'HH24MISS') BETWEEN '210000' and '219999' ), sum_clock_data (v_itm,v_scd) as ( select '07:00 ~ 07:59' as v_itm ,v_count_time from v_clock_07 union all select '08:00 ~ 08:59' as v_itm ,v_count_time from v_clock_08 union all select '09:00 ~ 09:59' as v_itm ,v_count_time from v_clock_09 union all select '10:00 ~ 10:59' as v_itm ,v_count_time from v_clock_10 union all select '11:00 ~ 11:59' as v_itm ,v_count_time from v_clock_11 union all select '12:00 ~ 12:59' as v_itm ,v_count_time from v_clock_12 union all select '13:00 ~ 13:59' as v_itm ,v_count_time from v_clock_13 union all select '14:00 ~ 14:59' as v_itm ,v_count_time from v_clock_14 union all select '15:00 ~ 15:59' as v_itm ,v_count_time from v_clock_15 union all select '16:00 ~ 16:59' as v_itm ,v_count_time from v_clock_16 union all select '17:00 ~ 17:59' as v_itm ,v_count_time from v_clock_17 union all select '18:00 ~ 18:59' as v_itm ,v_count_time from v_clock_18 union all select '19:00 ~ 19:59' as v_itm ,v_count_time from v_clock_19 union all select '20:00 ~ 20:59' as v_itm ,v_count_time from v_clock_20 union all select '21:00 ~ 21:59' as v_itm ,v_count_time from v_clock_21 ) select (v_itm)"Time Clock",(v_scd)"Output Qty" from sum_clock_data -- 從表象看起來我是個沒啥事的閒人╭(─╴─)╮ 其實我的真實身份是............研究僧 研究如何將自己脫離去死團方法.........................Orz --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.216.86.175 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/Database/M.1595486385.A.D77.html
1F:推 hwChang: 看有沒有像是 MySQL 函數 DATE_FORMAT(`datetime`,'%h') 07/23 17:25
2F:→ hwChang: 例如 2020-07-23 17:26:22 會得到 17 07/23 17:26
3F:→ hwChang: 去 group by DATE_FORMAT(`datetime`, '%h') 07/23 17:26
4F:→ hwChang: 可以加總每個小時的產量 07/23 17:27







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