Sunday 15 June 2014

sql server 2008 - SQL MIN and MAX relations in SELECT CASE -


I am retrieving columns from the SQL table using the following code -

  MAX WHEN bd.DayText = 'Monday' and bd.BookingDuration = 3 and CONVERT (Time (0), bd.StartTime) & lt; CONVERT (Time (0), '12: 00: 00 ') and bd.NoOfHOurs & lt 5.5 and s.PrimarySchool = 1 THEN BD.ID ELSE Null and "Monday Haurim", Max (Cases when BD. Diodex = 'Monday' and BD. Booking Period = 3 & Convert (Time (0), BD. Starttime) & LT Convert (Time (0), '12: 00: 00 ') and BD .offhors <5.5 And s.PrimarySchool = 1 SS SchoolName ELSE Faucet END) "Monday Haurihmskoolnam", Max (Case when BD .. DIETEX = 'MONDAY' '& BW BOOKING DRIBATION = 3 & Convert (TY) (0), BD.starttime) and Convert (Time (0), '12: 00: 00 ') and B.D.offhors & lt; 5.5 and S. Primary School = 1 then replace (Replay (LTRIM (right) (VAcarar, BD. Starttime, 100), 7)) + '-' + + LTRIM (Correct (VAARAAR (20), BD and TIME, 7)), 'AM', ''), 'PM' ''), "Monday Hoorimitimes", Max (Case when BD.De Test = 'Monday' and BD. BookingDeurebill = 3 & Convert (Time (0), BD Starttaim) and LT; convert (time (0), 12: 00: 00 ") and BD Kofhors & lt; 5.5 and s.PrimarySchool = 1 then and bd TotalChargeAmount zero end) "MondayHourlyAMTotalChargeAmount", Max (case when bd.DayText = 'Monday' and bd.BookingDuration = 3 and converted (time (0), bd.StartTime) & Lt; CONVERT (time (0), '12: 00: 00 ') and bd.NoOfHOurs & lt; 5.5 and s.PrimarySchool = 1 then and bd. TotalPayAmount zero end) "MondayHourlyAMTotalPayAmount", Max (case when bd Convert (Time (0), '12: 00: 00 ') and BD. Forums and Lieutenant 5.5 and S. DAYText =' Monday 'and BWBidding Diffusion = 3 and convert (Time (0), BD. Primary school = 1 THEN bd. BandBookedAt or zero end) "MondayHourlyAMBandBookedAt", Min (case when bd.DayText = 'Monday' and bd.BookingDuration = 3 and Convert (Time (0), bd.StartTime) & lt; CONVERT (Time (0) '12:00:00') and B.D.offhors & lt; 5.5 and s.PrimarySchool = 1 THEN bd.ID ELSE NULL END) "Monday HourlyAM2", MIN (Case when BD. DayText = 'Monday' and BD .. Booking Period = 3 and Convert (Time (0), BD. Starttime) Convert (Time (0), '12: 00: 00 ') and BD OFFERS & lt; 5.5 and S. Primima RySchool = 1 SS SchoolName ELSE Faucet END) "Monday HourlyAM2SchoolName", MIN (Case when BD .. DayText = 'Monday' and BD. Booking Period = 3 & Convert (Time (0), BD. Starttime) & lt; Convert (Time (0), '12: 00: 00 ') and BD .offhors & lt; 5.5 and s.PrimarySchool = 1 then change (REPLACE (LTRIM (right (CONVERT (VARCHAR, bd.StartTime , 100), 7)) + '-' + LTRIM (right (CONVERT (VARCHAR (20), bd.EndTime, 100), 7)), 'AM', '', 'PM', '') And) "Monday Hormium 2 Times", Mr. (Case when BD. Diodex = 'Monday' and BD. And convert time (0), bd.StartTime & lt; CONVERT (time (0), '12: 00: 00 ') and bd.NoOfHOurs & lt; 5.5 and s.PrimarySchool = 1 then and bd TotalChargeAmount zero End: "MondayHourlyAM2TotalChargeAmount", MIN (Case when BD.Daytext = 'Monday' and BD.) Booking Period = 3 and Convert (Time (0), BD. Starttime) Convert (Time (0), '12: 00 : 00 ') and BD No Offhors and Lt. 5.5 and S. Elementary School = 1 THEN BD Kulapakase AMAS Null and "Monday HauraM 2 TallPayout", Min (Case when BD. DIETEX = 'MONDAY' AND BD. Booking Period = 3 and Convert (Time (0), BD. Starttime) & lt; CONVERT (time (0), '12: 00: 00 ') and bd.NoOfHOurs & LT; 5.5 and s.PrimarySchool = 1 then and bd.BandBookedAt zero end) "MondayHourlyAM2BandBookedAt",   

When this code runs, I get 2 tickets, though the charges for other bookings / Wage rate i.e. -

bookings should be 1 - charge £ 20, salary £ 10 booking 2 - charge £ 100, salary £ 50

but I get bookings - £ 100 in charge , Pay £ 50 bookings 2 - fee £ 20, pay £ 10

I have reason to think because its maximum and maximum use is using IM and MIN "I Tamiam "How do address the MAX and MIN values ​​returned in the column?

Hope that I explained it well! Thanks

I am not being transmitted through my criteria without a better understanding of my data, but I think you'll get a better service using ROW_NUMBER ():

  SELECT * FROM (SELECT *, ROW_NUMBER (Monday HOURLYAMTimes DESC Monday) 'Booking1', ROW_NUMBER () In the general criteria from the 'Booking 2' table (Monday morning on Monday), where booking1 = 1 or booking2 = 1   

The idea is that each of you Specify a ranking for a row that meets your criteria on a field, then return the full line based on that rank.

No comments:

Post a Comment