Select 多行具有不同的列且具有条件
Select multiple rows having distinct columns with a condiction
我正在为一个查询而苦苦挣扎,我想 select 所有图书数据行由某些列区分。
我的table数据:
BookRef BookFloor BookSection Orders OrderType
------- --------- ----------- ------ ---------
4 7 2 null 1
4 7 2 null 3
4 7 2 null 2
4 7 2 8 2
4 1 5 null 3
对于一个 BookRef,在 BookFloor 中,在 BookSection 中,具有不同的 OrderType,我只想 select 有订单的行。如果没有命令,我想select只有一行。
我想要的输出:
BookRef BookFloor BookSection Orders OrderType
------- --------- ----------- ------ ---------
4 7 2 null 1
4 7 2 null 3
4 7 2 8 2
4 1 5 null 3
我正在尝试使用 HAVING
子句。但是它不起作用。我怎样才能执行我的代码来完成我需要的事情?
我的查询:
select BookRef, BookFloor, BookSection, Orders, OrderType
from #myTempTable
GROUP BY OrderType, Orders, BookSection, BookFloor, BookRef
having count(BookRef) = 1 and count(BookFloor) = 1 and (count(OrderType) = 1 or (count(OrderType) > 1 and count(Orders) = 1))
这是一个优先查询。一种方法是:
select t.*
from #temp t
where t.OrderType is not null
union all
select t.*
from #temp t
where t.OrderType is null and
not exists (select 1 from #temp t2 where t2.ordertype = t.ordertype and t2.bookref and t2.booksection = t.booksection and t2.bookfloor = t.bookfloor and t2.ordertype is null);
嗯,这也用 GROUP BY
:
select BookRef, BookFloor, BookSection, max(Orders) Orders, OrderType
from #myTempTable
where OrderType is not null -- May be Orders is not null
group by BookRef, BookFloor, BookSection, OrderType;
但是,max()
函数在这里是多余的,您可以直接在 GROUP BY
子句中使用它来实现 唯一性 。
我最终使用 WHERE
子句解决了我的问题。
select BookRef, BookFloor, BookSection, Orders, OrderType
from #myTempTable
where (select count(a.Id) from #myTempTable a
where a.BookRef = t.BookRef and a.BookFloor = t.BookFloor and a.BookSection = t.BookSection and a.OrderType = t.OrderType) = 1
or t.Orders is not null
但是我还不知道是否有更简单的方法可以对 HAVING
子句执行相同的操作。
尝试在分区
上访问用户row_number
SELECT * FROM (
SELECT * ,ROW_NUMBER()OVER(PARTITION BY BookRef,BookFloor,BookSection,OrderType ORDER BY orders) AS seq FROM MyTable
) AS t WHERE t.seq=1
我正在为一个查询而苦苦挣扎,我想 select 所有图书数据行由某些列区分。
我的table数据:
BookRef BookFloor BookSection Orders OrderType
------- --------- ----------- ------ ---------
4 7 2 null 1
4 7 2 null 3
4 7 2 null 2
4 7 2 8 2
4 1 5 null 3
对于一个 BookRef,在 BookFloor 中,在 BookSection 中,具有不同的 OrderType,我只想 select 有订单的行。如果没有命令,我想select只有一行。
我想要的输出:
BookRef BookFloor BookSection Orders OrderType
------- --------- ----------- ------ ---------
4 7 2 null 1
4 7 2 null 3
4 7 2 8 2
4 1 5 null 3
我正在尝试使用 HAVING
子句。但是它不起作用。我怎样才能执行我的代码来完成我需要的事情?
我的查询:
select BookRef, BookFloor, BookSection, Orders, OrderType
from #myTempTable
GROUP BY OrderType, Orders, BookSection, BookFloor, BookRef
having count(BookRef) = 1 and count(BookFloor) = 1 and (count(OrderType) = 1 or (count(OrderType) > 1 and count(Orders) = 1))
这是一个优先查询。一种方法是:
select t.*
from #temp t
where t.OrderType is not null
union all
select t.*
from #temp t
where t.OrderType is null and
not exists (select 1 from #temp t2 where t2.ordertype = t.ordertype and t2.bookref and t2.booksection = t.booksection and t2.bookfloor = t.bookfloor and t2.ordertype is null);
嗯,这也用 GROUP BY
:
select BookRef, BookFloor, BookSection, max(Orders) Orders, OrderType
from #myTempTable
where OrderType is not null -- May be Orders is not null
group by BookRef, BookFloor, BookSection, OrderType;
但是,max()
函数在这里是多余的,您可以直接在 GROUP BY
子句中使用它来实现 唯一性 。
我最终使用 WHERE
子句解决了我的问题。
select BookRef, BookFloor, BookSection, Orders, OrderType
from #myTempTable
where (select count(a.Id) from #myTempTable a
where a.BookRef = t.BookRef and a.BookFloor = t.BookFloor and a.BookSection = t.BookSection and a.OrderType = t.OrderType) = 1
or t.Orders is not null
但是我还不知道是否有更简单的方法可以对 HAVING
子句执行相同的操作。
尝试在分区
上访问用户row_numberSELECT * FROM (
SELECT * ,ROW_NUMBER()OVER(PARTITION BY BookRef,BookFloor,BookSection,OrderType ORDER BY orders) AS seq FROM MyTable
) AS t WHERE t.seq=1