Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

tsql update table on select

Posted on 2012-12-29
4
Medium Priority
?
500 Views
Last Modified: 2012-12-30
i have a table statistics :

with fiELD day, CURRDATE REQPORT  TOTALS

I WANT TO UPDATE TOTALS EVERY ON HOURRS USING SQL AGENT

I HAVE A VIEW SELECT

I WANT TO UPDATE THE VALUE OF TOTALS FROM THIS VIEWS

UPDATE STATISTICS SET TOTALS = ( )
WHERE CURRDATE = XX AND REQPORT =

FROM ( SELECT CURRATE,REQPORT,TOTALS FROM MYVIEW )
0
Comment
Question by:afifosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Habib Pourfard
ID: 38728956
UPDATE  [STATISTICS]
SET     TOTALS = T.Totals
FROM    (SELECT CURRATE, REQPORT, TOTALS FROM MYVIEW) T
WHERE   CURRDATE = XX
        AND REQPORT = T.REQPORT

Open in new window

0
 
LVL 9

Expert Comment

by:sognoct
ID: 38729053
update t1 
set totals =t2.totals
from STATISTICS t1
INNER JOIN (SELECT CURRATE, REQPORT, TOTALS FROM MYVIEW)  t2 ON t1.CURRDATE = t2.currate AND t1.report = t2.reqport

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 38729275
Or simply:
UPDATE  s
SET     TOTALS = v.TOTALS
FROM    [STATISTICS] s
        INNER JOIN MYVIEW v ON s.REQPORT = v.REQPORT
WHERE   s.CURRDATE = XX 

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 38729278
If you also need to join to CURRDATE do it this way:
UPDATE  s
SET     TOTALS = v.TOTALS
FROM    [STATISTICS] s
        INNER JOIN MYVIEW v ON s.REQPORT = v.REQPORT AND s.CURRDATE = v.CURRDATE
WHERE   s.CURRDATE = XX 

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question