SQL Distinct value over joined tables - sql

I am trying to get the DISTINCT value of one column in a table. This column however is INNER JOINED from another table through an id.
When I try to use the DISTINCT on the column, it produces the same results because DISTINCT also takes into account the unique identifier ID. Is there any work around for this to just get the DISTINCT value of a column from a joined table???
EG.
SELECT val1, b.val2, val3
FROM TABLE 1
JOIN (SELECT DISTINCT val2
FROM TABLE 2) AS b ON val1 = b.val2

Try throwing in a GROUP BY instead of a DISTINCT:
SELECT val1
, b.val2
, val3
FROM TABLE 1
JOIN (SELECT val2
FROM TABLE 2 GROUP BY val2) AS b ON val1 = b.val2

To provide my solution:
I ended up using a nested distinct through a join and all the unnested values (all 20+) of them had to be wrapped around a MIN(x), seeing as those values didnt matter that much, so long as only one distinct value was returned.

Related

Select a row of the table with desired value, when a column's value which is not in the table is selected in where clause in keyword?

I have a table with one of the columns as ID. I have a set of values which I give in the where clause to compare the 'ID' column using 'in' keyword. I want to select the row if the value in that set of values has a record in the table. If not, the value that is not in the table has to be selected along with empty values other columns.
For example:
There is a table with columns ID & Animal. It has 8 records.
The table with all records
If I run the query:
SELECT ID, Animal from #Temp1 where ID in (4,8)
it will return the following result.
The table result filtered
But, if I run the query:
SELECT ID, Animal from #Temp1 where ID in (4,8,12)
it should return the following result.
The table result with desired values
Use a LEFT JOIN in concert with string_split() instead
Select ID = A.value
,Animal = coalesce(B.Animal,'ID Not Found')
From string_split('4,8,12',',') A
Left Join YourTable B on A.value=B.ID
Results
ID Animal
4 Donkey
8 Hampster
12 ID Not Found
If by chance string_split() is not available
Select ID = A.value
,Animal = coalesce(B.Animal,'ID Not Found')
From (values (4)
,(8)
,(12)
) A(value)
Left Join YourTable B on A.value=B.ID

How to select rows without duplicates when one column is different?

This is my table with 4 columns:
a b e d
a f c d
I want to get all 1st and 4th columns, so that the first two rows will be merged into one row in the example, since they are the same:
a d
a d
When I use the command:
select column1, column4 from my_table;
Would this automatically remove duplicates? If not, how to get distinct rows with only the 1 and 4 columns?
little confusing question.
Do you want to delete duplicate data or you want to just select non-duplicate data?
If you want to delete duplicate data then it will be like this -
insert overwrite my_table
select * from my_table
join (
Select col1||col2||col3||col4 key, row_number() over (partition by col1,col4 order by col1 ) as rn
from my_table) rs on rs.key = col1||col2||col3||col4 and rs.rn=1
If you want to select the unique col1 and col4 and dont want to change underlying data, you can simply fire
select distinct column1, column4 from my_table;

Count the number of pairs in SQL

I have a column and I would like to count the number of unique pairings of the elements within the column in SQL, for example, in Col 1 the number of unique pairings should be 6: ([1,2],[1,3],[1,4],[2,3],[2,4],[3,4]). Thanks!
col 1,
1
2
3
4
Consider a scenario where in we have dulpicates values in the table say
col1
1
1
2
3
4
5
The total number of unique combinations is 10:([1,2],[1,3],[1,4],[1,5],[2,3],[2,4],[2,5],[3,4][3,5],[4,5]).
But the given query below is giving me a count of 14 because of the dulplicate 1 which is counting 4 extra pairs [1,2],[1,3],[1,4],[1,5] twice.
select count(*)
from table t1 join
table t2
on t1.col1 < t2.col1;
To modify this defect I have the following query which ensures that the duplicates are removed and we get the correct output.The table name I have chosen is countunique which can store integer values in it in column named col1.
select count(*) from
(select distinct col1 from countunique) t1
join (select distinct col1 from countunique) t2
on t1.col1<t2.col1
SQL Fiddle for your reference SQLFIDDLE
Hope this answers to your question.
There are two ways. The cumbersome way is to generate the pairs and then count them:
select count(*)
from table t1 join
table t2
on t1.col1 < t2.col1;
The simpler way is to use a formula:
select count(*) * (count(*) - 1) / 2
from table t;

Returning not found values in query

I'm working on a project, and I have a list of several thousand records that I need check. I need to provide the list of the ones NOT found, and provide the query I use to locate them so my superiors can check their work.
I'll admit I'm relatively new to SQL. I don't have access to create temporary tables, which is one way I had thought of to do this.
A basic idea of what I'm doing:
select t.column1, t.column2
from table1 t
where t.column1 in ('value1','value2','value3')
If value1 and value3 are in the database, but value2 is not, I need to display value2 and not the others.
I have tried ISNULL, embedding the query, and trying to select NOT values from the query.
I have searched for returning records not found in a query on Google and on this site, and still found nothing.
I have tried something similar:
First create a table which will contain all such values that you need.
lets say
create table table_values(values varchar2(30));
then try the in clause as below:
select * from table_values tv where tv.value not in (select t.column1
from table1 t);
this will return the values needed.
In SQL Server 2008, you can make derived tables using the syntax VALUES(...)(...)(...), e.g.
select v.value
from (
values ('value1'),('value2'),('value3')
) v(value)
left join table1 t on t.column1 = v.value
where t1.column1 is null
Notes:
Each (...) after VALUES is a single row, and you can have multiple columns.
The v(value) after the derived table is the alias given to the table, and column name(s).
LEFT JOIN keeps the values from the derived table v even when the record doesn't exist in table1
Then, we keep only the records that cannot be matched, i.e. t1.column1 is null
I've switched the first column in your select to the column from v. column2 is removed because it is always null
solution might work in Oracle where dual is single row single column table. You need
one table where you can make virtual select of desired values!
WARNING as I don't have access to db I never tested query below.
SELECT tab_all.col_search, t.column1, t.column2
FROM
(
Select value1 AS col_search from dual
union all
Select value2 from dual
union all
Select value3 from dual
) tab_all left join table1 t
on col_search = t.column1
WHERE t.column1 is null;
I belive sqlserver equivalent of Oracle's
SELECT value1 FROM dual is
SELECT value1 OR SELECT 'value1'.
So try
SELECT tab_all.col_search, t.column1, t.column2
FROM
(
Select value1 AS col_search
union all
Select value2 AS col_search
union all
Select value3 AS col_search
) tab_all left join table1 t
on col_search = t.column1
WHERE t.column1 is null;
As I am not sqlserver person might be that
RichardTheKiwi version of Oracle's select from dual is better.

MSSQL: find records that has each value of column

Let's say I have some the following table schema:
year|val1|val2
I want to get all the val1 and val2 columns that has all values of year column. I suspect I need grouping here, but I can't imagine how.
Let's say we have set of years from query: SELECT DISTINCT YEAR FROM table. Let's say it returned 2000,2001. So If I have to rows as 2000|1|2 and 2001|1|2 then the query should return single row 1|2
Something like this may work
select
value1
from
table as t1 inner join
(select distinct year from table) as t2
on t1.year=t2.year
group by
val1
having count(distinct t1.year)=(count(distinct t2.year) )
Not sure about your question. But I think you need all the values of val1 and val2 which are there for all the valid values of years.If that's the case then you have to do inner join with the table
something like this
select #yearcnt = count(distinct year) from table
or select val1,count(distint year) as totalyear from table
group by val1
having count(distinct year) = #yearcnt