iT邦幫忙

0

SQL查詢語法請教(請各位大神幫小弟看看這段SQL語法錯在哪裡)

  • 分享至 

  • xImage

SQL查詢語法請教(請各位大神幫小弟看看這段SQL語法錯在哪裡)https://ithelp.ithome.com.tw/upload/images/20221028/20148637rYQtKYLrBU.png

--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        
        ;
shiaobin iT邦新手 4 級 ‧ 2022-10-28 16:14:39 檢舉
把 Line 29 ~ Line 30 的

```
)
--)B ON (a.member_code=b.member_code)
```

改成

```
)B ON (a.member_code=b.member_code)
```
johncoc iT邦新手 3 級 ‧ 2022-10-28 16:41:23 檢舉
樓主的註釋跟被註釋的SQL呈現的結果有些差異
yu0901 iT邦新手 4 級 ‧ 2022-10-28 16:47:17 檢舉
我知道
懶得改了
感謝!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

4
allenlwh
iT邦高手 1 級 ‧ 2022-10-28 16:15:54
最佳解答

有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)  
yu0901 iT邦新手 4 級 ‧ 2022-10-28 16:48:32 檢舉

感謝教學看到眼睛脫窗找到答案了!TKS.

我要發表回答

立即登入回答