Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

sql - Update the column according to order status

INPUT:

CUSTOMER_NAME       ORDER_ID      STATUS
-----------------------------------------------
  john                j1          delivered
  john                j2          delivered
  david               d1          submitted
  david               d3          created
  smith               s1          submitted
  krish               k1          created

A pizza company is taking orders from customers and each pizza order is added to their database as a separate order each order has an associated status "CREATED or SUBMITTED or DELIVERED". final status is calculated bases on status as follows

When all orders for a customer have a status of DELIVERED that customers order has a final status of "COMPLETED"

If a customer has some orders that are not DELIVERED and some orders that are DELIVERED that final status is "IN PROGRESS"

If all of a customers orders are SUBMITTED the final status is "AWAITING PROGRESS"

Otherwise the final status is "AWAITING SUBMISSION"

Desired output:

CUSTOMER_NAME                 FINAL_STATUS
--------------------------------------------------
david                         IN PROGRESS
john                          COMPLETED
krish                         AWAITING SUBMISSION
smith                         AWAITING PROGRESS
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

here is one way:

select customer_name 
   , case when count(case when status <> 'created' then 1 end) = 0 then 'awaiting submission'  
          when count(case when status <> 'submitted' then 1 end) = 0 then 'awaiting progress'
          when count(case when status <> 'delivered' then 1 end) > 0 then 'In progress'
          when count(case when status <> 'delivered' then 1 end) = 0 then 'Completed'          
     end
from orders
group by customer_name

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...