Under SQL SERVER: 1, recursively query the sub-categories under the parent classification. 2. Check the top two items in each product category SQL

Source: Internet
Author: User

1. Recursively query the sub-categories under the parent classification. Table Design:

Sql:

--CTE Statement (for later versions of MSSQL2005) withCte_testnavi (id,name,pid) as(--This is the query statementSELECTId,name,pid fromNaviWHEREName='Automotive'Union  All--This is the part where recursion is required, and the CTE itself calls the completion loop recursive lookupSELECTA.id,a.name,a.pid fromNavi aINNER JOINCte_testnavi b on(A.pid=b.id))Select *  fromCte_testnavi

2. Check the top two items in each product category SQL

Table Design:

Sql:

--ask for the top two most expensive items under each item by category--over (partition by C.classid order by I.price DESC) the over window function avoids the case where group by is not included in the child column.--but the window function will return multiple rows of results, according to the use of the decision, such as here I want to follow the CLASSID product category ID to group--the rank function is defined by Microsoft as: Returns the rank of each row within the result set partition. The rank of a row is a number of rows before the line involved. --like here I sort by price, take out the most expensive items in each category from top to bottomSELECT *  from(SELECTI.price,i.commodityname,c.classname,rank () Over(Partition byC.classidOrder  byI.priceDESC) Rank2 fromCommodityinfo iINNER JOINCommodityclass C onI.commodityclass=C.classid) RWHERERank2<=2;

Under SQL SERVER: 1, recursively query the sub-categories under the parent classification. 2. Check the top two items in each product category SQL

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.