Sql join shows null value - sql

I have 2 sql tables mtblSite_Lavel_Budget and mtblExpenditure_Site_Lavel. I want to join them, so I wrote the query below, but one column always give null result although there are values in that field.
Query is as below
select mtblExpenditure_Site_Lavel.SFTI_Id,
mtblExpenditure_Site_Lavel.Site_Lavel_Interventions,
mtblExpenditure_Site_Lavel.Sub_Site_Lavel_Interventions,
mtblExpenditure_Site_Lavel.Bill_No, mtblExpenditure_Site_Lavel.Date_Of_Bill,
mtblExpenditure_Site_Lavel.Eligible_Exp,
mtblExpenditure_Site_Lavel.Non_Eligible_Exp,
mtblExpenditure_Site_Lavel.Total,
mtblExpenditure_Site_Lavel.Physical_Progress,
mtblSite_Lavel_Budget.Sanction_Amount_DPR
from mtblExpenditure_Site_Lavel
left join mtblSite_Lavel_Budget
on mtblExpenditure_Site_Lavel.Sub_Site_Lavel_Interventions= mtblSite_Lavel_Budget.Sub_Site_Lavel_Interventions
WHERE mtblExpenditure_Site_Lavel.SFTI_Id =13
ORDER BY Date_Of_Bill desc
and the result is as below
Please advise regarding the issue.

Try this
from mtblExpenditure_Site_Lavel left join mtblSite_Lavel_Budget on
mtblExpenditure_Site_Lavel.sfti_id = mtblSite_Lavel_Budget.sfti_id
I think its confused becouse you are joining on a string column its always best to join on the id where posible.

Related

Duplicate results on inner join

I've written the below query but I'm getting multiple duplicate rows in the results, please can anyone see where I'm going wrong?
use Customers
select customer_details.Customer_ID,
customer_details.customer_name,
metering_point_details.MPAN_ID,
Agents.DA_DC_Charge
from Customer_Details
left join Metering_Point_Details
on customer_details.customer_id = Metering_Point_Details.Customer_ID
left join agents
on customer_details.Customer_ID = agents.customer_id
order by customer_id
It doesn't really matter, but you're not using an INNER JOIN. Regardless, your unexpected rows indicate that your JOIN criteria is not specific enough to return your expected output. You can use SELECT DISTINCT if your results are fully duplicative, and if you'd like to see why you're getting those duplicates you can just use SELECT * to see the full detail between the multiple rows that are returned using your JOIN criteria, which should help you either make your criteria more specific or show you that you've got duplicated records in one of the tables you're using in your JOIN.
With sample data we can dissect the problem more, but odds are you won't need it once you see why the rows are duplicated.

Getting way more results than expected in SQL left join query

My code is such:
SELECT COUNT(*)
FROM earned_dollars a
LEFT JOIN product_reference b ON a.product_code = b.product_code
WHERE a.activity_year = '2015'
I'm trying to match two tables based on their product codes. I would expect the same number of results back from this as total records in table a (with a year of 2015). But for some reason I'm getting close to 3 million.
Table a has about 40,000,000 records and table b has 2000. When I run this statement without the join I get 2,500,000 results, so I would expect this even with the left join, but somehow I'm getting 300,000,000. Any ideas? I even refered to the diagram in this post.
it means either your left join is using only part of foreign key, which causes row multiplication, or there are simply duplicate rows in the joined table.
use COUNT(DISTINCT a.product_code)
What is the question are are trying to answer with the tsql?
instead of select count(*) try select a.product_code, b.product_code. That will show you which records match and which don't.
Should also add a where b.product_code is not null. That should exclude the records that don't match.
b is the parent table and a is the child table? try a right join instead.
Or use the table's unique identifier, i.e.
SELECT COUNT(a.earned_dollars_id)
Not sure what your datamodel looks like and how it is structured, but i'm guessing you only care about earned_dollars?
SELECT COUNT(*)
FROM earned_dollars a
WHERE a.activity_year = '2015'
and exists (select 1 from product_reference b ON a.product_code = b.product_code)

SQLite query select all records that does not exist in another table

I am having some problem when trying to perform a SQLite query to get the records which does not exist from another table. Basically I have two database tables:
My exercise table stored all the exercises available whereas the bookedExercise table store the exercises booked by each users. What I am trying to do is, for example if the exercise does exist in the bookedExercise, it should be filtered out.
Here is the SQLite query which I used:
SELECT exercise.exerciseID, exercise.exerciseType, exercise.amout FROM exercise LEFT JOIN bookedExercise WHERE exercise.exerciseID = bookedExercise.exerciseID AND bookedExercise.exerciseID IS NULL
However, it returned me empty records. Any ideas?
Thanks in advance.
If you're fine with not using joins you could use
SELECT * FROM exercise WHERE exerciseID not in (SELECT exerciseID FROM bookedExercise)
When you are using LEFT JOIN, you must put the join condition into the ON clause:
SELECT exercise.exerciseID,
exercise.exerciseType,
exercise.amout
FROM exercise /* !! */
LEFT JOIN bookedExercise ON exercise.exerciseID = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL
Your SQL looked okay... I think the problem might be you have a datatype mismatch. You have exercise ID as an integer in one table and text in another.
Also, if you have huge data volumes, you may want to consider an anti-join:
select
e.*
from
exercise e
where not exists (
select 1
from bookedExercise be
where
e.excerciseId = be.exerciseID
)
-- edit --
On second glance, your SQL was not okay. You had your join condition in the where clause. This little change would fix your existing SQL:
SELECT exercise.excerciseId , exercise.exerciseType , exercise.amout
FROM exercise LEFT JOIN bookedExercise on
exercise.excerciseId = bookedExercise.exerciseID
WHERE bookedExercise.exerciseID IS NULL

Getting Null Values on a Left Join

I have the query below. I am trying to left join to another table. I know for a fact that the query should return values but the values are coming back as null. Does anyone see anything I am doing wrong? My brain is fried at this point.
SELECT
ss.NDC,
ss.ShipToAddressNo,
ss.QuantityShipped,
c.AccrualType,
ss.ExtendedPrice ExtendedPrice,
c.AccrualAmount CBAccrualAmount
FROM
SalesSummary ss left join ChargebackAccruals c on
ss.ClassOfTrade = c.TradeClass and
c.AddressNo is null and
c.ShipTo is null and
c.ShortItemNo is null and
c.LotNumber is null and
ss.InvoiceDate = #RunDate and
ss.InvoiceDate between AccrualBeginDate and AccrualEndDate
Any condition in the ON-clause is part of the join, so any row with data in AddressNo or ShipTp or ShortItemNo orLotNumber will be NULLed.
You probably want to move them into WHERE.
What do you want as result?
Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.

SQL Join Not Returnig Desired Results

I'm having a little trouble getting a SQL query working to give me the desired results. I have three tables. The SQL query should return all records with an employee name, along with their corresponding department name, and a division should it exist. If they do not, they should still display the employee with a Null division. Here is a link to my SQL Fiddle...
http://sqlfiddle.com/#!3/76fbb/1/0
Use LEFT OUTER JOIN instead of RIGHT OUTER JOIN
use a LEFTjoin instead of a RIGHTjoin.