Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to make one column distinct in inner join three tables
I need to make videoName distinct only not other columns
how can I write the query

please help

What I have tried:

SElECT distinct t1.MID, distinct t1.VideoName, t2.*,t3.UniID,t3.user_type from dbo.Material AS t1 INNER JOIN dbo.ModuleRelation As t2 ON t2.MID=t1.MID INNER JOIN dbo.Users AS t3 ON t3.UniID=t2.UId where t3.user_type='Staff' and t1.IsDeleted = 0 AND t2.MID='CM1034D';

and this
SElECT distinct t1.MID, t4.VideoName, t2.*,t3.UniID,t3.user_type from dbo.Material AS t1 INNER JOIN dbo.ModuleRelation As t2 ON t2.MID=t1.MID INNER JOIN dbo.Users AS t3 ON t3.UniID=t2.UId
INNER JOIN (select max(MID) as MID,videoname from dbo.Material group by Videoname) AS t4 ON t1.MID=t4.MId
where t3.user_type='Staff' and t1.IsDeleted = 0 AND t2.MID='CM1034D';

but video name still duplicated
Posted
Updated 25-Aug-16 0:56am
v2
Comments
Maciej Los 17-Aug-16 5:52am    
What is input and expected output?
Member 12618369 17-Aug-16 6:00am    
what do mean bu input the output is values of selected columns but one column has repeated value I need to make it distinct
Aless Alessio 17-Aug-16 11:57am    
first: for god's sake, use a Sql Formatter http://www.dpriver.com/pp/sqlformat.htm your SQL is unreadable.
second: you need to tell us the tables structure and the relations between them, otherwise how can we know how to write the query?
Member 12618369 17-Aug-16 13:04pm    
the structure of tables are
Materials[id, videoName,MID(forign key),UID,ISPosted]
ModuleRelation[id, Uid(foring key),MID(forign key)]
users[Uid,user_type]
please help

Distinct in such condition will work when only one column i.e. 'VideoName' is selected. If distinct 'VideoName' is required along with other columns then it will bring distinct combination of all the columns instead of distinct of one column.
 
Share this answer
 
Use group by Instead of Distinct:_
SQL
SElECT t1.MID, t1.VideoName, t2.*,t3.UniID,t3.user_type from dbo.Material AS t1 INNER JOIN dbo.ModuleRelation As t2 ON t2.MID=t1.MID INNER JOIN dbo.Users AS t3 ON t3.UniID=t2.UId where t3.user_type='Staff' and t1.IsDeleted = 0 AND t2.MID='CM1034D' Group by t1.VideoName; 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900