SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Problems with a order of the rows


Problems with a order of the rows

Author
Message
rodrigo_fabiam
rodrigo_fabiam
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
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


Evil Kraig F
Evil Kraig F
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20703 Visits: 7660
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
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42107 Visits: 19829
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
rodrigo_fabiam
rodrigo_fabiam
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
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

Evil Kraig F
Evil Kraig F
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20703 Visits: 7660
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
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66869 Visits: 18570
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

rodrigo_fabiam
rodrigo_fabiam
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 11
Ok guys, thanks a lot.
I will think a bit little more about the design of the data.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search