SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Update a column matching multiple records


Update a column matching multiple records

Author
Message
rp2018
rp2018
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 16
I have a customer and order table. There is one to many relation between customer and order table. I have a custid = 101 and there are 5 orders in order table for it.
I need to match each individual orders for this custid and update RecentOrders column for OrderNbr = 1.
I need to do this in a procedure. There can be any # of orders for a customer.
Custid = 101
OrderNbr = 1,2,3,4,5

If custid=101 and (OrderNbr =1 and order_date > 10-20-2017) or (OrderNbr = 2 and order_date > 11-15-2017) or (OrderNbr=4 and Order_date > 12-05-2017)
Then update OrderNbr =1 record in Orders table and set column called RecentOrders = ‘3 Orders’ Else 'None'
george_at_sql
george_at_sql
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1272 Visits: 1745
Can you share a tiny yet complete DDL statements DML statements for Order and Customer table and share how the output should look like?
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)

Group: General Forum Members
Points: 210855 Visits: 24507
rp2018 - Thursday, March 1, 2018 7:23 PM
I have a customer and order table. There is one to many relation between customer and order table. I have a custid = 101 and there are 5 orders in order table for it.
I need to match each individual orders for this custid and update RecentOrders column for OrderNbr = 1.
I need to do this in a procedure. There can be any # of orders for a customer.
Custid = 101
OrderNbr = 1,2,3,4,5

If custid=101 and (OrderNbr =1 and order_date > 10-20-2017) or (OrderNbr = 2 and order_date > 11-15-2017) or (OrderNbr=4 and Order_date > 12-05-2017)
Then update OrderNbr =1 record in Orders table and set column called RecentOrders = ‘3 Orders’ Else 'None'

The reason you've had no solutions is that you have not stated the problem in a way which is clear enough for people to understand and write code.
As the previous poster suggested, we need some consumable code for setting up example tables, with insert statements to create some sample data along with desired results. The link in my signature should help you understand what is required.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
rp2018
rp2018
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 16
Create table customer
(custid int constraint ct_id primary key,
cust_name nvarchar(100),
cust_addr1 nvarchar(100),
cust_addr2 nvarchar(100),
city,
cust_zip number(5),
cust_plus4 number(4))

Create table Orders
(OrderNbr int constraint ord_id primary key,
custid int,
Order_date datetime,
OrderQty int,
RecentOrders Nvarchar(10),
Constraint fk_custid foreign key (custid) references customer (custid))

Insert into customer(cust_name,cust_addr1,cust_addr2,cust_zip,cust_plus4)
Values(‘Mike Smith’,111 Smith Rd,,61111,1111),
(‘Nicole Smith’,143 Nick Lane,,61112,1101),
(‘Jose Gomez’,155 New York St, 55555,2323)

Insert into Orders(custid,order_date,orderQty,RecentOrders)
Values(101,’11-22-2017’,20,1),
(101,’11-23-2017’,11,2),
(101,’11-24-2017’,5,’3),
(101,’12-02-2017’,25,4),
(101,’01-09-2018’,2,5),
(102,’09-02-2017’,55,1),
(102,’11-05-2017,6,2),
(103,’01-20-2016’,1,1),
(103,’11-10-2017’,13,2),
(103,’01-12-2018’,25,3)

Output should look like this.

OrderNbr,custid,order_date,orderQty,RecentOrders
1,101,11-22-2017,20,1,3
2,101,11-23-2017,11,2,null
3,101,11-24-2017,5,’3,null
4,101,12-02-2017,25,4,null
5,101,01-09-2018,2,5,null
1,102,09-02-2017,55,1,none
2,102,11-05-2017,6,2,null
1,103,01-20-2016,1,1,3
2,103,11-10-2017,13,2,null
3,103,01-12-2018,25,3,null
george_at_sql
george_at_sql
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1272 Visits: 1745
Here is a "working" version of the script you have shared.

Create table customer
(custid int constraint ct_id primary key,
cust_name nvarchar(100),
cust_addr1 nvarchar(100),
cust_addr2 nvarchar(100),
city nvarchar(100),
cust_zip numeric(5),
cust_plus4 numeric(4)
);

Create table Orders
(OrderNbr int identity constraint ord_id primary key,
custid int,
Order_date datetime,
OrderQty int,
RecentOrders Nvarchar(10),
Constraint fk_custid foreign key (custid) references customer (custid)
);

Insert into customer(custid,cust_name,cust_addr1,cust_addr2,cust_zip,cust_plus4)
Values
('101','Mike Smith','111 Smith Rd',61111,1111,1111),
('102','Nicole Smith','143 Nick Lane',61112,1101,1111),
('103','Jose Gomez','155 New York St', 55555,2323,1111)


Insert into Orders(custid,order_date,orderQty,RecentOrders)
Values
(101,cast('11-22-2017' as date),20,1),
(101,cast('11-23-2017' as date),11,2),
(101,cast('11-24-2017' as date),5,3),
(101,cast('12-02-2017' as date),25,4),
(101,cast('01-09-2018' as date),2,5),
(102,cast('09-02-2017' as date),55,1),
(102,cast('11-05-2017' as date),6,2),
(103,cast('01-20-2016' as date),1,1),
(103,cast('11-10-2017' as date),13,2),
(103,cast('01-12-2018' as date),25,3)

Still not able to identify the logic to be used

You said..
If custid=101 and (OrderNbr =1 and order_date > 10-20-2017) or (OrderNbr = 2 and order_date > 11-15-2017) or (OrderNbr=4 and Order_date > 12-05-2017)
Then update OrderNbr =1 record in Orders table and set column called RecentOrders = ‘3 Orders’ Else 'None'

For 101 custid...

1,101,11-22-2017,20,1,3
2,101,11-23-2017,11,2,null
3,101,11-24-2017,5,’3,null
4,101,12-02-2017,25,4,null
5,101,01-09-2018,2,5,null

(OrderNbr =1 and order_date > 10-20-2017) or (OrderNbr = 2 and order_date > 11-15-2017) or (OrderNbr=4 and Order_date > 12-05-2017)
What is the rule to be used for 102 and 103 custid. Better still explain what is the general rule to be used for updating the column
HappyGeek
HappyGeek
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14439 Visits: 7016
Homework?

...
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