[英]Is there way to display record month by month?
所以..我正在嘗試逐月顯示記錄。
例如,
select X, Y, Z, (X + Y + Z) as total
from (
select
(select count(x) from table1 a, table2 b where date between '2020-01-01' and '2020-05-01') as X,
(select count(y) from table3 a, table4 b where date between '2020-01-01' and '2020-05-01') as Y,
(select count(z) from table5 a, table6 b where date between '2020-01-01' and '2020-05-01') as Z
from dual
);
這將顯示這些日期范圍之間的 x 計數,但是如果我將其顯示為 2 列,例如第一列顯示月份,第二列顯示 X 並逐月顯示。 所以結果如下所示。 這在 sql 中可行嗎?
Month || X || Y || Z || Total
January || 125 || 133 || 155 || 413
February || 150 || 123 || 129 || 402
March || 170 || 177 || 155 || 502
....
....
如果您希望能夠按月份按時間順序排序,並假設 pdate 是實際的日期數據類型,則需要在 order by 中以數字格式包含它。 所以是這樣的:
select to_char(pdate,'Month') month,
count(*)
from TableA
where pdate between to_date('2019-01-01','YYYY-MM-DD')
and to_date('2020-08-31','YYYY-MM-DD')
group by to_char(pdate,'Month')
order by to_char(pdate,'MM');
你只需要按月分組
Select count(X), TO_CHAR(PDATE , 'MONTH') AS Month_name
From TableA
Where PDATE between '2020-01-01' to '2020-01-31'
group by TO_CHAR(PDATE , 'MONTH')
Select
To_char(trunc(pdate, 'mm' ), 'month' ), count(X)
From TableA
Group by trunc(pdate, 'mm' )
Order by trunc(pdate, 'mm' )
將您的日期轉換為所需的顯示格式“月”,但按日期排序
我假設您不關心您的記錄關聯的年份,因此我將日期轉換為與記錄月份關聯的字符串。
接下來,我按記錄的月份進行匯總,並通過將記錄的月份轉換回日期來執行排序,如下所示:
--sample data---------------
WITH smple ( record_month ) AS (
SELECT
to_char(last_ddl_time, 'Month') record_month
FROM
all_objects
)
--end sample data-----------
SELECT
record_month,
COUNT(1)
FROM
smple
GROUP BY
record_month
ORDER BY
to_date(record_month || '-01-2020', 'MONTH-DD-YYYY') ASC;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.