March 1, 2018 at 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'
March 2, 2018 at 3:51 am
Can you share a tiny yet complete DDL statements DML statements for Order and Customer table and share how the output should look like?
March 2, 2018 at 5:45 am
rp2018 - Thursday, March 1, 2018 7:23 PMI 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,5If 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.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
March 10, 2018 at 11:29 am
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
March 11, 2018 at 6:54 am
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
March 11, 2018 at 12:01 pm
Homework?
...
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply