Click here to Skip to main content
15,922,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I count the number of times a location(zip code) is an intermediate destination and also count that it is a final destination? Because some locations have different number of destination.
So is wrong to count when sequence is 1 or 2 or 3 and when sequence is 4 as shown in my code? Sequence 1,2,3,4 means dest1,2,3,4

Eg booking id 1001
Pickup location 44456
Destination 1 43367
Destination 2 43876
Dest1 will be intermediate
Dest 2 will be final
Eg booking id 2005
Destination 1 56721
Destination 2 53242
Destination 3 45234
Dest1,2 intermediate
Dest 3 final

My code
select l.zip_code,sum(dum.tab) from location l,
(select count( Pickup_location) as tab from booking b,location l where b.pickup_location = l.Zip_Code group by l.zip_Code
union select count( destination) as tab from booking_destinations bd inner join location l on bd.destination = l.Zip_Code where bd.sequence in(1,2,3) group by l.zip_Code
union select count( destination) as tab from booking_destinations bd,location l where bd.destination = l.Zip_Code and sequence = 4 group by l.zip_Code
) as dum group by l.zip_Code ;

What I have tried:

Searching online and removing the select statement when sequence = 4
Posted
Updated 26-Mar-16 11:29am

1 solution

You don't want to count the destinations, you just want to know which was the first and which was the last. You can do this by using GROUP BY on the booking code and getting the MIN and MAX of the sequence number. E.g.
SQL
select booking, max(sequence) as final, min(sequence) as [first]
from booking_destinations bd
group by booking

You can then incorporate that sub-query into an overall result by either using a sub-query OR (my preference) using a Common Table Expression...

Sub-query example:
SQL
select fl.booking, b.Pickup_location, bd.sequence, bd.destination,
	case when bd.sequence = fl.final then 'Final' 
		 when bd.sequence = fl.[first] then 'First'
		 else 'Interim' 
	end
from 
(	select booking, max(sequence) as final, min(sequence) as [first]
	from booking_destinations bd 
	group by booking
) fl
join booking b ON fl.booking = b.id
join booking_destinations bd on b.id = bd.booking
join location l on bd.destination = l.zip_code

CTE example:
SQL
;with CTE as
(
	select booking, max(sequence) as final, min(sequence) as [first]
	from booking_destinations bd 
	group by booking
)
select CTE.booking, b.Pickup_location, bd.sequence, bd.destination,
	case when bd.sequence = CTE.final then 'Final' 
		 when bd.sequence = CTE.first then 'First'
		 else 'Interim' 
	end
from CTE
join booking b ON CTE.booking = b.id
join booking_destinations bd on b.id = bd.booking
join location l on bd.destination = l.zip_code

I made some assumptions about your table structure ... this is the code I used to create some test data (only the columns I needed have been included)
SQL
create table booking
 (
	id int,
	Pickup_location bigint
	
 )
 insert into booking values (1001, 44456),(2005, 44457)
 
  create table booking_destinations
 (
	booking int,
	[sequence] int,
	destination bigint
 )
 insert into booking_destinations values 
 (1001,1,43367),
 (1001,2,43876),(2005,1,56721),(2005,2,53242),(2005,3,45234)
 
 create table location
 (
	id int identity(1,1),
	zip_code bigint
 )
insert into location values(44456),(44457),(43367),(43876),(56721),(53242),(45234)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900