SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Iron Speed
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old April 7th, 2008, 12:42 PM
boba5555 boba5555 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 13 boba5555 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 29 sec
Reputation Power: 0
Use value from select in same query

Hello,

I want to do something like this
UPDATE mytable m SET m.value = (SELECT SUM(l.val) FROM list l WHERE m.id = l.id) * 3;

I've tried this, but it doesn't work. I thought about using GROUP BY, but I am not so sure how that will help. Can someone help me?

Thank you.

Reply With Quote
  #2  
Old April 7th, 2008, 02:26 PM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,483 mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 3 Weeks 3 Days 21 h 7 m 11 sec
Reputation Power: 1450
try this:
Code:
update mytable 
set value = strUpdate.sum_Value
from mytable m inner join
	(select id, sum(val) as sum_Value
	from list group by id)
as strUpdate
on m.id = strUpdate.id
__________________
Come JOIN the party!!!

Quote of the Month:
Trouble: Luck can't last a lifetime unless you die young.

Questions to Ponder:
Do cemetery workers prefer the graveyard shift?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright© 2008 sbenj69

Reply With Quote
  #3  
Old April 7th, 2008, 04:29 PM
boba5555 boba5555 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 13 boba5555 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 29 sec
Reputation Power: 0
Quote:
Originally Posted by mehere
try this:
Code:
update mytable 
set value = strUpdate.sum_Value
from mytable m inner join
	(select id, sum(val) as sum_Value
	from list group by id)
as strUpdate
on m.id = strUpdate.id


I think I still need help :|
This is my complete query (of course, with your suggestion)
Code:
UPDATE links SET ratio = (incl.cSUM + 1.0) / (outcl.cSUM + 1.0) 
FROM links l INNER JOIN (SELECT link, SUM(cnt) AS cSUM FROM 
inclicks GROUP BY link) AS incl on l.id = incl.link INNER JOIN 
(SELECT link, SUM(cnt) AS cSUM FROM outclicks GROUP BY link) 
AS outcl on l.id = outcl.link;


And when I try to start that I am getting message
"MySQL][ODBC 3.51 Driver][mysqld-5.0.45-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM links l INNER JOIN (SELECT link, SUM(cnt) AS cSUM FROM inclicks GROUP BY li' at line 1"

I was looking on net for tutorial about inner join and I have found (and understand) I will get temporary table that contains 'links', 'inclicks' and 'outlicks' (in my case these tables) and one row that correspond to l.id. But how ratio knows which row to take? So, with join I should have
l.id | incl.cSUM | outcl.cSum
-----------------------------
1 | 10 | 20
-----------------------------
2 | 3 | 15
-----------------------------
5 | 8 | 33
-----------------------------

But how ratio for id = 5 will know that it is equal to 9 / 34. I believe that should work, but I don't understand why. I will understand if there is something like this

UPDATE links l1 SET [other part of query] WHERE l.id = l1.id;

but there is not.

Thank you.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Use value from select in same query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway