SQL查詢語法請教(請各位大神幫小弟看看這段SQL語法錯在哪裡)
--2020年購買初階課程會員資料
SELECT sp.shop_cname 註冊店,ms.member_code 會員編號 ,MS.Chinese_name 會員名稱,(CASE ms.sex WHEN 'M' then '男' when 'F' then '女' ELSE ' ' end )性別,to_char(MS.Birthday,'YYYY/MM/DD') 生日 ,MS.Age 年齡, to_char(sysdate,'YYYY')-to_char(MS.Birthday,'YYYY') 目前年齡,
ms.Phone_Night 手機號碼,ms.Phone_Day 電話 ,ms.e_mail 郵件,to_char(ms.joint_date,'YYYY/MM/DD') 加入會員日期,
to_char(sysdate,'YYYY')-to_char(ms.joint_date,'YYYY') 已加入時間,ms.city_code 郵遞區號, ms.address 地址
FROM MEMBERSHIP ms
join shop sp on (ms.shop_code = sp.shop_code)
where ms.member_code in (
select a.member_code from (
--202108月份有購買初級班會員編號(dd.ITEM_CODE = ('8601020002') OR dd.ITEM_CODE = ('8602040001') OR dd.ITEM_CODE = ('8604020001'))
SELECT distinct dh.member_code
FROM DINV_DTL dd
join dinv_hdr dh ON (dd.inv_num=dh.inv_num)
WHERE (dd.ITEM_CODE = ('9401000002') OR dd.ITEM_CODE = ('9402000002') OR dd.ITEM_CODE = ('9403000002'))
AND to_char(DH.inv_date,'YYYY/MM/DD') >='2020/10/28' and to_char(DH.inv_date,'YYYY/MM/DD') <= '2022/10/28'
and dh.closed = '1' AND dh.voided='0'
and trim(dh.member_code) is not NULL
)A left join (
--202108月份有購買主產品的會員資料
select distinct dh.member_code from dinv_hdr dh
join DINV_DTL dd ON (dd.inv_num = dh.inv_num)
where to_char(dh.inv_date,'YYYY/MM/DD') >='2020/10/28' and to_char(dh.inv_date,'YYYY/MM/DD') <= '2022/10/28'
and dd.item_code in ( --縫紉機的型號
select item_code from stock
where bran_code = 'S' and useserial = '1' and cate_code in ('SM1','SM2','SM3','SM4')
)
and dh.closed = '1' AND dh.voided='0'
and trim(dh.member_code) is not NULL
)
--)B ON (a.member_code=b.member_code)
--where b.member_code is NULL --排除沒有買主產品的會員編號
)
and to_char(ms.joint_date,'YYYY/MM/DD') >='2020/10/28' and to_char(ms.joint_date,'YYYY/MM/DD') <= '2022/10/28' --找出202108月份才加入的新會員
ORDER BY ms.shop_code,ms.city_code
;
有A left join (,沒有後半段 )B ON a.xxx=b.xxx
A left join (
--202108月份有購買主產品的會員資料
select distinct dh.member_code from dinv_hdr dh
join DINV_DTL dd ON (dd.inv_num = dh.inv_num)
where to_char(dh.inv_date,'YYYY/MM/DD') >='2020/10/28' and to_char(dh.inv_date,'YYYY/MM/DD') <= '2022/10/28'
and dd.item_code in ( --縫紉機的型號
select item_code from stock
where bran_code = 'S' and useserial = '1' and cate_code in ('SM1','SM2','SM3','SM4')
)
and dh.closed = '1' AND dh.voided='0'
and trim(dh.member_code) is not NULL
)B ON (a.member_code=b.member_code)