I have two columns(column1, column2) in my oracle database table named as demo. I want to select same column1 having different column2.
SQL> select * from demo;
column1 | column2
--------|--------
A | 10
A | 20
A | 30
A | 30
I want the following output with count(*):
column1 | column2
--------|--------
A | 10
A | 20
How can i do this? Any help is much appreciated
Try:
select column1, column2
from myTable
group by column1, column2
having count(*) = 1
Related
I have a table with data like the following:
Column1 | Column2 | Column3 | Value
SQ03 | D | 1000040 | 1000
SQ03 | | 1000040 | 1000
SQ03 | | 1000050 | 2000
SQ03 | | 1000060 | 3000
SQ03 | L | 1000060 | 3000
SQ03 | D | 1000060 | 3000
What I need to do is to get a single value based on column3. Is a value in column3 is unique, I need to get that value. But if there are duplicates in Column3, I need to get the value where Column2 is not null. But like in the example that I showed in above, there are values for Column3 where Column2 is marked more than once, in these cases I need to get only one of these values, doesn't matter what.
So I thought on flagging which line I would need with the following solution:
select *, CASE
WHEN "Column2" != ' '
THEN 'X'
WHEN "Column2" = ' ' AND row_number() over(PARTITION BY "Column3" ORDER BY "Column2" DESC, "Column3") = 1
THEN 'X'
ELSE 'O'
END AS "FLAG" from DUMMY
WHERE "Column1" = 'SQ03'
But the problem with this solution is that it's aggregating the value from Column3. Like, it sums the values where Column3 has duplicates.
Can anyone help me with a solution where I don't get the values aggregated?
EDIT:
My expected output would be this:
Column1 | Column2 | Column3 | Value
SQ03 | D | 1000040 | 1000
SQ03 | | 1000050 | 2000
SQ03 | L | 1000060 | 3000
You can use a subquery to generate row numbers for each Column3 value (ordered by Column2 DESC to make NULL values come last), and then select the rows which have row_number = 1:
SELECT Column1, Column2, Column3, Value
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Column3 ORDER BY Column2 DESC) AS rn
FROM DUMMY
WHERE Column1 = 'SQ03'
) D
WHERE rn = 1
Alternatively you can use a CTE:
WITH CTE AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Column3 ORDER BY Column2 DESC) AS rn
FROM DUMMY
WHERE Column1 = 'SQ03'
)
SELECT Column1, Column2, Column3, Value
FROM CTE
WHERE rn = 1
Output for both queries:
Column1 Column2 Column3 Value
SQ03 D 1000040 1000
SQ03 (null) 1000050 2000
SQ03 L 1000060 3000
Demo on SQLFiddle
I think an aggregation function (as a window function) does what you want:
select t.*,
max(column3) over (partition by column1)
from t;
I need to find a way in SQL Server 2014 Management Studios to find the next unique value in a column that shares the value of a different column.
So for example below I would want my results to be
Column 1 - A
Column 2 - 1
Column 3 - 4
As that is the first time that A has unique values in column 2 and 3
Column1 | Column2 | Column3
---------+---------+---------
| A | X | 1 |
| A | X | 2 |
| B | Y | 3 |
| A | Z | 4 |
Query:
SELECT
Column1,
LEAD(Column3) OVER (PARTITION BY Column2 ORDER BY Column3) AS FindValue
FROM
Table
If I understand it correctly I would try something like this:
-- first we find minimum values for column1, column2 variations
WITH min_values AS (
SELECT
column1,
column2,
min(column3) AS min_value
FROM
table
GROUP BY 1,2
)
-- then we find bottom 2 values for column1
,bottom_2 AS (
SELECT
column1,
min_value,
row_number() OVER (PARTITION BY column1 ORDER BY min_value ASC) AS rn
FROM
min_values
)
-- THEN we JOIN results INTO single record
SELECT
b1.column1, b2.min_value, b1.min_value
FROM
bottom_2 b1
JOIN
bottom_2 b2 ON b1.column1 = b2.column1 AND b2.rn < b1.rn
WHERE b1.rn <= 2
I just checked comments above and would like to add some notes.
If you want to find next value ordered by column2 then you have to change order by from min_value to column2 in row_number() line. Otherwise, if you are looking for next inserted value then you need a timestamp or some kind of id.
Say I have a table like this:
column1 | column2
---------------------
1 | a
1 | b
1 | c
2 | a
2 | b
I need an SQL query to show the distinct values from column 1, and a count of the related distinct values from column 2. The output would look like:
column1 | count
-------------------
1 | 3
2 | 2
You could do something like this:
SELECT column1, count(column2)
FROM table
GROUP BY column1
You should do a COUNT(DISTINCT ...) with a GROUP BY:
Select Column1,
Count(Distinct Column2) As Count
From Table
Group By Column1
Hello what I want is to write a query which will fetch me 3 column:-
nvarchar column1
integer Values column2
single cell of the summed column2
is it possible , I am getting the following error:-
Msg 8120, Level 16, State 1, Line 1
Column 'tablename.columnname' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
What is the correct procedure to get data in the format I wish to get.
Edit
Jaques' answer works but I dont get what I want. What I want is:
column 1 PID | column 2 SID | column 3 | column 4 | Column 5(Total of 4)
-------------------------------------------------------------------------
1 | 1 | ABC | 125.00 | 985.00
2 | 2 | XYZ | 420.00 |
3 | 3 | DEF | 230.00 |
4 | 4 | GHI | 210.00 |
i suspect you are using some aggregate function on some columns and not listing your remaining columns in group by clause. your query should look like this.
select sum(column2), column1 from table1
group by column1
You can do it in the following way, because you need to add all non aggregated values in the group by, which makes it difficult
Select column1, column2, SUM(column2) OVER (PARTITION BY column1) as Total from [Table]
This should work.
You can do it with a subselect from your edited answer, but why do you want it like that?
Select Column1, Column2, Column3, Column4, (Select SUM(Column4) from Table) as Column 5 from Table
You must include the same columns in the select and group by clauses.
If you want to sum a column with all the values, you must include in the select clause a column with different value for each row, like this:
SELECT columnId, sum(column4) as total
FROM MyTable
GROUP BY columnId
or simply don't include on the select any extra column, like this:
select sum(column4) from MyTable
I can't seem to know how to find something on this.
Here is my example which shows me just the duplicate column:
select column1,COUNT(column1)
from table
where column1> 0
GROUP BY column1
HAVING COUNT(column1) > 1
OUTPUT:
----------------------------
column1 | (name not defined)
----------------------------
2134567 | 2
2881992 | 3
What I want is:
------------------------------------------------
column0 | column2 |column1 | (name not defined)
------------------------------------------------
1 | abc |2134567 | 2
2 | cde |2881992 | 3
How can I achieve this?
You need to join your query back to the table you want to pull in the additional data from
SELECT column0, column2, table.column1, columncount
FROM table
INNER JOIN (SELECT column1, COUNT(column1) columncount
FROM table
WHERE column1> 0
GROUP BY column1 HAVING COUNT(column1) > 1) t2 ON table.column1 = t2.column1
Try something like:
SELECT * FROM [Tanle1]
WHERE Column0 IN
(SELECT Column0 FROM [Table1]
GROUP BY Column0
HAVING COUNT(Column0) > 1)