Query is not returning the correct count while joining two tables in SQL Server 2017 - sql

Table A has 2500 rows
Table B has 12000 rows
Common column between these two tables is Email_ID.
Note: Email ID column may have nulls in table B.
If I join these tables using this query
Select count(*)
from A
left join B on A.Email_ID = B.Email_ID
it is returning a value of more than 2500 records.
But I want only 2500 records from Table A
Please suggest how to do this.

* will return total record count regardless of duplicate or nulls :
Select count(b.Email_ID) -- if you have a duplicate email ids then use `distinct`
from A left join
B
on A.Email_ID = B.Email_ID;
However, you can also use exists, join will return multiple rows if b table has more rows matching email id available in table a. So, count will always be increased.
select count(a.Email_ID)
from a
where exists (select 1 from b where b.Email_ID = a.Email_ID);

If you want only 2,500 hundred records, then why are you joining? That is, these return 2,500:
select count(*)
from A;
select count(distinct A.Email_id)
from A left join
B
on A.Email_ID = B.Email_ID;
If you actually want one row returned from B for each A, then use outer apply rather than left join:
select . . .
from a outer apply
(select top (1) b.*
from b
where b.email_id = a.email_id
) b;
This returns an arbitrary matching row from b. Normally, the subquery would have an order by to provide more control over the matching row. For instance, order by newid() would return a random row.

Related

PostgreSQL cross join using max returns null

I want to select the max() value of the modified_at column in each of several tables:
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a, b, c;
This works correctly as long as each of the tables has at least 1 row.
The problem is that when just one of the tables has 0 rows, null is returned for all tables:
null,null,null
What is a solution that returns the correct values for the tables that do have rows?
PostgreSQL-10
Using OUTER JOINs should do it
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a outer join b outer join c;
but a possibly simpler option would be to use 3 subqueries instead:
select
(select max(modified_at) from a) as a_modified_at,
(select max(modified_at) from b) as b_modified_at,
(select max(modified_at) from c) as c_modified_at;

SQL MS Access Summing Results Based on Two Table Criteria

I've been asked to find if totals from one table exceed a certain value. However the identifier I need to group these is stored in another table. So I've figured how to isolate what I need and then copy into excel. However I do understand the principle of summing in SQL, as I've made my own queries that look like this:
SELECT * FROM
(SELECT ID, SUM(Table1.Amount) AS Subtotal FROM Table1 WHERE LineNumber = 1 GROUP BY ID) AS a, Table2 AS b
WHERE a.ID = b.ID
AND a.Subtotal > b.Threshold
In this case, the totals I need to sum all have the same LineNumber from the original table (Table1) so it's easy to compare to a value in a different table. What I want to do is be able to is join a subquery back to the original table, then GROUPBY the identifier and sum from the other table, adding a criteria that the original ID from table one must be a duplicate:
SELECT * FROM
((Table1 as t
INNER JOIN
(SELECT ID FROM Table1
GROUP BY ID HAVING COUNT(ID) >1) as b
ON t.ID = b.ID
Joining criteria back to the original table, I need to do this because the table I need to sum doesn't contain ID,
INNER JOIN Table2 as c
ON t.ID2 = c.ID2
WHERE c.LineNumber = 4
This is where I'm stuck. I want to sum all the LineNumber = 4 for each ID. Again, I had to join using ID2 because ID1 isn't in Table2
Perhaps I'm making this too complicated? Any suggestions would be welcome
Thanks!

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

left join on MS SQL 2008 R2

I'm trying to left join two tables. Table A contains unique 100 records with field_a_1, field_a_2, field_a_3. The combination of field_a_1 and field_a_2 is unique.
Table B has multi-million records with multiple fields. field_b_1 is same as field_a_1 and field_b_2 is same as field_a_2.
I join the two tables together like this:
select a.*, b.*
from a
left join b
on field_a_1 = field_b_1
and field_a_2 = field_b_2
Instead of getting 100 records, I get multi-million records. Why is this?
Because table B has multiple rows for each table A entry.
For example:
TableA (ID)
1
2
3
TableB (ID, data)
1 hello
1 world
1 foo
1 bar
2 data
2 words
2 more
3 words
3 boring
If you left join from TableA to TableB, you will get a row for every TableB record that matches a TableA record - ie. all of them.
Can you explain what results you are looking for?
Because a left join returns all of the rows from the first table + all of the matching rows from the second table. Which of the millions of matching rows did you expect to get?
Left join or inner join don't really make a difference. A JOIN will return all rows that match the join condition. So if table b has millions of rows that match the JOIN criteria, then all the rows will be returned.
Depending on what you wish to accomplish you should consider using the DISTINCT keyword or GROUP BY to perform aggregate functions.

Select multiple tables when one table is empty in MySQL

I'm trying to do
SELECT * FROM a, b
However, it doesn't return anything if one of the tables is empty. How do I make it so it returns 'a' even if the other one is empty?
Using two tables in the from clause is functionally equivalent to a cross join:
select *
from A
cross join
B
This returns a row of A for every row in B. When B is empty, the result is empty too. You can fix that by using a left join. With a left join, you can return rows even if one of the tables is empty. For example:
select *
from A
left join
B
on 1=1
As the condition 1=1 is always true, this is just like a cross join except it also works for empty tables.
SELECT * FROM a LEFT JOIN b ON a.ID = b.ID
Will return everything from a even if b is empty.
You should do a left join.
Like this
SELECT *
FROM A
LEFT JOIN B ON A.ID = B.ID
Then you receive the rows in A and the respective row in B if exists.
SELECT a.*, b.* FROM a LEFT JOIN b ON a.id = b.id
in this example id is just example name for join key
The query mentioned above display join of both tables if a contain 2 record and b contain 7 records it displays 7*2 = 14 records. In your case one of the table is empty( with 0 records), it will not display any data. If still you want to display data and tables are not having any relationship, you need to check if count of both tables greater that 0. Otherwise display records from only one table which is not empty.