Tôi đang cố gắng kiểm tra xem ID có mặt trong một truy vấn con hay không. Tôi đã chạy chỉ số phụ và nó tạo ra một danh sách tất cả các ID có phí chống lại nó, vì vậy những gì tôi muốn làm là kiểm tra xem ID trong truy vấn chính có trong trình điều khiển phụ hay không. Nếu nó có mặt thì hãy trả lại 1, khác trở lại 0. Đây là một truy vấn dễ dàng nhưng tôi không biết mình đang sai ở đâu, tôi đã thử sử dụng exists
thay vì in
nhưng điều này cũng không hoạt động.
case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement" and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
Dưới đây là toàn bộ mã, khi tôi xóa mã trên khỏi truy vấn chính bên dưới, nó hoạt động hoàn hảo, vì vậy có điều gì đó không ổn trong tuyên bố trường hợp của tôi.
with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
13.2.11.6 & NBSP; Các nhóm phụ có tồn tại hoặc không tồn tại
Nếu một công cụ con trả về bất kỳ hàng nào, EXISTS
subquery
là
0 và with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
1 là with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
2. Ví dụ:with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
Theo truyền thống, một truy vấn con
with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
3 bắt đầu với with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
4, nhưng nó có thể bắt đầu bằng with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
5 hoặc with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
6 hoặc bất cứ điều gì. MySQL bỏ qua danh sách with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
7 trong một truy vấn con như vậy, vì vậy nó không có gì khác biệt.Đối với ví dụ trước, nếu
with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
8 chứa bất kỳ hàng nào, thậm chí các hàng không có gì ngoài các giá trị with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
9, điều kiện with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
3 là with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
0. Đây thực sự là một ví dụ không thể xảy ra bởi vì một truy vấn con SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
2 hầu như luôn luôn chứa các mối tương quan. Dưới đây là một số ví dụ thực tế hơn:Những loại cửa hàng có mặt trong một hoặc nhiều thành phố?
SELECT DISTINCT store_type FROM stores WHERE EXISTS [SELECT * FROM cities_stores WHERE cities_stores.store_type = stores.store_type];
Loại cửa hàng nào có mặt ở No City?
SELECT DISTINCT store_type FROM stores WHERE NOT EXISTS [SELECT * FROM cities_stores WHERE cities_stores.store_type = stores.store_type];
Những loại cửa hàng có mặt ở tất cả các thành phố?
SELECT DISTINCT store_type FROM stores s1 WHERE NOT EXISTS [ SELECT * FROM cities WHERE NOT EXISTS [ SELECT * FROM cities_stores WHERE cities_stores.city = cities.city AND cities_stores.store_type = stores.store_type]];
Ví dụ cuối cùng là truy vấn
SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
3 được lồng đôi. Đó là, nó có một mệnh đề SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
3 trong mệnh đề SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
5. Chính thức, nó trả lời câu hỏi mà một thành phố có tồn tại với một cửa hàng không có trong SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
6 không? Nhưng dễ dàng hơn để nói rằng một SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
5 được lồng vào câu hỏi là SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
8 with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
0 cho tất cả SELECT DISTINCT store_type FROM stores
WHERE EXISTS [SELECT * FROM cities_stores
WHERE cities_stores.store_type = stores.store_type];
0?“does a city exist with a store that is not in SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
6”? But it is easier to say that a nested SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
5 answers the question “is SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
8 with cte_1
as
[
select
debtor._rowid as casref
,concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']] as `F/Y`
,date_format[debtor._createddate, '%M %Y'] as `Loaded Month`
,ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-'] as `LO F/Y`
,coalesce[date_format[debtor.offence_date,'%M %Y'],'-'] as `Liability Order Month`
,scheme.name as `Scheme`
,branch.name as `Branch`
,count[debtor._rowid] as `Cases Received`
,count[debtor.linkid] as `LinkID`
,[case
when concat[date_format[date_sub[debtor._createddate, interval 3 month], '%y'], '/', date_format[date_add[debtor._createddate, interval 9 month], '%y']]
= ifnull[concat[date_format[date_sub[debtor.offence_date, interval 3 month], '%y'], '/', date_format[date_add[debtor.offence_date, interval 9 month], '%y']],'-']
then 1 else 0 end ] as `Same Year`
, case when debtor._rowid in [
select distinct note.debtorid from note
left join debtor on note.debtorid = debtor._rowid
left join fee on fee.debtorid = debtor._rowid
where fee.type = "Enforcement"
and note.type = "Stage"]
then 1 else 0 end] as `Enforcement`
from debtor
left join clientscheme on debtor.clientschemeID = clientscheme._rowid
left join scheme on clientscheme.schemeID = scheme._rowid
left join branch on clientscheme.branchID = branch._rowid
left join fee on debtor._rowid = fee.debtorid
left join note on debtor._rowid = note.debtorid
where clientscheme.branchID in [1,10,24]
and debtor._createddate >= '2017-04-01'
group by debtor._rowid
]
,
cte_2
as
[
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,sum[`Cases Received`] as `Case Count`
,sum[`LinkID`] as `Linked Accounts`
,sum[`Same Year`] as `In Year LO`
,sum[Enforcement] as `Enforcement Applied`
from cte_1
group by
`Loaded Month`
,`Liability Order Month`
,`Scheme`
, `Branch`
]
select
`F/Y`
,`Loaded Month`
,`LO F/Y`
,`Liability Order Month`
,`Scheme`
,`Branch`
,`Case Count`
,`Linked Accounts`
,round[[`Linked Accounts`/`Case Count`],2] * 100 as `% of Linked Accounts`
,round[[`In Year LO`/`Case Count`],2] * 100 as `In Year LO's`
,`Enforcement Applied`
from cte_2
0 for all SELECT DISTINCT store_type FROM stores
WHERE EXISTS [SELECT * FROM cities_stores
WHERE cities_stores.store_type = stores.store_type];
0?”Trong MySQL 8.0.19 và sau đó, bạn cũng có thể sử dụng
SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
3 hoặc SELECT column1 FROM t1 WHERE EXISTS [SELECT * FROM t2];
5 với SELECT DISTINCT store_type FROM stores
WHERE EXISTS [SELECT * FROM cities_stores
WHERE cities_stores.store_type = stores.store_type];
3 trong truy vấn con, như thế này:SELECT column1 FROM t1 WHERE EXISTS [TABLE t2];
Các kết quả giống như khi sử dụng
SELECT DISTINCT store_type FROM stores
WHERE EXISTS [SELECT * FROM cities_stores
WHERE cities_stores.store_type = stores.store_type];
4 mà không có mệnh đề SELECT DISTINCT store_type FROM stores
WHERE EXISTS [SELECT * FROM cities_stores
WHERE cities_stores.store_type = stores.store_type];
5 trong truy vấn con.