select * using Group By Oracle SQL - 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

Related

Can't figure out how to create a select statement from this scenario?

Can you please help formulate a select statement from the scenario below?
column1 column2 column3 column4 column5 column6
a b c alt1 alt2 alt3
aa bb cc alt1 alt2
aaa bbb ccc alt1
if column6 !=Null then results should give me 3 rows of data
abcalt3
abcalt2
abcalt1
if column6 = Null and column5 !=Null then results should give me 2 rows of data
aabbccalt2
aabbccalt1
if column6 = Null and column5 = Null and column4 != Null then results should give me 1 row of data
aaabbbcccalt1
You can try with UNION ALL with separate select statement for different conditions as below-
DEMO HERE
SELECT column1+column2+column3+column4
FROM your_table where column6 is not null
UNION ALL
SELECT column1+column2+column3+column5
FROM your_table where column6 is not null
UNION ALL
SELECT column1+column2+column3+column6
FROM your_table where column6 is not null
UNION ALL
SELECT column1+column2+column3+column4
FROM your_table where column6 is null AND column5 IS NOT NULL
UNION ALL
SELECT column1+column2+column3+column5
FROM your_table where column6 is null AND column5 IS NOT NULL
UNION ALL
SELECT column1+column2+column3+column4
FROM your_table where column6 is null AND column5 IS NULL AND column4 IS NOT NULL

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

Grouping by all columns except one to count distinct values on the remaining column

I have table A with five rows and the following values:
Column1 Column2 Column3 Column4
------- ------- ------- -------
anna ben cat d
anna ben cat e
anna ben cat f
gina hugh ken m
gina hugh ken p
I want to add another column called Column5. The value of Column 5 will be 3 for the first 3 rows and 2 on the next 2 rows:
Column1 Column2 Column3 Column4 Column5
------- ------- ------- ------- -------
anna ben cat d 3
anna ben cat e 3
anna ben cat f 3
gina hugh ken m 2
gina hugh ken p 2
How I did this:
SELECT DISTINCT COUNT (DISTINCT t1.Column4) AS Column5,
Column1, Column2, Column3, Column4
FROM TableA AS t1
GROUP BY Column1, Column2, Column3;
This doesn't work:
Msg 8120, Level 16, State 1, Procedure COUNT, Line 29
Column 'Column4' invalid in the select list because it is not contained in either an
aggregate function or the GROUP BY clause.
Any help please? Much appreciated.
PS: If I add Column4 in the group by clause, I get only values of "1" in the result table in Column5.
One other way to do what you want would be to select distinct rows first, then apply the windowed COUNT() function:
WITH distinctrows AS (
SELECT DISTINCT
Column1,
Column2,
Column3,
Column4
FROM TableA
)
SELECT
Column1,
Column2,
Column3,
Column4,
COUNT(Column4) OVER (PARTITION BY Column1, Column2, Column3) AS Column5
FROM distinctrows
;
If you didn't need DISTINCT this would be easy.
SELECT Column1,
Column2,
Column3,
Column4,
Count(Column4) OVER (partition BY Column1, Column2, Column3) AS Column5
FROM TableA AS t1
But windowed aggregates in SQL Server don't currently support DISTINCT so you can use
WITH CTE
AS (SELECT Column1,
Column2,
Column3,
Count(DISTINCT Column4) AS Column5
FROM TableA
GROUP BY Column1,
Column2,
Column3)
SELECT A.Column1,
A.Column2,
A.Column3,
A.Column4,
C.Column5
FROM TableA A
JOIN CTE C
ON A.Column1 = C.Column1
AND A.Column2 = C.Column2
AND A.Column3 = C.Column3
(I have assumed the columns are not nullable for simplicity)
Is this what you are looking for?
SELECT COUNT (DISTINCT t1.Column4) AS Column5,
Column1, Column2, Column3
FROM TableA AS t1
GROUP BY Column1, Column2, Column3;
This should do it:
;WITH
countCol4 As
(
SELECT Column1, Column2, Column3, Column4
, ROW_NUMBER() OVER(PARTITION BY Column1, Column2, Column3, Column4
ORDER BY Column4) As Col4Count
FROM TableA As t1
)
SELECT Column1, Column2, Column3, Column4
, COUNT(*) OVER(PARTITION BY Column1, Column2, Column3) As Column5
FROM countCol4
WHERE Col4Count = 1
Apart from 'unsetting' the "only full group by" mode which I just read about but haven't tried yet, I just applied the following faster solution, it's a trick, I did this to avoid getting that error:
SELECT
COUNT (DISTINCT t1.Column4) AS Column5, Column1, Column2, Column3, MAX(Column4) AS Column4
FROM TableA AS t1
GROUP BY Column1, Column2, Column3;
I have character values on Column4. This seems OK and I now see values ranging from 1-6 in Column5, as I properly expected. Thanks!
Warning: This is not a good answer. See comments below for reason.