i wanna get the five of the four columns with the highest value in the table.(sorry, pool english, google translate)
nodejs, sequelize, postgresql
i tried to select 20 rows and order by it try to get each highest value top 5 but faild. order by can't performed exactly what i want, only worked in the first order others failed
select * from page_item_detail as detail group by detail.id, detail.like,
detail.collection, detail.share, detail.discuss ORDER BY detail.like DESC,
detail.collection DESC, detail.share DESC, detail.discuss DESC limit 20
i expect to get data like this
like: [top five highest value],
collection: [top five highest value],
share: [top five highest value],
discuss: [top five highest value],
sequelize function better, or sql language.
thanks a lot~
Related
I have four groups of people, and they each have an average for a given metric. The following query would yield four values, one for each group.
SELECT group, AVG(metric) AS 'avg_metric'
FROM table
GROUP BY group
Now one of those averages will be the max. I want to also capture the avg_metric / MAX(avg_metric) in my SELECT statement. Since I can't use MAX and AVG(MAX...) in the same query, I thought something like this would work:
SELECT group, (100 * sub.avg_metric / MAX(sub.avg_metric)) AS 'percentage'
FROM table
JOIN (SELECT AVG(metric) AS 'avg_metric'
FROM table
GROUP BY group
ON table.group = sub.group) AS sub
Unfortunately, I can't seem to get the syntax right. (I'm using SQLite.) Additionally, it would be much nicer to have
GROUP AVG_METRIC PERCENTAGE
group 1 57 45
group 2 ....
In that order, but I don't see how to do that either.
I have the following sample from a table with students results with date for a school entry exam
First student passed exam - This is the most common record found for most students
Second student failed 1st time entry and passed second time based on the date
3rd student had a failed input entry and was corrected based on the Version
I need the results to like like the picture above, so we take into regard using the latest date and highest version!
My basic query thus far is
select studentid
,examdate --(Date)
,result -- (charvar)
from StudentEntryExam
How should I approach this issue?
demo:db<>fiddle
SELECT DISTINCT ON (studentid)
*
FROM mytable
ORDER BY studentid, examdate DESC, version DESC
DISTINCT ON returns the first record of an ordered group. In that case the groups are the studentids. You must find the correct order to set the required record first. So, you need to order by studentid, of course. Then you need the most recent examdate first, which can be achieved with DESC order. If there are two records on the same date, you need to order the highest version first as well using the DESC modifier, too.
Question is regarding how to get top x% of records according to their ratings.
For example I have a table with a few columns, one of which is rating:
rating smallint
value of rating is always positive.
My goal is to select top x% of entries according to their rating.
For example, for top 20%, if set of selected rows contains ratings like:
1,3,4,4,5,2,7,10,9
Then top 20% would be records with range from 8 to 10 → records with rating 9 and 10.
I implemented it in Django but it takes 2 calls to DB and I believe it can be easily achieved via SQL in PostgreSQL by just one call.
Any ideas how to implement it?
Considering that the max rating available in the column is your base for max calculation.
Try this workaround:
select * from sample where rating >=(select max(rating)-max(rating)*20/100 from sample)
Demo on fiddle
For my company, I am writing a query that is aggregating/counting our inventory by part number. This works well and returns a data set that shows the volume we have in inventory for each part number.
However, because we have a large number of part numbers where the volume is just 1, I would like to exclude any such part number from the results when the count falls under a certain threshhold, like 5.
I've looked at the FETCH, LIMIT, and other functions but they don't seem to capture what I need.
select "Part#", "Description", "Cond_Code", "PO_NUM", count(*)
from "Inventory"
group by "Part#","Description","Cond_Code", "PO_NUM"
Order by "count" DESC,"Part#", "Description","Cond_Code", "PO_NUM"
This gives me the results where each part number total is displayed, but as I said, I would like to display only part numbers where the totals meet a certain minimum threshhold.
You need a HAVING clause:
select "Part#", "Description", "Cond_Code", "PO_NUM", count(*) "Counter"
from "Inventory"
group by "Part#","Description","Cond_Code", "PO_NUM"
having count(*) > 4
order by "Counter" desc, "Part#", "Description","Cond_Code", "PO_NUM"
How come in my code when i use #"select Score from tblMed order by Score desc limit 10" everything works fine, but when I change "desc" to "asc" the scores don't show up???
Because:
ORDER BY score ASC
...means that the lowest values will be at the top of the list - adding LIMIT 10 to the query will return the first 10 records with the lowest scores, so I imagine you have entries with score values of zero and/or null.
Do you have NULL scores in your records?
If you change the ordering to asc, then it would start with NULLs before selecting records with an actual value.