select
sys_guid(),
count(jsb1.zgh) nanrs,
count(jsb2.zgh) nvrs,
count(jsb3.zgh) nan_nv_rs,
t.yxmc
from dm_yxb t
left join (select zgh,xb,xydm from sz_jsxxb where xb='1' and xydm is not null) jsb1 on t.yxdm=jsb1.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb='2' and xydm is not null) jsb2 on t.yxdm=jsb2.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null) jsb3 on t.yxdm=jsb3.xydm
group by t.yxmc
正確的解答可以是這樣的,這是我最終的應用結果。但是我就是不知道上面的寫法,為什么會導致資料重復!
select
sys_guid(),
x.rs nanrs,
y.rs nvrs,
z.rs nan_nv_rs,
t.yxmc
from dm_yxb t
left join (select count(zgh) rs,xydm from sz_jsxxb where xb='1' and xydm is not null group by xydm) x on t.yxdm=x.xydm
left join (select count(zgh) rs,xydm from sz_jsxxb where xb='2' and xydm is not null group by xydm) y on t.yxdm=y.xydm
left join (select count(zgh) rs,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null group by xydm) z on t.yxdm=z.xydm
uj5u.com熱心網友回復:
你這樣寫試一下:
select
count(jsb1.zgh) nanrs,
count(jsb2.zgh) nvrs,
count(jsb3.zgh) nan_nv_rs,
t.yxdm
from dm_yxb t
left join (select zgh,xb,xydm from sz_jsxxb where xb='1' and xydm is not null) jsb1 on t.yxdm=jsb1.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb='2' and xydm is not null) jsb2 on t.yxdm=jsb2.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null) jsb3 on t.yxdm=jsb3.xydm
group by t.yxdm
uj5u.com熱心網友回復:
試了不行,不是sys_guid()的問題。
uj5u.com熱心網友回復:
用yxdm進行group
uj5u.com熱心網友回復:
你這樣寫試一下:
select
count(jsb1.zgh) nanrs,
count(jsb2.zgh) nvrs,
count(jsb3.zgh) nan_nv_rs,
t.yxdm
from dm_yxb t
left join (select zgh,xb,xydm from sz_jsxxb where xb='1' and xydm is not null) jsb1 on t.yxdm=jsb1.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb='2' and xydm is not null) jsb2 on t.yxdm=jsb2.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null) jsb3 on t.yxdm=jsb3.xydm
group by t.yxdm
試了不行,不是sys_guid()的問題。
用yxdm進行group
上面這個就是使用yxdm進行分組的啊,不好使。除非在left join里面分組才是對的
uj5u.com熱心網友回復:
你這樣寫試一下:
select
count(jsb1.zgh) nanrs,
count(jsb2.zgh) nvrs,
count(jsb3.zgh) nan_nv_rs,
t.yxdm
from dm_yxb t
left join (select zgh,xb,xydm from sz_jsxxb where xb='1' and xydm is not null) jsb1 on t.yxdm=jsb1.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb='2' and xydm is not null) jsb2 on t.yxdm=jsb2.xydm
left join (select zgh,xb,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null) jsb3 on t.yxdm=jsb3.xydm
group by t.yxdm
我想樓主大概是想統計這樣的結果吧
select
sys_guid(),
sum(jsb1.zgh) nanrs,
sum(jsb2.zgh) nvrs,
sum(jsb3.zgh) nan_nv_rs,
t.yxmc
from dm_yxb t
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb='1' and xydm is not null group by yxdm) jsb1 on t.yxdm=jsb1.xydm
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb='2' and xydm is not null group by yxdm) jsb2 on t.yxdm=jsb2.xydm
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null group by yxdm) jsb3 on t.yxdm=jsb3.xydm
group by t.yxmc
uj5u.com熱心網友回復:
是不是想要這樣呢?
select sys_guid(),
count(case when xb ='1' then jsb1.zgh end) nanrs,
count(case when xb ='2' then jsb1.zgh end) nvrs,
count(jsb3.zgh) nan_nv_rs,
t.yxmc
from dm_yxb t
left join (select zgh, xb, xydm
from sz_jsxxb
where xb in ('1','2')
and xydm is not null) jsb1
on t.yxdm = jsb1.xydm
left join (select zgh, xb, xydm
from sz_jsxxb
where xb != '1'
and xb != '2'
and xydm is not null) jsb3
on t.yxdm = jsb3.xydm
group by t.yxmc
uj5u.com熱心網友回復:
是不是想要這樣呢?
select sys_guid(),
count(case when xb ='1' then jsb1.zgh end) nanrs,
count(case when xb ='2' then jsb1.zgh end) nvrs,
count(jsb3.zgh) nan_nv_rs,
t.yxmc
from dm_yxb t
left join (select zgh, xb, xydm
from sz_jsxxb
where xb in ('1','2')
and xydm is not null) jsb1
on t.yxdm = jsb1.xydm
left join (select zgh, xb, xydm
from sz_jsxxb
where xb != '1'
and xb != '2'
and xydm is not null) jsb3
on t.yxdm = jsb3.xydm
group by t.yxmc
是這樣的結果,但是這樣效率很低。因為資料量都成萬級的
uj5u.com熱心網友回復:
把jsb1做成臨時表再關聯應該會好很多吧,再不行的話是不是可以用游標呢?
uj5u.com熱心網友回復:
我想樓主大概是想統計這樣的結果吧
select
sys_guid(),
sum(jsb1.zgh) nanrs,
sum(jsb2.zgh) nvrs,
sum(jsb3.zgh) nan_nv_rs,
t.yxmc
from dm_yxb t
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb='1' and xydm is not null group by yxdm) jsb1 on t.yxdm=jsb1.xydm
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb='2' and xydm is not null group by yxdm) jsb2 on t.yxdm=jsb2.xydm
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null group by yxdm) jsb3 on t.yxdm=jsb3.xydm
group by t.yxmc
select
sys_guid(),
count(case when jsb1.xb='1' then jsb1.zgh end) nanrs,
count(case when jsb1.xb='2' then jsb1.zgh end) nvrs,
count(case when jsb1.xb not in ('1','2') then jsb1.zgh end) nan_nv_rs,
t.yxmc
from dm_yxb t
left join sz_jsxxb jsb1 on t.yxdm=jsb1.xydm
where jsb1.xydm is not null
group by t.yxmc;
我想樓主大概是想統計這樣的結果吧
select
sys_guid(),
sum(jsb1.zgh) nanrs,
sum(jsb2.zgh) nvrs,
sum(jsb3.zgh) nan_nv_rs,
t.yxmc
from dm_yxb t
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb='1' and xydm is not null group by yxdm) jsb1 on t.yxdm=jsb1.xydm
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb='2' and xydm is not null group by yxdm) jsb2 on t.yxdm=jsb2.xydm
left join (select count(zgh) zgh,xydm from sz_jsxxb where xb!='1' and xb!='2' and xydm is not null group by yxdm) jsb3 on t.yxdm=jsb3.xydm
group by t.yxmc
**桔妹導讀:**深耕人工智能領域,致力于探索AI讓出行更美好的滴滴AI Labs再次斬獲國際大獎,這次獲獎的專案是什么呢?一起來看看詳細報道吧! 近日,由國際計算語言學協會ACL(The Association for Computational Linguistics)舉辦的世界最具影響力的機器 ......
我們經常在資料庫中使用 LIKE 運算子來完成對資料的模糊搜索,LIKE 運算子用于在 WHERE 子句中搜索列中的指定模式。 如果需要查找客戶表中所有姓氏是“張”的資料,可以使用下面的 SQL 陳述句: SELECT * FROM Customer WHERE Name LIKE '張%' 如果需要 ......
關于MySQL的二進制日志(binlog),我們都知道二進制日志(binlog)非常重要,尤其當你需要point to point災難恢復的時侯,所以我們要對其進行備份。關于二進制日志(binlog)的備份,可以基于flush logs方式先切換binlog,然后拷貝&壓縮到到遠程服務器或本地服務器 ......