tags:

views:

95

answers:

3

Hi,

I have the following MySQL table:

+---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+
| Version | Yr_Varient | FY   | Period | CoA  | Company | Item       | Mvt   | Ptnr_Co | Investee | GC         | LC         |
+---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+
| 201     | 1          | 2010 | 1      | 11   | 23      | 1110105000 | 60200 |         |          | 450000     | 450000     |
| 201     | 1          | 2010 | 1      | 11   | 23      | 2110300000 | 60200 |         |          | -520000    | -520000    |
| 201     | 1          | 2010 | 1      | 11   | 23      | 1220221600 |       |         |          | 78080      | 78080      |
| 201     | 1          | 2010 | 1      | 11   | 23      | 2130323000 |       |         |          | 50000      | 50000      |
| 201     | 1          | 2010 | 1      | 11   | 23      | 2130322000 |       |         |          | -58080     | -58080     |
| 201     | 1          | 2010 | 1      | 11   | 23      | 3100505000 |       |         |          | -275000    | -275000    |
| 201     | 1          | 2010 | 1      | 11   | 23      | 3200652500 |       |         |          | 216920     | 216920     |
| 201     | 1          | 2010 | 1      | 11   | 23      | 3900000000 |       |         |          | 58080      | 58080      |
| 201     | 1          | 2010 | 1      | 11   | 26      | 1110105000 | 60200 |         |          | 376000     | 376000     |
| 201     | 1          | 2010 | 1      | 11   | 26      | 2110300000 | 60200 |         |          | -545000    | -545000    |
| 201     | 1          | 2010 | 1      | 11   | 26      | 1220221600 |       |         |          | 452250     | 452250     |
| 201     | 1          | 2010 | 1      | 11   | 26      | 2130323000 |       |         |          | -165000    | -165000    |
| 201     | 1          | 2010 | 1      | 11   | 26      | 2130322000 |       |         |          | -118250    | -118250    |
| 201     | 1          | 2010 | 1      | 11   | 26      | 3100505000 |       |         |          | -937750    | -937750    |
| 201     | 1          | 2010 | 1      | 11   | 26      | 3200652500 |       |         |          | 819500     | 819500     |
| 201     | 1          | 2010 | 1      | 11   | 26      | 3900000000 |       |         |          | 118250     | 118250     |
| 201     | 1          | 2010 | 1      | 11   | 37      | 1110105000 | 60200 |         |          | 777000     | 777000     |
| 201     | 1          | 2010 | 1      | 11   | 37      | 2110308000 | 60200 | 43      |          | -255000    | -255000    |
| 201     | 1          | 2010 | 1      | 11   | 37      | 2130321500 |       |         |          | 180000     | 180000     |
| 201     | 1          | 2010 | 1      | 11   | 37      | 2130322000 |       |         |          | -77000     | -77000     |
| 201     | 1          | 2010 | 1      | 11   | 37      | 2310407001 |       | 1       |          | -625000    | -625000    |
| 201     | 1          | 2010 | 1      | 11   | 37      | 3100505000 |       |         |          | -2502500   | -2502500   |
| 201     | 1          | 2010 | 1      | 11   | 37      | 3200652500 |       |         |          | 2425500    | 2425500    |
| 201     | 1          | 2010 | 1      | 11   | 37      | 3900000000 |       |         |          | 77000      | 77000      |
| 201     | 1          | 2010 | 1      | 11   | 43      | 1110105000 | 60200 |         |          | 2600000    | 2600000    |
| 201     | 1          | 2010 | 1      | 11   | 43      | 1140161000 | 60200 |         | 23       | 430000     | 430000     |
| 201     | 1          | 2010 | 1      | 11   | 43      | 1140161000 | 60200 |         | 26       | 505556     | 505556     |
| 201     | 1          | 2010 | 1      | 11   | 43      | 1140160000 | 60200 | 37      |          | 255000     | 255000     |
| 201     | 1          | 2010 | 1      | 11   | 43      | 1160163000 | 60200 | 99999   | 48       | 49428895   | 49428895   |
| 201     | 1          | 2010 | 1      | 11   | 43      | 1160163000 | 60200 | 99999   | 49       | 188260175  | 188260175  |
| 201     | 1          | 2010 | 1      | 11   | 43      | 2310405500 |       |         |          | -237689070 | -237689070 |
| 201     | 1          | 2010 | 1      | 11   | 43      | 2110300000 | 60200 |         |          | -1000      | -1000      |
| 201     | 1          | 2010 | 1      | 11   | 43      | 2110300500 | 60200 |         |          | -3999000   | -3999000   |
| 201     | 1          | 2010 | 1      | 11   | 43      | 1220221600 |       |         |          | 1571112    | 1571112    |
| 201     | 1          | 2010 | 1      | 11   | 43      | 2130321500 |       |         |          | -805556    | -805556    |
| 201     | 1          | 2010 | 1      | 11   | 43      | 2130322000 |       |         |          | -556112    | -556112    |
| 201     | 1          | 2010 | 1      | 11   | 43      | 3100505000 |       |         |          | -836000    | -836000    |
| 201     | 1          | 2010 | 1      | 11   | 43      | 3200652500 |       |         |          | 781000     | 781000     |
| 201     | 1          | 2010 | 1      | 11   | 43      | 3300715700 |       | 99999   | 32       | -440000    | -440000    |
| 201     | 1          | 2010 | 1      | 11   | 43      | 3300715700 |       | 99999   | 26       | -61112     | -61112     |
| 201     | 1          | 2010 | 1      | 11   | 43      | 3900000000 |       |         |          | 556112     | 556112     |
+---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+

I need to take all rows with Mvt = 60200 and multiply every GC and LC record in that row by 1.1 and add a new row containing the changes back into the same table with FY set to 2011.

How can I do all this in 1 statement?
Is it even possible to do all this in 1 statement (I know very little about SQL)?
Can this be done in standard SQL as the database will be ported to another Database Server?
I don't know which server it will be.

+9  A: 

In standard SQL (there may be better ways in vendor-specific implementations but I tend to prefer standard stuff where possible):

insert into mytable (
    Version, Yr_Varient, Period, CoA, Company, Item, Mvt, Ptnr_Co, Investee,
    FY, GC, LC
) select
    Version, Yr_Varient, Period, CoA, Company, Item, Mvt, Ptnr_Co, Investee,
    2011, GC*1.1, LC*1.1
    from mytable
    where Mvt = 60200
    -- and FY = 2010

You may also want to limit your select statement a little more depending on the results of your testing, such as uncommenting the and FY = 2010 line above to stop copying all your 2009 and 2008 data as well, if any. I asume you only wanted to carry forward the previous year's stuff with a 10% increase on GC and LC.

The way this works is to run the select which gives modified data for FY, GC and LC as per your request, and pump all those rows back into the insert.

paxdiablo
On some DBMS, adding "and FY = 2010" is an absolute requirement. otherwise the statement would cause an endless loop of inserts (until the disk gets full or some other limit of table size reached).
Milan Babuškov
A: 
  insert into mytable (
    Version,Yr_Varient,FY,Period,CoA,Company,Item,Mvt,Ptnr_Co,Investee,GC,LC)
    SELECT Version ,Yr_Varient,"2011" as FY, Period, CoA, Company , Item , Mvt ,Ptnr_Co ,   Investee , GC*1.1 as GC, LC*1.1 as LC FROM <table Name> 
WHERE Mvt = 60200
Treby
A: 
INSERT INTO _table_ 
    (Version, 
    Yr_Varient, 
    FY, 
    Period, 
    CoA, 
    Company, 
    Item, 
    Mvt, 
    Ptnr_Co,
    Investee,
    GC, 
    LC)
SELECT
    Version,
    Yr_Varient,
    2011,
    Period,
    CoA,
    Company,
    Item,
    Mvt,
    Ptnr_Co,
    Investee,
    GC * 1.1,
    LC * 1.1
FROM
    _table_
WHERE
    Mvt = 60200
    AND FY <> 2011

This statement should work in any SQL-Database.

Edit: Too slow

Pesse