locked
sql question RRS feed

  • Question

  • User-125499312 posted

    i have a query that returns categories and subcategories in sorted order.

    i would like to display the output in columns - the category at the top and then its sub-categories under it.

    how would i do this?

    thx for ur help

    Thursday, November 9, 2017 2:13 PM

All replies

  • User1120430333 posted

    i have a query that returns categories and subcategories in sorted order.

    i would like to display the output in columns - the category at the top and then its sub-categories under it.

    how would i do this?

    thx for ur help

    You requirements are limited in details.

    1) What query how did you do it?

    2) Display how using what technology?

    Thursday, November 9, 2017 11:21 PM
  • User465171450 posted

    There's nothing SQL can do for this in a single query. This would be a display issue and I believe Grouping would be the feature you're looking for. Here is an example of a jQuery Grid that has grouping capabilities. 

    https://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxgrid/jquery-grid-grouping.htm

    The WebForms GridView may have similar though I haven't used the GridVIew in over a decade so I can't be sure what it currently supports.

    Friday, November 10, 2017 1:39 AM
  • User-832373396 posted

    Hi yzidell,

    I have a query that returns categories and subcategories in sorted order.

    I would like to display the output in columns - the category at the top and then its sub-categories under it.

    It seems that you are looking for group by in SQL https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql 

    Or maybe you are looking for CTE(SQL)

    Specifies a temporary named result set, known as a common table expression (CTE). This is derived from a simple query and defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement. This clause can also be used in a CREATE VIEW statement as part of its defining SELECT statement. A common table expression can include references to itself. This is referred to as a recursive common table expression.

    Usage:

    WITH cte_name ( column_name [,...n] )
    AS
    (
    CTE_query_definition –- Anchor member is defined.
    UNION ALL
    CTE_query_definition –- Recursive member is defined referencing cte_name.
    )
    -- Statement using the CTE
    SELECT *
    FROM cte_name

    Something result looks like:

    Quickly to view https://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx  

    More examples, please refer to the official document https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql 

    With regards, Angelina Jolie 

    Friday, November 10, 2017 6:53 AM