由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 包子题 (1st answer 100新币)
相关主题
How to write this query跪求高人指点:一道SQL题3/5个包子可以么?先谢了!
Merge table with one single query?ask for help
问一个sql查询语句的问题How to concatenate NULL value with a string in SQL Server?
请问这种MYSQL的语法在SQL Server里面改成什么格式?MS SQL = or like?
How to write this query in Oracle?抱怨一下 数据库 里头的一些不严谨。
Help on Oracle Query请教:为什么死活连部上数据库?!
t-sql chanllege 20recursive sql?
大家帮忙看看这个oracle statement, 怎么总是报错?A sql question
相关话题的讨论汇总
话题: code话题: d1话题: dt话题: select话题: d2
进入Database版参与讨论
1 (共1页)
c**t
发帖数: 2744
1
目的:create a view
数据库: oracle
输入: tbl
DT Code Type Value
d1 RM1 A ###
d1 RM2 A ###
d1 RM3 ###
d2 RM1 B ###
d2 RM2 B ###
d2 RM3 B ###
输出:
DT Code TYPE Value
d1 RA1 A ###
d1 RA2 A ###
d1 RA3 A ###
d1 RB1 B 0
d1 RB2 B 0
d1 RB3 B 0
d2 RA1 A 0
d2 RA2 A 0
d2 RA3
B*****g
发帖数: 34098
2
看到包子,先站个坑,这个解法挺烂。
CREATE OR REPLACE FORCE VIEW beijing_test_view
(DT, CODE, TYPES, VALUE) AS
WITH TEMP_A AS
(SELECT DISTINCT DT FROM beijing_test),
TEMP_B AS
(SELECT 'RA1' CODE, 'A' TYPES FROM DUAL
UNION ALL
SELECT 'RA2' CODE, 'A' TYPES FROM DUAL
UNION ALL
SELECT 'RA3' CODE, 'A' TYPES FROM DUAL
UNION ALL
SELECT 'RB1' CODE, 'B' TYPES FROM DUAL
UNION ALL
SELECT 'RB2' CODE, 'B' TYPES FROM DUAL
UNION ALL
SELECT 'RB3' CODE, 'B' TYPES FROM DUAL),
TEMP_C AS
(SELECT * FROM TEMP_A, TEMP_B)
SELECT b.dt, b

【在 c**t 的大作中提到】
: 目的:create a view
: 数据库: oracle
: 输入: tbl
: DT Code Type Value
: d1 RM1 A ###
: d1 RM2 A ###
: d1 RM3 ###
: d2 RM1 B ###
: d2 RM2 B ###
: d2 RM3 B ###

c**t
发帖数: 2744
3
too much union.. my solution:
CREATE OR REPLACE FORCE VIEW cogt_view
(DT, CODE, TYPES, VALUE) AS
WITH TMP AS
(SELECT DT, CODE, MAX(TYPE) over (PARTITION BY DT) as tmpType,
SUM( DECODE( TYPE, '', 0, 1) ) over (PARTITION BY DT) as tmpCnt
FROM TBL)
select DT, REPLACE(CODE, 'M', tmpType) as CODE, tmpType, VALUE from TMP
UNION ALL
select DT, REPLACE(CODE, 'M', decode(tmpType, 'A', 'B', 'A')) as CODE,
decode(tmpType, 'A', 'B', 'A') as TYPE, 0 from TMP

【在 B*****g 的大作中提到】
: 看到包子,先站个坑,这个解法挺烂。
: CREATE OR REPLACE FORCE VIEW beijing_test_view
: (DT, CODE, TYPES, VALUE) AS
: WITH TEMP_A AS
: (SELECT DISTINCT DT FROM beijing_test),
: TEMP_B AS
: (SELECT 'RA1' CODE, 'A' TYPES FROM DUAL
: UNION ALL
: SELECT 'RA2' CODE, 'A' TYPES FROM DUAL
: UNION ALL

1 (共1页)
进入Database版参与讨论
相关主题
A sql questionHow to write this query in Oracle?
Oracle 求助Help on Oracle Query
Help on migrating oracle to db2, outerjoint-sql chanllege 20
Help on SQL statement大家帮忙看看这个oracle statement, 怎么总是报错?
How to write this query跪求高人指点:一道SQL题3/5个包子可以么?先谢了!
Merge table with one single query?ask for help
问一个sql查询语句的问题How to concatenate NULL value with a string in SQL Server?
请问这种MYSQL的语法在SQL Server里面改成什么格式?MS SQL = or like?
相关话题的讨论汇总
话题: code话题: d1话题: dt话题: select话题: d2