Sql 如何进行查询以查找此信息?
我在表格中有这样的数据:Sql 如何进行查询以查找此信息?,sql,oracle,Sql,Oracle,我在表格中有这样的数据: column1 column2 a 1 a 2 b 2 b 3 a 4 c 5 我想要这样的输出: column1 column2 a 1-2 b 2-3 a 4-0 c 5-0 你真的没有提供很多信息。 -对于第1列中的每个值,第2列中是否始终只有1或2个值? -第二栏是否总是有序的? -等等,等等 但是,对于给定的数
column1 column2
a 1
a 2
b 2
b 3
a 4
c 5
我想要这样的输出:
column1 column2
a 1-2
b 2-3
a 4-0
c 5-0
你真的没有提供很多信息。 -对于第1列中的每个值,第2列中是否始终只有1或2个值? -第二栏是否总是有序的? -等等,等等 但是,对于给定的数据,下面应该给出您要求的结果
SELECT
column1,
MIN(column2) AS first_column2,
CASE WHEN COUNT(*) = 1 THEN 0 ELSE MAX(column2) END AS final_column2
FROM
(
SELECT
ROW_NUMBER() OVER ( ORDER BY column2, column1) AS sequence_main,
ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column2 ) AS sequence_c1,
*
FROM
your_table
)
AS sequenced_table
GROUP BY
sequence_main - sequence_c1,
column1
ORDER BY
MIN(sequence_main)
计算示例:
column1 column2 | sequence_main sequence_c1 main - c1 | group
a 1 | 1 1 0 | a1
a 2 | 2 2 0 | a1
b 2 | 3 1 2 | b2
b 3 | 4 2 2 | b2
a 4 | 5 3 2 | a2
c 5 | 6 1 5 | c5
请尝试以下查询:
with vw1 as
(select table1.*,rownum rn from table1),
vw2 as (select col1,col2,rn,rn - col2 dis from vw1),
vw3 as (select col1,min(rn),to_char(min(col2))||' - '||
case when min(col2) = max(col2) then '0' else to_char(max(col2)) end col2 from vw2
group by col1,dis order by min(rn))
select col1,col2 from vw3;
with data_aggr as (
select column1,
case
when lead(column1,1,' ') over (order by id)<>column1
and lag(column1,1,' ') over (order by id)<>column1
then column2 || '-0'
when lead(column1,1,' ') over (order by id)=column1
and lag(column1,1,' ') over (order by id)<>column1
then column2 || '-' || lead(column2,1) over (order by id)
else null
end aggr_col2
from table1)
select column1, aggr_col2 from data_aggr where not aggr_col2 is null
请尝试此查询
select * from
(
select col1 as column1,case when LEAD(col1 , 1, 0) OVER (ORDER BY col2) = col1
then concat( LEAD(col2 , 1, 0) OVER (ORDER BY col2),'-'||col2)
else (case when lag(col1,1,0) over (ORDER BY col2) <> col1 then
concat(col2,'-'||'0')else '0' end)
end as column2
from table
order by col2
)
where column2<>'0'
;
你应该小心使用类似的东西
select column1, column2, rownum from table1
为数据创建一些唯一的ID。Per definition是SQL中未定义order by的排序。因此,巧合的是
select * from table1
返回您在数据库中插入的行的顺序。随着数据的增长,您将获得此排序的例外情况。因此,强烈建议在数据表中放置一个主键列,以保留此插入顺序。我包括了这个的列id
使用此pimped数据集,您可以使用以下查询获取请求的数据:
with vw1 as
(select table1.*,rownum rn from table1),
vw2 as (select col1,col2,rn,rn - col2 dis from vw1),
vw3 as (select col1,min(rn),to_char(min(col2))||' - '||
case when min(col2) = max(col2) then '0' else to_char(max(col2)) end col2 from vw2
group by col1,dis order by min(rn))
select col1,col2 from vw3;
with data_aggr as (
select column1,
case
when lead(column1,1,' ') over (order by id)<>column1
and lag(column1,1,' ') over (order by id)<>column1
then column2 || '-0'
when lead(column1,1,' ') over (order by id)=column1
and lag(column1,1,' ') over (order by id)<>column1
then column2 || '-' || lead(column2,1) over (order by id)
else null
end aggr_col2
from table1)
select column1, aggr_col2 from data_aggr where not aggr_col2 is null
你试过分组计数吗?你需要更详细地解释你想做什么。为什么我们有两个带“a”的结果行?几乎不错。您错过了一个子查询,因为在获取rownum之前必须对数据进行排序。不,您没有。以下是更正的查询:vw1作为select col1,col2,rownum rn from select test.COLLIN1 col1,test.COLLIN2,vw2作为select col1,col2,rn-col2 dis from vw1 select col1,以|charmincol2 | |-| |为例,当mincol2=maxcol2时,则“0”否则以|charmincol2结束col2 col2,以col1,dis结束vw2组中的col2;sqlfiddle在MySQL下工作,我想MySQL的解决方案会有点不同。关键是,在获取rownum之前,您的查询不会对数据进行排序,通常是错误的。在问题的数据集上-这是可以的,但是尝试添加更多的数据,您将看到什么是错误的。。试试这个数据集'a',1'a',2'b',2'b',3'a',4'a',5'a',6'c',5;您将看到您的查询出了什么问题。对不起,没有看到oracle选项:给您:您可以在同一数据集上尝试您的查询,并查看差异您很受欢迎,我的朋友,如果您查看问题,您将看到您答案中的行与问题结果中显示的行不同。