简体   繁体   English

在where子句中选择与众不同

[英]Selecting distinct on where clause

I am using following query to select certain values 我正在使用以下查询来选择某些值

select 
    sb.company
    ,b.id  as Id
    ,bds.id as PId
    ,brp.bId
from supp b
left outer join sales bds on (bds.sId = b.id)
left outer join tperiod brp on (brp.id = bds.rId)
left outer join tbuyer sb on (sb.id = brp.bId)
where  
    b.ownerId = @oId;

In where b.ownerId = @old ownerId has multiple same values in the column, I want to select distinct or unique on it. where b.ownerId = @old ownerId列中具有多个相同值的情况下,我想在其上选择不同的或唯一的。 So that query is done on distinct ownerId only. 因此,该查询仅在不同的ownerId上完成。

Please let me know how to achieve this. 请让我知道如何实现这一目标。

For example : column ownerId has values 例如:列ownerId具有值

2231
2231
2231
3341
2231

So I want the query to use only 2231 once rather than for all occurrences of 2231 因此,我希望查询仅使用2231次,而不是所有2231次出现

You need to user GROUP BY on the ownerId, but then you will need to use some aggregate function on all the other fields in the select statment 您需要在ownerId上使用GROUP BY,但是随后您将需要在select语句中的所有其他字段上使用一些聚合函数

If you want a unique ownerId and the other related fields are different, SQL needs to know what to display. 如果您希望唯一的ownerId且其他相关字段不同,则SQL需要知道要显示的内容。 Do you just want the MAX value on the related fields? 您是否只想要相关字段上的MAX值? You are asking for a unique value for the owner, so what do you just want to display if the joined fields have different values for that ownerId? 您正在要求所有者的唯一值,因此,如果联接的字段对该所有者ID具有不同的值,您只想显示什么?

This is something that could work, depending on what you want. 根据您的需要,这可能会起作用。

select 
 MAX(sb.company)
,MAX(b.id)  as Id
,MAX(bds.id) as PId
,MAX(brp.bId)
from supp b
  left outer join sales bds on (bds.sId = b.id)
  left outer join tperiod brp on (brp.id = bds.rId)
  left outer join tbuyer sb on (sb.id = brp.bId)
where  
  b.ownerId = @oId
GROUP BY b.ownerId

Since you want to select the values based on Owner ID uniqueness, you have use group by clause and give "distinct" in select clause like 由于要基于所有者ID唯一性选择值,因此可以使用group by子句,并在select子句中输入“ distinct”,例如

select distinct
   sb.company
   ,b.id  as Id
   ,bds.id as PId
   ,brp.bId
from supp b
left outer join sales bds on (bds.sId = b.id)
left outer join tperiod brp on (brp.id = bds.rId)
left outer join tbuyer sb on (sb.id = brp.bId)
where  b.ownerId = @oId
group by b.ownerId

This is the simple way…………. 这是简单的方法………​​…。 get the @oid inside the table Input_table and join with the table Supp. 在表Input_table中获取@oid并与表Supp联接。

select sb.company ,b.id as Id ,bds.id as PId ,brp.bId from 从中选择sb.company,b.id作为Id,bds.id作为PId,brp.bId

(select max(b.ownerId) ownerId from supp where b.ownerId = @oId;) Input_table (从supp中选择max(b.ownerId)ownerId,其中b.ownerId = @oId;)Input_table

Inner Join supp b on temp_tab.ownerId = b.ownerId left outer join sales bds on (bds.sId = b.id) left outer join tperiod brp on (brp.id = bds.rId) left outer join tbuyer sb on (sb.id = brp.bId) temp_tab.ownerId = b.ownerId上的内部联接支持b左(bds.sId = b.id)上的外部联接销售bds上(brp.id = bds.rId)左外部联接tbriod左上(sb .id = brp.bId)

Sorry for bad format ! 对不起,格式不好!

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

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