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
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:
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)
I have a table with count by product. I am trying to add a new column that would find % as compared to sum of all rows in that column.
prod_name,count
prod_a,100
prod_b,50
prod_c,150
For example, I want to find % of prod_a as compared to the total count and so on.
Expected output:
prod_name,count,%
prod_a,100,0.33
prod_b,50,0.167
prod_c,150,0.5
Edit on SQL:
select count(*),ratio_to_report(prod_name)
over (partition by count(*))
from sales
group by prod_name;
Using window functions.
select t.*,100.0*cnt_by_prod/sum(cnt_by_prod) over() as pct
from tbl t
Edit: Based on OP's question change, To compute the counts and then percentage, use
select prod_name,100.0*count(*)/sum(count(*)) over()
from tbl
group by prod_name
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;