My Query-
SELECT id, MAX(sl), details_sp FROM shareholderreceive GROUP BY id
and result
id MAX(sl) details_sp
1 76
2 74
3 64
4 67
5 69
6 70
10 72
But i need Those MAX(sl) Where details_sp column has last entered value.
My expected table is -
id MAX(sl) details_sp
1 72 Jul'16
2 74
3 64
4 62 Aug'16
5 69
6 70
10 71 Aug'16
here, data type details_sp is Varchar.
What query do I need to get this in MySQL?
What I think you mean is that when there is a details_sp filled in you need the sl value of that row and when there is no details_sp you need the max sl value?
If so, don't use MAX() but use the ORDER BY with the GROUP BY.
SELECT id, sl, details_sp
FROM shareholderreceive
ORDER BY details_sp DESC, sl DESC
GROUP BY id
The only problem with this is that you can never order by a date when it is a varchar value. This means you can never get the latest entry with just a varchar as a date.
So if there is a way, make the details_sp a date value (when you don't have a day just use the first day of the month, so 2017-06-01 for juli 2017) and use DATE_FORMAT() in MySQL or date_format in PHP (or JAVA or etc).
Related
I'm newbie, and I'm trying to delete duplicate rows with highest value in another column,
This is My database and result of below query
SELECT file_id,src,dst,durations,date_time, COUNT(*), MAX(durations) as
Max_durations
FROM C2
WHERE durations in (
SELECT max(durations)
FROM C2
GROUP BY src, dst
HAVING COUNT(*) >1)
GROUP BY src,dst
HAVING COUNT(*)>1
Now I want to remove Duplicate rows but keep rows that have maximum duration or equal Max_duration column and if have same duration delete one of them.
How can i do it...?
src
dst
duration
COUNT
Max_duration
12014504264
9726341011
464
20
684
12014504264
9726645434
320
8
875
12014556435
9726876431
765
4
900
12014576565
9726347656
43
7
600
12014508754
9726344537
233
2
233
12014764532
9726877654
655
2
54
12014587875
9726645443
1345
5
982
12014654536
9726766433
73
2
84
Assuming you are trying to actually delete rows from the table, not just have a query that omits certain rows:
To do this, just self-join with a row that would be preferred to the current one, so only rows where that is found are deleted:
delete C2
from C2
join C2 preferred on
preferred.src=C2.src and
preferred.dst=C2.dst and
preferred.durations >= C2.durations and
(preferred.durations > C2.durations or preferred.file_id < C2.file_id)
But to do this, you need some unique column to establish a precedence between multiple rows with the maximum duration; here I am using file_id and keeping the row with the lowest file_id.
I'm just stuck with this issue atm and I'm not 100% sure how to deal with it.
I have a table where I'm aggregating data on week
select week(create_date),count(*)
from user
where create_date > '2015-02-01'
and id_customer between 9 and 17
group by week(create_date);
the results that I'm getting have missing values in the count, as shown below
5 334
6 376
7 394
8 405
9 504
10 569
11 709
12 679
13 802
14 936
15 1081
16 559
21 1
24 9
25 22
26 1
32 3
34 1
35 1
For example here from 16 to 21 there a obviously 4 values missing I would like these values to be included and count to be 0. I want this because I want the weeks to be matching with other metrics as we are outputting them in an excel file for internal analysis.
Any help would be greatly appreciated.
The problem is that an sql query cannot really produce data that is not there at all.
You have 3 options:
If you have data for each week in your entire table for the period you are querying, then you can use a self join to get the missing weeks:
select week(t1.create_date), count(t2.id_customer)
from customer t1
left join customer t2 on t1.id_customer=t2.id_customer and t1.create_date=t2.create_date and t2.id_customer between 9 and 17
where t1.create_date > '2015-02-01'
group by week(t1.create_date)
If you have missing weeks from the customer table as whole, then create a helper table that contain week numbers from 1 or 0 (depending on mysql config) to 53 and do a left join to this helper table.
Use a stored procedure that loops through the results of your original query and inserts the missing data in the resultset using a temporary table and then returns the extended dataset as result.
The problem is that there is no data matching your criteria for the missing weeks. A solution will be to join from a table that has all week numbers. For example if you create a table weeknumbers with one field weeknumber containing all the numbers from 0 to 53 you can use something like this
select weeknumber,count(user.*)
from weeknumbers left join user on (weeknumbers.weeknumber=week(user.create_date)
and user.create_date > '2015-02-01'
and user.id_customer between 9 and 17)
group by weeknumber;
Additionaly you might want to limit the week numbers you do not want to see.
The other way is to do it in the application.
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 7 years ago.
I seem to remember this working as expected in Oracle, but I can't figure out how to get it right in MySQL.
Here is my query:
SELECT DateTimeStamp, MAX(Value) FROM t1
GROUP BY YEAR(DateTimeStamp), MONTH(DateTimeStamp), DAY(DateTimeStamp);
Running this produces results like:
DateTimeStamp Value
2015-09-09 00:00:29 100
2015-09-10 00:00:05 58
2015-09-11 00:00:57 62
2015-09-12 00:00:49 69
2015-09-13 00:00:43 97
But I was expecting it to look like this, where the DateTimeStamps match up with the values:
DateTimeStamp Value
2015-09-09 03:28:29 100
2015-09-10 03:29:05 58
2015-09-11 03:31:57 62
2015-09-12 03:30:49 69
2015-09-13 03:28:43 97
The correct maximum values are being selected, but the matching DateTimeStamps for those maximum values are not. Instead, it looks like the first DateTimeStamp value for each day is being selected. How can I change my query to display the matching DateTimeStamps?
If all you need is to strip the time section, you need to use the date function:
SELECT date(DateTimeStamp) AS DateTimeStamp, MAX(Value) AS MaxValue
FROM t1
GROUP BY date(DateTimeStamp);
Note: Grouping by something usually make sense when you select it. There is no point to group by YEAR(DateTimeStamp), MONTH(DateTimeStamp), DAY(DateTimeStamp) if all you need is the maximum value per each day.
id date calls
5 2015-02-17 01:06:01 1
6 2015-02-17 11:07:01 2
7 2015-02-17 23:06:01 3
8 2015-02-18 03:07:01 1
9 2015-02-18 09:06:01 2
10 2015-02-18 17:07:01 3
11 2015-02-18 22:06:01 4
12 2015-02-19 01:07:01 1
13 2015-02-19 08:06:01 2
14 2015-02-19 18:07:01 3
15 2015-02-19 23:06:01 4
my table structure is like this and I need to calculate the sum of call in each days. In this table, you can see that, the last call in feb 17 was at 23:06:01 and call count was 3. In feb 18 was at 22:06:01 and call count was 4. Can I get the sum of all this last call counts of each day.
You can use a subquery to determine which rows to sum (the ones matching the last call for each date, using MySQL it would be:
select sum(calls) sum_last_calls
from your_table
where `date` in (
select max(date) max_date
from your_table
group by date(`date`)
)
This query will return 11 as the sum (from 3+4+4).
The date() function used in the subquery is specific to your database and might need to be changed according to your specific database syntax - the point is that it should return the date without time (it could be date::date (Postgresql) or cast(date as date) (MSSQL and others)).
Sample SQL Fiddle for MySQL and Postgresql
Postgresql version:
select sum(calls) as calls
from (
select max(calls) as calls
from t
where date::date between '2015-02-17' and '2015-02-19'
group by date::date
) s
Hi i have following mysql table
id item_name user_id wishlist item_url id_category cost
30 kiko 76 1 70 10
31 test1 76 1 70 20
32 test12 76 1 68,67 30
How can i get max cost item. Which means i need max cost item based on category id.
My attempt of using SELECT MAX(cost),id FROM item_tbl WHERE (FIND_IN_SET('68','70,68,67')) not return a correct output thought it gives max(cost) of 30. Thanks
This might give you the desired output.
SELECT ID_CATEGORY, MAX(COST) FROM ITEM_TBL
GROUP BY ID_CATEGORY;