SQL Performance multiple exclusion from the same table - sql
I have a table where I have a list of people, lets say i have 100 people listed in that table
I need to filter out the people using different criteria's and put them in groups, problem is when i start excluding on the 4th-5th level, performance issues come up and it becomes slow
with lst_tous_movements as (
select
t1.refid_eClinibase
t1.[dthrfinmouvement]
t1.[unite_service_id]
t1.[unite_service_suiv_id]
from sometable t1
)
,lst_patients_hospitalisés as (
select distinct
t1.refid_eClinibase
from lst_tous_movements t1
where
t1.[dthrfinmouvement] = '4000-01-01'
)
,lst_patients_admisUIB_transferes as (
select distinct
t1.refid_eClinibase
from lst_tous_movements t1
left join lst_patients_hospitalisés t2 on t1.refid_eClinibase = t2.refid_eClinibase
where
t1.[unite_service_id] = 4
and t1.[unite_service_suiv_id] <> 0
and t2.refid_eClinibase is null
)
,lst_patients_admisUIB_nonTransferes as (
select distinct
t1.refid_eClinibase
from lst_tous_movements t1
left join lst_patients_admisUIB_transferes t2 on t1.refid_eClinibase = t2.refid_eClinibase
left join lst_patients_hospitalisés t3 on t1.refid_eClinibase = t3.refid_eClinibase
where
t1.[unite_service_id] = 4
and t1.[unite_service_suiv_id] = 0
and t2.refid_eClinibase is null
and t3.refid_eClinibase is null
)
,lst_patients_autres as (
select distinct
t1.refid_eClinibase
from lst_patients t1
left join lst_patients_admisUIB_transferes t2 on t1.refid_eClinibase = t2.refid_eClinibase
left join lst_patients_hospitalisés t3 on t1.refid_eClinibase = t3.refid_eClinibase
left join lst_patients_admisUIB_nonTransferes t4 on t1.refid_eClinibase = t4.refid_eClinibase
where
t2.refid_eClinibase is null
and t3.refid_eClinibase is null
and t4.refid_eClinibase is null
)
as you can see i have a multi level filtering out going on here...
1st i get the people where t1.[dthrfinmouvement] = '4000-01-01'
2nd i get the people with another criteria EXCLUDING the 1st group
3rd i get the people with yet another criteria EXCLUDING the 1st and
the 2nd group
etc..
when i get to the 4th level, my query takes 6 - 10 seconds to complete
is there any way to speed this up ?
this is my dataset i'm working with:
+------------------+-------------------------------+------------------+------------------+-----------------------+
| refid_eClinibase | nodossierpermanent_eClinibase | dthrfinmouvement | unite_service_id | unite_service_suiv_id |
+------------------+-------------------------------+------------------+------------------+-----------------------+
| 25611 | P0017379 | 2013-04-27 | 58 | 0 |
| 25611 | P0017379 | 2013-05-02 | 4 | 2 |
| 25611 | P0017379 | 2013-05-18 | 2 | 0 |
| 85886 | P0077918 | 2013-04-10 | 58 | 0 |
| 85886 | P0077918 | 2013-05-06 | 6 | 12 |
| 85886 | P0077918 | 4000-01-01 | 12 | 0 |
| 91312 | P0083352 | 2013-07-24 | 3 | 14 |
| 91312 | P0083352 | 2013-07-24 | 14 | 3 |
| 91312 | P0083352 | 2013-07-30 | 3 | 8 |
| 91312 | P0083352 | 4000-01-01 | 8 | 0 |
| 93835 | P0085879 | 2013-04-30 | 58 | 0 |
| 93835 | P0085879 | 2013-05-07 | 4 | 2 |
| 93835 | P0085879 | 2013-05-16 | 2 | 0 |
| 93835 | P0085879 | 2013-05-22 | 58 | 0 |
| 93835 | P0085879 | 2013-05-24 | 4 | 0 |
| 93835 | P0085879 | 2013-05-31 | 58 | 0 |
| 93836 | P0085880 | 2013-05-20 | 58 | 0 |
| 93836 | P0085880 | 2013-05-22 | 4 | 2 |
| 93836 | P0085880 | 2013-05-31 | 2 | 0 |
| 97509 | P0089576 | 2013-04-09 | 58 | 0 |
| 97509 | P0089576 | 2013-04-11 | 4 | 0 |
| 102787 | P0094886 | 2013-04-08 | 58 | 0 |
| 102787 | P0094886 | 2013-04-11 | 4 | 2 |
| 102787 | P0094886 | 2013-05-21 | 2 | 0 |
| 103029 | P0095128 | 2013-04-04 | 58 | 0 |
| 103029 | P0095128 | 2013-04-10 | 4 | 1 |
| 103029 | P0095128 | 2013-05-03 | 1 | 0 |
| 103813 | P0095922 | 2013-07-02 | 58 | 0 |
| 103813 | P0095922 | 2013-07-03 | 4 | 6 |
| 103813 | P0095922 | 2013-08-14 | 6 | 0 |
| 105106 | P0097215 | 2013-08-09 | 58 | 0 |
| 105106 | P0097215 | 2013-08-13 | 4 | 0 |
| 105106 | P0097215 | 2013-08-14 | 58 | 0 |
| 105106 | P0097215 | 4000-01-01 | 4 | 0 |
| 106223 | P0098332 | 2013-06-11 | 1 | 0 |
| 106223 | P0098332 | 2013-08-01 | 58 | 0 |
| 106223 | P0098332 | 4000-01-01 | 1 | 0 |
| 106245 | P0098354 | 2013-04-02 | 58 | 0 |
| 106245 | P0098354 | 2013-05-24 | 58 | 0 |
| 106245 | P0098354 | 2013-05-29 | 4 | 1 |
| 106245 | P0098354 | 2013-07-12 | 1 | 0 |
| 106280 | P0098389 | 2013-04-07 | 58 | 0 |
| 106280 | P0098389 | 2013-04-09 | 4 | 0 |
| 106416 | P0098525 | 2013-04-19 | 58 | 0 |
| 106416 | P0098525 | 2013-04-23 | 4 | 0 |
| 106444 | P0098553 | 2013-04-22 | 58 | 0 |
| 106444 | P0098553 | 2013-04-25 | 4 | 0 |
| 106609 | P0098718 | 2013-05-08 | 58 | 0 |
| 106609 | P0098718 | 2013-05-10 | 4 | 11 |
| 106609 | P0098718 | 2013-07-24 | 11 | 12 |
| 106609 | P0098718 | 4000-01-01 | 12 | 0 |
| 106616 | P0098725 | 2013-05-09 | 58 | 0 |
| 106616 | P0098725 | 2013-05-09 | 4 | 1 |
| 106616 | P0098725 | 2013-07-27 | 1 | 0 |
| 106698 | P0098807 | 2013-05-16 | 58 | 0 |
| 106698 | P0098807 | 2013-05-22 | 4 | 6 |
| 106698 | P0098807 | 2013-06-14 | 6 | 1 |
| 106698 | P0098807 | 2013-06-28 | 1 | 0 |
| 106714 | P0098823 | 2013-05-20 | 58 | 0 |
| 106714 | P0098823 | 2013-05-21 | 58 | 0 |
| 106714 | P0098823 | 2013-05-24 | 58 | 0 |
| 106729 | P0098838 | 2013-05-21 | 58 | 0 |
| 106729 | P0098838 | 2013-05-23 | 4 | 1 |
| 106729 | P0098838 | 2013-06-03 | 1 | 0 |
| 107038 | P0099147 | 2013-06-25 | 58 | 0 |
| 107038 | P0099147 | 2013-06-28 | 4 | 1 |
| 107038 | P0099147 | 2013-07-04 | 1 | 0 |
| 107038 | P0099147 | 2013-08-13 | 58 | 0 |
| 107038 | P0099147 | 2013-08-15 | 4 | 6 |
| 107038 | P0099147 | 4000-01-01 | 6 | 0 |
| 107082 | P0099191 | 2013-06-29 | 58 | 0 |
| 107082 | P0099191 | 2013-07-04 | 4 | 6 |
| 107082 | P0099191 | 2013-07-19 | 6 | 0 |
| 107157 | P0099267 | 4000-01-01 | 13 | 0 |
| 107336 | P0099446 | 4000-01-01 | 6 | 0 |
+------------------+-------------------------------+------------------+------------------+-----------------------+
thanks.
It is hard to understand exactly what all your rules are from the question, but the general approach should be to add a "Grouping" column to a singl query that uses a CASE statement to categorize the people.
The conditions in a CASE are evaluated in order, so that if the first criteria is met, then the subsequent criteria are not even evaluated for that row.
Here is some code to get you started....
select t1.refid_eClinibase
,t1.[dthrfinmouvement]
,t1.[unite_service_id]
,t1.[unite_service_suiv_id]
CASE WHEN [dthrfinmouvement] = '4000-01-01' THEN 'Group1 Label'
WHEN condition2 = something THEN 'Group2 Label'
....
WHEN conditionN = something THEN 'GroupN Label'
ELSE 'Catch All Label'
END as person_category
from sometable t1
Related
How I can I add a count to rank null values in SQL Hive?
This is what I have right now: | time | car_id | order | in_order | |-------|--------|-------|----------| | 12:31 | 32 | null | 0 | | 12:33 | 32 | null | 0 | | 12:35 | 32 | null | 0 | | 12:37 | 32 | 123 | 1 | | 12:38 | 32 | 123 | 1 | | 12:39 | 32 | 123 | 1 | | 12:41 | 32 | 123 | 1 | | 12:43 | 32 | 123 | 1 | | 12:45 | 32 | null | 0 | | 12:47 | 32 | null | 0 | | 12:49 | 32 | 321 | 1 | | 12:51 | 32 | 321 | 1 | I'm trying to rank orders, including those who have null values, in this case by car_id. This is the result I'm looking for: | time | car_id | order | in_order | row | |-------|--------|-------|----------|-----| | 12:31 | 32 | null | 0 | 1 | | 12:33 | 32 | null | 0 | 1 | | 12:35 | 32 | null | 0 | 1 | | 12:37 | 32 | 123 | 1 | 2 | | 12:38 | 32 | 123 | 1 | 2 | | 12:39 | 32 | 123 | 1 | 2 | | 12:41 | 32 | 123 | 1 | 2 | | 12:43 | 32 | 123 | 1 | 2 | | 12:45 | 32 | null | 0 | 3 | | 12:47 | 32 | null | 0 | 3 | | 12:49 | 32 | 321 | 1 | 4 | | 12:51 | 32 | 321 | 1 | 4 | I just don't know how to manage a count for the null values. Thanks!
You can count the number of non-NULL values before each row and then use dense_rank(): select t.*, dense_rank() over (partition by car_id order by grp) as row from (select t.*, count(order) over (partition by car_id order by time) as grp from t ) t;
How do I get around aggregate function error?
I have the following sql to calculate a % total: SELECT tblTourns_atp.ID_Ti, Sum([FS_1]/(SELECT Sum(FSOF_1) FROM stat_atp WHERE stat_atp.ID_T = tblTourns_atp.ID_T)) AS S1_IP FROM stat_atp INNER JOIN tblTourns_atp ON stat_atp.ID_T = tblTourns_atp.ID_T GROUP BY tblTourns_atp.ID_Ti I'm getting the 'aggregate error' because it wants the ID_T fields either grouped or in an aggregate function. I've read loads of examples but none of them seem to apply when the offending field is the subject of 'WHERE'. Tables and output as follows: +----------+------+--------+--+---------------+-------+--+--------+--------+ | stat_atp | | | | tblTourns_atp | | | Output | | +----------+------+--------+--+---------------+-------+--+--------+--------+ | ID_T | FS_1 | FSOF_1 | | ID_T | ID_Ti | | ID_Ti | S1_IP | | 1 | 20 | 40 | | 1 | 1 | | 1 | 31.03% | | 2 | 30 | 100 | | 2 | 1 | | 2 | 28.57% | | 3 | 40 | 150 | | 3 | 1 | | 3 | 33.33% | | 4 | 30 | 100 | | 4 | 2 | | | | | 5 | 30 | 100 | | 5 | 2 | | | | | 6 | 40 | 150 | | 6 | 2 | | | | | 7 | 20 | 40 | | 7 | 3 | | | | | 8 | 30 | 100 | | 8 | 3 | | | | | 9 | 40 | 150 | | 9 | 3 | | | | | 10 | 20 | 40 | | 10 | 3 | | | | +----------+------+--------+--+---------------+-------+--+--------+--------+
Since you already have an inner join between the two tables, a separate subquery isn't required: select t.id_ti, sum(s.fs_1)/sum(s.fsof_1) as pct from tbltourns_atp t inner join stat_atp s on t.id_t = s.id_t group by t.id_ti
How to select data order by sort value and sort NULL By name
I wrote following SQL query to select data from #tmp table variable. SELECT #rowCount AS [row-count], t.[row-no] AS [row-no], t.[ServiceID] AS ServiceID, t.ServiceName AS ServiceName, t.[BranchServiceSortValue] AS SortValue, (CASE WHEN t.OptIn = 1 THEN 'Yes' ELSE 'No' END) AS OptIn FROM #tmp t INNER JOIN dbo.Category ON Category.CategoryId = t.FkCategoryId INNER JOIN dbo.ServiceType ON ServiceType.ServiceTypeId = t.FkServiceTypeId WHERE t.[row-no] >= #startRow AND t.[row-no] <= #endRow ORDER BY t.BranchServiceSortValue,t.serviceName According to the data in #tmp table,my above query return following output. | row-count | row-no | ServiceID | ServiceName | SortValue | OptIn | |-----------|--------|-----------|-------------|-----------|-------| | 24 | 4 | 1088 | AAB | NULL | No | | 24 | 5 | 1089 | AAC | NULL | No | | 24 | 6 | 1090 | AAD | NULL | No | | 24 | 1 | 1093 | GDGD | 0 | Yes | | 24 | 7 | 1091 | EETETE | 1 | Yes | | 24 | 8 | 1092 | CSCDF | 2 | Yes | | 24 | 3 | 1086 | CXCX | 3 | Yes | | 24 | 9 | 16 | ASA | 4 | Yes | | 24 | 2 | 1087 | BFB | 5 | Yes | | 24 | 10 | 7 | Mortgage | 6 | Yes | | 24 | 11 | 17 | DDWW | 7 | Yes | | 24 | 12 | 11 | IL | 8 | Yes | | 24 | 13 | 5 | SAA | 9 | Yes | | 24 | 14 | 9 | CD | 10 | Yes | You can see according to my above query data rows are sorted by SortValue and when SortValue = NULL, those 3 rows sorted by its ServiceName, But I need to displaySortValue = NULLrows at the bottom of the other rows.Its mean I need to display Null rows after the SortValue Not NULL data and SortValue = NULL should be display order by its ServiceName. My Expected Output is: | row-count | row-no | ServiceID | ServiceName | SortValue | OptIn | |-----------|--------|-----------|-------------|-----------|-------| | 14 | 1 | 1093 | GDGD | 0 | Yes | | 14 | 7 | 1091 | EETETE | 1 | Yes | | 14 | 8 | 1092 | CSCDF | 2 | Yes | | 14 | 3 | 1086 | CXCX | 3 | Yes | | 14 | 9 | 16 | ASA | 4 | Yes | | 14 | 2 | 1087 | BFB | 5 | Yes | | 14 | 10 | 7 | Mortgage | 6 | Yes | | 14 | 11 | 17 | DDWW | 7 | Yes | | 14 | 12 | 11 | IL | 8 | Yes | | 14 | 13 | 5 | SAA | 9 | Yes | | 14 | 14 | 9 | CD | 10 | Yes | | 14 | 4 | 1088 | AAB | NULL | No | | 14 | 5 | 1089 | AAC | NULL | No | | 14 | 6 | 1090 | AAD | NULL | No | How should I need to change my query to get above output? please help me
NULL has the lowest value, so you'll need to use a CASE to put NULL at the end, and then sort by SortValue: ORDER BY CASE WHEN t.BranchServiceSortValue IS NULL THEN 1 ELSE 0 END, t.BranchServiceSortValue, t.serviceName;
Just add a key to the ORDER BY: ORDER BY (CASE WHEN t.BranchServiceSortValue IS NOT NULL THEN 1 ELSE 2 END), t.BranchServiceSortValue, t.serviceName The SQL standard provides the options NULLS FIRST and NULLS LAST for ORDER BY clauses. SQL Server does not (yet) implement these.
select all rows that match criteria if not get a random one
+----+---------------+--------------------+------------+----------+-----------------+ | id | restaurant_id | filename | is_profile | priority | show_in_profile | +----+---------------+--------------------+------------+----------+-----------------+ | 40 | 20 | 1320849687_390.jpg | | | 1 | | 60 | 24 | 1320853501_121.png | 1 | | 1 | | 61 | 24 | 1320853504_847.png | | | 1 | | 62 | 24 | 1320853505_732.png | | | 1 | | 63 | 24 | 1320853505_865.png | | | 1 | | 64 | 29 | 1320854617_311.png | 1 | | 1 | | 65 | 29 | 1320854617_669.png | | | 1 | | 66 | 29 | 1320854618_636.png | | | 1 | | 67 | 29 | 1320854619_791.png | | | 1 | | 74 | 154 | 1320922653_259.png | | | 1 | | 76 | 154 | 1320922656_332.png | | | 1 | | 77 | 154 | 1320922657_106.png | | | 1 | | 84 | 130 | 1321269380_960.jpg | 1 | | 1 | | 85 | 130 | 1321269383_555.jpg | | | 1 | | 86 | 130 | 1321269384_251.jpg | | | 1 | | 89 | 28 | 1321269714_303.jpg | | | 1 | | 90 | 28 | 1321269716_938.jpg | 1 | | 1 | | 91 | 28 | 1321269717_147.jpg | | | 1 | | 92 | 28 | 1321269717_774.jpg | | | 1 | | 93 | 28 | 1321269717_250.jpg | | | 1 | | 94 | 28 | 1321269718_964.jpg | | | 1 | | 95 | 28 | 1321269719_830.jpg | | | 1 | | 96 | 43 | 1321270013_629.jpg | 1 | | 1 | +----+---------------+--------------------+------------+----------+-----------------+ I have this table and I want to select the filename for a given list of restaurants ids. For example for 24,29,154: +----+--------------- | filename | +----+--------------- 1320853501_121.png (has is_profile 1) 1320854617_311.png (has is_profile 1) 1320922653_259.png (chosen as profile picture because restaurant doesn't have a profile pic but has pictures) I tried group by and case statements but I got nowhere.Also if you use group by it should be a full group by.
You can do this with aggregation and some logic: select restaurant_id, coalesce(max(case when is_profile = 1 then filename end), max(filename) ) as filename from t where restaurant_id in (24, 29, 154) group by restaurant_id; First look for the/a profile filename. Next just choose an arbitrary one.
How can I do this in SQL in a Single Statement?
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.
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.
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
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