Mohamad Simo

Mohamad Simo

  • NA
  • 14
  • 8.7k

DISTINCT into SQL statement is not working very well

Apr 23 2016 6:56 AM

Hi

The below code is working fine with one problem that is shown many records where it suppose to show only one. (for example: if a user from UserInfo table has 10 ads into ads table where 5 of those ads has value similar to Query String result value, so he repeat the user 5 times where it suppose to appear once whatever he have ads where Wtag column.)

i make quick screen record explaining the issue hope that will be more clear to understand the issue, please have a look

https://www.youtube.com/watch?v=lh2GY2_whVM&feature=youtu.be

 
  1. SELECT DISTINCT UI.[UID]  
  2. ,UI.[Country]  
  3. ,UI.[State]  
  4. ,UI.[City]  
  5. ,UI.[Logo]  
  6. ,UI.[Website]  
  7. ,UI.[UsrType]  
  8. ,SUBSTRING(UI.[CompDesc], 1, 60) AS CompDesc  
  9. ,UI.[BizCateg]  
  10. ,UI.[BizSubCateg]  
  11. ,UI.[Twitter]  
  12. ,UI.[GooglePlus]  
  13. ,UI.[Facebook]  
  14. ,UI.[CompNme]  
  15. ,UI.[RegDate]  
  16.   
  17. ,AD.[Wtags]   
  18. FROM UserInfo AS UI  
  19. JOIN ads AS AD ON AD.[UID] = UI.[UID]  
  20. WHERE UI.[Country] = @Location AND UI.[UsrType] = 'Business'   
  21. AND CHARINDEX(@Wtag, AD.[Wtags])> 0 ORDER BY UI.[RegDate] DESC  

Answers (6)