Click here to Skip to main content
15,897,371 members
Home / Discussions / Database
   

Database

 
Questioncursor in pl/sql Pin
richa02127-May-10 5:31
richa02127-May-10 5:31 
AnswerRe: cursor in pl/sql Pin
David Skelly27-May-10 6:04
David Skelly27-May-10 6:04 
AnswerRe: cursor in pl/sql Pin
Eddy Vluggen27-May-10 6:41
professionalEddy Vluggen27-May-10 6:41 
AnswerRe: cursor in pl/sql Pin
DABBee15-Jun-10 20:31
DABBee15-Jun-10 20:31 
QuestionSelect records using distinct and order by [modified] Pin
popchecker27-May-10 3:13
popchecker27-May-10 3:13 
AnswerRe: Select records using distinct and order by [modified] Pin
Blue_Boy27-May-10 4:34
Blue_Boy27-May-10 4:34 
GeneralRe: Select records using distinct and order by Pin
popchecker27-May-10 5:46
popchecker27-May-10 5:46 
AnswerRe: Select records using distinct and order by Pin
Eddy Vluggen27-May-10 6:27
professionalEddy Vluggen27-May-10 6:27 
Let's start out with a basic testscript that other posters can abuse to test on their machines;
SQL
DECLARE @Product TABLE (
 [Name]		NVARCHAR(250)
,[refCategory]	BIGINT
,[Added]	DATETIME DEFAULT GETDATE()

 PRIMARY KEY ([Name])
)

INSERT INTO @Product 
--      Name		Category	 Added
SELECT 'Product1',	1,		'20101204' UNION
SELECT 'Product2',	1,		'20101206' UNION
SELECT 'Product3',	2,		'20101205' UNION
SELECT 'Product4',	1,		'20101204' UNION
SELECT 'Product5',	1,		'20101209' UNION
SELECT 'Product6',	2,		'20101206' UNION
SELECT 'Product7',	2,		'20101204' UNION
SELECT 'Service1',	3,		'20100101'
The testset is somewhat larger to make it somewhat easier to interpret what I'm looking at.
popchecker wrote:
I want to Select records from distinct categories. I requirment is to select records order by AddedDate with distinct CategoryId.


If I understand correctly, then you want to group the data by category, sort within that group on the date, and take the top 2 records from each group?

Sql Server 2005 supports "Common Table Expressions", which you could use as an alternative to a temporary table, like this;
SQL
SELECT [Name], [refCategory], [Added]
  FROM(
    SELECT [refCategory],
       [Added],
       [Name],
       row_number() OVER (
           PARTITION BY [refCategory]
           ORDER BY [Added] ASC
       ) AS [catGroup]
      FROM @Product
     GROUP BY [refCategory], [Added], [Name]
   ) AS ProductCTE
WHERE [catGroup] <= 2
ORDER BY [refCategory] ASC, [Added] ASC

I are Troll Suspicious | :suss:

GeneralRe: Select records using distinct and order by Pin
i.j.russell27-May-10 9:45
i.j.russell27-May-10 9:45 
GeneralRe: Select records using distinct and order by [modified] Pin
Tripathi Swati27-May-10 19:39
Tripathi Swati27-May-10 19:39 
GeneralRe: Select records using distinct and order by Pin
Mycroft Holmes27-May-10 21:53
professionalMycroft Holmes27-May-10 21:53 
GeneralRe: Select records using distinct and order by Pin
Eddy Vluggen28-May-10 1:40
professionalEddy Vluggen28-May-10 1:40 
QuestionMS SQL Connection Failed eg: "Trusted Connection"... Pin
PDTUM26-May-10 19:08
PDTUM26-May-10 19:08 
AnswerRe: MS SQL Connection Failed eg: "Trusted Connection"... Pin
_Damian S_26-May-10 19:23
professional_Damian S_26-May-10 19:23 
AnswerRe: MS SQL Connection Failed eg: "Trusted Connection"... Pin
PDTUM27-May-10 5:10
PDTUM27-May-10 5:10 
QuestionExporting only Defination not data Pin
Hum Dum26-May-10 1:30
Hum Dum26-May-10 1:30 
AnswerRe: Exporting only Defination not data Pin
Mike Osbahr26-May-10 2:17
Mike Osbahr26-May-10 2:17 
AnswerRe: Exporting only Defination not data Pin
Wayne Gaylard26-May-10 15:45
professionalWayne Gaylard26-May-10 15:45 
AnswerRe: Exporting only Defination not data Pin
SomeGuyThatIsMe27-May-10 2:03
SomeGuyThatIsMe27-May-10 2:03 
AnswerRe: Exporting only Defination not data Pin
Md. Marufuzzaman28-May-10 20:05
professionalMd. Marufuzzaman28-May-10 20:05 
QuestionA tale of two queries [solved] Pin
Mike Osbahr25-May-10 7:47
Mike Osbahr25-May-10 7:47 
AnswerRe: A tale of two queries Pin
Chris Meech25-May-10 8:01
Chris Meech25-May-10 8:01 
GeneralRe: A tale of two queries Pin
Mike Osbahr25-May-10 8:13
Mike Osbahr25-May-10 8:13 
AnswerInclude actual execution plan Pin
David Mujica25-May-10 8:06
David Mujica25-May-10 8:06 
GeneralRe: Include actual execution plan Pin
Mike Osbahr25-May-10 8:17
Mike Osbahr25-May-10 8:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.