Saturday, 15 June 2013

sql server - GROUP BY multiple derived Max() and Min() columns -



sql server - GROUP BY multiple derived Max() and Min() columns -

i trying create script grouping tables contents series of columns have taken max() of.

it hard describe without scenario:

i have table of bookings have create table of single customer, customers split downwards res system, each res scheme requires different grouping using different columns.

i.e:

277000 bookings ipcos 289300 bookings daph 300000 bookings tard

they stored in same table , want take max of columns except couple have cast integers , sum() other columns up.

my problem comes when have grouping casted value have created , min() value have created.

i tried joining table onto each other didn't work, can point me in right direction please getting frustrated.

code selecting ipcos

select sum(cast(totalcost money)) totalcost, nettcost = null, paid = null, balance = null, discount = null, commission = null, max(adults) adults, max(children)as children, max(infants) infants, max(pax) pax, sum(cast(duration int)) duration, max([identityvalue]) identityvalue, max([filename]) filename, max([sheetname]) sheetname, max([linenum]) linenum, max([bookingdate]) bookingdate, cast([departuredate] int) intdeparturedate, cast([bookingdate] int) intbookingdate, min(linenum) minlinenum booking resid = 3 grouping minlinenum, intbookingdate, intdeparturedate

the other systems similar this

any help fantastic cheers in adavnce

you can utilize on ( partition ) statement in order tou grouping different columns. here over clause

sql-server tsql group-by max

No comments:

Post a Comment