473,890 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Many fields update from a Group by Clause

Hi All,

In Oracle, I can easily make this query :

UPDATE t1 SET (f1,f2)=(SELECT AVG(f3),SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

I cannot seem to be able to do the same thing with MS-SQL. There are
only 2 ways I've figured out, and I fear performance cost in both cases,
which are these :
1)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

and then the same statement but with f2, and

2)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6),
f2=(SELECT SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

Is there a way with MS-SQL to do the Oracle equivalent in this case ?

Thanks,

Michel
Jul 20 '05 #1
3 14888
Hi

You could try something like

UPDATE t
SET f1 = dt.avgcol, f2 = dt.sumcol
FROM t1 JOIN ( SELECT f5, AVG(f3) AS avgcol, SUM(f4) AS SumCol
FROM t2
WHERE f5 = 'Something'
GROUP BY f5 ) dt ON dt.f5=t1.f6

John
"Michel" <Mi****@askme.c om> wrote in message
news:mG******** *********@news2 0.bellglobal.co m...
Hi All,

In Oracle, I can easily make this query :

UPDATE t1 SET (f1,f2)=(SELECT AVG(f3),SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

I cannot seem to be able to do the same thing with MS-SQL. There are
only 2 ways I've figured out, and I fear performance cost in both cases,
which are these :
1)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

and then the same statement but with f2, and

2)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6),
f2=(SELECT SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

Is there a way with MS-SQL to do the Oracle equivalent in this case ?

Thanks,

Michel

Jul 20 '05 #2
It works fine, thank you very much. I did try something similar just before,
but I made the links in the where clause instead of the joins, and for some
reason it wasn't working.

Well, thanks, now it works fine and I'm saving 50% time on the queries!

Michel

"John Bell" <jb************ @hotmail.com> wrote in message
news:3f******** **************@ reading.news.pi pex.net...
Hi

You could try something like

UPDATE t
SET f1 = dt.avgcol, f2 = dt.sumcol
FROM t1 JOIN ( SELECT f5, AVG(f3) AS avgcol, SUM(f4) AS SumCol
FROM t2
WHERE f5 = 'Something'
GROUP BY f5 ) dt ON dt.f5=t1.f6
John
"Michel" <Mi****@askme.c om> wrote in message
news:mG******** *********@news2 0.bellglobal.co m...
Hi All,

In Oracle, I can easily make this query :

UPDATE t1 SET (f1,f2)=(SELECT AVG(f3),SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

I cannot seem to be able to do the same thing with MS-SQL. There are
only 2 ways I've figured out, and I fear performance cost in both cases,
which are these :
1)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

and then the same statement but with f2, and

2)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6),
f2=(SELECT SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

Is there a way with MS-SQL to do the Oracle equivalent in this case ?

Thanks,

Michel


Jul 20 '05 #3
Is that 50% over Oracle :)

"Michel" <Mi****@askme.c om> wrote in message
news:0B******** *********@news2 0.bellglobal.co m...
It works fine, thank you very much. I did try something similar just before, but I made the links in the where clause instead of the joins, and for some reason it wasn't working.

Well, thanks, now it works fine and I'm saving 50% time on the queries!

Michel

"John Bell" <jb************ @hotmail.com> wrote in message
news:3f******** **************@ reading.news.pi pex.net...
Hi

You could try something like

UPDATE t
SET f1 = dt.avgcol, f2 = dt.sumcol
FROM t1 JOIN ( SELECT f5, AVG(f3) AS avgcol, SUM(f4) AS SumCol
FROM t2
WHERE f5 = 'Something'
GROUP BY f5 ) dt ON

dt.f5=t1.f6

John
"Michel" <Mi****@askme.c om> wrote in message
news:mG******** *********@news2 0.bellglobal.co m...
Hi All,

In Oracle, I can easily make this query :

UPDATE t1 SET (f1,f2)=(SELECT AVG(f3),SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

I cannot seem to be able to do the same thing with MS-SQL. There are only 2 ways I've figured out, and I fear performance cost in both cases, which are these :
1)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

and then the same statement but with f2, and

2)
UPDATE t1 SET f1=(SELECT AVG(f3)
FROM t2
WHERE t2.f5=t1.f6),
f2=(SELECT SUM(f4)
FROM t2
WHERE t2.f5=t1.f6)
WHERE f5='Something'

Is there a way with MS-SQL to do the Oracle equivalent in this case ?

Thanks,

Michel



Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
248509
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a particular surname in a particular town. I can select the records fine with this syntax (testing in Oracle SQL* Plus) SELECT NAMEINFO.LASTNAME, NAMEINFO.FIRSTNAME, NAMEINFO.MIDDLENAME, NAMEINFO.GENDER, ADDRESSINFO.REGION FROM NAMEINFO, ADDRESSINFO...
8
89328
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON tblMyTable.UniqueID = tblMyTableTEMP.UniqueID SET tblMyTable.myField = tblMyTableTEMP.myField, tblMyTable.myField2 = tblMyTableTEMP.myField2,
3
3847
by: kattukuyil | last post by:
Guys, this is what I want to do. How can I do this ? Select employid, Gross=( Select sum (uprtrxam) from Fleet..upr30300 where pyrlrtyp=1), DedofWages=( Select sum (uprtrxam) from Fleet..upr30300 where pyrltyp=2), Gross-DedofWages from FLEET..UPR00100 The problem is the third field (Gross-DedofWages). It says Invalid
17
5040
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by cust_no, ded_type_cd, chk_no)
2
2527
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an Update statement. I have a sample code to reproduce my problem. To simplify the scenario I am trying to use Order related tables to explain a little better the tables i have to work with.
7
2037
by: Dan | last post by:
(Using Classic ASP & MS Access) I have a page that has 120 fields on it (mostly checkboxes). I cannot split this into smaller pages. So what i want to do is write a class that handles this. in the code it would be something like: Set thisForm = new classBigForm
9
1753
by: Ryan | last post by:
I'm learning when it comes to Datasets. Should I just have a single dataset to handle my entire application data or should I use multiple - a dataset for each function or form within my application? Is the data actually written from and queried to the dataset only when needed per a table adapter? It looks like the table adapters .fill method is only invoked when required, so I assume a single dataset would suffice. Is this right? ...
9
4401
by: P3Eddie | last post by:
Hello all! I don't know if this can even be done, but I'm sure you will either help or suggest another avenue to accomplish the same. My problem may be a simple find duplicates / do something with only one of the duplicates issue, but I got to the point where an update query with a nested select subquery would work wonderfully, if only it would work! I have several fields in a master Access 2000 table, some of which are id, fname,...
3
2010
by: Icemokka | last post by:
Hi, I've got a table in MsAccess with 100+ fields. If I fill a tabletable with this table , change some values , get the update-command via commandbuilder , the update fails. This because the commandbuilder adds a where clause that contains all the fields in the datatable ( to check whether the record has been changed since the fill ). Because MSAccess does not allow more then 99 fields in the where clause, the update fails offcourse.
0
9826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10830
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6061
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3283
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.