This question already has answers here:
How to round an average to 2 decimal places in PostgreSQL?
(8 answers)
Changing data type to float and rounding to 2 decimal digits
(2 answers)
Closed 1 year ago.
How do you use MAX and ROUND at the same time?
i.e.
SELECT
customer_id,
MAX(some_number)
FROM
customer_data
GROUP BY
customer_id;
60.2354125
Would like to return the result of 60.24
You would write this as:
SELECT customer_id, ROUND(MAX(some_number), 2)
FROM customer_data
GROUP BY customer_id;
EDIT:
Based on your comment, you can convert to a numeric first:
SELECT customer_id, ROUND(MAX(some_number)::numeric, 2)
FROM customer_data
GROUP BY customer_id;
Thanks for pointing me in the right direction #Gordon. The solution was casting as numeric
round(MAX(cast(some_number as numeric)),2)
Related
This question already has answers here:
Why can't I use column aliases in the next SELECT expression?
(4 answers)
Closed 3 months ago.
I'm trying to avoid writing twice the EXTRACT condition. There is any way to do that? Count over the previous alias apparently is not valid.
SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade,
count(decade) AS total_by_decade
FROM album ...
Basically you can avoit it completely, but you can make a CTE
WITH
CTE as(
SELECT EXTRACT(DECADE FROM to_date(released_year::text, 'yyyy')) AS decade
FROM album ...)
SELECT decade, count(decade) AS total_by_decade FROM CTe GROUP BY decade
This question already has answers here:
Fetch the rows which have the Max value for a column for each distinct value of another column
(35 answers)
GROUP BY with MAX(DATE) [duplicate]
(6 answers)
Select First Row of Every Group in sql [duplicate]
(2 answers)
Oracle SQL query: Retrieve latest values per group based on time [duplicate]
(2 answers)
Return row with the max value of one column per group [duplicate]
(3 answers)
Closed 1 year ago.
I want to select starting semester of the student.
select distinct (stdcode),altcode,name,admdate,sem_code
,min(startsem)
from V_ALLSTUDATAAA
GROUP BY stdcode,altcode,name,degree_code,sem_code
order by altcode;
Desired Results:
Sample Data is attached below.
https://docs.google.com/spreadsheets/d/1-oqXgGfhIiLwWKLpUx94P9n1hXUAUE3dn3jAPV8HQ_k/edit?usp=sharing
One option is to use a correlated subquery; something like this:
select a.stdcode, a.altcode, a.name, a.admdate, a.degree_code, a.sem_code, a.startsem
from v_allstudataaa a
where a.startsem = (select min(b.startsem)
from v_allstudataaa b
where b.stdcode = a.stdcode
);
This question already has answers here:
Function to Calculate Median in SQL Server
(37 answers)
Closed 4 years ago.
Please assist me, I need to pick 790 since its in middle but not able to figure it out.
id Value
12 780
123 796
124 790
Thank you in advance
This should help to get what you want. Get the average value from your table, then get first record with smallest difference with average value.
SELECT TOP 1 id, value FROM yourTable
ORDER BY ABS(value - (SELECT AVG(value) FROM yourTable))
or using variables, depending on the size of your table
DECLARE #median DECIMAL
SELECT #median = AVG(value) FROM yourTable
SELECT TOP 1 * FROM yourTable
ORDER BY ABS( value - #median)
This question already has answers here:
Product() aggregate function
(3 answers)
Closed 6 years ago.
I'd like to be able to create my own GROUP BY function. Like one has AVG() or SUM() functions which can be used in GROUP BY where average and sum is calculated from values in each particular group, I'd like to be able to create my own function which will calculate something from values in group. Let's say I'd like to create MYFN() function which will calculate Σx^2/n (x is value in group and n number of values in group). The usage of this function should look like this:
SELECT mygroup,MYFN(x)
FROM mytable
GROUP BY mygroup
ORDER BY 2 DESC
How to do that?
SELECT mygroup, sum(ColFun) As ColFun FROM
(
SELECT mygroup,MYFN(x) As ColFun FROM mytable
)Y
GROUP BY mygroup
This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 8 years ago.
I have a table in sql server and I want to summarize a cell with upper rows in T-Sql like this:
Quantity Total
-----------------------------
1 1
5 6
12 18
20 38
I use SQL server 2008, How can I achieve this?
Regards, Majid.
You are looking for a cumulative sum, it would appear. If you are using SQL Server 2012 or later, just do:
select quantity, sum(quantity) over (order by quantity)
from table t;
Note the order by quantity. You need to specify the ordering for the cumulative sum, typically doing another sum.
In earlier versions, you can do this using a correlated subquery:
select quantity,
(select sum(t2.quantity)
from table t2
where t2.quantity <= t.quantity
) as total
from table t;