如何使用 select 语句插入带有 2 table 组合的虚拟短划线值的第一行
How to use select statement insert first row of dummy dash value with 2 table combination
table_A
no desciption
1 Apple
2 orange
3 banana
4 kiwi
5 papaya
table_B
no price mydate
1 10.00 20210801
2 8.00 20210802
3 5.00 20210803
4 12.00 20210804
5 4.00 20210805
你好,我尝试使用这个 SQL 和 union all 但显示错误。
select '-' a.description, '-' b.price from dual union all select a.description,sum(b.price) from table_A a, table_B b where a.no=b.no and b.mydate='20210801' group by a.description;
ORA-00923 : FROM keyword not found where expected
我需要结果
a.description sum(b.price)
- - <-----dummy dash always on first row
Apple 10.00
非常感谢任何人的帮助。
一个选择是
- 使用子查询
- 它包含额外的
rn
列(其中 1
表示这些值将代表结果中的“行号 1”)
- 将
to_char
函数应用于 sum(b.price)
因为 - 对于 union
- 查询必须匹配列数及其数据类型
所以:
SELECT description, price
FROM (SELECT 1 rn, '-' description, '-' price FROM DUAL
UNION ALL
SELECT 2 rn, a.description, TO_CHAR (SUM (b.price))
FROM table_A a, table_B b
WHERE a.no = b.no
AND b.mydate = '20210801'
GROUP BY a.description)
ORDER BY rn
哪个 - 应用于您的示例数据 - returns
DESCRIPTION PRICE
--------------- ----------------------------------------
- -
apple 10
注意:
mydate
应该是真正的 date
数据类型列;不要使用 strings 因为没有什么能阻止你输入例如2f-23
到 varchar2
列,这肯定不是有效的日期值
- 尝试切换到
join
;保留 条件的 where
子句 数据应满足才能返回
像这样:
from table_a a join table_b b on a.no = b.no --> JOIN
where b.mydate = date '2021-08-01' --> MYDATE being DATE datatype
union all
的第一部分没有别名 a
和 b
,所以只需删除 a.
和 b.
- 不要使用从字符文字到日期的隐式转换,而是使用文字日期:
select '-' as description, '-' as price from dual
union all
select a.description,to_char(sum(b.price))
from
table_A a
join table_B b
on a.no=b.no
where b.mydate=date'2021-08-01'
group by a.description;
table_A
no desciption
1 Apple
2 orange
3 banana
4 kiwi
5 papaya
table_B
no price mydate
1 10.00 20210801
2 8.00 20210802
3 5.00 20210803
4 12.00 20210804
5 4.00 20210805
你好,我尝试使用这个 SQL 和 union all 但显示错误。
select '-' a.description, '-' b.price from dual union all select a.description,sum(b.price) from table_A a, table_B b where a.no=b.no and b.mydate='20210801' group by a.description;
ORA-00923 : FROM keyword not found where expected
我需要结果
a.description sum(b.price)
- - <-----dummy dash always on first row
Apple 10.00
非常感谢任何人的帮助。
一个选择是
- 使用子查询
- 它包含额外的
rn
列(其中1
表示这些值将代表结果中的“行号 1”) - 将
to_char
函数应用于sum(b.price)
因为 - 对于union
- 查询必须匹配列数及其数据类型
所以:
SELECT description, price
FROM (SELECT 1 rn, '-' description, '-' price FROM DUAL
UNION ALL
SELECT 2 rn, a.description, TO_CHAR (SUM (b.price))
FROM table_A a, table_B b
WHERE a.no = b.no
AND b.mydate = '20210801'
GROUP BY a.description)
ORDER BY rn
哪个 - 应用于您的示例数据 - returns
DESCRIPTION PRICE
--------------- ----------------------------------------
- -
apple 10
注意:
mydate
应该是真正的date
数据类型列;不要使用 strings 因为没有什么能阻止你输入例如2f-23
到varchar2
列,这肯定不是有效的日期值- 尝试切换到
join
;保留 条件的where
子句 数据应满足才能返回
像这样:
from table_a a join table_b b on a.no = b.no --> JOIN
where b.mydate = date '2021-08-01' --> MYDATE being DATE datatype
union all
的第一部分没有别名a
和b
,所以只需删除a.
和b.
- 不要使用从字符文字到日期的隐式转换,而是使用文字日期:
select '-' as description, '-' as price from dual
union all
select a.description,to_char(sum(b.price))
from
table_A a
join table_B b
on a.no=b.no
where b.mydate=date'2021-08-01'
group by a.description;