I have a table with the following columns; id, post_id, status, and datetime. Every time a post(post_id) is updated, a row is inserted with the latest status and datetime timestamp. I am created a pie chart of by status. Therefore I must first SELECT the latest entry of a post (ignoring all past updates), then COUNT how many rows are returned and group by status. What does my query look like?
SELECT status, COUNT(*) AS statusCnt
FROM inspections
WHERE id IN (SELECT MAX(id) FROM inspections GROUP BY post_id)
GROUP BY status
Untested alternative:
SELECT i1.status, COUNT(i1.*) AS statusCnt
FROM inspections i1
JOIN (
SELECT MAX(i2.id) AS maxID FROM inspections i2 GROUP BY i2.post_id
) AS innerTbl ON i1.id = innerTbl.id
GROUP BY i1.status
SELECT count(*) FROM (SELECT *,MAX(time) FROM table GROUP BY post_id) As b
As per my interpretation of your question, query should be like this
Related
I know how to query to find records with duplicate records based on a field or fields e.g.
Select Customer,Count(*) from Table1 group by Customer,Month having count(*)>1
which would give me a list of all customers who ordered more than once in a given month.
However from that select I'd like to:
Refine the group to show only dupes where the product is DIFFERENT. I know if I wanted to do the same I'd simple add to group by ',Product' but in my case it is Product != Product and I'm not sure how to indicate that in the group
Instead of getting a list of just which Customers ordered more than one product in a given month a list of all those orders. In other words instead of this type of list from the group:
Bob,December
Mary,June
I am trying to return:
Bob,Widget,December
Bob,Pipes,December
Mary,Books,June
Mary,Cars,June
If product field is in the same table, then you can use count with distinct on the product field to get the number of distinct products:
Select Customer, Month, Count(distinct product)
from Table1
group by Customer, Month
having count(distinct product)>1
If you want to know what they ordered, then join it back as a subquery to your main table:
select distinct t1.customer, t1.month, t1.product from table1 t1
inner join
(Select Customer, Month, Count(distinct product)
from Table1
group by Customer, Month
having count(distinct product)>1
) t2 on t1.customer=t2.customer and t1.month=t2.month
The distinct in the outer select depends on your exact needs.
I'm trying to write a query that finds each time the same person occurs in my table between a specific date range. It then groups this person and totals their spending for a specific range. If their spending habits are greater than X amount, then return each and every row for this person between date range specified. Not just the grouped total amount. This is what I have so far:
SELECT member_id,
SUM(amount) AS total
FROM `sold_items`
GROUP BY member_id
HAVING total > 50
This is retrieving the correct total and returning members spending over $50, but not each and every row. Just the total for each member and their grand total. I'm currently querying the whole table, I didn't add in the date ranges yet.
JOIN this subquery with the original table:
SELECT si1.*
FROM sold_items AS si1
JOIN (SELECT member_id
FROM sold_items
GROUP BY member_id
HAVING SUM(amount) > 50) AS si2
ON si1.member_id = si2.member_id
The general rule is that the subquery groups by the same column(s) that it's selecting, and then you join that with the original query using the same columns.
SELECT member_id, amount
FROM sold_items si
INNER JOIN (SELECT member_id,
SUM(amount) AS total
FROM `sold_items`
GROUP BY member_id
HAVING total > 50) spenders USING (member_id)
The query you have already built can be used as a temporary table to join with. if member_id is not an index on the table, this will become slow with scale.
The word spenders is a table alias, you can use any valid alias in its stead.
There are a few syntaxes that will get the result you are looking, here is one using an inner join to ensure that all rows returned have a member_id in the list returned by the group by and that the total is repeated for each a certain member has:
SELECT si.*, gb.total from sold_items as si, (SELECT member_id as mid,
SUM(amount) AS total
FROM `sold_items`
GROUP BY member_id
HAVING total > 50) as gb where gb.mid=si.member_id;
I think that this might help:
SELECT
member_id,
SUM(amount) AS amount_value,
'TOTAL' as amount_type
FROM
`sold_items`
GROUP BY
member_id
HAVING
SUM(amount) > 50
UNION ALL
SELECT
member_id,
amount AS amount_value,
'DETAILED' as amount_type
FROM
`sold_items`
INNER JOIN
(
SELECT
A.member_id,
SUM(amount) AS total
FROM
`sold_items` A
GROUP BY
member_id
HAVING
total <= 50
) AS A
ON `sold_items`.member_id = A.member_id
Results of the above query should be like the following:
member_id amount_value amount_type
==========================================
1 55 TOTAL
2 10 DETAILED
2 15 DETAILED
2 10 DETAILED
so the column amount_type would distinguish the two specific member groups
You could do subquery with EXISTS as an alternative:
select *
from sold_items t1
where exists (
select * from sold_items t2
where t1.member_id=t2.member_id
group by member_id
having sum(amount)>50
)
ref: http://dev.mysql.com/doc/refman/5.7/en/exists-and-not-exists-subqueries.html
In case you need to group by multiple columns, you can use a composite identifier with concatenate in combination with a group by subquery
select id, key, language, group
from translation
--query all key-language entries by composite identifier...
where concat(key, '_', language) in (
--by lookup of all key-language combinations...
select concat(key, '_', language)
from translation
group by key, language
--that occur more than once
having count(*) > 1
)
Table :
I am new to query writing. Now I am stuck on retrieving 2 rows from above table.
Data will be date sorted in descending order for only 2 different topic_id. There won't be a third different topic_id.
So I want to retrieve two rows only that will have different topic_id, one data for each topic_id having most recent date.
The result would be
try sql fiddle
http://sqlfiddle.com/#!2/f37963/9
SELECT t1.* FROM temp t1
JOIN (SELECT question_id, MAX(`date`) as `date` FROM temp GROUP BY topic_id) t2
ON t1.question_id= t2.question_id AND t1.`date`= t2.`date`;
The logic is to find the latest date in each group (subquery) and join it with the table again to retrieve other particulars.
use this
$qry="SELECT * FROM table_name GROUP BY TOPIC_ID ORDER BY DATE desc";
I want to create a table in mysql the table has 3 columns user_id,activity - online or offline - and timestamp,
Now I want the table to show all user_id's along with most recent status. Currently using
SELECT DISTINCT user_id,activity FROM activity ORDER BY timestamp DESC
statement shows when the user was last online and offline which is not what I want.
Try this:
select act.*
from activity as act
inner join (
select user_id, max(timestamp) as max_ts
from activity
group by user_id) as a on act.user_id=a.user_Id and act.timestamp=a.max_ts
This will return the most recent record for each user.
select user_id, max(timestamp) ts from activity a
inner join
(select user_id, activity, max(timestamp) ts
group by user_id, activity) iam
on iam.user_id =a.user_id and a.ts = iam.ts
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