Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Newbie - Help please

Author  Topic 

sqlnewbie11
Starting Member

2 Posts

Posted - 2011-02-08 : 11:44:40
I want to retrieve one record for each supplier identifying up to 4 of the suppliers locations, please can someone help me achieve this.

Here are the tables utilized, please can someone point me in the direction to get my desired result.

What type of query will i need to run? example syntax

SELECT * FROM SUPPLIER

supplier_num supplier_name
------------- ----------------
100001 Test Supplier 1
100002 Test Supplier 2



SELECT * FROM SUPPLIER_LOCATIONS

supplier_num supplier_name supplier_location
------------- ---------------- ----------------
100001 Test Supplier 1 Milton Keynes
100001 Test Supplier 1 Reading
100001 Test Supplier 1 London
100001 Test Supplier 1 Hertfordshire
100002 Test Supplier 2 Bedfordshire
100002 Test Supplier 2 Hertfordshire
100002 Test Supplier 2 London
100002 Test Supplier 2 Luton
100002 Test Supplier 2 Manchester
100002 Test Supplier 2 Liverpool
100002 Test Supplier 2 Birmingham


i want a result set like the below.

supplier_num supplier_name supplier_location1 supplier_location2 supplier_location3 supplier_location4
------------- ---------------- ------------------- ------------------- ------------------- -------------------
100001 Test Supplier 1 Milton Keynes Reading London Hertfordshire
100002 Test Supplier 2 Bedfordshire Hertfordshire London Luton

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-02-08 : 12:28:56
Well you really want two things here. One of which is a good database task and one of which is not.

1) You want each supplier and the first four locations for that supplier (ordered somehow). -- a database task

2) You want to pivot that list to get the output you stated: [one line per supplier] -- not a database task. You *can* do this in the database but it's really not a good idea. What you want instead is to have whatever consumes your database output format it for you (using a very simple loop)

Here's what I would initially do to get the basic output: (before pivoting).

SELECT
s.[supplier_number]
, s.[supplier_name]
, sl.[supplier_location]
FROM
supplier AS s

OUTER APPLY (
SELECT TOP 4
[supplier_location]
FROM
supplier_locations AS loc
WHERE
loc.[supplier_num] = s.[supplier_num]
ORDER BY
loc.[supplier_location]
)
AS sl

There are other ways to do it also.

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

sqlnewbie11
Starting Member

2 Posts

Posted - 2011-02-08 : 16:11:22
Hi Charlie, thanks for your feedback.. I have had a look at pivot queries and have found this works with some agregae function, if this is the case, this will not work for me. Any other suggestions?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-02-09 : 04:06:36
See point (2) in my post.

There is many tricks to get the pivoted output you are after but they are all ways of coding round a problem that isn't a good database task.

Where is this output going?

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-02-09 : 04:24:03
This is a sample of the kind of crazy stuff you'd have to do to pivot this in the database. It's much, much eaiser in another app:
DECLARE @supplier TABLE (
[supplier_number] INT PRIMARY KEY
, [supplier_name] VARCHAR(255)
)

DECLARE @supplierLocation TABLE (
[supplierLocationID] INT IDENTITY (1,1) PRIMARY KEY
, [supplier_number] INT
, [supplier_location] VARCHAR(MAX)
)

INSERT @supplier ([supplier_number], [supplier_name])
SELECT 1, 'ACME'
UNION SELECT 2, 'UNIVERSAL'
UNION SELECT 3, 'FOO INDUSTRIES'

INSERT @supplierLocation ([supplier_number], [supplier_location])
SELECT 1, 'COLORADO'
UNION SELECT 1, 'NORTH SPRINGS'
UNION SELECT 1, 'EASTERHOUSE'
UNION SELECT 1, 'MUSSLEBURGH'
UNION SELECT 1, 'KENTISH TOWN'
UNION SELECT 1, 'GRANTHON'
UNION SELECT 2, 'WEST LOTHIAN'
UNION SELECT 2, 'LENZIE'

SELECT
[supplier_number]
, [supplier_name]
, MAX(CASE WHEN [rowPos] = 1 THEN [supplier_location] ELSE '' END) AS [Supplier_location1]
, MAX(CASE WHEN [rowPos] = 2 THEN [supplier_location] ELSE '' END) AS [Supplier_location2]
, MAX(CASE WHEN [rowPos] = 3 THEN [supplier_location] ELSE '' END) AS [Supplier_location3]
, MAX(CASE WHEN [rowPos] = 4 THEN [supplier_location] ELSE '' END) AS [Supplier_location4]
FROM
(
SELECT
s.[supplier_number] AS [supplier_number]
, s.[supplier_name] AS [supplier_name]
, sl.[supplier_location] AS [supplier_location]
, ROW_NUMBER() OVER (PARTITION BY s.[supplier_number] ORDER BY sl.[supplier_location]) AS [rowPos]
FROM
@supplier AS s
LEFT JOIN @supplierLocation AS sl ON sl.[supplier_number] = s.[supplier_number]
)
AS prePivot
WHERE
prePivot.[rowPos] <= 4
GROUP BY
[supplier_number]
, [supplier_name]


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -