I have a situation where I need to find the number of days that have passed between two rows with date fields. The rows that the calculation needs to be made are not sorted.
Here is the structure of the table
Folio DATE
1 6/1/2015
2 4/1/2015
1 3/1/2015
4 2/1/2015
1 1/1/2015
Basically, I would need to sort by date and keep only the last two transactions grouped by folio. so in this example, the transaction by folio 1 on 1/1/2015 would be ignored.
Suppose that I need to do the following:
1. Group by folio number
2. only count the days between the last two transactions by folio. For example, folio #1 would only include the transactions from 6/1/2015 and 3/1/2015.
The result I'm looking for:
Folio FirstDATE LastDate #ofDays
1 3/1/2015 6/1/2015 90
Any MySQL pros out there? My skills are still in newbie territory. Thank you!
UPDATE:
I've managed to come up with the following:
SELECT
SubQuery.`Folio Number`,
SubQuery.LatestClosing,
SubQuery.FirstClosing,
DATEDIFF(SubQuery.LatestClosing, SubQuery.FirstClosing) AS numofdays
FROM (SELECT
Subquery.`Folio Number`,
SubQuery.LatestClosing,
SubQuery.FirstClosing
FROM (SELECT t.`Folio Number`,
MAX(t.`Closing Date`) AS LatestClosing,
(SELECT
s.`Closing Date`
FROM MLSFinalimport s
WHERE t.`Folio Number` = s.`Folio Number`
ORDER BY s.`Closing Date` DESC
LIMIT 1, 1) AS FirstClosing,
FROM MLSFinalimport t
GROUP BY t.`Folio Number`) SubQuery) SubQuery
This is generating a result that looks like this:
LatestClosing First Closing numofdays
7/20/2016 5/9/2006 3725
This is what I need. However, I'm stuck trying to add the original column for each row called "Folio Number". How do I proceed?
Thank you very much.
Pros for MySQL at this? Probably the opposite.. MySQL doesn't support window functions so you can try using a correlated query with LIMIT/OFFSET:
SELECT p.folio,p.max_d,p.second_d,DATEDIFF(p.max_d,p.second_d) as NumOfDays
FROM (
SELECT t.folio,MAX(t.date) as max_d,
(SELECT s.date FROM YourTable s
WHERE t.folio = s.folio
ORDER BY s.date DESC
LIMIT 1,1) as second_d
FROM YourTable t
GROUP BY t.folio) p
Related
I have tbl_events
key userID date
1 1 1.1.2000
2 1 1.1.2017
3 2 2.2.1990
I'm trying to get this:
select distinct userID, date [that is earliest eg row 1] as earliest_date,
date [that is latest eg row 2] as latest_date
from tbl_events
if a userid has only one row in tbl_events then tbl_events.date would serve as both the earliest and latest_date
ie my result would be:
userID earliest_date latest_date
1 1.1.2000 1.1.2017
2 2.2.1990 2.2.1990
I can do this rather inefficiently with lots of looping but I'm wondering if there is a way to do this with "group by" or i have seen queries that seem to contain additional select statements in brackets
If anyone can please point me in the right direction I'd be very grateful.
Thank you.
Nick
You can use MAX() and MIN() aggregate function probably saying
select userid, max(`date`) as earliest_date,
min(`date`) as furthest_date
from tbl
group by userid;
I'm trying to get the total of votes for each FANOF_ID (ex: Me). The problem is that a FAN can vote each day for the same FANOF_ID (ex: David Bowie RIP)
So each day I could vote for David Bowie as my favorite singer
ID CREATED FAN_ID FANOF_ID
15 2016-01-24 3 3
16 2016-01-25 3 3
17 2016-01-25 2 3
So from that example I should get a result of 2 fans for 'total' for FANOF_ID (3)
This is my actual SQL
SELECT
distinct `fans_fanofvote`.`fan_id`,
COUNT(`fans_fanofvote`.`fanof_id`) AS `total`
FROM `fans_fanofvote`
GROUP BY `fans_fanofvote`.`fanof_id`
ORDER BY `total` DESC
But it returns 3 records even if I use distinct on fan_id it wont work. How can I get mySQL to do a distinct on FAN_ID
My SQL should return one record like that:
FANOF_ID TOTAL
3 2
You want COUNT(DISTINCT). However, you have to be careful about what you are counting (fan_id) and what you are aggregating by (fanof_id):
SELECT fov.fanof_id,
COUNT(DISTINCT fov.fan_id) AS total
FROM fans_fanofvote fov
GROUP BY fov.fanof_id
ORDER BY total DESC;
Note that table aliases make the query easier to read. And don't use back tick unless really needed.
You didn't mention expected output earlier so it was confusing.
SELECT
`fans_fanofvote`.`fanof_id`,
COUNT(`fans_fanofvote`.`fan_id`) AS `total`
FROM `fans_fanofvote`
GROUP BY `fans_fanofvote`.`fanof_id`
ORDER BY `total` DESC
Use count(distinct )
SELECT
COUNT(distinct `fans_fanofvote`.`fanof_id`) AS `total`
FROM `fans_fanofvote`
GROUP BY `fans_fanofvote`.`fanof_id`
ORDER BY `tot
I have a table with 2 dates in it and a product, and I need to get the average days difference between them considering just the last 3 rows for each product.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121
This gives me the average of all the date differences for product 121
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121 LIMIT 3
Still gives me the average off all the records, ignoring the LIMIT argument.
Also when I try a different approach, it also does ignore the last argument and shows the average off all the rows.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product =121 && date1 > 2015-01-01
Any idea on how to fix this or what I'm doing wrong?
When you have problems like this, I recommend breaking it up and putting it back.
Before doing any calculations, you know that you need the last three rows for each product. So, if you want for example the rows with the latest date2 you can select them by doing the following:
SELECT *
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3;
That will select the 3 latest rows you want. Then, just use that as a subquery to preform the aggregation. This way, the calculations are only made on the rows you are concerned with:
SELECT product, AVG(DATEDIFF(date2, date1))
FROM(
SELECT product, date1, date2
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3) tempTable;
I have a SQL/Java code issue. The basic overlay is as follows: a MySQL database with a table. In this table there are multiple columns. One column consists of names. An associated column is months. In the third column there is counts. So a sample table would be
john - january - 5
john - january - 6
mary - january - 5
Alex - February- 5
John - February - 6
John - February - 4
Mary - February - 3
John - march - 4
The table continues to month May.
So John appears in five months, Mary in 3, and Alex in one. Currently, my SQL query somewhat looks like this.
select name, sum(count)/4
from table where (category ='something'
AND month not like 'May') group by name;
Basically, what this query is supposed to do is just display each name with the average counts per month. Hence, the sum will be divided by four (because I exclude May, so it must divide Jan-April/4). However, the issue is that some names only appear in one month (or two or three).
This means for that name, the sum of the counts would only be divided by that specific number, to get the average counts over the months. How would I go about this? I feel as if this will require some if statement in a where clause. Kind of like where if the count of the distinct (because months may repeat) is a certain number, then divide the sum(count) by that number for each name?
Also, I think it may not be a where if clause issue. I've read some forums where possibly some use of case could be utilized?
If you need average per month, you can GROUP BY name and month and use AVG function:
SELECT `name`, `month`, avg(`count`)
FROM table
WHERE `category` ='something' AND `month` NOT LIKE 'May'
GROUP BY `name`, `month`;
If you need average for all period, just GROUP BY name and AVG count:
SELECT `name`, avg(`count`)
FROM table
WHERE `category` ='something' AND `month` NOT LIKE 'May'
GROUP BY `name`;
And another option, if you don't like AVG:
SELECT `name`, sum(`count`)/(SELECT count(*) FROM `table` AS `t2` WHERE `category` ='something' AND `month` NOT LIKE 'May' and `t1`.`name` = `t2`.`name`)
FROM `table` AS `t1`
WHERE `category` ='something' AND `month` NOT LIKE 'May')
GROUP BY name;
But I would stay with AVG.
Actually, i prefer to use != instead of NOT LIKE it's improves readability
Just for completness sake here is a WORKING FIDDLE. using the AVG function is the way to go as it will do the average per person per month. look at John in January.. his result is 5.5 when the count (in january) is 5 and 6.. average = 5.5.
SELECT
person,
month,
avg(counter)
FROM testing
where
(
category ='something'
AND month <> 'May'
)
GROUP BY person, month;
If you want to see the data in one like as it sounds like that from your post then you can do this. ANOTHER FIDDLE
SELECT
person,
group_concat(month),
group_concat(average_count)
FROM(
SELECT
person,
month,
avg(counter) as average_count
FROM testing
where
(
category ='something'
AND month <> 'May'
)
GROUP BY person, month
) as t
group by person;
Try this :
SELECT name, SUM(count) / COUNT(DISTINCT month)
FROM table
WHERE month != 'May'
AND category = 'something'
GROUP BY name
I have a MySQL table where there are many rows for each person, and I want to write a query which aggregates rows with special constraint. (one per person)
For example, lets say the table is consist of following data.
name date reason
---------------------------------------
John 2013-04-01 14:00:00 Vacation
John 2013-03-31 18:00:00 Sick
Ted 2012-05-06 20:00:00 Sick
Ted 2012-02-20 01:00:00 Vacation
John 2011-12-21 00:00:00 Sick
Bob 2011-04-02 20:00:00 Sick
I want to see the distribution of 'reason' column. If I just write a query like below
select reason, count(*) as count from table group by reason
then I will be able to see number of reasons for this table overall.
reason count
------------------
Sick 4
Vacation 2
However, I am only interested in single reason from each person. The reason that should be counted should be from a row with latest date from the person's records. For example, John's latest reason would be Vacation while Ted's latest reason would be Sick. And Bob's latest reason (and the only reason) is Sick.
The expected result for that query should be like below. (Sum of count will be 3 because there are only 3 people)
reason count
-----------------
Sick 2
Vacation 1
Is it possible to write a query such that single latest reason will be counted when I want to see distribution(count) of reasons?
Here are some facts about the table.
The table has tens of millions of rows
For most of times, each person has one reason.
Some people have multiple reasons, but 99.99% of people have fewer than 5 reasons.
There are about 30 different reasons while there are millions of distinct names.
The table is partitioned based on date range.
SELECT T.REASON, COUNT(*)
FROM
(
SELECT PERSON, MAX(DATE) AS MAX_DATE
FROM TABLE-NAME
GROUP BY PERSON
) A, TABLE-NAME T
WHERE T.PERSON = A.PERSON AND T.DATE = A.MAX_DATE
GROUP BY T.REASON
Try this
select reason, count(*) from
(select reason from table where date in
(select max(date) from table group by name)) t
group by reason
In MySQL, it's not very efficient to do this kind of query since you don't have access to tools like partitionning query in SQL Server or Oracle.
You can still emulate it by doing a subquery and retrieve the rows based on the condition you need, here the maximum date :
SELECT t.reason, COUNT(1)
FROM
(
SELECT name, MAX(adate) AS maxDate
FROM #aTable
GROUP BY name
) maxDateRows
INNER JOIN #aTable t ON maxDateRows.name = t.name
AND maxDateRows.maxDate = t.adate
GROUP BY t.reason
You can see a sample here.
Test this query on your samples, but I'm afraid that it will be slow as hell.
For your information, you can do the same thing in a more elegant and much much faster way in SQL Server :
SELECT reason, COUNT(1)
FROM
(
SELECT name
, reason
, RANK() OVER(PARTITION BY name ORDER BY adate DESC) as Rank
FROM #aTable
) AS rankTable
WHERE Rank = 1
GROUP BY reason
The sample is here
If you are really stuck to MySql, and the first query is too slow, then you can split the problem.
Do a first query creating a table:
CREATE TABLE maxDateRows AS
SELECT name, MAX(adate) AS maxDate
FROM #aTable
GROUP BY name
Then create index on both name and maxDate.
Finally, get the results :
SELECT t.reason, COUNT(1)
FROM maxDateRows m
INNER JOIN #aTable t ON m.name = t.name
AND m.maxDate = t.adate
GROUP BY t.reason
The solution you are looking for seems to be solved by this query :
select
reason,
count(*)
from (select * from tablename group by name) abc
group by
reason
It is quite fast and simple. You can view the SQL Fiddle
Apologies if this answer duplicates an existing. Maybe I'm suffering from some form aphasia but I cannot see it...
SELECT x.reason
, COUNT(*)
FROM absentism x
JOIN
( SELECT name,MAX(date) max_date FROM absentism GROUP BY name) y
ON y.name = x.name
AND y.max_date = x.date
GROUP
BY reason;