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.
Related
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.
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~
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"
So, simple question, I have combo box in my database and I want items that I have selected most frequently to appear first the next time that I add a record.
I would suggest adding a Long Integer field to the table consituting the row source for your combobox, and incrementing the value held by such field either on the AfterUpdate event of the combobox, or following the main operation being performed by your form.
Then, sort the items in the combobox by this new field in descending order.
I assume that your combo box selects a lookup value in for a property (PropertyID) that is saved in a table (MainTable).
You can get the number of times this property has been selected with
SELECT PropertyID, COUNT(*) AS SelectedTimes
FROM MainTable
GROUP BY PropertyID
Now get the sorted lookup table by using this query as sub-query:
SELECT L.PropertyID, L.Name
FROM
LookupTable L
( SELECT PropertyID, COUNT(*) AS SelectedTimes
FROM MainTable
GROUP BY PropertyID) X
ON L.PropertyID = X.PropertyID
ORDER BY X.SelectedTimes DESC, L.Name
I'm also sorting by name in case two entries have the same count.
As usage may change over time, you should record the time when an item was selected. Then you can weight the usage, so recent usage of an item have higher weight than those items used, say, a year ago.
Then you can run a query to list the usage having the most recently used items at top:
Select Item, Sum(1 / DateDiff("h", [SelectedTime], Now())) As Usage
From ItemUsage
Group By Item
Order By Sum(1 / DateDiff("h", [SelectedTime], Now())) Desc
Of course, this linear weighting may be too simple. You can apply any math to the usage like square or log.
I want average but its showing null. I want the average of score present in different ids
select avg(score*100)from daily_stats1 where id=10 and id=11
Just get rid of null values using coalesce function:
select avg(coalesce(score, 0)*100)from daily_stats1 where id=10 and id=11