F
F
Fyodor2015-04-09 14:29:53
SQL Server
Fyodor, 2015-04-09 14:29:53

How to make SQL query to four tables?

The bottom line is this - there are recipient tables - clients, human - additional information about clients (including date of birth), tables have the same id, they can be linked by them
reservation - these are applications from tourists, it indicates who issued (humanid). which tourists belong to the application is recorded in the people table.
It is necessary - for the manager (reservation.humanid) whose humanid we know in advance, to find the people (recipient) whom he sent on a trip and who have a birthday today. I came up with this request

SELECT *
  FROM [samo7.0.2].[dbo].[recipient] t1 
  RIGHT JOIN [samo7.0.2].[dbo].[human] t2 ON t2.id = t1.id
  RIGHT JOIN [samo7.0.2].[dbo].[people] t4 ON t1.id = t4.humanid
  RIGHT JOIN [samo7.0.2].[dbo].[reservation] t3 ON t3.id = t4.reservationid  
  WHERE MONTH(t2.born) = 4 AND (DAY(t2.born) = 9) AND t3.humanid = ?

But, let's say the manager sent the tourist 5 times, then we will see 5 lines in this sample, respectively. And you only need to get the last one. Ideally, immediately get the number of applications from the manager with this person.
It will be very good if you additionally select reservation requests here where the tourist was a buyer (since out of 5 trips, let's say he went only 3 times, and 2 others were only a payer). Payer - reservation.recipientid where recipientid is recipient.id
I still can't figure out how the current day and current month are directly in the request so that it is substituted, although this is not a problem.
I hope I explained it clearly =) I would be grateful for your help!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Fadmin, 2015-04-09
@Fadmin

The last by what criteria? by date? Sort by date and in select top 1 add.
reservation to accept again, by recipient.id.
current day - date or day of the week? select DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0)

S
SagePtr, 2015-04-09
@SagePtr

GROUP BY add I guess

A
Alexander, 2015-04-09
@NeiroNx

And if there are no reservations, then there will be no person on the list ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question