1

I have the following table

ID      TYPE        Text       ImagePath     Title    Date

 1      Text        Test        NULL          NULL     14/10/2013
 2      Image       NULL        /test/test    NULL     14/10/2013
 3      Title       NULL        NULL          Test     14/10/2013
 4      Text        Test2       NULL          NULL     20/12/2012

How can I retrieve ONLY the last 3 (sorted by date) records, 1 with type Text, 1 with type Image and 1 with type Title only? (Basically I only need the type and related field which is not NULL.

6
  • What is the expected output? Commented Oct 14, 2013 at 15:23
  • Are you looking for only the most recent record of each type? Commented Oct 14, 2013 at 15:25
  • @nycdan yes that is exactly what im looking for. Sorry for not being in title/description but did not know exactly how to word it. Commented Oct 14, 2013 at 15:27
  • 2
    When you have a column "type" and the 3 columns all are varchar, why don't you combine them in one column? Commented Oct 14, 2013 at 15:28
  • @fancyPants yes that actually makes more sense and would solve my problem. Thank you Commented Oct 14, 2013 at 15:37

2 Answers 2

1

Apart from restructuring your database as I said in the comment on your question, this should give you what you want (if I understood the question properly):

SELECT
TOP 1 
'Text' AS Type,
Text
FROM
your_table
ORDER BY Date DESC
UNION ALL
SELECT
TOP 1 
'ImagePath',
ImagePath
FROM
your_table
ORDER BY Date DESC
UNION ALL
SELECT
'Title', 
TOP 1 Title
FROM
your_table
ORDER BY Date DESC

You may have to put some parantheses here and there to make it work...

Sign up to request clarification or add additional context in comments.

Comments

0

One idea could be:

SELECT  * FROM table where TYPE = 'Text' OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY 
union
SELECT  * FROM table where TYPE = 'Image' OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY 
union
SELECT  * FROM table where TYPE = 'Title' OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY 

6 Comments

Use UNION ALL when you don't need to eliminate duplicates - is this even compatible with MS Sql Server
Yes, we could use union all here, but there's no need because it won't return duplicate rows as long as the type betweens the select are always different ;-)
does this order properly to select the most recent? Also, I agree with Charleh that this isn't SQL Server syntax.
You misunderstood him. Using UNION ALL is a performance tweak, as the Server doesn't need to look for duplicates. Also your answer is totally wrong. OP asked for "last 3".
Yes - avoid UNION because it's a performance penalty - especially for larger data sets when you don't need to eliminate dupes. My rule of thumb is UNION ALL unless you know why you are using UNION.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.