我有以下MySQL表:
+---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+ | 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 | +---------+------------+------+--------+------+---------+------------+-------+---------+----------+------------+------------+
我需要获取Mvt = 60200的所有行,并将该行中的每个GC和LC记录乘以1.1,并将包含更改的新行添加回同一个表中,并将FY设置为2011.
我怎样才能在一份声明中做到这一切? 是否有可能在1个语句中完成所有这些(我对SQL知之甚少)? 这可以在标准SQL中完成,因为数据库将被移植到另一个数据库服务器吗? 我不知道它将是哪个服务器。
答案 0 :(得分:9)
在标准SQL中(在特定于供应商的实现中可能有更好的方法,但我倾向于更喜欢标准的东西):
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
您可能还希望根据测试结果稍微限制您的选择语句,例如取消注释上面的and FY = 2010
行以停止复制所有2009和2008数据(如果有的话)。我认为你只想在GC
和LC
上增加10%,继续前一年的事情。
这样做的方法是运行select
,根据您的请求提供FY
,GC
和LC
的修改数据,并将所有这些行重新输入insert
。
答案 1 :(得分:0)
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
答案 2 :(得分:0)
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
此语句应适用于任何SQL数据库。
编辑:太慢