You are here: Query question « MsSQL Server « IT news, forums, messages
Query question

Posted by Yannick Turgeon on 11/29/05 21:38

Hello all,

Say we've got these data:

----------------------------------------
CREATE TABLE #Test (
pid INT PRIMARY KEY NOT NULL,
type CHAR NOT NULL,
data VARCHAR(10) NOT NULL
)

INSERT INTO #Test (pid, type, data)
SELECT 1, 'A', 'pizza' UNION ALL
SELECT 2, 'A', 'cake' UNION ALL
SELECT 3, 'A', 'spagetti' UNION ALL
SELECT 4, 'B', 'beer' UNION ALL
SELECT 5, 'B', 'rice' UNION ALL
SELECT 6, 'B', 'hammer'
----------------------------------------


What I'd like to get is the "data" associated whit the biggest "pid"
with "type" 'A': spaghetti.

I often face this kind of query and I'm always wondering if there is a
better way to do that. Here is the way I do this:


----------------------------------------
SELECT data
FROM #Test
WHERE pid = (SELECT TOP 1 pid FROM #Test WHERE type = 'A' ORDER BY pid
DESC)
----------------------------------------


I suspect this is not the more comprehensive way to do this. Anybody do
this differently?


Yannick

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация