如何使用sql获得所需的输出

如何使用sql获得所需的输出,sql,sql-server,select,Sql,Sql Server,Select,我的DB表如下所示 IDCol Col1 Col2 Col3 ID1 1 3 1000 ID1 6 6 1000 ID2 3 4 500 ID2 1 7 500 我需要如下输出 IDCol Col1 Col2 Col3 ID1 1 6 1000 ID2 3 7

我的DB表如下所示

IDCol    Col1    Col2    Col3
ID1      1       3       1000
ID1      6       6       1000
ID2      3       4       500
ID2      1       7       500
我需要如下输出

IDCol    Col1    Col2    Col3
ID1      1       6       1000
ID2      3       7       500
是否有可能使用SQL获得上述所需的输出格式?
我是SQL的新手,有人能帮我吗?多谢各位

根据您的样本数据,我相信这就是您想要的:

SELECT IDCol
    , MIN(Col1)    AS [Col1]
    , MAX(Col2)    AS [Col2]
    , Col3
FROM Table
GROUP BY IDCol
    , Col3
根据您的评论中的信息,如果您添加了一个行号列,那么其中一个解决方案将采用以下形式:

select t1.idcol
    , t1.col1
    , t2.col2
    , t1.col3
from 
(select idcol
    , min(rn) over (partition by idcol) first_val
    , max(rn) over (partition by idcol) last_val
from table
group by idcol, rn) r
    inner join table t1 on r.first_val = t1.rn
        and r.idcol = t1.idcol
    inner join table t2 on r.last_val = t2.rn
        and r.idcol = t2.idcol
group by t1.idcol
    , t1.col1
    , t2.col2
    , t1.col3
在示例数据上运行的示例代码:

declare @tbl table (rn int identity(1,1), idcol varchar(4), col1 int, col2 int, col3 int)
insert @tbl values ('id1', 1, 3, 1000)
    , ('id1', 6, 6, 1000)
    , ('id2', 3, 4, 500)
    , ('id2', 1, 7, 500);


select t1.idcol
    , t1.col1
    , t2.col2
    , t1.col3
from 
(select idcol
    , min(rn) over (partition by idcol) first_val
    , max(rn) over (partition by idcol) last_val
from @tbl
group by idcol, rn) r
    inner join @tbl t1 on r.first_val = t1.rn
        and r.idcol = t1.idcol
    inner join @tbl t2 on r.last_val = t2.rn
        and r.idcol = t2.idcol
group by t1.idcol
    , t1.col1
    , t2.col2
    , t1.col3

我认为您必须指定此任务的抽象级别。您可以使用许多语句来实现此输出。问题是数据行代表什么以及为什么需要预期输出。

使用
LEAD尝试的另一种方法(从2012年起支持):


你的目标是什么?我看不出以您要求的格式返回数据背后的逻辑。看起来您需要一个groupBy您的IDCOLExplainlogic@mihaiID1有多行。我需要col1的第一个数据和col2的最后一个数据。一起形成所需的输出[如上所示]。最后一个和第一个基于什么条件?没有表的顺序是无序的。@pree欢迎使用SO。。无论那里的人们多么善良,他们都尽力回答你的问题。。如果你能揭露出你想要的解决方案背后的动机,那就太好了。谢谢你的回答。很抱歉,我没有指定Col1和Col2并不总是绑定到min或max。例如,ID1的第二行可以是ID1、6、4。所以此解决方案不起作用,对吗?@Pree您需要提供有关如何拾取这些行或值的详细信息。@Pree。这是你所问问题的正确答案。如果你还有其他问题,那么请用更合适的数据和规则作为另一个问题提问。人们已经做出努力来回答这个问题,这一点比较清楚。改变问题会吸引反对票。我的数据矩阵很大,包含大约300万行。由于数据保密性,我只介绍了它的简化版本。谢谢通过这篇文章,我的意思是你可以提供许多(甚至几乎是硬编码的)公式来提供这个输出(比如按值选择或其他)。由此看来,每个答案都是故意错误的,因为我们不知道作者的意图。当我发表评论时,只提供了uotput的形状,没有有用的信息。
declare @t1 table
(
IDCol char(5),
Col1  INT,
Col2  INT,
Col3 INT
)

Insert into @t1 values ('ID1',      3 ,      3 ,      1000);
Insert into @t1 values ('ID1',      6 ,      6 ,      1000);
Insert into @t1 values ('ID1',      6 ,      4 ,      1000);
Insert into @t1 values ('ID2',      11 ,      3 ,      1000);
Insert into @t1 values ('ID2',      6 ,      6 ,      1000);

Select * from
(
    Select t1.IDCol, t1.Col1,
    Lead(t1.Col2) Over (Partition by t1.IDCol Order By t1.IDCol) Col2,
    t1.Col3 From
    (
        Select IDCol, Col1, Col2, Col3,
        ROW_NUMBER() over(partition by IDCol order by IDCol) as RowFlg
        from @t1
    ) t1,
    (
        Select IDCol, Min(RowFlg) Col1MinVal, Max(RowFlg) Col2MaxVal from
        (
            Select IDCol, ROW_NUMBER() over(partition by IDCol order by IDCol) as RowFlg from @t1
        ) z
        Group By IDCol
    ) x
    Where t1.IDCol = x.IDCol AND (t1.RowFlg = x.Col1MinVal OR t1.RowFlg = x.Col2MaxVal)
) main
Where Col2 is not null