Hello, tress
Try something like this:
CREATE TABLE tbl (
ID int PRIMARY KEY,
Address varchar(50)
)
INSERT INTO tbl VALUES (1,'The High Street')
INSERT INTO tbl VALUES (2,'Chester')
INSERT INTO tbl VALUES (3,'Cheshire')
INSERT INTO tbl VALUES (4,'CH')
INSERT INTO tbl VALUES (5,'The Road')
INSERT INTO tbl VALUES (6,'Northhop')
INSERT INTO tbl VALUES (7,'Flintshire')
INSERT INTO tbl VALUES (8,'CH')
SELECT (ID-1)/4+1 as RowNum,
MIN(CASE WHEN ID%4=1 THEN Address END) AS Street,
MIN(CASE WHEN ID%4=2 THEN Address END) AS City,
MIN(CASE WHEN ID%4=3 THEN Address END) AS County,
MIN(CASE WHEN ID%4=0 THEN Address END) AS Code
FROM tbl GROUP BY (ID-1)/4+1

Signature
Razvan Socol
SQL Server MVP
Razvan Socol - 30 Jul 2008 11:01 GMT
> Hello, tress
>
[quoted text clipped - 20 lines]
> MIN(CASE WHEN ID%4=0 THEN Address END) AS Code
> FROM tbl GROUP BY (ID-1)/4+1
In SQL 2005 you can use the PIVOT operator, like this:
SELECT RowNum, Col0 as Street, Col1 as City, Col2 as County, Col3 as Code
FROM (
SELECT (ID-1)/4+1 as RowNum,
'Col'+CONVERT(char,(ID-1)%4) as ColNum,
Address
FROM tbl
) x PIVOT (MIN(Address) FOR ColNum IN (Col0, Col1, Col2, Col3)) AS p
But (to me) it seems more complicated and it will probably have the same
performance as the first query.

Signature
Razvan Socol
SQL Server MVP
tress - 30 Jul 2008 13:06 GMT
Hi,
Thanks to both of you for your responses, both as you have said do the same
thing, but I think its always worth knowing a couple of ways of doing the
same thing, you never know when it might come in handy.
Thanks again for the help!
> > Hello, tress
> >
[quoted text clipped - 33 lines]
> But (to me) it seems more complicated and it will probably have the same
> performance as the first query.