Avatar billede Lasse Novice
05. januar 2005 - 22:42 Der er 20 kommentarer og
1 løsning

Sql query til sql eksperten

Jeg har en tabel(t1) hvori der en kolonne(Billede) der skal opdateres.

t1
--

  Klient, Kode, Billede
1) 1    , 1  , NULL
2) 2    , 1  , NULL
3) 3    , 1  , 'c:\test2.png'
4) 1    , 1  , 'c:\test.png'
5) 1    , 2  , NULL

Alle de raekker hvor Billede = NULL skal opdateres. Dataen der skal indsaettes i Billede skal tages fra en anden raekke der har samme kode - og samme klient hvis samme klient eksisterer. Ser man i eksemplet oppe over, saa er raekke 1) og 2) eksempler paa raekker der skal opdateres. Raekke 1) SKAL opdateres fra raekke 4), hvorimod raekke 2) enten skal opdateres fra raekke 3) eller 4) - det er ligegyldigt.

Jeg har proevet at lave en UPDATE FROM.... problemet er at den ikke tolorerer en ORDER BY clause... Skal jeg goere dette via 2 queries(een hvor der er klient specifik og een hvor det ikke er klient specifik), eller er der en smartere maade at goere dette paa?
Avatar billede Syska Mester
06. januar 2005 - 08:11 #1
lytter lige med
Avatar billede jtbroad Nybegynder
06. januar 2005 - 11:15 #2
2 queries lyder fornuftig i den første opdaterer fra der hvor både klient og kode er lige med i den anden bare distinct hvor kode er lige med.
eller er jeg bare forvirret?
Avatar billede Lasse Novice
06. januar 2005 - 19:13 #3
Desvaerre er eksemplet dette kun en simplificeret version af den virkelige verden hvilken kraever en del JOINS osv => det tager tid. Hvis jeg kunne noejes med 1 query, saa ville det vaere det bedste.

Er 2 queries virkelig den eneste loesning?
Avatar billede terry Ekspert
06. januar 2005 - 19:58 #4
This is NOT tested
UPDATE T1 SET T1.Billede = (SELECT TOP 1 Billede FROM T1 AS T2 WHERE T1.Klient = T2.Klient and t1.Kode = T2.Kode and (NOT T2.Billede Is Null))
WHERE T1.Billede) Is Null
Avatar billede terry Ekspert
08. januar 2005 - 12:34 #5
can you use this gooky?
Avatar billede Lasse Novice
10. januar 2005 - 17:25 #6
Sorry Terry, I will take a look at it later on today.
Avatar billede terry Ekspert
10. januar 2005 - 18:41 #7
OK, no rush :o)
Avatar billede terry Ekspert
15. januar 2005 - 09:50 #8
how it going gooky, cant you get it working?
Avatar billede Lasse Novice
17. januar 2005 - 19:43 #9
Ok, now I got some time to look at it.

Terry>> It wont work as expected, since you are not taking the ORDER BY into account. The way you handle it is to always take a specific client, which isn't my case.
Avatar billede terry Ekspert
17. januar 2005 - 20:00 #10
OK, so what you want is the TOP 1 where kode is the same and ORDER BY what?
Avatar billede terry Ekspert
17. januar 2005 - 20:11 #11
If you have an ID (primary key) then you could try

UPDATE T1 SET T1.Billede = (SELECT TOP 1 Billede FROM T1 AS T2 WHERE  t1.Kode = T2.Kode and (NOT T2.Billede Is Null) ORDER BY T2.ID)
WHERE (T1.Billede) Is Null

otherwise you need to change ORDER BY T2.ID to a field in the table which you can sort on
Avatar billede Lasse Novice
18. januar 2005 - 17:27 #12
This wont do it as well...

I need to do something like this (have not tested if it is valid sql)

UPDATE T1 SET T1.Billede = (SELECT TOP 1 Billede FROM T1 AS T2 WHERE  t1.Kode = T2.Kode and (NOT T2.Billede Is Null) ORDER BY CASE T2.Klient WHEN T1.Klient THEN 1 ELSE 0 END)
WHERE (T1.Billede) Is Null

Is this possible?
Avatar billede terry Ekspert
18. januar 2005 - 18:55 #13
If you can tell me what you are trying to do (with an example) then it might be easier to make a solution.
Avatar billede Lasse Novice
18. januar 2005 - 21:11 #14
table1:
Client, code, picturepath
1, 1, null
2, 1, 'foobar1'
3, 1, 'foobar2'
1, 1, 'foobar3'

I made the table simple, so it is easier to understand.

I need to update rows where picturepath is null. The value should be taken from another row in the same table, WHERE the code is the same and the picturepath is not null.... AND PREFEREBLY from the same client (if not, then whatever client).
Avatar billede Lasse Novice
18. januar 2005 - 21:13 #15
oops...
in this case, row 1) containing:
1, 1, null

should be updated from

1, 1, 'foobar3'

and not

2, 1, 'foobar1'
3, 1, 'foobar2'

since the client matches exactly
Avatar billede Lasse Novice
26. januar 2005 - 23:56 #16
The one I sugested works. Thanks for helping out...

UPDATE T1 SET T1.Billede = (SELECT TOP 1 Billede FROM T1 AS T2 WHERE  t1.Kode = T2.Kode and (NOT T2.Billede Is Null) ORDER BY CASE T2.Klient WHEN T1.Klient THEN 1 ELSE 0 END)
WHERE (T1.Billede) Is Null
Avatar billede Lasse Novice
27. januar 2005 - 00:00 #17
Terre, the real situation is a little more complex. Actually I need to set 2 column values, instead of 1. How can I fix that in a neat way?
Avatar billede terry Ekspert
27. januar 2005 - 12:13 #18
Hi gooky, I'll take a look as soon as possible, I'm rather busy right now!
Avatar billede terry Ekspert
27. januar 2005 - 20:26 #19
You should be able to update one or more columns in the same record jsut by seperating the fields with a ,

UPDATE T1 SET T1.Billede = (SELECT TOP 1 Billede FROM T1 AS T2 WHERE  t1.Kode = T2.Kode and (NOT T2.Billede Is Null) ORDER BY CASE T2.Klient WHEN T1.Klient THEN 1 ELSE 0 END), T1.SomeOtherField = (....)
WHERE (T1.Billede) Is Null
Avatar billede Lasse Novice
27. januar 2005 - 23:03 #20
yes... the problem is that the 2 columns being updated needs to be updated with data from the same row found in:

(SELECT TOP 1 Billede FROM T1 AS T2 WHERE  t1.Kode = T2.Kode and (NOT T2.Billede Is Null)

I guess it is deep overhead... to make the same query appear twice in the update statement, but maybe that is the only solution :-) It is not a big deal though.
Avatar billede terry Ekspert
28. januar 2005 - 13:11 #21
thats what I would do
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Computerworld tilbyder specialiserede kurser i database-management

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester