Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jason_moutia
Starting Member
3 Posts |
Posted - 2010-05-22 : 06:15:08
|
| I have the following tables:EVENT TABLETourID EventDate ExpertID GiftID Price MealB1 18/02/2010 1 G01 $85.00 NoB1 18/02/2010 1 G03 $85.00 NoB1 18/02/2010 1 G05 $85.00 NoB1 18/02/2010 1 G07 $85.00 NoR2 18/02/2010 12 G01 $125.00 NoR2 18/02/2010 12 G03 $125.00 NoR2 18/02/2010 12 G07 $125.00 NoR2 18/02/2010 12 G09 $125.00 NoR2 25/02/2010 1 G02 $170.00 YesR2 25/02/2010 1 G05 $170.00 YesR2 25/02/2010 1 G08 $170.00 YesS1 3/06/2010 5 G02 $130.00 NoS1 3/06/2010 5 G04 $130.00 NoS1 3/06/2010 5 G09 $130.00 NoW1 3/06/2010 5 G01 $105.00 NoW1 3/06/2010 5 G04 $105.00 NoW1 3/06/2010 5 G06 $105.00 NoBOOKING_CUST TABLECustID TourID EventDate PaidC078 R2 18/02/2010 YesC078 W1 3/06/2010 NoC123 B1 18/02/2010 YesC123 W1 3/06/2010 NoC178 B1 18/02/2010 YesC191 B1 18/02/2010 YesC191 R2 25/02/2010 YesC218 B1 18/02/2010 YesC218 R2 25/02/2010 YesC225 R2 18/02/2010 YesC267 R2 18/02/2010 YesC334 B1 18/02/2010 YesC367 B1 18/02/2010 YesC555 B1 18/02/2010 YesI need to list all number of people participating in each tour. Include event where no people have booked yet. In Date / Tour Code Sequence. The result should be:TourCode EventDate CountB1 18/02/2010 7R2 18/02/2010 3R2 25/02/2010 2S1 03/06/2010 0W1 03/06/2010 2My statement is as follow:SELECT EVENT.TourID, EVENT.EventDate, count(BOOKING_CUST.TourID) AS [Count]FROM EVENT LEFT JOIN BOOKING_CUST ON EVENT.TourID = BOOKING_CUST.TourIDGROUP BY EVENT.TourID, EVENT.EventDate;AND I HAVE THE FOLLOWING RESULT:TourID EventDate CountB1 18/02/2010 28R2 18/02/2010 20R2 25/02/2010 15S1 3/06/2010 0W1 3/06/2010 6My count is not correct. Can anyone help me please |
|
|
malpashaa
Constraint Violating Yak Guru
264 Posts |
Posted - 2010-05-22 : 06:54:28
|
You should get distinct tour id and event date befor joining like this:SELECT E.TourID, E.EventDate, COUNT(BC.TourID) AS [Count] FROM (SELECT E.TourID, E.EventDate FROM EVENT AS E GROUP BY E.TourID, E.EventDate) AS E LEFT OUTER JOIN BOOKING_CUST AS BC ON E.TourID = BC.TourID GROUP BY E.TourID, E.EventDate; |
 |
|
|
jason_moutia
Starting Member
3 Posts |
Posted - 2010-05-22 : 07:49:49
|
| Thanks it work.I have another query issue. Pretty much the same as above. There's an additional tableTABLE EXPERTExpertID E_name E_surname E_phone1 Geoff Millar 921411115 Sue Davies 921422228 Hillary Cork 9214444412 Penny Folds 92143333I need to list Tour Code, Event Date, Expert name and Total Payment made for each tour event.The list must be in Tour Code / Event Date sequence.The result should be:TourCode EventDate ExpertFirstName ExpertSurname TotalB1 18/02/2010 Geoff Millar 510R2 18/02/2010 Penny Folds 375R2 25/02/2010 Geoff Millar 340My statement is as follow:SELECT BOOKING_CUST.TourID, BOOKING_CUST.EventDate, EXPERT.E_name, EXPERT.E_surname, (count(BOOKING_CUST.TourID) * EVENT.price) AS [Total]FROM (BOOKING_CUST LEFT OUTER JOIN EVENT ON BOOKING_CUST.TourID = EVENT.TourID)LEFT OUTER JOIN EXPERTON EVENT.ExpertID = EXPERT.ExpertIDWHERE EVENT.EventDate = BOOKING_CUST.EventDateAND PAID = YESGROUP BY BOOKING_CUST.TourID, BOOKING_CUST.EventDate, EXPERT.E_name, EXPERT.E_surname, EVENT.price;AND I HAVE THE FOLLOWING RESULT:TourID EventDate E_name E_surname TotalB1 18/02/2010 Geoff Millar $2,380.00R2 18/02/2010 Penny Folds $1,500.00R2 25/02/2010 Geoff Millar $1,020.00I tried to DISTINCT the TourID and EventDate but I got it wrong. Probably made a mistake. Can you please help me? |
 |
|
|
|
|
|
|
|