Author |
Topic |
fuzzyGURU
Starting Member
3 Posts |
Posted - 2010-02-12 : 11:34:04
|
This might just be a complete lack of caffeine on my part but I am not able to get this issue solved.I have a table that has mismatched data in it. What I need to do is update the [required] field in all rows of the same [group] and [fieldid] with the value of the [required] from the row that has the [language] = 1My problem has been trying to get all the rows that do not match. I have tried subqueries, group bys, joins, etc. but have been unable to get it to give me an accurate list. CREATE TABLE [Fields] ( [id] [int] IDENTITY(1,1) NOT NULL, [group] [nvarchar](5) NOT NULL, [fieldid] [int] NOT NULL, [fieldtype] [int] NOT NULL, [required] [bit] NOT NULL, [label] [nvarchar](100) NOT NULL, [language] [int] NOT NULL)INSERT INTO [Fields] SELECT 'aaa01', 1, 1, 1, 'Customer', 1 UNION SELECT 'aaa01', 2, 3, 0, 'Amount', 1 UNION SELECT 'aaa01', 3, 4, 1, 'Rate', 1 UNION SELECT 'aaa01', 4, 1, 0, 'Vendor', 1 UNION SELECT 'aaa01', 5, 2, 0, 'Region', 1 UNION SELECT 'aaa01', 1, 1, 0, 'Client', 2 UNION SELECT 'aaa01', 2, 3, 0, 'Montant', 2 UNION SELECT 'aaa01', 3, 4, 1, 'Taux', 2 UNION SELECT 'aaa01', 4, 1, 0, 'Vendeur', 2 UNION SELECT 'aaa01', 5, 2, 0, 'Région', 2 UNION SELECT 'xyz01', 1, 1, 0, 'Cliente', 3 UNION SELECT 'xyz01', 2, 3, 0, 'Cantidad', 3 UNION SELECT 'xyz01', 3, 4, 1, 'Ritmo', 3 UNION SELECT 'xyz01', 4, 1, 0, 'Distribuidor', 3 UNION SELECT 'xyz01', 5, 2, 1, 'Región', 3 UNION SELECT 'bbb01', 1, 1, 1, 'Kunde', 4 UNION SELECT 'bbb01', 2, 3, 0, 'Betrag', 4 UNION SELECT 'bbb01', 3, 4, 1, 'Rate', 4 UNION SELECT 'bbb01', 4, 1, 0, 'Hersteller', 4 UNION SELECT 'bbb01', 5, 2, 0, 'Region', 4Any suggestions would be helpful. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 11:48:42
|
you've language = 1 only for [group]='aaa01'. what about other ones? you dont have to consider them at all?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
fuzzyGURU
Starting Member
3 Posts |
Posted - 2010-02-12 : 11:55:59
|
The data sample I provided is only a subset of what is really there. There are about 6 language entries for each fieldid, and there are varying amounts of fieldids per group. half the data is good, but due to a bug in the frontend code (and the update sql) , it corrupted the values for some language/fieldid entries. An analysis of the data revealed that language = 1 is the correct value to use.Of course, after I correct this, I then have to add in the missing group/fieldid rows for language entries that are missing (using language = 1 as the source row) |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 12:07:26
|
[code]CREATE TABLE #Fields ([id] [int] IDENTITY(1,1) NOT NULL,[group] [nvarchar](5) NOT NULL,[fieldid] [int] NOT NULL,[fieldtype] [int] NOT NULL,[required] [bit] NOT NULL,[label] [nvarchar](100) NOT NULL,[language] [int] NOT NULL)INSERT INTO #Fields ([group],[fieldid] ,[fieldtype] ,[required],[label] ,[language] )SELECT 'aaa01', 1, 1, 1, 'Customer', 1UNION SELECT 'aaa01', 2, 3, 0, 'Amount', 1UNION SELECT 'aaa01', 3, 4, 1, 'Rate', 1UNION SELECT 'aaa01', 4, 1, 0, 'Vendor', 1UNION SELECT 'aaa01', 5, 2, 0, 'Region', 1UNION SELECT 'aaa01', 1, 1, 0, 'Client', 2UNION SELECT 'aaa01', 2, 3, 0, 'Montant', 2UNION SELECT 'aaa01', 3, 4, 1, 'Taux', 2UNION SELECT 'aaa01', 4, 1, 0, 'Vendeur', 2UNION SELECT 'aaa01', 5, 2, 0, 'Région', 2UNION SELECT 'xyz01', 1, 1, 0, 'Cliente', 3UNION SELECT 'xyz01', 2, 3, 0, 'Cantidad', 3UNION SELECT 'xyz01', 3, 4, 1, 'Ritmo', 3UNION SELECT 'xyz01', 4, 1, 0, 'Distribuidor', 3UNION SELECT 'xyz01', 5, 2, 1, 'Región', 3UNION SELECT 'bbb01', 1, 1, 1, 'Kunde', 4UNION SELECT 'bbb01', 2, 3, 0, 'Betrag', 4UNION SELECT 'bbb01', 3, 4, 1, 'Rate', 4UNION SELECT 'bbb01', 4, 1, 0, 'Hersteller', 4UNION SELECT 'bbb01', 5, 2, 0, 'Region', 4select 'before update'select * from #Fields order by [group],[language]update fset f.[required] = t.[required]from #Fields fjoin #Fields ton t.[group]=f.[group]and t.fieldid=f.fieldidwhere t.language =1and f.language <> 1 and f.[required] <> t.[required]select 'after update'select * from #Fields order by [group],[language]drop table #Fieldsbefore updateid group fieldid fieldtype required label language----------- ----- ----------- ----------- -------- ---------------------------------------------------------------------------------------------------- -----------8 aaa01 4 1 0 Vendor 19 aaa01 5 2 0 Region 12 aaa01 1 1 1 Customer 13 aaa01 2 3 0 Amount 15 aaa01 3 4 1 Rate 16 aaa01 3 4 1 Taux 27 aaa01 4 1 0 Vendeur 24 aaa01 2 3 0 Montant 21 aaa01 1 1 0 Client 210 aaa01 5 2 0 Région 211 bbb01 1 1 1 Kunde 412 bbb01 2 3 0 Betrag 413 bbb01 3 4 1 Rate 414 bbb01 4 1 0 Hersteller 415 bbb01 5 2 0 Region 416 xyz01 1 1 0 Cliente 317 xyz01 2 3 0 Cantidad 318 xyz01 3 4 1 Ritmo 319 xyz01 4 1 0 Distribuidor 320 xyz01 5 2 1 Región 3------------after updateid group fieldid fieldtype required label language----------- ----- ----------- ----------- -------- ---------------------------------------------------------------------------------------------------- -----------8 aaa01 4 1 0 Vendor 19 aaa01 5 2 0 Region 12 aaa01 1 1 1 Customer 13 aaa01 2 3 0 Amount 15 aaa01 3 4 1 Rate 16 aaa01 3 4 1 Taux 27 aaa01 4 1 0 Vendeur 24 aaa01 2 3 0 Montant 21 aaa01 1 1 1 Client 210 aaa01 5 2 0 Région 211 bbb01 1 1 1 Kunde 412 bbb01 2 3 0 Betrag 413 bbb01 3 4 1 Rate 414 bbb01 4 1 0 Hersteller 415 bbb01 5 2 0 Region 416 xyz01 1 1 0 Cliente 317 xyz01 2 3 0 Cantidad 318 xyz01 3 4 1 Ritmo 319 xyz01 4 1 0 Distribuidor 320 xyz01 5 2 1 Región 3[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
fuzzyGURU
Starting Member
3 Posts |
Posted - 2010-02-12 : 12:27:16
|
yup.. total lack of caffeine.. visakh, it looks like that worked. Thank you very muchnow to add the missing rows... the joy of inherited code... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-02-12 : 12:29:06
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|