Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update table using Group By


Update table using Group By

Author
Message
DerbyNeal
DerbyNeal
SSC-Enthusiastic
SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)SSC-Enthusiastic (156 reputation)

Group: General Forum Members
Points: 156 Visits: 909
Hi,

I have a column (Total) which needs to be updated based on a calculation of 2 other columns(Assigned + Unassigned) and is Grouped By a 4th column (Delivery Date).

Before

DeliveryDate   Assigned   Unaasigned
2013-04-29 1 0
2013-04-29 1 1
2013-04-29 1 1
2013-04-29 1 1
2013-04-30 0 0
2013-04-30 0 1
2013-05-01 0 0





After

DeliveryDate   Assigned   Unaasigned   Total
2013-04-29 1 0 7
2013-04-29 1 1 7
2013-04-29 1 1 7
2013-04-29 1 1 7
2013-04-30 0 0 1
2013-04-30 0 1 1
2013-05-01 0 0 0




The combined Assigned and Unassigned values for 2013-04-29 is 7 and is to be displayed in each of the 4 rows.

Can someone show me how to do this using T-Sql please?

Thanks in advance,
Mark Cowne
Mark Cowne
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2091 Visits: 22784
Something like this??

DECLARE @t TABLE(DeliveryDate DATE,Assigned INT,Unassigned INT, Total INT)
INSERT INTO @t(DeliveryDate,Assigned,Unassigned)
VALUES
('2013-04-29',1,0),
('2013-04-29',1,1),
('2013-04-29',1,1),
('2013-04-29',1,1),
('2013-04-30',0,0),
('2013-04-30',0,1),
('2013-05-01',0,0);

SELECT DeliveryDate,Assigned,Unassigned,
SUM(Assigned + Unassigned) OVER(PARTITION BY DeliveryDate) Total
FROM @t
ORDER BY DeliveryDate



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




geoff5
geoff5
SSC-Enthusiastic
SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)SSC-Enthusiastic (180 reputation)

Group: General Forum Members
Points: 180 Visits: 543
It seems from the wording of your original post that you may want this to be a computed column in a permanent table. Unfortunately, window functions cannot be part of a computed column definition. So you will have to use a view solution (such as the one Mark suggested above) or use a trigger to update the value any time the Assigned, Unassigned, or DeliveryDate columns change.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search