0

I am new to SQL Server and need help with one of my SQL query.

I have 2 tables (Rating and LikeDislike).

I am trying to get data from both of these tables using a LEFT JOIN like this:

SELECT distinct LD.TopicID, R.ID, R.Topic, R.CountLikes, R.CountDisLikes, LD.UserName, LD.Clikes

FROM Rating As R

LEFT JOIN LikeDislike AS LD on LD.TopicID = R.ID

The above SELECT statement displays results fine but also includes duplicates. I want to remove duplicates when the data is displayed, I tried using DISTINCT and GROUP BY, but with no luck, maybe because I am not using it correctly.

To be more clear and less confusing let me tell you what exactly each table does and what I am trying to achieve.

The Rating table has following columns (ID, Topic, CountLikes, CountDisLikes, Extra, CreatedByUser). It stores topic information and number of likes and dislikes for each topics and the UserID of the user who created that topic.

Rating table with sample data

ID  Topic               CountLikes  CountDisLikes  Extra        CreatedByUser
1   Do You Like This       211          58          YesId                2
2   Or This                17           25          This also            3
79  Testing at home        1             0          Testing at home      2
80  Testing at home again  1             0          Testing              2
82  testing dislikes       0             1          Testing              2
76  Testing part 3         7             5          Testing 3            4
77  Testing part 4         16            6          Testing 4            5

The LikeDisLike table has following columns (ID, TopicID, UserName, Clikes). TopicID is a FK to the ID column in Rating table.

LikeDislike table with sample data

ID  TopicID UserName    Clikes
213     77      2       TRUE
214     76      2       FALSE
215     77      5       TRUE
194     77      3       TRUE
195     76      3       FALSE
196     2       3       TRUE
197     1       3       FALSE

Now what I am trying to do is get information from both of this table without duplicate rows. I need to get data all the columns from Rating table + UserName and Clikes columns from LikeDislike table without any duplicate rows

Below are the results with duplicates

TopicID ID  Topic            CountLikes   CountDislikes UserName    Clikes
NULL    79  Testing at home    1           0             NULL       NULL
NULL    80  Testing at home2   1           0             NULL       NULL 
NULL    82  testing dislikes   0           1             NULL       NULL
1       1   Do You Like This   211         58            3          FALSE
2       2   Or This            17          25            3          TRUE
76      76  Testing part 3     7           5             2          FALSE
76      76  Testing part 3     7           5             3          FALSE
77      77  Testing part 4     16          6             2          TRUE
77      77  Testing part 4     16          6             3          TRUE
77      77  Testing part 4     16          6             5          TRUE
4

1 回答 1

0

就像昨天的帖子一样,我认为您不了解 DISTINCT 应​​该返回您的内容。因为您的 LikeDislike 表中有不同的值,所以您将返回 DISTINCT 行。

我们以 TopicId 77 为例。它返回 3 DISTINCT 行,因为您的 LikeDislike 表中有 3 条匹配记录。如果您想要的输出是 UserName 和 Clikes 以逗号分隔的单行,那是可能的——考虑使用for xml,也许stuff这里是关于该主题的最新答案)。或者,如果您想返回与 TopicId 匹配的第一行,那么这也是可能的——考虑使用带有row_number.

请让我们知道您想要的输出,我们可以帮助提供解决方案。

祝你好运。

于 2013-01-18T15:58:28.063 回答