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. So that query is done on distinct ownerId only.
Please let me know how to achieve this.
For example : column ownerId
has values
2231
2231
2231
3341
2231
So I want the query to use only 2231 once rather than for all occurrences of 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
If you want a unique ownerId and the other related fields are different, SQL needs to know what to display. Do you just want the MAX value on the related fields? 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?
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
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.
select sb.company ,b.id as Id ,bds.id as PId ,brp.bId from
(select max(b.ownerId) ownerId from supp where 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)
Sorry for bad format !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.