返回SQL表中多个列上具有不同的所有列 [英] Return all columns with distinct on multiple columns in SQL table

查看:180
本文介绍了返回SQL表中多个列上具有不同的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sql表中有5列,我需要全部5列,但要对3列进行不同的操作

I have 5 columns in sql table and I need all the 5 columns as out put but with distinct operation on three columns

需要返回对三列进行不同操作的所有列

Need to return all the columns with distinct operation on three column

如何实现?

Timestamp                   Name    State     Context
2013-06-24 11:51:03.2550000 tst1    Started     E1
2013-06-24 11:56:03.2550000 tst1    Completed   E1
2013-06-24 11:51:03.2550000 tst1    Started     E1
2013-06-24 11:56:03.2550000 tst1    Completed   E1
2013-06-24 11:45:03.2550000 tst1    Started     E1
2013-06-24 11:50:03.2550000 tst1    Completed   E1
2013-06-24 11:45:03.2550000 tst1    Started     E1
2013-06-24 11:50:03.2550000 tst1    Completed   E1

在这里,我通过使用以下查询在三列上应用了distinct,从而获得了上表的所有distinct结果.但是我需要区分这三列,还需要选择上下文列而不在上下文列上应用Distict

Here I am getting all the distinct result for above table by applying distinct on three columns using below query. But I need the distinct of these three columns also need to select context column without applying distict on Context column

SELECT DISTINCT时间戳记,名称,状态(来自TableName) 时间戳记,名称,状态

SELECT DISTINCT Timestamp,Name,State FROM TableName group by Timestamp,Name,State

重述我的问题:

我需要从上表中选择唯一的列.在这里,只有unquie列选择被视为Timestamp,Name,State

I need to select unique columns from above table . Here only unquie column selection considered as Timestamp,Name,State

Timestamp                   Name    State     Context
2013-06-24 11:51:03.2550000 tst1    Started     E1
2013-06-24 11:56:03.2550000 tst1    Completed   E1
2013-06-24 11:45:03.2550000 tst1    Started E1
2013-06-24 11:50:03.2550000 tst1    Completed   E1

推荐答案

可以分组为3,但必须对其他两个使用诸如min或max之类的聚合函数

Can group by 3 but must use some type of aggregate function like min or max on the other two

select col1, col2, col3, max(col4), min(col5) 
from tbl 
group by col1, col2, col3

这篇关于返回SQL表中多个列上具有不同的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆