简体   繁体   English

优先选择一行作为值

[英]Select one row with priority for value

Ello! ELLO!

I'm trying to select a single row with either 'w', 'e' or 's' and an unique GUID. 我正在尝试选择具有“ w”,“ e”或“ s”和唯一GUID的一行。 'e' has a the highest priority so when a GUID contains that that line should be selected. “ e”具有最高优先级,因此当GUID包含该行时,应选择该行。 'w' the 2nd highest priority, so when there is no other than 's' 'w' should be selected. “ w”是第二高的优先级,因此当没有其他选项时,应选择“ w”。 Along with the entire row data. 连同整个行数据。

I've tried to use the following code: 我尝试使用以下代码:

SELECT * FROM TABLENAME 
WHERE GUID IN (SELECT DISTINCT GUID FROM TABLENAME ) 
AND (CATEGORY='S' OR CATEGORY='E' OR CATEGORY='W') 

Is there anyway how I can do what I'm describing? 无论如何,我该怎么做? I've been trying to use different ways of using distinct and other methods but it's still giving me more than one line per GUID. 我一直在尝试使用不同的方法来使用不同的方法和其他方法,但是每个GUID仍然给我提供了不止一行的信息。

Group by hasn't worked either. 分组依据也无效。 Since I get an error when I add that. 由于添加时出现错误。

EDIT: Added Images http://i.stack.imgur.com/3zgeF.png 编辑:添加了图像 http://i.stack.imgur.com/3zgeF.png

select guid, min(priority) from
(SELECT *, 
 case when category = 'E' then 1
      when category = 'W' then 2
      when category = 'S' then 3 
 end as priority 
FROM TABLENAME 
WHERE CATEGORY in ('S','E','W') ) a

The inner query assigns priority to the category column as per the question. 内部查询根据问题将优先级分配给category列。 In the outer query you select from whichever category has the highest priority. 在外部查询中,从优先级最高的类别中进行选择。 Also include all the other columns you need in the outer select . 还要在外部select包括您需要的所有其他列。

Edit: The method above isn't robust as you can't use the calculated priority column to select the corresponding category per guid. 编辑:上面的方法不是很可靠,因为您不能使用计算出的priority列来为每个GUI选择相应的类别。 Hence, Another approach is to use union . 因此,另一种方法是使用union

SELECT * FROM TABLENAME 
WHERE CATEGORY = 'E'
union
SELECT * FROM TABLENAME 
WHERE CATEGORY = 'W' 
and guid not in (select guid from tablename where category ='E')
union
SELECT * FROM TABLENAME 
WHERE CATEGORY = 'S'
and guid not in(select guid from tablename where category in ('E','W'))

The first query selects all rows where category is E . 第一个查询选择category为E所有行。 The second query selects all rows whose category is W and whose guid wasn't selected before. 第二个查询选择类别为W且之前未选择其guid的所有行。 The third query selects all rows whose category is S and whose guid wasn't selected in the previous two queries. 第三个查询选择类别为S并且在前两个查询中未选择其guid的所有行。 UNION ing all the queries should give you the results you need. 对所有查询进行UNION都应为您提供所需的结果。 Try the SQL fiddle with dummy data. 尝试对虚拟数据使用SQL提琴。 http://sqlfiddle.com/#!9/07c0d/1 http://sqlfiddle.com/#!9/07c0d/1

One method is to get the appropriate category for each guid and then join back. 一种方法是获取每个GUID的适当类别,然后重新join Another is a series of union all s. 另一个是一系列并union all The latter is possibly more efficient, because you don't have very many options to prioritize: 后者可能会更有效率,因为您没有太多的优先级选择:

select t.*
from tablename t
where category = 'e'
union all
select t.*
from tablename t
where category = 'w' and
      not exists (select 1 from tablename t2
                  where t2.category in ('e') and t2.guid  = t.guid
                 ) 
union all
select t.*
from tablename t
where category = 's' and
      not exists (select 1 from tablename t2
                  where t2.category in ('e', 'w') and t2.guid  = t.guid
                 ) ;

For performance, you want an index on category and guid, category . 为了提高性能,您需要在categoryguid, category上建立索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM