how to get min max in timestamp - mysql

I have table data and need to get a result set containing a row for the max(timestamp) for id. where timestamp between parameter
The data sample would look like: example data
I can't figure out how to get a row for the max(timestamp) for each id and between parameter.
select id,
tgl,
max(case when tgl::timestamp between '2018-08-01 06:00'::timestamp and '2018-08-02 06:00'::timestamp
then tgl::timestamp else null end) clock
from tb_raw_pola_internal
where id = '0023817'
group by 1, 2
order by 1,2 asc
limit 1
and the result is:
My result set should clock in 2018-08-02 02:05:00
Who can be my hero? :) thanks before

You are doing group by on aggregation column as well. Try below code :
select id,
tgl,
max(case when tgl::timestamp between '2018-08-01 06:00'::timestamp
and '2018-08-02 06:00'::timestamp
then tgl::timestamp else null end) clock
from tb_raw_pola_internal
where id = '0023817'
group by 1;

Related

sql update max(date) group by

raw data
no
group
date
value
flag
1
a
2022-10-13
old
y
2
a
2022-10-15
new
y
3
b
2022-01-01
old
n
4
b
2022-01-03
new
n
step1. insert no1 raw
step2. modify date value using by no2 raw
and I want to update latest date no1 raw using by no2 raw
and the condition is where `flag` = "y"
final sql table
no
group
date
value
flag
1
a
2022-10-15
old
y
3
b
2022-01-01
old
n
is it possible?
+) I insert/update raw data line by line.
Not entirely clear but I hope below answer gives you a hint if not the solution.
select no,
`group`,
case when flag='Y' then mx_dt else `date` end as new_date,
value,
flag
from ( select no,
`group`,
value,
`date`,
flag ,
row_number() over(partition by `group` order by `date` asc ) as rn,
max(`date`) over (partition by `group`,(case when flag <> 'Y' then `date` end) ) mx_dt
from raw_data
) as tbl
where rn=1;
Above code will select the max(date) per group if the flag=Y otherwise it will take the date per row.
https://dbfiddle.uk/JhRUti2h
The solution is to self join the source table and select the right field, prioritizing the latest date.
Here you have a working query:
WITH source_data AS (
SELECT 1 AS no_, 'a' AS group_, CAST('2022-10-13' AS DATE) AS date, 'old' AS value, 'y' AS flag
UNION ALL
SELECT 2, 'a', CAST('2022-10-15' AS DATE), 'new', 'y'
UNION ALL
SELECT 3, 'b', CAST('2022-01-01' AS DATE), 'old', 'n'
UNION ALL
SELECT 4, 'b', CAST('2022-01-03' AS DATE), 'new', 'n')
SELECT no_, group_, COALESCE(new_date, date), value, flag
FROM
(SELECT * FROM source_data WHERE value = 'old') old_values
LEFT JOIN (SELECT group_ AS new_group, date AS new_date FROM source_data WHERE value = 'new' AND flag='y') new_values
ON old_values.group_ = new_values.new_group
The result is what you expected:
no_ group_ f0_ value flag
1 a 2022-10-15 old y
3 b 2022-01-01 old n

How to write the a new select query with backward compatibility after adding a new column to a MySQL database

I have a MySQL table running for 4 months and I have a select statement in that table, like below.
SELECT
CONCAT(
YEAR(FROM_UNIXTIME(creation_time)),
'-',
IF(
MONTH(FROM_UNIXTIME(creation_time)) < 10,
CONCAT('0', MONTH(FROM_UNIXTIME(creation_time))),
MONTH(FROM_UNIXTIME(creation_time))
)
) AS Period,
(
COUNT(CASE
WHEN system_name = 'System' THEN 1
ELSE NULL
END)
) AS "Some data",
FROM table_name
GROUP BY
Period
ORDER BY
Period DESC
Lately, I've added a new feature and a column, let's say is_rerun. This value is just added and not exist previously. Now, i would like to write a query with the current statement which checks the system_name and also the is_rerun field and if this field exists and value is 1 then return 1 and if the column not exist or it its value is zero, then return null.
I tried IF EXISTS re_run THEN 1 ELSE NULL, but no luck. I can also insert values for the previous runs but i don't want to do that. Is there any solution. Thanks.
SELECT
CONCAT(
YEAR(FROM_UNIXTIME(creation_time)),
'-',
IF(
MONTH(FROM_UNIXTIME(creation_time)) < 10,
CONCAT('0', MONTH(FROM_UNIXTIME(creation_time))),
MONTH(FROM_UNIXTIME(creation_time))
)
) AS Period,
(
COUNT(CASE
WHEN system_name = 'System' AND IF EXISTS is_rerun THEN 1
ELSE NULL
END)
) AS "Some data",
FROM table_name
GROUP BY
Period
ORDER BY
Period DESC
As a starter: you have a group by query, so you need to put is_rerun in an aggregate function.
Based on your description, I think that something like case(case when is_rerun = 1 then 1 end) should do the work: it returns 1 if any is_rerun in the group is 1, else null.
Or if you can live with 0 instead of null, then you can use a simpler expression: max(is_rerun = 1).
Note that your query could be largely simplified as for the date formating logic and the conditional count. I would phrase it as:
select
date_format(from_unixtime(creation_time),'%Y-%m') period,
sum(system_name = 'System') some_data,
max(is_rerun = 1) is_rerun
from mytable
group by period
order by period desc

Get Data According to Group by date field

Here is my table
Which have field type which means 1 is for income and 2 is for expense
Now requirement is for example in table there is two transaction made on 2-10-2018 so i want data as following
Expected Output
id created_date total_amount
1 1-10-18 10
2 2-10-18 20(It calculates all only income transaction made on 2nd date)
3 3-10-18 10
and so on...
it will return an new field which contains only incom transaction made on perticulur day
What i had try is
SELECT * FROM `transaction`WHERE type = 1 ORDER BY created_date ASC
UNION
SELECT()
//But it wont work
SELECT created_date,amount,status FROM
(
SELECT COUNT(amount) AS totalTrans FROM transaction WHERE created_date = created_date
) x
transaction
You can Also See Schema HERE http://sqlfiddle.com/#!9/6983b9
You can Count() the total number of expense transactions using conditional function If(), on a group of created_date.
Similarly, you can Sum() the amount of expense done using If(), on a created_date.
Try the following:
SELECT
`created_date`,
SUM(IF (`type` = 2, `amount`, 0)) AS total_expense_amount,
COUNT(IF (`type` = 2, `id`, NULL)) AS expense_count
FROM
`transaction`
GROUP BY `created_date`
ORDER BY `created_date` ASC
Do you just want a WHERE clause?
SELECT t.created_date, SUM(amount) as total_amount
FROM transaction t
WHERE type = 2
GROUP BY t.created_date
ORDER BY created_date ASC ;

Union All does not return the same number of rows

I have a sql query that uses union all. When i run the following query only one row with NULL value is returned (the value is null because there is no data for that time period). The expectation was that 2 rows with null would be returned. What gives?
SELECT
SUM(val) AS GA
FROM
mytable
WHERE region = 'someRegion'
AND ACTIVITY_MTH BETWEEN '2014-04-01' AND '2014-04-30'
UNION
ALL
SELECT
quota
FROM
budgetTable
WHERE sales_id = 'someRegion'
AND start_date = '2014-04-01'
AND metric_id = 1

Getting a Rank out of a Total

I have been doing this for quite some time:
SELECT COUNT(*) AS 'Rank' FROM Table
WHERE Condition = 'Condition' AND Score >= 'Score';
SELECT COUNT(*) AS 'Total' FROM Table
WHERE Condition = 'Condition';
Is there a more efficient way of getting both Rank and Total?
You can calculate both at the same time with one pass through the data.
SELECT COUNT(*) AS 'Total',
SUM(CASE WHEN Score >= 'Score' THEN 1 ELSE 0 END) AS `Rank`
FROM Table
WHERE Condition = 'Condition';