How to Total up NULL results from JOIN Query [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have written a left Join Query that returns all NULLS.
SELECT JourneyID,`TrainSeatID`, PassengerID FROM `TrainSeating`
LEFT JOIN Passenger ON TrainSeating.TrainSeatID = Passenger.PassTrainSeatID
WHERE PassengerID IS NULL;
Does anyone know how I can total up these NULLS and include the total number in the data set?
EDIT: I want to calculate the amount of NULL returns from a specific JourneyID.
Many thanks!

You can use COUNT() to get the number of rows in TrainSeating that have not passengers:
SELECT COUNT(*)
FROM TrainSeating LEFT JOIN
Passenger
ON TrainSeating.TrainSeatID = Passenger.PassTrainSeatID
WHERE PassengerID IS NULL;
EDIT:
If you want this per JourneyId then aggregate
SELECT ts.JourneyId, COUNT(*)
FROM TrainSeating ts LEFT JOIN
Passenger p
ON ts.TrainSeatID = p.PassTrainSeatID
WHERE p.PassengerID IS NULL
GROUP BY ts.JourneyId

Related

Inner joining tables A and B returns 50 rows. Left join B to A returns 125 rows. What's going on here? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Inner joining tables A and B returns 50 rows. Left join B to A returns 125 rows. What's going on here?
Left join can be bigger than inner join, and contain the same data + more data from table A.
Look at the following chart for reference:

SQL returns null data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to remove values ​​that has null result (second row)
Your query that you posted uses correlated sub queries to get the results that could easily achieved with a left join. Changing that left join into an inner join will get rid of the nulls as long as you do not have nulls in the name column of the emp table.
select e.empno,e.ename,mngr.name
from emp e
inner join emp mngr on e.mgr=mngr.empno

how this query is working.....can anyone explain [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
SELECT productid,productname,price
FROM products E1
WHERE 4 = (SELECT count(*)
FROM products E2
WHERE E1.price =E2.price)
It's working like this
SELECT productid,productname,price
FROM products E1
WHERE (SELECT count(*) FROM products E2 WHERE E1.price =E2.price) = 4
:) Now does it make more sense?
Although it can be simplified
SELECT productid,productname,price,COUNT(*) AS c
FROM products
GROUP BY PRICE
HAVING COUNT(*) = 4
The outer query ill scan all products.
For each product the subquery ill count how many products got the same price.
The filter (where clause) ill avoid subquery counts different from four.
The output ill be all products where there are four products with same price.
If anyone is using MS-SQL the same thing can be done using aggregates (count and having)
Edit Hanky already posted the MS-SQL equivalent query using count and having

Select Max in a table to get name in other table SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 tables
Players
Pnr
Pname
Padress
Pcity
Tickets
Tnr
Pnr
Date
Costs
I want to get the name of the player with the highest Ticket Cost from the database with Select. And I want to know who has got a Ticket in May. I also want to know who has never got a Ticket.
How do I get each of these?
Pnr is the primary key of Players and is connected with Pnr from Tickets
I've tried
SELECT MAX(Costs) from Players, Tickets
Where max()
I hope i understand correctly. Try the following query. I think it will still have some errors, but you can comment them here and I will try to correct them.
select top 1 Pname from Players
inner join Tickets on Players.Pnr = Tickets.Pnr
where Date > 1.05.2014 and Date <31.05.2014
order desc by Tickets.Costs
For highest ticket cost:
SELECT P.*,T.Tnr,T.Date,T.cost
FROM Players P JOIN
Tickets T ON T.Pnr=B.Pnr
WHERE T.Cost= SELECT MAX(Cost) from Tickets
For player who never got a ticket:
SELECT P.*
FROM Players P LEFT JOIN
Tickets T ON T.Pnr=B.Pnr
WHERE T.Tnr IS NULL
AND T.Pnr IS NULL
AND T.Date IS NULL
AND T.Costs IS NULL

getting one to one result from one to many relationship [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
my situation is that i have one to many relation ,like order and orderdetails ,i need to get order which has single order details.
How about:
select *
from order
where order_number in
(select order_number
from order_details
group by order_number
having count(*) = 1)
SELECT O1.order_number
FROM Orders AS O1
WHERE 1 = (
SELECT COUNT(*)
FROM OrderDetails AS D1
WHERE O1.order_number = D1.order_number
);