473,405 Members | 2,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Selecting single sorted row from many column

Hi
Been at this for 2 days now.
Each business has several packages which they can sort using
sort_order.
I'm trying to get one package for each business(that I can do), however
I want it to be the one with the lowest sort_order value
As you can see below the first record has sort_order=5 when it should
be 1.

Most of the sort_order columns will be zero by default
Any help so i can get on with my life!

Cheers
Gary

------------Current select-------------------
SELECT *
FROM dbo.testAccommodation_Packages T1
WHERE (NOT EXISTS
(
SELECT *
FROM testAccommodation_Packages
WHERE business_id = T1.business_id AND Package_ID < T1.Package_ID)
)

--------------results:-----------------------
Package_ID business_id item_name sort_order
1 2 3rd Night FREE ... 5
11 3 Donegal Town ... 0
20 4 Executive ... 0

--------------To recreate----------------------
CREATE TABLE [testAccommodation_Packages] (
[Package_ID] [int] IDENTITY (1, 1) NOT NULL ,
[business_id] [int] NULL ,
[Item_Name] [nvarchar] (300) NOT NULL ,
[sort_order] [int] NULL CONSTRAINT
[DF_Accommodation_Packages_sort_order] DEFAULT (0),

)
-------------------------------------------------
INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('1','2','3rd Night FREE when you stay 2 nights MIDWEEK (129 Euro
PPS)','5')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('2','2','Selected Donegal Town Hotel Weekend Sale - 2 B&B and 1
Dinner Only € 129 PPS','4')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('3','2','2 Night Specials -Jan, Feb & Mar 2 B&B and 1 Dinner 149
Euro PPS','3')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('4','2','Easter Hotel Breaks in Donegal Town - 2 B&B + 1 D
€169pps','2')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('5','2','2005 Bluestack Hillwalking, 2 nights B&B, 1 Dinner, 5
course Lunch 159 Euros PPS (~109 Stg)','1')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('6','2','April Pamper Package - 2 Night Special ONLY
€195pps','10')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('7','2','Discount Hotel Prices for 8th & 9th April Only € 119
PPS','7')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('8','2','Golden Year Breaks in Donegal - 4B&B + 2 Dinner
€229pps','8')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('9','2','Hotel Summer Breaks Sale in Donegal - 2B&B + 1 Dinner
€169pps','9')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('10','2','STAY SUNDAY NIGHTS FOR €25PPS','6')
INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('11','3','Donegal Town Midweek Special 99 Euro PPS 3 Nights B&B
','0')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('12','3','Bridge Weekend 2 nights B&B 79 Euro PPS (approx 55
Stg) Double Room','0')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('13','3','Donegal Spring Weekend Specials 2 B&B 1 Dinner
109.00euros pps','0')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('14','3','Valentines Weekend 2 nights B&B and 1 four course
gourmet dinner 99Euro PPS','0')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('19','3','Golden Years Break.40% OFF 4 nights B&B
€129.00p.p.s.','0')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('20','4','Executive Celebration Offer 1 night B&B + Dinner €
139 PPS','0')

INSERT
testAccommodation_Packages(Package_ID,business_id, Item_Name,sort_order)
VALUES('21','4','Watercolour Painting Break 3 B&B Full Board and
Tuition € 335 PPS','0')

Jul 23 '05 #1
1 1141
Hi lads
Got it sorted using one of these for ref:

SELECT *
FROM Accommodation_Packages a
WHERE (Package_ID =
(SELECT MIN(Package_ID)
FROM Accommodation_Packages AS b
WHERE b.business_id = a.business_id
AND b.sort_order =
(SELECT
MIN(c.sort_order)
FROM
Accommodation_Packages AS c
WHERE
c.business_id = a.business_id)))
ORDER BY business_id

___________________

SELECT T5.*
FROM Accommodation_Packages T5 INNER JOIN
(SELECT MIN(T3.Package_ID) AS Package_ID
FROM Accommodation_Packages T3
JOIN
(SELECT
T1.business_id, MIN(T1.sort_order) AS min_sort_order
FROM
Accommodation_Packages T1
GROUP BY
T1.business_id) T2 ON T3.business_id = T2.business_id AND T3.sort_order
=
T2.min_sort_order
GROUP BY T3.Business_ID) T4 ON
T4.Package_ID =
T5.Package_ID
Cheers

Gary

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brett P. | last post by:
Hello, I am trying to have only one column sortable within a data grid. I know you set the allow sorting property to true on the style, but this enables sorting for all columns. Is there a way...
1
by: Deep Silent Ocean | last post by:
Hi All I have one specific question for DataGrid In the Form Load event of the Form, I am binding data with DataGrid. On clicking Row the CurrentRowIndex is updated and it gives me correct...
1
by: Randy Harris | last post by:
I can easily select a row in a listbox with: Me.lstMyListBox = "Some Data" if "Some Data" is in a record in the bound column. Is there any way to select a record based on a column other than the...
1
by: Alvey Sidecast | last post by:
This is probably embarrassingly simple, but I've been trawling through this ng for hours now and my brain hurts. I've got an unbound multi-column listbox (multi-select=none) whose rowsource is a...
2
by: Mike Kelly | last post by:
Hi. I have a data table where rows are grouped according to a certain criteria and I want to be able to display all the rows that belong to the same group together on the screen. In addition, I...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
1
by: Andy | last post by:
Hi Gang I have a simple single table that has many duplicate rows in them. The distinctrows only works on more than one table. Is there a quick and easy way to select only the distict rows...
2
by: IcedDante | last post by:
Working with a sorted group, the inability to use following-sibling (which uses Document Order) and convert an RTF (not avaible with the Parser that we are using) hampered our ability to solve the...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.