Used for e-commerce websites to query data order recommendations for the last 30 days

Source: Internet
Author: User

In some e-commerce websites, it is sometimes necessary to recommend order data generated in the last few days to calculate commodity recommendation information. At the data layer, We need to write some query statements or stored procedures. You can find out which order appears most frequently, sort it in descending order, and select the first 5 as the recommended items.

 

 

select productid,
name, description

from product
where productid in
(
select top 5 od2.productid
from orderdetails od1
join orderdetails od2 on
od1.orderid = od2.orderid
where od1.productid = @ productid and od2.productid! = @ Productid
and datediff (DD, orders. datecreated, getdate () <30

group by od2.productid
order by count (od2.productid) desc

)

The table connection operation is used here, and the two instances in the orders table are used for connection. od2 is selected. What other products can be ordered by productid? Here, productid! = @ Productid: Delete the same product number as od2.productid. Then, sort the number of productid occurrences in descending order and select the top 5 digits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.