[英]Concat/Union two tables in SQL
The sql query below creates two tables, tab1 with 3 columns (quarter, region and datasum) and tab2 with 2 columns (quarter and datasum).下面的 sql 查询创建了两个表,tab1 有 3 列(季度、区域和数据和),tab2 有 2 列(季度和数据和)。
I want to stack the values from tab1 and tab2 together (just like pd.concat([tab1, tab2]) in pandas/python).我想将 tab1 和 tab2 的值堆叠在一起(就像 pandas/python 中的 pd.concat([tab1, tab2]) 一样)。 For that I need to create a new column in tab2 called region and insert that in the same position as the corresponding column in tab1.为此,我需要在 tab2 中创建一个名为 region 的新列,并将其插入到与 tab1 中相应列相同的位置。 And after that I think I need to use UNION_ALL.之后我认为我需要使用 UNION_ALL。
In tab2 I would like the value of the column region to be 'all' for every instance.在 tab2 中,我希望每个实例的列区域的值都是“全部”。
How could I achieve this?我怎么能做到这一点?
I tried to use ALTER TABLE and ADD but I don't get that to work for me.我尝试使用 ALTER TABLE 和 ADD 但我没有让它为我工作。 Help would be much appreciated.帮助将不胜感激。 I work in SQL Oracle.我在 SQL Oracle 工作。
with base1 as(
select substr(municip,1,2) as region, data, age,
case when substr(time,6,2) in ('01','02','03') then substr(time, 1,4) || '_1'
when substr(time,6,2) in ('04','05','06') then substr(time, 1,4) || '_2'
when substr(time,6,2) in ('07','08','09') then substr(time, 1,4) || '_3'
else substr(time, 1,4) || '_4' end quarter
from sql_v1
where time >= '2021-01' and
),
base2 as(select data, age,
case when substr(time,6,2) in ('01','02','03') then substr(time, 1,4) || '_1'
when substr(time,6,2) in ('04','05','06') then substr(time, 1,4) || '_2'
when substr(time,6,2) in ('07','08','09') then substr(time, 1,4) || '_3'
else substr(time, 1,4) || '_4' end quarter
from sql_v1
where time >= '2021-01'),
tab1 as (select quarter, region,
sum (case when age between '16' and '64' then kvar else 0 end) datasum
from base
group by quarter, region
order by quarter, region),
tab2 as (select quarter,
sum (case when age between '16' and '64' then kvar else 0 end) datasum
from riket
group by quarter
order by quarter)
...select * from tab_union
It appears that you're using strings for storing dates???看来您正在使用字符串来存储日期??? Don't do that :(不要那样做:(
If you use native datetime datatypes then you can extract the quarter using things like TO_CHAR(datetime_column, 'Q')
如果您使用本机日期时间数据类型,则可以使用TO_CHAR(datetime_column, 'Q')
类的内容提取季度
For now, however, using the horrendous datatypes, you can restructure your query to use ROLLUP
in your GROUP BY
...但是,现在,使用ROLLUP
的数据类型,您可以重组查询以在GROUP BY
...
WITH
formatted AS
(
SELECT
SUBSTR(municip,1,2) AS region,
SUBSTR(time, 1,4) || CASE WHEN SUBSTR(time,6,2) IN ('01','02','03') THEN '_1'
WHEN SUBSTR(time,6,2) IN ('04','05','06') THEN '_2'
WHEN SUBSTR(time,6,2) IN ('07','08','09') THEN '_3'
ELSE '_4' END AS quarter,
age,
kvar
FROM
sql_v1
WHERE
time >= '2021-01'
)
SELECT
region,
COALESCE(quarter, 'All'),
SUM(CASE WHEN age BETWEEN 16 AND 64 THEN kvar ELSE 0 END)
FROM
formatted
GROUP BY
region, ROLLUP(quarter)
ORDER BY
region,
GROUPING(quarter),
quarter
Demo : https://dbfiddle.uk/?rdbms=oracle_21&fiddle=ab27d71ac81bb9bc7e5e06e7f5a44ba9演示: https ://dbfiddle.uk/?rdbms=oracle_21&fiddle=ab27d71ac81bb9bc7e5e06e7f5a44ba9
Or, using DATE datatype : https://dbfiddle.uk/?rdbms=oracle_21&fiddle=1544406dc2b6669bc62ed02a6155b1c2或者,使用 DATE 数据类型: https ://dbfiddle.uk/?rdbms=oracle_21&fiddle=1544406dc2b6669bc62ed02a6155b1c2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.