UDF for calculating median of a column in a table - pl/sql - plsql

Please help me.
I need an UDF for calculating median of a column (salary) in ascending order.
I only need user defined function for calculating median.

A general query to find a median value is to use one of the windowing functions :
PERCENTILE_CONT
PERCENTILE_DISC
With the WITHIN GROUP clause associated.
As an example, to find the median salay from a table of salary, you can write :
SELECT *, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) OVER () AS MEDIAN_SALARY
FROM SalariesTable

Related

Using group_by to determine median of second column after sorting/filtering for a specific value in first column?

I have a huge dataset which has been difficult to work with.
I want to find the median of a second column but only based on one value in the first column. I have used this formula to find general medians without specifying/sorting by the specific values in the first column:
df%>% +group_by(column1)%>% +summarise(Median=median(colum2))
However, there is a specific value in column1 I am hoping to sort by and I only want the medians of the second column based on this first value. Would I do something similar to the below?
df%>% +group_by(column1, specificvalue)%>% +summarise(Median=median(colum2))
Is there an easier way to do this? Would it be easier to make a new dataframe based on the specific value in the first column? How would that be done so that I could have column 1 only include the specific value I want but the rest of the rows included so I can easily determine the median of column2?
Thanks!!

How to find the max value of a specific column in a matrix

I am new to R. Many thanks in advance for your help.
I am trying to find the maximum value of a single (the first) column in a matrix, "bin.matrix". Of course, I have been able to find the max value for all columns using:
apply(bin.matrix, 2, max)
But I can't seem to figure out how to get the value for just the first column. It's a homework question, so just reading the first value won't do unfortunately. The next question asks for the max value in all but the first column.
Thanks again for your help.
We can select the first column by subsetting with numeric index and get the max
max(bin.matrix[,1])

Selecting multiple maximum values? In Sqlite?

Super new to SQLite but I thought it can't hurt to ask.
I have something like the following table (Not allowed to post images yet) pulling data from multiple tables to calculate the TotalScore:
Name TotalScore
Course1 15
Course1 12
Course2 9
Course2 10
How the heck do I SELECT only the max value for each course? I've managed use
ORDER BY TotalScore LIMIT 2
But I may end up with multiple Courses in my final product, so LIMIT 2 etc won't really help me.
Thoughts? Happy to put up the rest of my query if it helps?
You can GROUP the resultset by Name and then use the aggregate function MAX():
SELECT Name, max(TotalScore)
FROM my_table
GROUP BY Name
You will get one row for each distinct course, with the name in column 1 and the maximum TotalScore for this course in column 2.
Further hints
You can only SELECT columns that are either grouped by (Name) or wrapped in aggregate functions (max(TotalScore)). If you need another column (e.g. Description) in the resultset, you can group by more than one column:
...
GROUP BY Name, Description
To filter the resulting rows further, you need to use HAVING instead of WHERE:
SELECT Name, max(TotalScore)
FROM my_table
-- WHERE clause would be here
GROUP BY Name
HAVING max(TotalScore) > 5
WHERE filters the raw table rows, HAVING filters the resulting grouped rows.
Functions like max and sum are "aggregate functions" meaning they aggregate multiple rows together. Normally they aggregate them into one value, like max(totalscore) but you can aggregate them into multiple values with group by. group by says how to group the rows together into aggregates.
select name, max(totalscore)
from scores
group by name;
This groups all the columns together with the same name and then does a max(totalscore) for each name.
sqlite> select name, max(totalscore) from scores group by name;
Course1|15
Course2|12

Which function in SPSS emulates R summary() function?

I'm switching from R to SPSS for a specific project (I'm not allowed to use SPSS/R integration) and need to summarize quickly a big dataset. In R, it's quite simple, one can use the summary() function and in few seconds obtain the summary of each variable.
I would need to know if there is a function in SPSS that do the same job. If not, how could I achieve it.
For the non-R users summary.default would return labelled values for Min. , 1st Quartile, Median, Mean , 3rd Quartile, Max. for each numeric column and a counts of the 6 most common items and the count of the "(Other)" category if a factor or character variable.
Descriptives comes close.
descriptives var1 var2 var3
/statistics = mean median stddev variance min max .
(I'm not sure about quartiles).
If you have a mixture of continuous and categorical variables, use DESCRIPTIVES or SUMMARIZE for continuous and FREQUENCIES for categorical. You can use the SPSSINC SELECT VARIABLES extension command installed with Statistics to create macros listing variables according to the measurement level and then use the appropriate macro for each command.

How to get max value of a column based on value of another column?

In my Database I have a table called ARTICLES. In ARTICLES I have 2 columns.
Now I want to get the max value of NRRENDORDOK column where the TIP column has value 'A'.
e.g. the max number of cells A in TIP column is 8.
Any help will be appreciated.
SELECT tip, MAX(nrreddordok) FROM table
where tip='A'
GROUP BY tip
Use SQL MAX() aggregate function
select Max(NRRENDORDOK) from table where tip='A'
You should take use of the MAX function and then GOUP BY tip in order to get the max value for each tip:
SELECT tip, MAX(nrreddordok) FROM table GROUP BY tip
If you just want values for A then you can use following query:
SELECT MAX(nrreddordok) FROM table WHERE tip = 'A'
SELECT TOP 1 nrreddordok
FROM TableName
WHERE Tip = 'A'
ORDER BY nrreddordok DESC

Resources