Problems with a order of the rows

  • People,

    I am new here in the forum.

    I have a table that contains a list of equipment.

    The equipments was inserted in an order, but I would like to show the equipment in a order that form an kit, that is composed for 3 equipment.

    Follow the queries:

    IF OBJECT_ID('TempDB..#item','U') IS NOT NULL

    DROP TABLE #item

    --create table

    CREATE TABLE #item (

    num INT,

    name VARCHAR(30)

    );

    --insert the test data

    INSERT INTO #item (num, name)

    SELECT 1, 'Modem' UNION ALL

    SELECT 1, 'Modem' UNION ALL

    SELECT 1, 'Modem' UNION ALL

    SELECT 2, 'ODU' UNION ALL

    SELECT 2, 'ODU' UNION ALL

    SELECT 2, 'ODU' UNION ALL

    SELECT 3, 'Antenna' UNION ALL

    SELECT 3, 'Antenna' UNION ALL

    SELECT 3, 'Antenna'

    I want that the rows be displayed in this order, where each 3 rows are a kit.

    NUMNAME

    -------------------

    1MODEM

    2ODU

    3ANTENNA

    1MODEM

    2ODU

    3ANTENNA

    1MODEM

    2ODU

    3ANTENNA

  • Excellent Setup for clarity, Rodrigo. A question. I assume this is a simplified version of the real data. Is there a column that indicates which kit each piece belongs to?


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Hi Rodrigo,

    Craig has a point and we might need more information. The following query works with the sample data but might not be exactly what you need.

    SELECT *

    FROM #item

    ORDER BY ROW_NUMBER() OVER(PARTITION BY num ORDER BY (SELECT NULL)), num

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Hi, Evil Kraig F.

    There is not this colums.

    It is because this that I want to show this way for the user.

    Because they will have to mark what site is each kit. At the end, it will like this way:

    NUMNAMETO_SITE

    ---------------------------------

    1MODEMA

    2ODUA

    3ANTENNAA

    1MODEMB

    2ODUB

    3ANTENNAB

    1MODEMC

    2ODUC

    3ANTENNAC

  • rodrigo_fabiam (8/7/2014)


    Hi, Evil Kraig F.

    There is not this colums.

    It is because this that I want to show this way for the user.

    Because they will have to mark what site is each kit. At the end, it will like this way:

    How do you know what goes into each kit? The solution above by Luis will give you what you need with what's presented, but it comes with a concern: Your association is completely random. It's going to sort out in whatever order the database coughs it up in, and won't deal with, for example, two kit sets repeated 3 times each. At least not well.

    Your example is oversimplified for a production system, because it just can't work that way long term because there's no associations outside of a single table's row order. If you've got other controls already making sure that you're restricting it to a single kit type for a single invoice (I'm making a crapload of assumptions here), then Luis' solution will work. Otherwise, you'll need to get us a more holistic view of your schema to help you not shoot yourself in the foot.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Evil Kraig F (8/7/2014)


    rodrigo_fabiam (8/7/2014)


    Hi, Evil Kraig F.

    There is not this colums.

    It is because this that I want to show this way for the user.

    Because they will have to mark what site is each kit. At the end, it will like this way:

    How do you know what goes into each kit? The solution above by Luis will give you what you need with what's presented, but it comes with a concern: Your association is completely random. It's going to sort out in whatever order the database coughs it up in, and won't deal with, for example, two kit sets repeated 3 times each. At least not well.

    Your example is oversimplified for a production system, because it just can't work that way long term because there's no associations outside of a single table's row order. If you've got other controls already making sure that you're restricting it to a single kit type for a single invoice (I'm making a crapload of assumptions here), then Luis' solution will work. Otherwise, you'll need to get us a more holistic view of your schema to help you not shoot yourself in the foot.

    Very valid points by Kraig.

    If that is truly representative of a production system, then somebody has a nightmare worth of assumptions to make about the data.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Ok guys, thanks a lot.

    I will think a bit little more about the design of the data.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply