SQL join including all rows from one table irrespective of how many are represented in the other - sql

I have two tables:
I want to output the following:
I tried this statement:
SELECT TableA.bu_code
, SUM(TableB.count_invalid_date) AS TotalInvDate
FROM TableA
LEFT JOIN TableB ON TableA.bu_code = TableB.bu_code
GROUP BY TableA.bu_code
But it doesn't show every row represented in TableA, instead it does this:
Is there a single SQL statement that can output what I want?

You could use a left join after performing the group by:
SELECT a.bu_code, COALESCE (TotalInvDate, 0)
FROM TableA a
LEFT JOIN (SELECT bu_code, SUM(count_invalid_date) AS TotalInvDate
FROM TableB
GROUP BY bu_code) b ON a.bu_code = b.bu_code

There may be orphaned TableB rows without a parent row in TableA.
If so, use this GROUP BY syntax to see them.
GROUP BY ROLLUP(TableA.bu_code)
Refer to this Microsoft SQL-Server page on the GROUP BY clause for more details on the ROLLUP option.

SELECT A.bu_code AS [bu_code],
ISNULL(sum( B.count_invalid_date),0) AS [TotalInvDate]
FROM
TableA A
left JOIN
TableB B
ON
A.bu_code=B.bu_code
GROUP BY
A.bu_code

Related

Get all rows that are not exist in the appropriate table depending on the date in sql

I have two tables: TableA and TableB (as in the following picture):
The result should be as in the following figure:
What is the best way to get result (as in the table Result) using mssql query?
Thanks.
If I understand correctly, you want the date/value pairs that don't exist.
Generate the list of all date/value pairs using a cross join. Then filter out the ones you don't want:
select b.value, d.date
from tableb b cross join
(select distinct date from tablea a) d
where not exists (select 1 from tablea a where a.date = d.date and a.value = b.value)

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.

SQL join including all rows from one table irrespective of how many are represented in the other table

Following on from my previous question here.
I have two tables:
TableA
TableB
I want to output the following:
Instead, my query gives me this:
Here is my SQL statement:
SELECT TableA.BU, TableB.count_invalid_date
FROM TableA
LEFT JOIN TableB ON TableA.BU = TableB.BU
WHERE TableB.Month = 8
GROUP BY TableA.BU, TableB.count_invalid_date;
Is there a single SQL statement that can give me what I want?
SQL Fiddle is here: http://sqlfiddle.com/#!6/2ab0f/1
Thanks in advance!
Try this:
SELECT
TableA.BU,
TableB.count_invalid_date
FROM
TableA
LEFT JOIN TableB ON TableA.BU = TableB.BU and TableB.Month = 8
GROUP BY
TableA.BU,
TableB.count_invalid_date;
When doing an outer join, you can't reference any of the table in where clause or otherwise it will turn it into inner join.
Problem:
Since your where clause says TableB.Month=8, that is why other rows are getting eliminated from the result.
Solution:
Add one more condition to the where clause.
SELECT TableA.BU, TableB.count_invalid_date
FROM TableA LEFT JOIN
TableB ON TableA.BU=TableB.BU
WHERE (TableB.Month=8 OR TableB.Month IS NULL)
GROUP BY TableA.BU, TableB.count_invalid_date;
Result:
BU COUNT_INVALID_DATE
APAC (null)
EMEA 3
SG (null)
US (null)
See result in SQL Fiddle.

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

Outer Join with Where returning Nulls

Hi I have 2 tables. I want to list
all records in table1 which are present in
table2
all records in table2 which are not present in table1 with a where condition
Null rows will be returned by table1 in second condition but I am unable to get the query working correctly. It is only returning null rows
SELECT
A.CLMSRNO,A.CLMPLANO,A.GENCURRCODE,A.CLMNETLOSSAMT,
A.CLMLOSSAMT,A.CLMCLAIMPRCLLOSSSHARE
FROM
PAKRE.CLMCLMENTRY A
RIGHT OUTER JOIN (
SELECT
B.CLMSRNO,B.UWADVICETYPE,B.UWADVICENO,B.UWADVPREMCURRCODE,
B.GENSUBBUSICLASS,B.UWADVICENET,B.UWADVICEKIND,B.UWADVYEAR,
B.UWADVQTR,B.ISMANUAL,B.UWCLMNOREFNO
FROM
PAKRE.UWADVICE B
WHERE
B.ISMANUAL=1
) r
ON a.CLMSRNO=r.CLMSRNO
ORDER BY
A.CLMSRNO DESC;
Which OS are you using ?
Table aliases are case sensistive on some platforms, which is why your join condition ON a.CLMSRNO=r.CLMSRNO fails.
Try with A.CLMSRNO=r.CLMSRNO and see if that works
I'm not understanding your first attempt, but here's basically what you need, I think:
SELECT *
FROM TABLE1
INNER JOIN TABLE2
ON joincondition
UNION ALL
SELECT *
FROM TABLE2
LEFT JOIN TABLE1
ON joincondition
AND TABLE1.wherecondition
WHERE TABLE1.somejoincolumn IS NULL
I think you may want to remove the subquery and put its columns into the main query e.g.
SELECT A.CLMSRNO, A.CLMPLANO, A.GENCURRCODE, A.CLMNETLOSSAMT,
A.CLMLOSSAMT, A.CLMCLAIMPRCLLOSSSHARE,
B.CLMSRNO, B.UWADVICETYPE, B.UWADVICENO, B.UWADVPREMCURRCODE,
B.GENSUBBUSICLASS, B.UWADVICENET, B.UWADVICEKIND, B.UWADVYEAR,
B.UWADVQTR, B.ISMANUAL, B.UWCLMNOREFNO
FROM PAKRE.CLMCLMENTRY A
RIGHT OUTER JOIN PAKRE.UWADVICE B
ON A.CLMSRNO = B.CLMSRNO
WHERE B.ISMANUAL = 1
ORDER
BY A.CLMSRNO DESC;