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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cyberkiwiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.