How select from another table without duplicates?

How select from another table without duplicates?



I have such query:


SELECT activity.login_time, sum(activity.sessions) FROM activity
inner JOIN payments ON payments.user_id = activity.user_id
WHERE activity.login_time BETWEEN '2018-04-05' and '2018-04-18'
GROUP BY activity.login_time;



I've got two tables (columns: activity (user_id, sessions, login_time) and payments(column: user_id). The second one has duplicate values.
Need to select sessions for user_id from "payments" without duplicates.



My variant doesn't avoid duplicates. What should I fix in my query?



Thanks !


activity
user_id login_time sessions
101 2018-04-11 4
102 2018-04-02 1
103 2018-04-15 3
104 2018-04-06 2
104 2018-04-05 6
105 2018-04-16 1
105 2018-04-04 6
105 2018-04-01 14
106 2018-04-19 5
106 2018-04-17 3
106 2018-04-16 3
106 2018-04-15 4
106 2018-04-13 2



Payments


user_id
101
103
104
106



output


login_time sessions
2018-04-05 ?
2018-04-06 ?
2018-04-07 ?
2018-04-08 ?
2018-04-09 ?
2018-04-10 ?
2018-04-11 ?
2018-04-12 ?
2018-04-13 ?
2018-04-14 ?
2018-04-15 ?
2018-04-16 ?
2018-04-17 ?
2018-04-18 ?





Provide an example of actual and desired output.
– NiVeR
Sep 2 '18 at 18:18





Especially how to tell duplicated values apart.
– orhtej2
Sep 2 '18 at 18:20





@NiVeR, updated
– Елисей Горьков
Sep 2 '18 at 18:27




1 Answer
1



It seems to me from your description you want user aggregation


SELECT
a.user_id,
SUM(a.sessions) as user_session
FROM
activity a
INNER JOIN
payments p ON p.user_id = a.user_id
WHERE
a.login_time BETWEEN '2018-04-05' AND '2018-04-18'
GROUP BY
a.user_id;



But if you want login date with sum of each user then use subquery


SELECT
a1.login_time, t1.user_session
FROM
activity a1
INNER JOIN
(SELECT
a.user_id,
AVG(a.sessions) AS user_session
FROM
activity a
INNER JOIN
payments p ON p.user_id = a.user_id
WHERE
a.login_time BETWEEN '2018-04-05' AND '2018-04-18'
GROUP BY
a.user_id) AS t1 ON a1.user_id = t1.user_id





hello! But I need two columns : date and average value
– Елисей Горьков
Sep 2 '18 at 18:28





@ЕлисейГорьков which date you need as user 105 login multiple time 2018-04-16,2018-04-01,2018-04-04
– Zaynul Abadin Tuhin
Sep 2 '18 at 18:30






as I know in table 'activity' every user is only one time per day.
– Елисей Горьков
Sep 2 '18 at 18:33





@ЕлисейГорьков can you show your output for userid 105 then i can change my query ,cause what you said in your question that covers already in my answer
– Zaynul Abadin Tuhin
Sep 2 '18 at 18:34






I updated output
– Елисей Горьков
Sep 2 '18 at 18:39



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)