Left outer join in Teradata cannot get the full result of left subquery - left-join

I want to join two sub-queries:
(Select col1,col2,col3,col4, sum(money1) as sum_money
from table1 group by col1,col2,col3,col4)-----this is subquery1,
return 135 rows
((Select col1,col2,col3,col4, sum(money2) as sum_money_new
from table2 group by col1,col2,col3,col4) -----this is subquery2,
return 79rows
then I left join these two sub-queries like the following, still get 79 rows.
select subquery1.col1,subquery1.co2,subquery1.col3,subquery1.col4,subquery1.sum_money,subquery2.sum_money_new
from subquery1
left join subquery2
on subquery1.col1=subquery2.col1
and subquery1.col2=subquery2.col2
and subquery1.col3=subquery2.col3
and subquery1.col4=subquery2.col4
if I change the join above to be right join, still 79 rows. who can help me?
the version of Teradata is 16.20.53.27

Related

Inner join if tables are empty with Top 1000 results

I have three table valued parameters passed into an SP. These all contain data to filter a query. I want to join them to a table as below but only if there is data in a table valued parameter
SELECT DISTINCT TOP (1000)
Person.col1,
Person.col2,
FROM dbo.Person INNER JOIN
#tbl1 t1 ON Person.col3 = t1.val INNER JOIN
#tbl2 t2 ON Person.col4 = t2.val INNER JOIN
#tbl3 t3 ON Person.col5 = t3.val
I am aware I can do a Left Outer Join but I don't want results with NULL values. The top 1000 necessary as there is a lot of data and scanning the whole table causes performance issues
Try use coalesce to cater nulls check
SELECT DISTINCT TOP (1000)
Person.col1,
Person.col2,
FROM dbo.Person INNER JOIN
#tbl1 t1 ON coalesce(Person.col3, '111') = t1.val INNER JOIN
#tbl2 t2 ON coalesce(Person.col4 , '111')= t2.val INNER JOIN
#tbl3 t3 ON coalesce(Person.col5, '111') = t3.val

Sum of two columns showing wrong result tsql

I have query as
select distinct
EnglishProductCategoryName,
sum(isnull(fic.SalesAmount,0))InternetSalesAmount,
sum(isnull(frs.SalesAmount,0))ResellerSalesAmount
from dimproduct dp join DimProductSubcategory dps on dp.ProductSubcategoryKey=dps.ProductSubcategoryKey
join DimProductCategory dpc on dpc.ProductCategoryKey=dps.ProductCategoryKey
left join FactInternetSales fic on fic.ProductKey=dp.ProductKey
left join FactResellerSales frs on frs.ProductKey= dp.ProductKey
group by EnglishProductCategoryName
but when I do
select sum(isnull(fis.SalesAmount,0))InternetSalesAmount from FactInternetSales
select sum(isnull(frs.SalesAmount,0))resellerSalesAmount from FactResellerSales
and compare sum of whole englishproductcategoryname and sum of internet sales amount,reseller sales amount should be same but its not same
Help will be appriciated
Thanks
Check for missing ProductSubcategory or ProductCategory
select distinct
EnglishProductCategoryName,
sum(isnull(fic.SalesAmount,0))InternetSalesAmount,
sum(isnull(frs.SalesAmount,0))ResellerSalesAmount
from dimproduct dp left join DimProductSubcategory dps on dp.ProductSubcategoryKey=dps.ProductSubcategoryKey
left join DimProductCategory dpc on dpc.ProductCategoryKey=dps.ProductCategoryKey
left join FactInternetSales fic on fic.ProductKey=dp.ProductKey
left join FactResellerSales frs on frs.ProductKey= dp.ProductKey
group by EnglishProductCategoryName

What's the difference between these joins?

What's the difference between
SELECT COUNT(*)
FROM TOOL T
LEFT OUTER JOIN PREVENT_USE P ON T.ID = P.TOOL_ID
WHERE
P.ID IS NULL
and
SELECT COUNT(*)
FROM TOOL T
LEFT OUTER JOIN PREVENT_USE P ON T.ID = P.TOOL_ID AND P.ID IS NULL
?
The bottom query is equivalent to
SELECT COUNT(*)
FROM TOOL T
since it is not limiting the result set but rather producing a joined table with a lot of null fields for the right part of the join.
The first query is a left anti join.

Left Join Is Not Doing What I expect

Totally confused and I have been working at this for 2 hours
I thought restriction on the left side of the join are honored
On this query I am getting [docSVsys].[visibility] 1 and <> 1
I thought this would restrict [docSVsys].[visibility] to 1
select top 1000
[docSVsys].[sID], [docSVsys].[visibility]
,[Table].[sID],[Table].[enumID],[Table].[valueID]
from [docSVsys] with (nolock)
left Join [DocMVenum1] as [Table] with (nolock)
on [docSVsys].[visibility] in (1)
and [Table].[sID] = [docSVsys].[sID]
and [Table].[enumID] = '140'
and [Table].[valueID] in (1,7)
This works
select top 1000
[docSVsys].[sID], [docSVsys].[visibility]
,[Table].[sID],[Table].[enumID],[Table].[valueID]
from [docSVsys] with (nolock)
left Join [DocMVenum1] as [Table] with (nolock)
on [Table].[sID] = [docSVsys].[sID]
and [Table].[enumID] = '140'
and [Table].[valueID] in (1,7)
where [docSVsys].[visibility] in (1)
I am just having a really off day as I had it in my mind the left side honored the join
SELECT *
FROM A
LEFT JOIN B ON Condition
is equivalent to
SELECT *
FROM A
CROSS JOIN B
WHERE Condition
UNION ALL
SELECT A.*, NULL AS B
FROM A
WHERE NOT EXISTS (SELECT * FROM B WHERE Condition)
Some rough pseudo-code...
Note, that all rows from A get through. It's just that the columns from B can be NULL if the join fails for some particular row of A.
Put the filter on docSVsys into the WHERE clause.
LEFT JOINs preserve all rows from the left (first) table, no matter what. The condition in the ON clause is only for matching which rows from the right/second table should be paired with rows from the left/first table.
If you want to exclude some rows from the firs table, use the WHERE clause:
select top 1000
[docSVsys].[sID], [docSVsys].[visibility]
,[Table].[sID],[Table].[enumID],[Table].[valueID]
from [docSVsys] with (nolock)
left Join [DocMVenum1] as [Table] with (nolock)
on [Table].[sID] = [docSVsys].[sID]
and [Table].[enumID] = '140'
and [Table].[valueID] in (1,7)
where [docSVsys].[visibility] in (1)

Join table variable vs join view

I have a stored procedure which is running quite slow. Therefore I want to extract some of the query in a separate view.
My code looks something like this:
DECLARE #tmpTable TABLE(..)
INSERT INTO #tmpTable (..) *query* (returns 3000 rows)
Select ... from table1
inner join table2
inner join table3
inner join #tmpTable
...
I then extract (copy-paste) the *query* and put it in a view - i.e. vView.
Doing this will then give me a different result:
Select ... from table1
inner join table2
inner join table3
inner join vView
...
Why? I can see that the vView and the #tmpTable both returns 3000 rows, so they should match (also did a except query to check).
Any comments would be much appriciated as I feel quite stuck with this..
EDITED:
This is the full query for getting the result (using #tmpTable or vView gives me different results, although the appear the same):
select dep.sid as depsid, dep.[name], COUNT(b.sid) as possiblelogins, count(ls.clientsid) as logins
from department dep
inner join relationship r on dep.sid=r.primarysid and r.relationshiptypeid=27 and r.validto is null
inner join [user] u on r.secondarysid=u.sid
inner join relationship r2 on u.sid=r2.secondarysid and r2.validto is null and r2.relationshiptypeid in (1,37)
inner join client c on r2.primarysid=c.sid
inner join ***#tmpTable or vView*** b on b.sid = c.sid
left outer join (select distinct clientsid from logonstatistics) as ls on b.sid=ls.clientsid
GROUP BY dep.sid, dep.[name],dep.isdepartment
HAVING dep.isdepartment=1
You maybe don't need the view/table if you change to this.
It joins on to client c and appears to be there only to JOIN onto logonstatistics
--remove inner join ***#tmpTable or vView*** b on b.sid = c.sid
--change JOIN
left outer join (select distinct clientsid from logonstatistics) as ls on c.sid=ls.clientsid
And change COUNT(b.sid) to COUNT(c.sid) in the SELECT clause
Otherwise, if you get different results you have two options I can see:
Table and view have different data. Have you run a line by line comparsion?
One has NULL, one has a value (especially for the sid column which will affect the JOIN)
Finally, when you says "different results" do you mean you get x2 or x3 rows? A different COUNT? What?