How to select 3 of the latest value on "sum" column - plsqldeveloper

I want to get the latest "point_dttm" 3 value on "sum" column specific on the combination of "chart" and "equip" column
i have below a working code using max to get the latest 1 value on "sum" column specific on the combination of "chart" and "equip" column. What I want now is to get the 3 latest value. I've tried rownum <= 3 order by point_dttm asc but it only gives 3 values not considering the combination of "chart" and "equip" column.
select a.chart,a.equip,a.point_dttm,a.sum
from spc_point a,
(select chart, equip, max(point_dttm) as point_dttm from spc_point
where chart like 'TX%'
and equip like 'MP%'
group by chart,equip) b
where b.equip = a.equip
and a.chart = b.chart
and a.point_dttm = b.point_dttm
order by a.equip asc
I want to get:
image
but rownum <=3 will give you only this:
image

Try the below code
SELECT chart,
equip,
point_dttm,
SUM
FROM (SELECT a.chart,
a.equip,
a.point_dttm,
a.SUM,
RANK ()
OVER (PARTITION BY a.chart, a.equip, a.point_dttm
ORDER BY a.SUM DESC)
rk
FROM spc_point a)
WHERE rk <= 3;

Related

Finding the average of a column with a variable condition in the where clause

Suppose I have a table named Data:
id
value
1
4
2
8
.
.
.
.
.
.
50
10
And I want to find the first 'id' index (nFirst) where the average of the 'value' column is above some pre-given value ('avg'). In other words I want to solve for nFirst. I have this query
SELECT AVG(value)
FROM Data
WHERE id < nFirst AND AVG(value) > avg
GROUP BY id;
The above query has the general idea of what I'd like to calculate but it obviously won't work. Any ideas?
For MySql 8.0+ use AVG() window function to get the running average for each row and filter out the rows with less than the average that you want and finally pick the smallest id of the results:
SELECT *
FROM (
SELECT id, AVG(value) OVER (ORDER BY id) avg_value
FROM Data
) d
WHERE avg_value > ?
ORDER BY id LIMIT 1;
For previous versions use a self join and aggregation:
SELECT d1.id, AVG(d2.value) avg_value
FROM Data d1 INNER JOIN Data d2
ON d2.id <= d1.id
GROUP BY d1.id
HAVING avg_value > ?
ORDER BY d1.id LIMIT 1;
Replace ? with the average value that you want.
If you have DB version 8.0+, then use SUM() OVER () window function, presuming all id values are consecutively incrementing as in the shared example data set, in such a way
SELECT MIN(id) AS least_id
FROM
(SELECT id, SUM(value) OVER (ORDER BY id)/id AS avg_
FROM Data) AS d
WHERE avg_ > avg --> substitute 5 as an example value for the parameter "avg"
Demo

How to return the row value based on condition of comparing 2 columns

I wanna write a MySQL query to extract the data as below:
Return the row with the transaction date < Moved date. In the case of item B, it's clear that there is only 1 row with transactoin date < Moved Date.
However, with item A, if transaction date < moved date of 2 rows, return the row with the min transaction date
Original table:
Desired Table:
Thank you
Your rules seem to be:
select t.*
from (select t.*,
row_number() over (partition by item order by transaction_date) as seqnum
from t
) t
where seqnum = 1 and transaction_date < moveddate;
You can have simple subquery to get this:
SELECT * FROM TABLE1 WHERE (MOVEDDATE, ITEM) IN ( SELECT MIN(MOVEDDATE), ITEM
FROM TABLE1 A GROUP BY ITEM);

Mysql SELECT function inside mysql LIMIT function

SELECT COUNT('2018-02-12')
as ids FROM tg_partner_data
WHERE assign_to = '2'
AND
followup_date = '2018-02-12' AND active='Y'
LIMIT (SELECT count FROM tg_master_count WHERE count_for = 'followup')
I tried to get count as output but this query doesnot works . I am using like this because the limit setting is dynamic.
An immediate problem with your use of LIMIT is that you have no ORDER BY clause. Which first records do you expect from the result set? This is not clear. I will give a generic pattern which you can use to have a subquery limit the number of records.
Consider a simple table with just one column:
col
1
2
3
4
5
Now let's say that you want to limit the number of records in the result set, based on an ordering of col, using some subquery. We can write the following:
SET #rn=0;
SELECT col
FROM
(
SELECT
col,
#rn:=#rn+1 rn
FROM yourTable
ORDER BY
col
) t
WHERE t.rn <= (SELECT 3 FROM dual); -- replace with your own subquery
Demo
The basic idea here is that we simulate row number in a subquery, that is, we assign a row number to each record, ordered according the the col column. Then we subquery that table, and retain only records matching the subquery you want to use to limit.

Get highest percentage value from a column based on value from another column in mySQL

I have this query to get the percentages of different values in a specific column:
select dma, count(*) / const.cnt *100 as my_percent
from full_db3 cross join
(select count(*) as cnt from full_db3) const
group by dma
order by my_percent desc limit 10;
Step one: I need to amend this so it gives me the top 10 values in the column dma based on the value of another column. For example, if the value in column gender is "f", what are the top 10 values in column dma. Something like this (which doesn't work, of course).
select dma, count(*) where gender = 'female' / const.cnt *100 as my_percent
from full_db3 cross join
(select count(*) as cnt from full_db3) const
group by dma
order by my_percent desc limit 10;
The above throws an error, but should get the point across.
Step 2 would be looping through all my columns and returning the top 10 values in every column (not just dma) give the value of a specific column (e.g., gender= 'female'). What's the best way to do this?

select sum where sum

I want to select some entries based on a max+sum condition.
mytable
----------
id | col1 | col2
I want to select all entries that have the sum of col1 & col2 greater than or equal to the max of sum minus X. (don't ask me why :) )
So far I managed to get the sum OK (hereafter aliased as "total") with:
SELECT id,SUM(col1 + col2) AS total FROM mytable GROUP BY id;
I also managed to get the MAX of the sum OK (with a ORDER BY/LIMIT workaround though):
SELECT id,SUM(col + col) as total FROM mytable GROUP BY id ORDER BY total DESC LIMIT 1;
However everytime I try to re-use my alias as a condition (e.g. WHERE total >= ...) I get an "Unknown column" error
Anything would be greatly appreciated
You have some misconceptions about SUM. SUM is an aggregating function, means it works on many records and not just one.
To calculate the sum of two fields per record, you should use only the + operator.
SELECT id, col1+col2 AS 'total'
FROM T1
WHERE
(col1+col2+x) >=(SELECT MAX(col1+col2) from T1)
If you are using group by, you'll need to use a having clause:
SELECT id,SUM(col1+col2) as total FROM mytable GROUP BY id ORDER BY total HAVING total >= x