--一條拆成兩條
--統計當前客戶經理和客戶關係,上年底客戶經理和客戶關係
--如果客戶經理和客戶關係跨越這兩個時間段,那麼一條分成兩條
with
basic_t as (
--客戶和客戶經理關係表
select 123 cust_no,'a' am_no, '20091231' start_dt , '29991231' end_dt from dual
union all
select 456,'b', '20100401' start_dt , '29991231' end_dt from dual
union all
select 789,'c', '20100505' start_dt , '20110101' end_dt from dual
), rela_peroid as (
--在統計日期和上年底的關係
select t.cust_no
, t.am_no
, case when t.start_dt <= '20110425'
and t.end_dt > '20110425'
and t.start_dt <= substr('20110425',1,4)-1 || '1231'
and t.end_dt > substr('20110425',1,4) -1 || '1231'
then 'all'
when t.start_dt <= '20110425'
and t.end_dt > '20110425'
then '20110425'
when t.start_dt <= substr('20110425',1,4)-1 || '1231'
and t.end_dt > substr('20110425',1,4)-1 || '1231'
then substr('20110425',1,4)-1 ||'1231'
end stat_dt
from basic_t t
where(
t.start_dt <= '20110425' --當前
and t.end_dt > '20110425'
)
or (
t.start_dt <= substr('20110425',1,4)-1 || '1231' --上年底
and t.end_dt > substr('20110425',1,4)-1 || '1231'
) )
--select * from rela_peroid;
, one2two as (
select 'all' stat_dt,decode(rownum,1,'20110425',substr('20110425',1,4) -1 || '1231') rp_dt
from dual connect by rownum < 3;
)select t.cust_no
, t.am_no
, decode(one2two.rp_dt,null,t.stat_dt,one2two.rp_dt) as stat_dt
from rela_peroid t
left join one2two
on t.stat_dt = one2two.stat_dt
order by cust_no
將兩條資料合成一條
在查詢到資料後,需要根據資料中的某一欄位 a 或者 b 將兩條資料組合成一條,可通過建立乙個新的方法,根據這兩條資料中的某幾個類似於主鍵的字段將這兩條資料組成一條。例如 protected virtual decimal getvarproductprice datatable dt,string ...
兩條合併一條,求最簡單的解法
資料庫的資料如下 stampday employeecd stamptype stamptime 2007 02 28 2200399 1 08 02 00.000 2007 02 28 2200399 2 18 01 00.000 2007 03 01 2200399 1 08 08 00.000...
MySQL 兩條記錄合併查詢成一條顯示
資料庫資料如下 張三,屬於a部門也屬於b部門,在表裡存的是兩條記錄,但是在頁面上我想要根據人來顯示,在一條記錄裡顯示張三屬於a部門和b部門,張三隻顯一條記錄,不顯示兩條。select t.id,t.code,t.name,max case t.type when a then t.dept name...