SQL 2005 Allocate 1 or 0 if exists

I am creating single customer view of member bookings and i need allocate 1 if component accomodation exists 0 if not and then i need to do this for 7 different componenets. This willl give single booking view of our member. I added the code above but it's not working. Can anyone kindly assist me on this.
select [booking product],[booking client],[booking ref],[bookingstatus],
	   convert(varchar(22),[booking conf date],103) as bookingdate,
	   convert(varchar(22),[booking dep date],103) as departuredate,
	   CONVERT(DECIMAL(17, 2),COALESCE (SUM(price),0)) AS Price,
	   CONVERT(DECIMAL(17, 2),COALESCE (SUM(gbpcost),0)) AS gbpcost,
       
      ( 
		if exist (select [booking ref] 
				  from view_EUR_Accounts_Bookings_On_Components 
				  where [compname] = 'ACCOMMODATION') 
		then 1 else 0 end 
	  ) as Accom
	   	
	
from view_EUR_Accounts_Bookings_On_Components

where [booking client] = '12345' and [booking product] = 'Leis' 

group by [booking product],[booking client],[booking ref],[bookingstatus], [booking conf date],[booking dep date]

order by [booking ref]

Open in new window

wexasAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
Change this part

      (
            if exist (select [booking ref]
                          from view_EUR_Accounts_Bookings_On_Components
                          where [compname] = 'ACCOMMODATION')
            then 1 else 0 end
        ) as Accom

to

Case when exists (select [booking ref]
                          from view_EUR_Accounts_Bookings_On_Components
                          where [compname] = 'ACCOMMODATION')
then 1 else 0 end as Accom

And do the same for the rest
0
 
cyberkiwiCommented:
Please also remember to add the filters to the inner exists query... it does not seem to bring in the booking client for example.
0
 
wexasAuthor Commented:
Hi CyberKiwi, thanks for your prompt reply but unfortunaely it's not giving the correct result. It is allocating 1 to all the components. Attached is the actual data which i am using to derieve the results
For example booking ref no l001188 doesn't have single trip insurance in the booking but this query is allocating 1 to it should be 0.

Thanks for your assistance, i very new to sql 2005
select [booking product],[booking client],[booking ref],[bookingstatus],
	   convert(varchar(22),[booking conf date],103) as bookingdate,
	   convert(varchar(22),[booking dep date],103) as departuredate,
	   CONVERT(DECIMAL(17, 2),COALESCE (SUM(price),0)) AS Price,
	   CONVERT(DECIMAL(17, 2),COALESCE (SUM(gbpcost),0)) AS gbpcost,

      case when exists (select [booking ref] 
						 from view_EUR_Accounts_Bookings_On_Components 
				   		 where [compname] = 'ACCOMMODATION' and [booking product] = 'Leis')then 1 else 0 end as Accom,
	
	  case when exists (select [booking ref] 
						 from view_EUR_Accounts_Bookings_On_Components 
				   		 where [compname] = 'Flight'and [booking product] = 'Leis')then 1 else 0 end as 'Flight',

	  case when exists (select [booking ref] 
						 from view_EUR_Accounts_Bookings_On_Components 
				   		 where [compname] = 'SINGLE TRIP INSURANCE'and [booking product] = 'Leis')then 1 else 0 end as 'STI'

from view_EUR_Accounts_Bookings_On_Components

where [booking product] = 'Leis' 

group by [booking product],[booking client],[booking ref],[bookingstatus], [booking conf date],[booking dep date]

order by [booking ref]

Open in new window

Book2.xls
0
 
cyberkiwiCommented:
Hi,

Here's how I would change one of those exists subqueries

The code is telling it to look in the same table (aliases as C2) for component type Accom but only within the booking reference of the record from the main select

You can also add other conditions if the booking reference is not enough
case when exists (select *
from view_EUR_Accounts_Bookings_On_Components C2
where C2.[compname] = 'ACCOMMODATION'
and C2.[booking ref] = view_EUR_Accounts_Bookings_On_Components.[booking ref])
then 1 else 0 end as Accom,

Open in new window

0
 
wexasAuthor Commented:
thanks very much , i will try this and will definitely update you if it works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.