Retrieve last row with specific condition - mysql

Suppose we have a table like:
ID
Account
Amount
Date
1
4455
52
01-01-2022
2
4455
32
02-01-2022
3
4455
23
03-01-2022
4
4455
23
04-01-2022
5
6565
236
01-01-2022
6
6565
623
02-01-2022
7
6565
132
03-01-2022
8
2656
564
01-01-2022
9
2656
132
02-01-2022
We need to retrieve every last row of given account_no. We need output like:
ID
Account
Amount
Date
4
4455
23
04-01-2022
7
6565
132
03-01-2022
10
2656
13
03-01-2022
Kindly suggest me a query to retrieve data like this in table of 2000 records.

You want the last row of certain query. So you must be having an order by clause. Just reverse the ordering and use a limit clause with limit set to one row.
SELECT column_name(s)
FROM table_name
WHERE condition
order by your_reversed_orderby_clause
LIMIT 1;

If you are using MySQL 8, then you can use ROW_NUMBER() function for this:
WITH CTE AS
(
SELECT ID,Account,Amount,Date
,ROW_NUMBER() OVER(PARTITION BY Account ORDER BY ID DESC) AS RN
FROM Table1
)
SELECT * FROM CTE
WHERE RN=1
ORDER BY ID;
ID
Account
Amount
Date
RN
4
4455
23
2022-04-01 00:00:00
1
7
6565
132
2022-03-01 00:00:00
1
9
2656
132
2022-02-01 00:00:00
1
See this db<>fiddle

SELECT * FROM table_name
WHERE ID IN (
SELECT max(ID) FROM table_name
GROUP BY Acount
ORDER BY Account
)

Related

Mysq/Mariadb get newest entry for some ids in a IN Select group by id

Hello i have a table with some ids and values
for example:
SELECT instrumentid, value from `mytable` where instrumentid in (12,11, 14,15);
id, instrumentid, recorddate, value
33 12 2022-10-05 55
34 11 2022-10-05 33
30 14 2022-10-05 13
29 12 2022-10-03 12
28 11 2022-10-03 53
40 14 2022-10-03 4
44 15 2022-10-03 4
as result i want or better explained only the last newst entry for instrumentid
instrumentid, value
12 55
11 33
14 13
15 4
thanks and regards
running latest mariadb 10.9.3
You can use ROW_NUMBER() to identify the last row for each instrument.
For example:
select *
from (
select t.*,
row_number() over(partition by instrumentit order by recorddate desc) as rn
from mytable t
where instrumentid in (12,11, 14,15)
) x
where rn = 1

Use date (-1 day) on next row to be the end date for current row

I am trying to self join in my current script in order to find the next row and then whatever day specified it should minus 1 day from it and put that in the end date column for the current row, but I seem to be going wrong somewhere.
SELECT
BCG.BudgetId
,B.CustomerId
,CAST(BCG.StartOfPeriod AS DATE) AS StartOfPeriod
,BCG2.EndOfPeriod
,ROUND(SUM(BCG.Charge),2) AS ExpenditureBudget
,ROUND(SUM(BCG.Consumption),2) AS ConsumptionBudget
,ROW_NUMBER() OVER (PARTITION BY BCG.BudgetId ORDER BY BCG.StartOfPeriod ASC) AS rowNum
,B.Status
FROM Budgets_BudgetCalcGroup BCG
INNER JOIN Budgets_Budget B ON B.Id = BCG.BudgetId
LEFT JOIN Budgets_BudgetCalcGroup BCG2 ON
BCG2.EndOfPeriod = (SELECT MIN(StartOfPeriod)-1
FROM Budgets_BudgetCalcGroup AS t3
WHERE t3.StartOfPeriod > t1.StartOfPeriod
)
WHERE B.Status = 2
GROUP BY BCG.BudgetId,StartOfPeriod
Error Received:
Unknown Column BCG2.EndOfPeriod in field list
Expected Output:
254 41 2018-09-01 2018-09-30 29017.8 542331.59 1 2
254 41 2018-10-01 2018-10-31 27858.82 575545.97 2 2
254 41 2018-11-01 2018-11-30 28927.71 576106.15 3 2
254 41 2018-12-01 NULL 34639.71 613779.57 4 2
I found an alternative way other than doing a self join which utilises the LEAD() function.
DATE_ADD(CAST(LEAD(BCG.StartOfPeriod, 1) OVER (PARTITION BY BCG.BudgetId ORDER BY BCG.StartOfPeriod) AS DATE),INTERVAL -1 DAY) AS EndOfPeriod
Output:
254 41 2018-09-01 2018-09-30 29017.8 542331.59 1
254 41 2018-10-01 2018-10-31 27858.82 575545.97 2
254 41 2018-11-01 2018-11-30 28927.71 576106.15 3
254 41 2018-12-01 2018-12-31 34639.71 613779.57 4

Count without 2nd subquery in SQL

I am trying to attain the count of users that ordered at least 1 product on multiple days.
Transactions Table
usr_id|transt_id|product_id|spend| transaction_date
4 8 32 40 2020-05-08 17:54:59
4 7 31 20 2020-05-01 17:54:59
4 7 31 40 2020-05-01 17:54:59
4 6 20 30 2020-05-02 17:54:59
4 6 19 20 2020-05-02 17:54:59
4 6 18 10 2020-05-02 17:54:59
3 5 17 20 2020-05-04 17:54:59
3 5 16 10 2020-05-04 17:54:59
2 3 14 30 2020-05-04 18:54:59
2 3 13 50 2020-05-04 18:54:59
1 2 12 30 2020-05-05 20:54:59
1 2 12 40 2020-05-05 20:54:59
1 2 12 40 2020-05-04 20:54:59
1 1 11 20 2020-05-05 21:54:59
1 1 10 40 2020-05-05 21:54:59
3 4 10 60 2020-05-06 17:54:59
Through my code I have been able to reach to a point where the output is:
select user_id, count(*)
from (
select user_id, date(transaction_date)
from transactions
group by user_id, date(transaction_date)) as abc
group by user_id
having count(user_id)>1;
user_id | count
1 2
3 2
4 3
I want to write a code without writing another subquery to get the count of users having count(*)>1;
The output should be: 3.
In other words, I don't want the following code; I want to write one less subquery or a completely new query
select count(*)
from (
select user_id, count(*)
from (
select user_id, date(transaction_date)
from transactions
group by user_id, date(transaction_date)) as abc
group by user_id
having count(user_id)>1) as bcd;
The query that you already have could be written without a subquery:
select user_id, count(distinct date(transaction_date)) count
from transactions
group by user_id
having count(distinct date(transaction_date))>1;
So what you need now can be written with only 1 subquery:
select count(*) count
from (
select user_id
from transactions
group by user_id
having count(distinct date(transaction_date))>1
) t
You can get the same result with EXISTS:
select count(distinct t.user_id) count
from transactions t
where exists (
select 1
from transactions
where user_id = t.user_id and date(transaction_date) <> date(t.transaction_date)
)
See the demo.

How to select a few rows of Running total

I have running total
SELECT
id,
DepositValue,
action_date,
SUM(DepositValue) OVER(ORDER by action_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS Running_total
The above select returns me the following:
id action_date DepositValue Running_total
1 2020-04-01 20 20
2 2020-04-02 2 22
3 2020-04-03 8 30
4 2020-04-04 10 38
5 2020-04-05 14 48
6 2020-04-06 15 62
7 2020-04-07 22 77
8 2020-04-08 12 99
9 2020-04-09 4 103
What i want to achieve is selecting only part of Running_total depend on action_date with already calculated values like this.
id action_date DepositValue Running_total
3 2020-04-03 8 30
4 2020-04-04 10 38
5 2020-04-05 14 48
You can turn your query to a subquery and filter in the outer query:
SELECT *
FROM (
SELECT
id,
DepositValue ,
action_date,
SUM(DepositValue) OVER(ORDER by action_date) AS Running_total
FROM mytable
) t
WHERE action_date BETWEEN '2020-04-03' AND '2020-04-05'
Note that window specification ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW is actually the default when not specificed, hence you can just remove it.
Also, your original query was missing a FROM clause, I added it.

mysql select 2 table based on date and combine each other

i Need some help.. i have some case like this below
i have 2 table.. call ("in_table" and "out_table")
data "in_table" look like
stock_id item_id date qty_in
-----------------------------------------
1 11 2017-07-11 12
2 11 2017-07-11 10
3 12 2017-07-11 10
4 12 2017-07-19 10
And i have "out_table" is like
id_tr item_id date qty_out
-------------------------------------
1 11 2017-07-19 2
1 12 2017-07-19 1
2 11 2017-07-19 2
2 12 2017-07-19 1
And i want to combine the date and display all the data like this,
Update: the join is by item_id but i want to select by date
date item_id qty_in qty_out
---------------------------------------
2013-07-11 11 22 0
2013-07-11 12 10 0
2013-07-19 11 0 4
2013-07-19 12 10 2
Thank you for your help.
It looks like you need kind of a full outer join of two aggregate subqueries. But in your case I would get item_id and date in a union subquery (derived table) and the sums in correlated subqueries (subselect).
select item_id, date,
(select sum(qty_in) from in_table i where i.item_id = sub.item_id and i.date = sub.date) as qty_in,
(select sum(qty_out) from out_table o where o.item_id = sub.item_id and o.date = sub.date) as qty_out
from (
select item_id, date from in_table
union
select item_id, date from out_table
) sub
order by date, item_id