【问题标题】:expression of non-boolean type非布尔类型的表达式
【发布时间】:2011-05-25 12:20:08
【问题描述】:

使用这个语句:

select * from qvalues where rowid ,name,compound in (
    select rowid,name,compound from qvalues where rowid in (select rowid from batchinfo where instrument='tf1')
    group by rowid,name,compound
    having COUNT(*)>1 
    )
    group by rowid,name,compound
having rid=min(rid)

出现此错误:

消息 4145,第 15 级,状态 1,第 3 行 在预期条件的上下文中指定的非布尔类型的表达式,靠近“,”。 消息 156,第 15 级,状态 1,第 8 行 关键字“组”附近的语法不正确。

这个 SQL 语句有什么问题?我需要在表having min(rid) 中找到这三个字段的所有匹配项。

更新 使用此查询,外部选择不起作用。我做错了什么?

select * from qvalues where rid not in (
select q.rowid, q.name, q.compound, min(q.rid)
    from qvalues q
        inner join batchinfo b
            on q.rowid = b.rowid
                and b.instrument = 'tf1'
    group by q.rowid, q.name, q.compound
    having count(*) > 1)

【问题讨论】:

  • 你说的SQL Server? 8 号线在哪里?

标签: sql sql-server tsql sql-server-2008


【解决方案1】:

我认为这与您想要实现的目标相同。

select min(q.rid)
    from qvalues q
        inner join batchinfo b
            on q.rowid = b.rowid
                and b.instrument = 'tf1'
    group by q.rowid, q.name, q.compound
    having count(*) > 1

【讨论】:

  • @joe 非常感谢您的帮助。我又增加了一点复杂性,你能帮忙吗?
  • 如果给定 qvalues 行有多个 batchinfo 行,那么您将获得多行 bollixing u phave count(*)
  • @herrow:如果你要说where rid not in (...,那么内部查询应该修改为只返回一列min(q.rid)
  • @joe 非常感谢你,我将如何从系统中删除所有不是 min(q.rid) 的 RID,相反,我将如何只保留 max(rid)?
【解决方案2】:

最后的having min(rid)需要比较

也就是说,如果你重新排列你的代码 sn-p,你可以看到没有关联的 GROUP BY

select * from qvalues
where rowid ,name,compound in (
               select rowid,name,compound from qvalues
               where rowid in (select rowid from batchinfo where instrument='tf1')
               group by rowid,name,compound
               having COUNT(*)>1 
               )
--missing group by
having min(rid) -- > foo

编辑:重新制定:

select
   *
from
    (
    select
       rowid,name,compound, min(q.rid) as minrid
    from
       qvalues q
    where
       EXISTS (SELECT * FROM batchinfo b where b.instrument='tf1' AND b.rowid = q.rowid)
    group by
        rowid,name,compound
    having
        COUNT(*)>1
    ) foo
    JOIN
    qvalues q2 On foo.rowid = q.rowid AND foo.name = q.name AND foo.compound = q.compound
                   AND foo.minrid = q2.rid

而你原来的错误可能是因为派生表缺少别名,再加上SQL Server不支持多列IN。我使用 EXISTS not JOIN 因为您可能从 qvalues JOIN batchinfo 中获得比您预期的更多的行(基于 Joe 的回答)

【讨论】:

  • @gbn 消息 102,级别 15,状态 1,第 16 行 'foo' 附近的语法不正确。
  • @herrow: 我在 EXISTS 子句中有一个 )
  • @gbn 消息 4104,级别 16,状态 1,第 18 行 无法绑定多部分标识符“q.rowid”。消息 4104,级别 16,状态 1,第 18 行 无法绑定多部分标识符“q.name”。消息 4104,级别 16,状态 1,第 18 行无法绑定多部分标识符“q.compound”。
  • @herrow:最后一次 JOIN 中的别名错误。应该是“q2”。您可以自己思考:我无法测试您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 2013-10-03
相关资源
最近更新 更多