select
t1.PI_NO
,t1.CREATION_DATE
,t2.CUSTOM_NAME
,t3.USER_NAME
from (select
*
from PIM_HEADERS
where CREATION_DATE between
to_date('2011-03-01','yyyy-mm-dd')
and
to_date('2011-03-25','yyyy-mm-dd')
) t1
left join CTM_CUSTOMERS t2
on t1.CUSTOMER_ID = t2.CUSTOMER_ID
left join SM_USER t3
on t1.OPERATOR_ID = t3.USER_ID
注:
实际上不用to_date也行,此函数是把字符串转换为日期
select t1._DEPARTMENT_NAME,count(t2.USER_NAME),count(t3.PI_NO)
from SM_DEPT t1
left join SM_USER_DEPT t2
on t1.dept_id = t2.dept_id
left join (select
*
from PIM_HEADERS
where CREATION_DATE between
to_date('2011-01-01','yyyy-mm-dd')
and
to_date('2011-03-01','yyyy-mm-dd')
) t3
on t2.user_id = t3.OPERATOR_ID
group by t1.DEPT_ID
select
t1.*
,t2.PI_NO
,t2.CREATION_DATE
,t3.USER_NAME
,t5.DEPARTMENT_NAME
from SM_CUSTOMERS t1
left join PIM_HEADERS t2
on t1.CUSTOER_ID = t2.CUSTOMER_ID
left join SM_USER t3
on t2.OPERATOR_ID = t3.user_id
left join SM_USER_DEPT t4
on t3.USER_ID = t4.USER_ID
left join SM_DEPT t5
on t4.DEPT_ID = t5.DEPT_ID
第一种
update CTM_CUSTOMERS t1 set COMMENTS = '从未反生业务往来'
where not exists (
select PI_NO from PIM_HEADERS t2
where t2.CUSTOMER_ID = t1.CUSTOMER_ID
) ;
commit;
注:exists与in的用法有异曲同工之妙,MySQL内部优化器会把第一条使用 in 的语句转化为第二条使用 exists 的语句执行。执行的结果当然就是一样的了。
想了解exists,点此链接
select CUSTOM_NAME,CITY
from CTM_CUSTOMERS
order by city desc,CUSTOMER_NAME asc
select
t2.CUSTOMER_NAME
,t1.CURRENCY_CODE
,t3.TOTAL
,t3.TOTAL_MONEY
from PIM_HEADERS t1
left join CTM_CUSTOMERS t2
on t1.CUSTOMER_ID = t2.CUSTOMER_ID
left join (
select PI_ID,sum(QUANTITY) TOTAL,sum(QUANTITY*PRICE) TOTAL_MONEY
from PIM_LINES
group by PI_ID
) t3
on t1.PI_ID = t3.PI_ID;
create or replace function getUserByDeptId
( dept_id number) return varchar2
is
res varchar2(200);
cursor mycursor(did number) is
select t2.CUSTOMER_NAME
from (select *
from SM_USER_DEPT
where dept_id = did
) t1
left join SM_USER t2
on t1.USER_ID = t2.USER_ID ;
begin
for c_row in mycursor(dept_id) loop
res := res ||','|| c_row
end loop;
return res;
end;
想了解编写函数,点此链接