Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Problems with a order of the rows Expand / Collapse
Author
Message
Posted Thursday, August 7, 2014 4:05 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, November 10, 2014 8:19 AM
Points: 4, Visits: 8
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.
NUM		NAME
-------------------
1 MODEM
2 ODU
3 ANTENNA
1 MODEM
2 ODU
3 ANTENNA
1 MODEM
2 ODU
3 ANTENNA

Post #1600926
Posted Thursday, August 7, 2014 4:14 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, November 21, 2014 12:18 PM
Points: 5,438, Visits: 7,606
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1600930
Posted Thursday, August 7, 2014 4:41 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 8:12 AM
Points: 3,908, Visits: 8,867
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1600939
Posted Thursday, August 7, 2014 4:49 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, November 10, 2014 8:19 AM
Points: 4, Visits: 8
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:

NUM		NAME		TO_SITE
---------------------------------
1 MODEM A
2 ODU A
3 ANTENNA A
1 MODEM B
2 ODU B
3 ANTENNA B
1 MODEM C
2 ODU C
3 ANTENNA C
Post #1600941
Posted Thursday, August 7, 2014 5:02 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, November 21, 2014 12:18 PM
Points: 5,438, Visits: 7,606
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 | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1600948
Posted Thursday, August 7, 2014 5:12 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 7:51 PM
Points: 17,948, Visits: 15,947
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
Post #1600955
Posted Thursday, August 7, 2014 5:23 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, November 10, 2014 8:19 AM
Points: 4, Visits: 8
Ok guys, thanks a lot.
I will think a bit little more about the design of the data.
Post #1600958
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse