Hey All.I need some help with T-SQL. I've been struggling for many many hours with it and it is pissing me up.I can't use loops, cursors, etc. This is because I need high performance.I would be very grateful if you may help me.[b]Here is my problem:[/b]I have a table called __tt_Freight_Product which has the following columns:dt_reference_date, id_contract, qtt_terminal_loadedAnd a table called Product which has the following columns:dt_reference_date, id_contract, id_fixing, qtt_fixing, qtt_terminalThere are NO foreign key between them, yet id_contract and dt_reference_date are supposed to be the same.Example data on __tt_Freight_Product:('2015-02-25', '0000006-t12', 200000)('2015-02-26', '0000006-t12', 200000)('2015-02-28', '0000006-t12', 100000)That data means that 200,000 TONS was loaded on contract '0000006-t12' into terminal on '2015-02-25', and so onExample data on Product:('2015-02-24', '0000006-t12', 1, 300000, 0)('2015-02-25', '0000006-t12', 1, 300000, 0)('2015-02-26', '0000006-t12', 1, 300000, 0)('2015-02-27', '0000006-t12', 1, 300000, 0)('2015-02-28', '0000006-t12', 1, 300000, 0)('2015-02-29', '0000006-t12', 1, 300000, 0)('2015-02-24', '0000006-t12', 2, 200000, 0)('2015-02-25', '0000006-t12', 2, 200000, 0)('2015-02-26', '0000006-t12', 2, 200000, 0)('2015-02-27', '0000006-t12', 2, 200000, 0)('2015-02-28', '0000006-t12', 2, 200000, 0)('2015-02-29', '0000006-t12', 2, 200000, 0)What I need to accomplish is to split the loaded volumes that are on __tt_Freight_Product into the Product table, per dt_reference_date/id_contract/id_fixing. That splited volume will be updated on the Product table.It's kinda we had to divide the volume per "id_fixing".So, considering the example data on __tt_Freight_Product we will update:dt_reference_date: '2015-02-24'Nothing will be updated as the cargo loaded just con 25thdt_reference_date: '2015-02-25'qtt_terminal of id_fixing "1" will be updated to 200,000qtt_terminal of id_fixing "2" won't changedt_reference_date: '2015-02-26'qtt_terminal of id_fixing "1" will be updated to 300,000qtt_terminal of id_fixing "2" will be updated to 100,000dt_reference_date: '2015-02-27'qtt_terminal of id_fixing "1" will be updated to 300,000, as we had cargo on the previous day and no loaded cargo on 27th OR considering we already have id_fixing '1' "*full allocated*"qtt_terminal of id_fixing "2" will be updated to 100,000, as we had cargo on the previous day and no loaded cargo on 27thdt_reference_date: '2015-02-28'qtt_terminal of id_fixing "1" will be updated to 300,000, as we had cargo on the previous day and no loaded cargo on 27th OR considering we already have id_fixing '1' "*full allocated*"qtt_terminal of id_fixing "2" will be updated to 200,000dt_reference_date: '2015-02-29'qtt_terminal of id_fixing "1" will be updated to 300,000, as we had cargo on the previous day and no loaded cargo on 29th OR considering we already have id_fixing '1' "*full allocated*"qtt_terminal of id_fixing "2" will be updated to 200,000, as we had cargo on the previous day and no loaded cargo on 29thAny ideas?
↧