I have an sql table called messages, where i receive all messages from various devices.
The structure is (start_time is a unique key):
explanation: first remove all duplicates from one session and then sum all quantities.
I will be very thankful for your help
Try this query. It should do it.
select t.product, sum(t.quantity) as sum_quantity
from
(
select distinct product, quantity
from
messages
) t
group by t.product
order by t.product asc
Try doing a group by with a distinct
Select Product, Sum(Distinct Quatity) As Qty
From messages
Group By Product
Related
I have two tables: Subscriptions and Items:
Subscriptions (sub_id PK, user_id, value)
Downloads (download_id PK, user_id, category_id)
My goal is to get a result table of form (user_id, sum_subscription_value, num_download_categories). In other words: each row is unique to a user_id, in which the total value of subscriptions that user has purchased is given alongside the number of categories the user has downloaded things from.
I've attempted by solving using the following code, but the categories aren't being counted correctly. I think the issue might be the join, as the value rows are being repeated, but I'm not sure how to exactly circumvent the issue. Any help is appreciated
SELECT
DISTINCT subscriptions.user_id,
SUM(value) OVER (PARTITION by subscriptions.user_id, category_id) AS user_purchases,
COUNT(category_id) OVER (PARTITION by subscriptions.user_id) AS user_downloads
FROM subscriptions
LEFT JOIN downloads on subscriptions.user_id = downloads.user_id;
My goal is to get a result table of form (user_id, sum_subscription_value, num_download_categories).
One method is to aggregate before joining. But in this case, I suppose that a user could be missing from either table. And aggregating before would require full join to avoid losing data.
Instead, you can use union all and group by:
select user_id,
sum(value) as subscription_value,
count(distinct category_id) as num_categories
from ((select user_id, value, null as category_id
from subscriptions
) union all
(select user_id, NULL as value, category_id
from downloads
)
) sd
group by user_id;
I'm trying to get the rank of a particular lap time of a specific track owned by a particular user.
There are multiple rows (laps) in this table for a specific user. So I'm trying to GROUP BY as seen in the subquery of FIND_IN_SET.
Right now MySQL (latest version) is complaining that my session_id,user_id,track_id,duration are not aggregated for the GROUP BY.
Which I don't understand why its complaining about this since the GROUP BY is in a subquery.
session_lap_times schema:
session_id, int
user_id, int
track_id, int
duration, decimal
This is what I've got so far.
SELECT
session_id
user_id,
track_id,
duration,
FIND_IN_SET( duration,
(SELECT GROUP_CONCAT( duration ORDER BY duration ASC ) FROM
(SELECT user_id,track_id,min(duration)
FROM session_lap_times GROUP BY user_id,track_id) AS aa WHERE track_id=s1.track_id)
) as ranking
FROM session_lap_times s1
WHERE user_id=1
It seems like its trying to enforce the group by rules on the parent queries as well.
For reference, this is the error I'm getting: http://imgur.com/a/ILufE
Any help is greatly appreciated.
If I'm not mistaken, the problem is here (broken out for clarity):
SELECT user_id,track_id,any_value(duration)
FROM session_lap_times
GROUP BY user_id
The query is probably barfing because track_id is in the select and not in the group by. That means the subselect doesn't stand on its own and makes the whole thing fail.
Try adding track_id to your group by and adjust from there.
You are grouping by user_id but you do not do any aggregation in select or having in the following sub-query
SELECT
user_id,any_value(track_id),any_value(duration)
FROM session_lap_times GROUP BY user_id
You are using GROUP_CONCAT in a wrong context in the following sub-query because you do not group any column in ranking temporary table.
(SELECT GROUP_CONCAT( duration ORDER BY duration ASC ) FROM
(SELECT user_id,track_id,any_value(duration)
FROM session_lap_times GROUP BY user_id,track_id) AS aa WHERE track_id=s1.track_id)
) as ranking
I need help returning a relevant result for this query. I have one table that I am hitting with three columns. trans_date, trans_amount and user_id
what I am trying to determine is this. For a given user_id when was the last trans_date and what was the trans_amount.
I'm having trouble returning the correct transaction_amount. Here is my code so far. It's returning the correct date but the amount is not right
select user_id, trans_date, trans_credit
from table
WHERE trans_credit =
(select max(trans_date) from inclick_account_act as f
where f.user_id = table.user_id);
Thanks in advance
If I understand you correctly you just want to get the most recent transaction for all users.
SELECT user_id, trans_date, trans_credit
FROM `table`
GROUP BY user_id
ORDER BY trans_date DESC;
How about something like
SELECT t.*
FROM table t INNER JOIN
(
SELECT user_id,
MAX(trans_date) max_trans_date
FROM table
GROUP BY user_id
) MaxDates ON t.user_id = MaxDates.max_trans_date
I have a photos table, with two columns in that table named "id" and "user_id". Obviously one user can have many photos. I'd like to run a query that can give me the photo count for each user.
Any help is appreciated.
select user_id, count(*) as photo_count from photos group by user_id
Use:
SELECT t.user_id,
COUNT(*) AS photo_count
FROM PHOTOS
GROUP BY t.user_id
use count as your aggregate function and groupby clause to give the count of specific attribute,
here is the sample:
SELECT user_id, count(*) 'Photos Count'
from photos
group by user_id
(Sorry for the title, I don't really know how to phrase that :-) )
I have a table that has a date and a uid fields.
I need to get the number of uid for each date, currently I'm doing it in php running multiple queries like this one:
SELECT COUNT(uid) FROM users where Date = 'xxx';
Is there a simple way to achieve this with only an sql query?
Use the group by clause:
SELECT Date, COUNT(uid) FROM users group by Date;
To count the number of unique values use DISTINCT:
SELECT COUNT(DISTINCT uid) AS cnt
FROM users
GROUP BY `Date`
If your column is a datetime then you should use the DATE function to get the date part only:
SELECT COUNT(DISTINCT uid) AS cnt
FROM users
GROUP BY DATE(`Date`)
SELECT Date, COUNT(uid) FROM users GROUP BY Date