An aggregate may not appear in the set list of an UPDATE statement Error with randomized set value

StackOverflow https://stackoverflow.com/questions/22561522

Domanda

I am trying to execute this SQL script via Management Studio, but I get this error: An aggregate may not appear in the set list of an UPDATE statement. I've tried some other statements but I couldn't find any solution for my randomized set value. Many thanks for helps!

UPDATE G SET G.SAAT = DATEADD(d, ROUND(DATEDIFF(d, DATEADD(hh,2, MIN(G2.SAAT)),
DATEADD(dd,6, T.CIKIS_TARIHI)) * RAND(CHECKSUM(NEWID())), 0),DATEADD(second,CHECKSUM(NEWID())%48000, G2.SAAT))
FROM GONDERI_HAREKET G
INNER JOIN GONDERI_HAREKET G2 ON G2.TAKIP_NO = G.TAKIP_NO
INNER JOIN TESLIM_FORMU T ON G.TAKIP_NO = T.TAKIP_NO
WHERE G.ISLEM_KODU = 15
AND G2.ISLEM_KODU = 10
AND G.SAAT < G2.SAAT
AND YEAR(T.CIKIS_TARIHI) = 2014
AND (MONTH(T.CIKIS_TARIHI) = 1 OR MONTH(T.CIKIS_TARIHI) = 2)
AND T.MUSTERI_KODU = 2483
È stato utile?

Soluzione

Try this... You could use a CTE to get the MIN val

WITH MIN_G2 as
(SELECT MIN(SAAT) SAAT
, TAKIP_NO
FROM GONDERI_HAREKET
GROUP BY TAKIP_NO
)
UPDATE G SET G.SAAT = DATEADD(d, ROUND(DATEDIFF(d, DATEADD(hh,2, MIN_G2.SAAT),
DATEADD(dd,6, T.CIKIS_TARIHI)) * RAND(CHECKSUM(NEWID())), 0),DATEADD(second,CHECKSUM(NEWID())%48000, G2.SAAT))
FROM GONDERI_HAREKET G
INNER JOIN GONDERI_HAREKET G2 ON G2.TAKIP_NO = G.TAKIP_NO
INNER JOIN MIN_G2 on G2.TAKIP_NO = MIN_G2.TAKIP_NO
INNER JOIN TESLIM_FORMU T ON G.TAKIP_NO = T.TAKIP_NO
WHERE G.ISLEM_KODU = 15
AND G2.ISLEM_KODU = 10
AND G.SAAT < G2.SAAT
AND YEAR(T.CIKIS_TARIHI) = 2014
AND (MONTH(T.CIKIS_TARIHI) = 1 OR MONTH(T.CIKIS_TARIHI) = 2)
AND T.MUSTERI_KODU = 2483
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top