Distinct IDs from one table for inner join SQL - sql

I'm trying to take the distinct IDs that appear in table a, filter table b for only these distinct IDs from table a, and present the remaining columns from b. I've tried:
SELECT * FROM
(
SELECT DISTINCT
a.ID,
a.test_group,
b.ch_name,
b.donation_amt
FROM table_a a
INNER JOIN table_b b
ON a.ID=b.ID
ORDER by a.ID;
) t
This doesn't seem to work. This query worked:
SELECT DISTINCT a.ID, a.test_group, b.ch_name, b.donation_amt
FROM table_a a
inner join table_b b
on a.ID = b.ID
order by a.ID
But I'm not entirely sure this is the correct way to go about it. Is this second query only going to take unique combinations of a.ID and a.test_group or does it know to only take distinct values of a.ID which is what I want.

Your first and second query are similar.(just that you can not use ; inside your query) Both will produce the same result.
Even your second query which you think is giving you desired output, can not produce the output what you actually want.
Distinct works on the entire column list of the select clause.
In your case, if for the same a.id there is different a.test_group available then it will have multiple records with same a.id and different a.test_group.

Related

Iterate over the rows of a second table to return resultset

I'm trying to achieve a SELECT query that takes in consideration the result of a second table, by making combinations, replicating a result. I must follow a certain order to don't get all combinations, using a ORDER column.
To ilustrate what I'm trying to achieve:
Until now, I tried to use SUBSELECT with JOIN to replicate the results based on a second table.
SELECT a.table_a_id, b.label_x, b.label_y
FROM table_a a
JOIN
(
SELECT label_x, label_y
FROM table_b
WHERE b.table_a_id = a.table_a_id
) b
ON b.table_a_id = a.table_a_id
But, of course, I cant reference the table_a from inside the SUBSELECT.
What should be my next steps for achieving my desired ResultSet?
Use a self join here on the table_b table, with the join condition being that the table_a_id values match, but label_y > label_x.
SELECT
b1.table_a_id,
b1.label_x,
b2.label_y
FROM table_a a
INNER JOIN table_b b1
ON b1.table_a_id = a.table_a_id
INNER JOIN table_b b2
ON b2.table_a_id = b1.table_a_id AND
b2.label_y > b1.label_x
ORDER BY
b1.table_a_id,
b1.label_x,
b2.label_y;
Demo

SQL Server hierarchy, selecting parent nodes for distinct IDs

Stuck on this hierarchy problem, so here goes.
I have the following hierarchy table, which has been truncated.
The hierarchy has been joined to another table on the 'codes' column, with the following sample result. Let's call this table1.
For each distinct ID in table1, I want to utilize the hierarchy to find a parent, if it exists.
For example, under the codes column, 18 is the parent of 19.
I would like to fold or group the 19 with 18 and eliminate that row.
The desired result is something like this.
This does sort of what I want, using the GetAncestor method:
SELECT A.OrgNode, A.Codes, A.ID, B.OrgNode, B.Codes, B.ID FROM table1 A INNER JOIN table1 B ON A.OrgNode.GetAncestor(1) = B.OrgNode WHERE A.ID = B.ID
This self-join, with the GetAncestor function, did what I wanted it to do, with the 'where' on the same ID.
SELECT A.OrgNode, A.Codes, A.ID, B.OrgNode, B.Codes, B.ID FROM table1 A INNER JOIN table1 B ON A.OrgNode.GetAncestor(1) = B.OrgNode WHERE A.ID = B.ID

join or merge two table based on id merge

I have two tables:
I am looking for the results like mentioned in the last.
I tried union (only similar col can be merged), left join, right join i am getting repeated fields in Null areas what can be other options where i can get null without column repeating
A full join would get all results from both tables.
select
A.ID,
A.ColA,
A.ColB,
B.ColC,
B.ColD
from TableA A
full join Table B on A.ID = B.ID
Here is a good post to understand joins
You can try distinct:
select distinct * from
tableA a,
tableB b
where a.id = b.id;
It will not give any duplicate tuples.

Select returns the same row multiple times

I have the following problem:
In DB, I have two tables. The value from one column in the first table can appear in two different columns in the second one.
So, the configuration is as follows:
TABLE_A: Column Print_group
TABLE _B: Columns Print_digital and Print_offset
The value from the different rows and Print_group column of the Table_A can appear in one row of the Table_B but in different column.
I have the following query:
SELECT DISTINCT * FROM Table_A
INNER JOIN B ON (Table_A. Print_digital = Table_B.Print_group OR
Table_A.Print_offset = Table_B.Print_group)
The problem is that this query returns the same row from the Table_A two times.
What I am doing wrong? What is the right query?
Thank you for your help
If I'm understanding your question correctly, you just need to clarify your fields to come from Table_A:
SELECT DISTINCT A.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group
EDIT:
Given your comments, looks like you just need SELECT DISTINCT B.*
SELECT DISTINCT B.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group
I've still another question... first,to be clear, the right query version is
SELECT DISTINCT A.*
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group.
If I want it returns also one column from the B table it again returns duplicate rows. My query (the bad one) is the following one:
SELECT DISTINCT A.*, B.Id
FROM Table_A A
INNER JOIN B ON A.Print_digital = B.Print_group
OR A.Print_offset = B.Print_group

SQL: How to add a column in the SELECT query result?

Usually we will select the field(s) in the SQL query. e.g.
SELECT A.id FROM Member A
But what if I want to align a column which elements correspond to the other selected field?
For example I want to select the member ID from a member table, and the COUNT that count how many times the member appear in the tuple of other table
So how do I make the COUNT column that align together with the select result?
If I understood you correctly, this is what you want:
SELECT A.id, count(B.MemberID)
FROM Member A
LEFT JOIN TableB B on A.id = B.MemberID
group by A.id
The LEFT JOIN will include records in A that do not have any corresponding records in B. Also, COUNT only counts non-null values, so you need to use it with B.MemberID. This way the count for records in A that do not have any corresponding records in B will be 0, since B.MemberID will be NULL.
I agree with #Adrian's solution, but if there were many columns in the original SELECT list, they all would have to be listed in GROUP BY. I mean something like this:
SELECT
A.id,
A.name,
A.whatever,
...
COUNT(B.member_id)
FROM Member A
LEFT JOIN Member_Something B ON A.id = B.member_id
GROUP BY
A.id,
A.name,
A.whatever,
...
It is not always convenient, especially when the columns are actually expressions. You could take another approach instead:
SELECT
A.id,
A.name,
A.whatever,
...
COALESCE(B.member_count, 0)
FROM Member A
LEFT JOIN (
SELECT member_id, COUNT(*) AS member_count
FROM Member_Something
GROUP BY member_id
) B ON A.id = B.member_id
select member_id, count(*)
from table
group by member_id;