I have a sql query that is outputting:
col1 col2
A 1
B 3
C 4
D 5
Is there a way to add a column where it outputs the sum of all the numbers in col2?
col1 col2 col3
A 1 13
B 3 13
C 4 13
D 5 13
You can use window functions:
select col1, col2, sum(col2) over () as col3
from t;
Related
Suppose my table has below 3 columns:
Col1 Col2 Col3
2 A A
3 B A
4 C A
4 D B
5 E B
6 F B
Output
Col1 Col2 Col3
9 A A
9 B A
9 C A
15 D B
15 E B
15 F B
I want grouping based on column 3 and want to sum first 3 rows and they should show as 9,9,9 in output. I want to fetch all records from column 2 also.
Select Sum(Col1), Col2, Col3 from Table
Group by Col2, Col3
I have used group by and over (). Not able to get this output.
Kindly let me know how this can be achieve.
We can try using SUM() as an analytic function here:
SELECT SUM(Col1) OVER (PARTITION BY Col3) AS Col1, Col2, Col3
FROM yourTable
ORDER BY Col2;
I have the following table with Postgres:
Id Col1 Col2 Col3
1 A 1 x
2 A 0 y
3 A 0 z
4 B 0 x
5 B 1 y
6 C 0 z
As part of a select query, I want to be able to drop duplicates in Col1, based on the highest Col2 values (where will never be multiple highest values per Col1 value), and keep the corresponding Col2, Col3 values.
Desired output:
Id Col1 Col2 Col3
1 A 1 x
5 B 1 y
6 C 0 z
In Postgres, you can use distinct on:
select distinct on (col1) t.*
from t
order by col1, col2 desc;
Let's suppose a have a very simple query in SQL
SELECT Col1,Col2 From Table1
and it gives me result:
Col1 Col2
A 5
A 7
A 2
B 1
B 1
B 4
B 0
C 4
C 1
C 2
I want to count rows in groups made by Col1 and in order made by Col2. If values in Col2 for some rows in group are equal then they should have different numbers, as shown in example
So I want to have
Col1 Col2 Nr
A 5 2
A 7 3
A 2 1
B 0 1
B 1 2
B 1 3
B 4 4
C 4 3
C 1 1
C 2 2
Any ideas how to make it?
If your database supports window functions, use ROW_NUMBER
select col1,col2,row_number() over(partition by col1 order by col2) as nr
from tablename
If your database doesn't support window functions, use
select col1,col2,
(select count(*)+1 from tablename t1 where t1.col1=t.col1 and t1.col2<t.col2) as nr
from tablename t
You can use the row_number window function:
SELECT col1,
col2,
ROW_NUMBER() OVER (PARTITION BY col1 ORDER BY col2 ASC) AS Nr
FROM table1
ORDER BY 1, 2, 3
Let's say I have the dataset that looks like:
col1 col2 col3
a 2 20
a 3 12
a 4 34
b 2 44
c 3 23
c 5 13
....
What I want is a count of col1.
Output:
col1 col2 col3 count
a 2 20 3
a 3 12 3
a 4 34 3
b 2 44 1
c 3 23 2
c 5 13 2
.......
I know I can do by:
with cte as (
select col1, count(*) count
from tab1)
select a.col1,a.col2,a.col3,cte.count
from tab1
join cte on a.col1=cte.col1
But is there any other I can do that without cross apply or cte?
Also, assuming there are more than 3 letters in col1, so I couldn't use sum function either:
SUM(CASE WHEN ItemID = 'a' THEN 1 ELSE 0 END) AS count_a
If you're using SQL Server 2008+, you can use COUNT() OVER():
SELECT *,
COUNT(*) OVER(PARTITION BY col1)
FROM tab1
ONLINE DEMO
I have a query, that I'm looking to select a column that doesn't exist and just fill it with "NULL" in the results. The data is being exported to a .CSV File for import to another database which has the column. For example:
My query is:
Select col1, col2, col3
from table1
Output is:
col1 col2 col3
1 5 9
2 6 10
3 7 11
4 8 12
I'd like the output to be:
col1 col2 col3 col4
1 5 9 NULL
2 6 10 NULL
3 7 11 NULL
4 8 12 NULL
You can select a null literal:
SELECT col1, col2, col3, NULL AS col4
FROM mytable
Select col1, col2, col3, null as col4 from table1