I've trouble writing select where I can get multiple Counts for every unique row, I've searched whole web and wrote select but it's not working the way I want. So, I have two tables banners and bannerhistory
banners
Id UserId BannerName
167 35 Polly
168 35 Joke
169 21 Kim
170 35 Buck
................
BannerHistory
BannerId UserId IP
167 35 123asd123
167 35 123asd123
168 35 BBB123sss
.......................
So my query is:
SELECT
banners.Banner_Name,
banners.Counter,
banners.ClickCounter,
COUNT(DISTINCT(bannerhistory.Ip))
FROM
bannerhistory
INNER JOIN
banners
ON bannerhistory.BannerId = banners.ID
WHERE
bannerhistory.BannerId IN
(
SELECT
bannerhistory.BannerId
FROM
bannerhistory
WHERE
bannerhistory.UserId = $ UserId
)
GROUP BY
banners.Banner_Name
I want to get all bannerNames for userId ad I want to count distinct IP-s for each banner separately. My code is not working cause in count it gets First banners distinct(count(Ip)) for each banner I mean almost always count is the same. could anyone help to correct my query? Or show me different approach.
don't worry about counter and clickcounter columns, they are in banners table nothing to calculate there, just get them as well.
Sorry for my English.