选择一列 DISTINCT SQL

Select one column DISTINCT SQL(选择一列 DISTINCT SQL)
本文介绍了选择一列 DISTINCT SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

添加:使用 SQL Server 2000 和 2005,因此必须同时处理这两个版本.另外,value_rk 不是数字/整数(错误:操作数数据类型 uniqueidentifier 对 min 运算符无效)

Added: Working with SQL Server 2000 and 2005, so has to work on both. Also, value_rk is not a number/integer (Error: Operand data type uniqueidentifier is invalid for min operator)

当我不关心返回的其他列时,有没有办法进行单列DISTINCT"匹配?示例:

Is there a way to do a single column "DISTINCT" match when I don't care about the other columns returned? Example:

**Table**
Value A, Value L, Value P
Value A, Value Q, Value Z

我只需要根据第一行(值 A)中的内容返回这些行中的一个.我仍然需要第二列和第三列的结果(无论如何,第二列实际上应该完全匹配,但第三列是唯一键,我至少需要其中一个).

I need to return only one of these rows based on what is in the first one (Value A). I still need results from the second and third columns (the second should actually match all across the board anyway, but the third is a unique key, which I need at least one of).

这是我到目前为止所得到的,虽然它显然不起作用:

Here's what I've got so far, although it doesn't work obviously:

SELECT value, attribute_definition_id, value_rk
FROM attribute_values
WHERE value IN (
    SELECT value, max(value_rk)
    FROM attribute_values
)
ORDER BY attribute_definition_id

我在 ColdFusion 工作,所以如果有一个简单的解决方法,我也愿意接受.我试图限制或分组"第一列值".value_rk 是我的大问题,因为每个值都是唯一的,但我只需要一个.

I'm working in ColdFusion so if there's a simple workaround in that I'm open to that as well. I'm trying to limit or "group by" the first column "value". value_rk is my big problem since every value is unique but I only need one.

注意:value_rk 不是数字,因此这不起作用

NOTE: value_rk is not a number, hence this DOES NOT WORK

更新:我有一个工作版本,它可能比纯 SQL 版本慢很多,但老实说,在这一点上任何工作都比没有好.它从第一个查询中获取结果,执行第二个查询,除了将结果限制为一个,并为匹配的值获取匹配的 value_rk.像这样:

UPDATE: I've got a working version, it's probably quite a bit slower than a pure SQL version, but honestly anything working at this point is better than nothing. It takes the results from the first query, does a second query except limiting it's results to one, and grabs a matching value_rk for the value that matches. Like so:

<cfquery name="queryBaseValues" datasource="XXX" timeout="999">
    SELECT DISTINCT value, attribute_definition_id
    FROM attribute_values
    ORDER BY attribute_definition_id
</cfquery>

<cfoutput query="queryBaseValues">
    <cfquery name="queryRKValue" datasource="XXX">
        SELECT TOP 1 value_rk
        FROM attribute_values
        WHERE value = '#queryBaseValues.value#'
    </cfquery>
    <cfset resourceKey = queryRKValue.value_rk>
    ...

这样就完成了,在 ColdFusion 中明确地选择了一个列.仍然非常欢迎任何纯 SQL Server 2000/2005 建议:)

So there you have it, selecting a single column distinctly in ColdFusion. Any pure SQL Server 2000/2005 suggestions are still very welcome :)

推荐答案

这可能有效:

SELECT DISTINCT a.value, a.attribute_definition_id, 
  (SELECT TOP 1 value_rk FROM attribute_values WHERE value = a.value) as value_rk
FROM attribute_values as a
ORDER BY attribute_definition_id

.. 未测试.

这篇关于选择一列 DISTINCT SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)