SQL Aggregate function based on 1 column and display the remaining - sql

I am trying to sum the values for 1 column and group it by only 1 column but show the other columns as well.
Basically, if the data looks like:
Column1 Column2 Column3 Column4
----------------------------------
Col1Data0 Test1 Test2 1
Col1Data0 Test1 Test2 3
Col1Data1 Test1 Test2 2
Col1Data1 Test1 Test2 5
The query should output:
Column1 Column2 Column3 Column4
Col1Data0 Test1 Test2 4
Col1Data0 Test1 Test2 4
Col1Data1 Test1 Test2 7
Col1Data1 Test1 Test2 7

You can use a SUM window function:
Test data:
declare #t table (Column1 varchar(10), Column2 varchar(10), Column3 varchar(10), Column4 int)
insert into #t values
('Col1Data0','Test1','Test2',1),
('Col1Data0','Test1','Test2',3),
('Col1Data1','Test1','Test2',2),
('Col1Data1','Test1','Test2',5)
Query:
SELECT Column1, Column2, Column3,
SUM(Column4) OVER (PARTITION BY Column1) AS SumColumn4
FROM #t
Returns:
Column1 Column2 Column3 SumColumn4
Col1Data0 Test1 Test2 4
Col1Data0 Test1 Test2 4
Col1Data1 Test1 Test2 7
Col1Data1 Test1 Test2 7

You can use a subquery to get the sum for column 1 and join it to get the output
Select a.Column1,a.Column2,a.Column3,b.summ
FROM Table a
INNER JOIN (Select column1,sum(Column4) summ from Table group by column1) b
on b.column1 = a.column1

use scalar subquery
select Column1,Column2,Column3,
(
select sum(Column4) from tablename b
where a.Column1=b.Column1 and a.Column2=b.Column2 and a.Column3=b.column3
) as column4
from tablename a

Related

select * using Group By Oracle SQL

I have this tables
column1 column2 colum3 column4 colum5 column6
a b 1 2 0 x
a b 1 2 0 y
a b 3 0 0 z
I want to group the same record
my sql code looks like this but it gives the same result (select * from employee where column1 = 'a' and column2= 'b')
SELECT a.column1,a.column2,a.column3,a.column4,a.column5
FROM employee a,
(SELECT column3, column4,column5
FROM employee
WHERE column1 = 'a' and column2= 'b'
GROUP BY column3 colum4 column5) b
WHERE a.column3 = b.column3,
and a.column4 = b.column4,
and a.column5 = b.column5,
the result should be like this:
column1 column2 colum3 column4 colum5
a b 1 2 0
a b 3 0 0
Try the following with window function row_number(), here is the demo.
select
column1,
column2,
colum3,
column4,
colum5
from
(
select
column1,
column2,
colum3,
column4,
colum5,
row_number() over (partition by column1, column2, colum3, column4, colum5 order by column1) as rnk
from myTable
) val
where rnk = 1
Output:
*---------------------------------------*
| COLUMN1 COLUMN2 COLUM3 COLUMN4 COLUM5|
*---------------------------------------*
| a b 1 2 0 |
| a b 3 0 0 |
*---------------------------------------*
For this dataset, a simple distinct on the first five columns would return the result that you expect:
select distinct column1, column2, column3, colum4, column5
from employee

Show 2 columns in 2 different rows - SQL

I need to separate two columns into two rows in sql
I have this:
Column1 Column2 Column3
Car 2 5
Boat 4
Truck 6
And I want this:
Column1 Column2
Car 2
Car 5
Boat 4
Truck 6
How can I do this in SQL?
This operation is unpivoting. I would recommend apply:
select t.column1, v.col
from t cross apply
(values (col1), (col2)) v(col)
where v.col is not null;
This should do it
select Column1, Column2
from tbl where Column2 is not null and Column2 <> ''
union
select Column1, Column3
from tbl where Column3 is not null and Column3 <> ''

MS SQL Server select rows with max values

Good day
I have Table1:
COLUMN1 COLUMN2 Column3
----------------------------
Eva Apple 1
Eva Apple 2
Eva Apple 3
Eva Apple 4
Eva Apple 5
Eva Apple 6
Bob Apple 1
Bob Samsung 1
Bob Samsung 2
... ... ...
I need
COLUMN1 COLUMN2 Column3
----------------------------
Eva Apple 6
Bob Samsung 2
Bob Apple 1
... ... ...
How i can setup string for select only rows with MAX values in Column3 ?
My version of string is :
SELECT MAX(Column3) , [column2], [Column2]
FROM Table1
WHERE Column3 = MAX ;
Thanks for Opinions
You can use row_number
Select top (1) with ties *
from table1
order by row_number() over (partition by Column1, Column2 order by Column3 desc)
Other way is to use outer query:
Select * from (
Select *, RowN = row_number() over (partition by Column1, Column2 order by Column3 desc) from table1 ) a
Where a.RowN = 1
You want to find the maximum Column3 for each combination of Column1 and Column2.
You can achieve this with a GROUP BY
SELECT Column1, Column2, MAX(Column3)
FROM Table1
GROUP BY Column1, Column2
See https://learn.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql
select * from (
SELECT *, rn=ROW_NUMBER() over (partition by COLUMN1,COLUMN2,Column3 order by
Column3 desc)
FROM Table1
)
WHERE rn=1
Please try the following:
WITH B AS
(
SELECT
Column1, Column2, Column3,
ROW_NUMBER() OVER (PARTITION BY Column1, Column2 ORDER BY Column3 DESC) AS row_num
FROM
Table1
)
SELECT Column1, Column2, Column3
FROM B
WHERE row_num = 1
You need to add a group by. In queries of this type, you have a set of columns you want the values from (these are the columns you group by) and you have other columns most of whose values you'll throw away. You use a function like MAX, MIN, SUM, AVG to specify what to do with the data from rows that are "thrown away". The result is a unique set of values from the columns that were grouped, and a single value corresponding to the min/max/avg etc from the columns that were not grouped:
SELECT [column1], [Column2], MAX(Column3) as Column3
FROM Table1
GROUP BY Column3 ;
Add a Group by on column 3.
Select col1, col2, max(col3)
from test
Group By col3
You can use a function like DENSE_RANK().
In this example, if there are duplicate values you want to retrieve.
declare #t as table (COLUMN1 char(3), COLUMN2 varchar(10), COLUMN3 int)
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Eva', 'Apple' ,1
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Eva', 'Apple' ,2
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Eva', 'Apple' ,3
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Bob', 'Apple' ,1
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Bob', 'Samsung' ,1
INSERT #t (COLUMN1 , COLUMN2 , COLUMN3 ) select 'Bob', 'Samsung' ,2
SELECT * FROM (
SELECT DENSE_RANK() OVER (PARTITION BY COLUMN1, COLUMN2 ORDER BY COLUMN1, COLUMN3 desc) [Max] , * from #t ) as T
WHERE max = 1 -- Set here what position do you want
Order by COLUMN3

MS SQL count AS to new column

Good day
I have problem with my table and counting
TABLE1
COLUMN1 COLUMN2
3 jjd
5 jd
3 jjd
4 kg
5 jd
48 gjh
446 djj
… …
I need
TABLE1
COLUMN1 COLUMN2 COLUMN3
3 jj 2
5 jd 2
4 kg 1
48 gjh 1
446 djj 1
... ... …
Iam doing but not working well.
SELECT * , COUNT(Column1) as column3 FROM TABLE1
Thanks for help withh my counting
Use a GROUP BY and ORDER BY with DESC to put them in COUNT total order.
SELECT COLUMN1, COLUMN2, COUNT(Column1) AS COLUMN3
FROM Table1
GROUP BY COLUMN1, COLUMN2
ORDER BY COUNT(Column1) DESC
Output
COLUMN1 COLUMN2 COLUMN3
5 jd 2
3 jjd 2
4 kg 1
446 djj 1
48 gjh 1
SQL Fiddle: http://sqlfiddle.com/#!6/89f49/4/0
Try by using group by
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1) As COLUMN3 FROM cte_TABLE1
Group by COLUMN1 ,COLUMN2
Order by COLUMN1
By using Window function
SELECT DISTINCT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Result
COLUMN1 COLUMN2 column3
-----------------------
3 jjd 2
4 kg 1
5 jd 2
48 gjh 1
446 djj 1
Using OVER we can achieve it easily
SELECT COLUMN1 ,
COLUMN2 ,
COUNT(Column1)OVER(Partition by COLUMN1,COLUMN2 ORder by COLUMN1 ) As COLUMN3 FROM cte_TABLE1
Group By COLUMN1,COLUMN2

update same rows

I have a table like below:
Current
Column1 Column2 column3
-------------------------
abc cat1 1
efg cat1 3
hij cat1 2
klm cat2 1
nop car2 2
qrs cat2 3
I want to update column1 where all rows matching cat1 from rows matching cat2 , considering rows will be updated for id which matches column 3 ids
Expected
Column1 Column2 column3
-------------------------
klm cat1 1
qrs cat1 3
nop cat1 2
klm cat2 1
nop car2 2
qrs cat2 3
Use below query :
CREATE TABLE #Table (Column1 VARCHAR(100),Column2 VARCHAR(100), Column3
VARCHAR(100))
INSERT INTO #Table ( Column1 , Column2 , Column3 )
SELECT 'abc','cat1',1 UNION ALL
SELECT 'efg','cat1',3 UNION ALL
SELECT 'hij','cat1',2 UNION ALL
SELECT 'klm','cat2',1 UNION ALL
SELECT 'nop','cat2',2 UNION ALL
SELECT 'qrs','cat2',3
UPDATE #Table SET Column1 = A.Column1
FROM
(
SELECT Column1 , Column3
FROM #Table
WHERE Column2 = 'cat2'
)A WHERE #Table.Column2 = 'cat1' AND A.Column3 = #Table.Column3
SELECT * FROM #Table
http://rextester.com/QYHLTE44670
Please try with below code:
DECLARE #Table TABLE
(Column1 VARCHAR(100),Column2 VARCHAR(100), Column3 INT)
INSERT INTO #Table VALUES
('abc','cat1',1),
('efg','cat1',3),
('hij','cat1',2),
('klm','cat2',1),
('nop','cat2',2),
('qrs','cat2',3)
UPDATE
t
SET t.Column1 = d.Column1
FROM #Table t INNER JOIN #Table d
ON t.Column3 = d.Column3
WHERE d.Column2 ='cat2'