現在的想法是根據傳入的nf1和nf2動態拼接union all來實作,但我不想使用union all,因為這樣很影響效率。
union all想法的SQL陳述句如下(2009年至2011年):
select 2009 as 年份, count(*)
from A a
where to_char(a.begin,'yyyy')<='2009' and to_char(a.end,'yyyy')>'2009'
union all
select 2010 as 年份, count(*)
from A a
where to_char(a.begin,'yyyy')<='2010' and to_char(a.end,'yyyy')>'2010'
union all
select 2011 as 年份, count(*)
from A a
where to_char(a.begin,'yyyy')<='2011' and to_char(a.end,'yyyy')>'2011'
uj5u.com熱心網友回復:
對于跨年的資料,如果按照開始時間來算的話
select to_char(a.begin, 'yyyy'), count(*)
from A a
where to_char(a.begin, 'yyyy') <= 'nf1'
and to_char(a.end, 'yyyy') > 'nf2'
group by to_char(a.begin, 'yyyy')
uj5u.com熱心網友回復:
統計nf1到nf2所有資料的where應該是【begin<=nf2 and end>nf1】吧。
剛才想了一下,這個問題的描述可以簡化一下,統計2016和2017年的資料,對于【to_char(a.begin,'yyyy')<='nf' and to_char(a.end,'yyyy')>'nf'】這樣的條件,表中的記錄可能既屬于2016年也屬于2017年,我可以用【begin<=2017 and end>2016】這個條件統計出2016年和2017年所有的資料,比如有100條,但單獨的統計出2016年的再加上2017年的,這個數量肯定是>=100的。在這種情況下根據年份進行分組。。。
uj5u.com熱心網友回復:
統計nf1到nf2所有資料的where條件這樣寫好像也不對
uj5u.com熱心網友回復:
?查詢 SELECT
http://www.verejava.com/?id=17173779389953
uj5u.com熱心網友回復:
統計nf1到nf2所有資料的where條件這樣寫好像也不對
統計nf1到nf2所有資料的where應該是【begin<=nf2 and end>nf1】吧。
剛才想了一下,這個問題的描述可以簡化一下,統計2016和2017年的資料,對于【to_char(a.begin,'yyyy')<='nf' and to_char(a.end,'yyyy')>'nf'】這樣的條件,表中的記錄可能既屬于2016年也屬于2017年,我可以用【begin<=2017 and end>2016】這個條件統計出2016年和2017年所有的資料,比如有100條,但單獨的統計出2016年的再加上2017年的,這個數量肯定是>=100的。在這種情況下根據年份進行分組。。。
對于跨年的資料,如果按照開始時間來算的話
select to_char(a.begin, 'yyyy'), count(*)
from A a
where to_char(a.begin, 'yyyy') <= 'nf1'
and to_char(a.end, 'yyyy') > 'nf2'
group by to_char(a.begin, 'yyyy')
想跨年的資料對于每一年分別統計一次?
with tab1 as (
select 1 id, date'2017-01-01' be, date'2018-01-01' en from dual union all
select 2 id, date'2015-01-01' be, date'2018-01-01' en from dual union all
select 3 id, date'2018-01-01' be, date'2018-08-01' en from dual
)
select count(1), to_char(add_months(be, 12 * (level - 1)), 'yyyy')
from tab1 t1
connect by trunc(add_months(be, 12 * (level - 1)), 'y') <= trunc(en, 'y')
and prior id = id
and prior dbms_random.value is not null
group by to_char(add_months(be, 12 * (level - 1)), 'yyyy');
**桔妹導讀:**深耕人工智能領域,致力于探索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,然后拷貝&壓縮到到遠程服務器或本地服務器 ......