如何不显示 select 查询中的项目?

How to not display an item in select query?

我觉得问这个问题有点愚蠢,因为我觉得这很简单,但出于某种原因,我无法根据两个条件将查询更新为非 select 特定项目。

假设我有这样的数据:

 ID       Name       Variant     Count1
 110      Bob        Type1       0
 110      Bob        Type2       1
 120      John       Type1       1

如您所见,我们有两个 BOB 行具有相同的 ID 但不同的变体(type1 和 type2)。我希望只能看到 Bob 的其中一个。

想要的结果:

 110      Bob        Type2
 120      John       Type1

所以我一直在做的是

 Select ID, Name, Variant, sum(count1) from tbl1
 where (id not in (110) and Variant <> 'type1')
 Group by Id,name,variant

请不要使用 COUNT 作为标准,因为在我的示例中,对于我不想看到的行,恰好 Count=0。它可以变化。

我有很多行,其中我可以有多个具有各种不同 VARIANTS 的相同 ID 的实例。我希望根据变体值

排除某些 ID 实例

更新:

它与最新变体无关,它与 特定 变体有关。所以我只是希望基本上能够使用我使用 ID 和 VARIANT 的子句,以便删除该特定行。

像您所做的那样聚合(分组)数据是一种方法,尽管 where 条件有点矫枉过正。如果您只想查看 ID 和名称的唯一组合,那么另一种方法就是使用 "distinct" 语句。

select distinct Id, Name 
from tbl1

如果您总是想查看来自特定 Variant 的数据,那么只需将该条件包含在您的 where 子句中,您无需担心使用 distinct 或聚合。

select * 
from tbl1
where Variant = 'Type 1'

如果您总是想查看与最新变体关联的记录,那么您可以使用 window 函数来实现。

select a.Id, a.Name, a.Variant
from 
    (
        select *, row_number() over (partition by Id order by Variant desc) as RowRank
        from tbl1
    ) a
where RowRank = 1
;

如果没有用于排除的谓词table 模式,那么您将不得不维护一个排除列表。这并不理想,但如果您想在 SQL 本身中维护它,那么您可以进行如下查询。

select *
from tbl1
-- Define rows to exlcude
where not (Id = 110 and Variant = 'Type 1') -- Your example
and not (Id = 110 and Variant = 'Type 3') -- Theoretical example
;

更好的解决方案是创建排除引用 table 以维护其中的所有排除项。然后你可以简单地否定加入那个 table 来检索你想要的结果。

我认为你想要的逻辑是这样的:

Select ID, Name, Variant, sum(count1)
from tbl1
where not (id = 110 and variant = 'type1')
Group by Id, name, variant;

对于第二个条件,继续添加即可:

where not (id = 110 and variant = 'type1') and
      not (id = 314 and variant = 'popsicle')

您也可以使用排除列表来表达这一点:

select t.ID, Name, t.Variant, sum(t.count1)
from tbl1 t left join
     (values (111, 'type1'),
             (314, 'popsicle')
     ) v(id, excluded_variant)
     on t.id = v.id and
        t.variant = v.excluded_variant
where v.id is not null  -- doesn't match an exclusion criterion
group by Id, name, variant;

您是否考虑过使用排除 table 来放置要排除的 ID 和变体组合? (我只是在这个例子中使用了 temp tables,你总是可以使用 user tables 所以你的排除 table 将始终可用)

根据您的示例,这是我的意思的示例:

if object_id('tempdb..#temp') is not null
    drop table #temp

create table #temp (
     ID int,      
     Name  varchar(20),
     Variant varchar(20),
     Count1 int 
)

if object_id('tempdb..#tempExclude') is not null
    drop table #tempExclude

create table #tempExclude (
     ID int,      
     Variant varchar(20)

)

insert into #temp values 
    (110,'Bob','Type1',0),
    (110,'Bob','Type2',1),
    (120,'John','Type1',1),
    (120,'John','Type2',1),
    (120,'John','Type2',1),
    (120,'John','Type2',1),
    (120,'John','Type3',1)

insert into #tempExclude values (110,'Type1')

select
    t.ID,
    t.Name
    ,t.Variant
    ,sum(t.Count1) as TotalCount
from
    #temp t

    left join
    #tempExclude te
        on t.ID = te.ID
            and t.Variant = te.Variant
where
    te.id is null
group by
    t.ID,
    t.Name
    ,t.Variant

结果如下: