由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - Help on Oracle Query
相关主题
Re: Recordset - I stuck! Help!How to write this query in Oracle?
怎么写这个Query,谢谢Help on Oracle Query
怎么写个query 把输出变成横排.user_objects 与CAT view
问一个简单的Query求工具
[转载] Can anyone interpret this simple SQL?How to write this query
A sql question怎么用sql query 实现这个功能?
query questions誰來幫我開來開悄? Interesting SQL query
Need help, about select count(xyz) from ...包子请教query
相关话题的讨论汇总
话题: null话题: cusipcol话题: valuecol话题: idcol话题: query
进入Database版参与讨论
1 (共1页)
b******y
发帖数: 139
1
I have a recordset ordered by CusipCol, IDCol like this:
CusipCol IDCol ValueCol
1C 1 A
2C 2 B
3C 1 A
3C 1 Null
4C 1 A
4C 2 Null
5C 1 A
5C 2 B
6C 1 Null
6C 2 B
7C 1 Null
7C 2 Null
I need write a oracle query to get the fol
c***c
发帖数: 6234
2
我觉得纯query是没法做到的。因为纯query没法用if判断。
而且没法知道是第一和第二。
用stored procedure试试把,也得建立temp table或者返回3个arrays,就是CusipCol
,IDCol, ValueCol各在一个array里面

【在 b******y 的大作中提到】
: I have a recordset ordered by CusipCol, IDCol like this:
: CusipCol IDCol ValueCol
: 1C 1 A
: 2C 2 B
: 3C 1 A
: 3C 1 Null
: 4C 1 A
: 4C 2 Null
: 5C 1 A
: 5C 2 B

c**t
发帖数: 2744
3
不要轻易说没办法。我觉给你一个纯query吧:
with TEMP as (
select CusipCol, IDCol, ValueCol,
row_number() over ( partition by CusipCol order by CusipCol) as RN,
count(CusipCol) over (partition by CusipCol order by CusipCol) as CNT
from test)
select CUSIPCOL, IDCOL, VALUECOL from TEMP
where CNT=1
UNION ALL
select CUSIPCOL, IDCOL, VALUECOL from TEMP WHERE CNT>1 and RN=1 and VAlueCol
is NOT NULL
UNION ALL
select A.CUSIPCOL,
decode(a.VALUECOL, null, decode(B.ValueCol, null, A.IDCOL, B.IDCol),
a.IDCOL) as IDCOL,

【在 c***c 的大作中提到】
: 我觉得纯query是没法做到的。因为纯query没法用if判断。
: 而且没法知道是第一和第二。
: 用stored procedure试试把,也得建立temp table或者返回3个arrays,就是CusipCol
: ,IDCol, ValueCol各在一个array里面

c**t
发帖数: 2744
4
You may use LAG to simplify the query

VAlueCol

【在 c**t 的大作中提到】
: 不要轻易说没办法。我觉给你一个纯query吧:
: with TEMP as (
: select CusipCol, IDCol, ValueCol,
: row_number() over ( partition by CusipCol order by CusipCol) as RN,
: count(CusipCol) over (partition by CusipCol order by CusipCol) as CNT
: from test)
: select CUSIPCOL, IDCOL, VALUECOL from TEMP
: where CNT=1
: UNION ALL
: select CUSIPCOL, IDCOL, VALUECOL from TEMP WHERE CNT>1 and RN=1 and VAlueCol

s******s
发帖数: 508
5
select
CusipCol
, case when ValueCol is null and nextValue is not null then nextID else
IDCol end IDCol
, case when ValueCol is null and nextValue is not null then nextValue else
ValueCol end ValueCol
from
(
select CusipCol, IDCol, ValueCol
,lead(ValueCol) over (partition by CusipCol order by IDCol) NextValue
,lead(ValueCol) over (partition by CusipCol order by IDCol) NextID
,row_number() over (partition by CusipCol order by IDCol) rownum
from mytable
) x
where rownum =
c**t
发帖数: 2744
6
a typo: the 2nd lead should be lead(IDCol)

NextValue

【在 s******s 的大作中提到】
: select
: CusipCol
: , case when ValueCol is null and nextValue is not null then nextID else
: IDCol end IDCol
: , case when ValueCol is null and nextValue is not null then nextValue else
: ValueCol end ValueCol
: from
: (
: select CusipCol, IDCol, ValueCol
: ,lead(ValueCol) over (partition by CusipCol order by IDCol) NextValue

b******y
发帖数: 139
7
Yes, this works well.
Thumb up!

NextValue

【在 s******s 的大作中提到】
: select
: CusipCol
: , case when ValueCol is null and nextValue is not null then nextID else
: IDCol end IDCol
: , case when ValueCol is null and nextValue is not null then nextValue else
: ValueCol end ValueCol
: from
: (
: select CusipCol, IDCol, ValueCol
: ,lead(ValueCol) over (partition by CusipCol order by IDCol) NextValue

b******y
发帖数: 139
8
You guys are really good.
Thanks!

VAlueCol

【在 c**t 的大作中提到】
: 不要轻易说没办法。我觉给你一个纯query吧:
: with TEMP as (
: select CusipCol, IDCol, ValueCol,
: row_number() over ( partition by CusipCol order by CusipCol) as RN,
: count(CusipCol) over (partition by CusipCol order by CusipCol) as CNT
: from test)
: select CUSIPCOL, IDCOL, VALUECOL from TEMP
: where CNT=1
: UNION ALL
: select CUSIPCOL, IDCOL, VALUECOL from TEMP WHERE CNT>1 and RN=1 and VAlueCol

n*****2
发帖数: 35
9
SELECT A.CUSIPCOL, B.IDCOL, B.VALUECOL
FROM ( SELECT CUSIPCOL,
MIN (DECODE (VALUECOL, NULL, 'ZZZZZZ', VALUECOL)) AS
ND
FROM YOUR_TABLE_NAME
GROUP BY CUSIPCOL) A,
YOUR_TABLE_NAME B
WHERE A.ND = DECODE (B.VALUECOL, NULL, 'ZZZZZZ', VALUECOL)
AND A.CUSIPCOL = B.CUSIPCOL
ORDER BY A.CUSIPCOL, B.IDCOL

【在 b******y 的大作中提到】
: I have a recordset ordered by CusipCol, IDCol like this:
: CusipCol IDCol ValueCol
: 1C 1 A
: 2C 2 B
: 3C 1 A
: 3C 1 Null
: 4C 1 A
: 4C 2 Null
: 5C 1 A
: 5C 2 B

1 (共1页)
进入Database版参与讨论
相关主题
包子请教query[转载] Can anyone interpret this simple SQL?
请帮我看看,什么地方错了?A sql question
急问Access Query问题:怎样查询最后一个非空的数值,谢谢query questions
[Mysql] how to return NULL count in group by query (转载)Need help, about select count(xyz) from ...
Re: Recordset - I stuck! Help!How to write this query in Oracle?
怎么写这个Query,谢谢Help on Oracle Query
怎么写个query 把输出变成横排.user_objects 与CAT view
问一个简单的Query求工具
相关话题的讨论汇总
话题: null话题: cusipcol话题: valuecol话题: idcol话题: query