选择表中的多个列

时间:2017-01-24 03:38:28

标签: sql-server sql-server-2016

我想选择guid和uuid出现在内部表中,但SQL Server 2016中不允许使用以下语法,我应该怎么做?

select * 
from myTable
where (guid, uuid) in (select max(guid) as maxguid, convert(nvarchar(max), uuid) as uuid 
                       from myTable
                       group by convert(nvarchar(max), uuid) 
                      )

我见过其他答案,例如SQL WHERE.. IN clause multiple columns但是我的内部表中有一个group by语句,不知道如何使用它们。

2 个答案:

答案 0 :(得分:1)

将分组放在内部子查询中,并将“存在”用于多列

select * from myTable
where exists
(select 1
 from (select max(guid) as maxguid, convert(nvarchar(max), uuid) as uuid
       from myTable
       group by convert(nvarchar(max), uuid)) innerTable
 where myTable.guid = innerTable.maxguid
 and myTable.uuid = innerTable.uuid)

答案 1 :(得分:0)

您可以使用子查询和连接 -

select 
  myTable.* 
from 
  myTable join 
  (  
    select 
      max(guid) as maxguid, 
      convert(nvarchar(max), uuid) as uuid 
    from 
      myTable
    group by
      convert(nvarchar(max), uuid) 
  ) x
  on 
    x.maxguid = myTable.guid and 
    x.uuid = myTable.uuid