Sql Problem

  • Hi,

    I m sarvesh,

    i have a problem in sql. i have a table and i want fetch distinct records on two fields, then what should do me for it.

    pls help.:)

  • Can you provide the DDL (definition) for your table. What fields does it contain? Which two fields are required to be "distinct" in your result set?

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • i have a table.

    fields =

    1) Id

    2) name

    3) EmailId

    4) Age

    5) Salary

    now i want distinct recordsbased on name and EmailId then what should do me

  • SELECTID,

    Name,

    EmailID,

    Age,

    Salary

    FROM(

    SELECTID,

    Name,

    EmailID,

    Age,

    Salary,

    COUNT(*) OVER (PARTITION BY Name, EmailID) AS RecID

    FROMTable1

    ) AS d

    WHERERecID = 1


    N 56°04'39.16"
    E 12°55'05.25"

  • Hey Peter ,

    Just Check this,

    I am getting the Output for only ID 3 , why so?

    CREATE TABLE #temp

    (

    Id int,

    tname varchar(10),

    Email varchar(10),

    Age int,

    Salary int

    )

    INSERT INTO #temp

    SELECT '1','aaa','1@abc.com',20,1000

    UNION

    SELECT '2','aaa','1@abc.com',20,1000

    UNION

    SELECT '3','bbb','1@abc.com',20,1000

    SELECT ID, tName, Email, Age, Salary

    FROM( SELECT ID, tName, Email, Age, Salary,

    COUNT(*) OVER (PARTITION BY tName, Email) AS RecID

    FROM #Temp

    ) AS d

    WHERE RecID = 1

    Cheers!

    Sandy.:)

    --

  • there is Error on query analyzer by executing this query.:)

  • Sarvesh,

    First... when you get an error, you need to post it so we can see what's going on.

    Second... are you actually using SQL Server 2005?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • My dear,

    below error is coming

    Incorrect syntax near the keyword 'OVER' 🙂

  • No, i m using SQL SERVER 2000

  • If you ARE using SQL Server 2005, make sure COMPATIBILITY LEVEL is set to 90.


    N 56°04'39.16"
    E 12°55'05.25"

  • You want row_number() not count(*) ....

    CREATE TABLE #temp

    (

    Id int,

    tname varchar(10),

    Email varchar(10),

    Age int,

    Salary int

    )

    INSERT INTO #temp

    SELECT '1','aaa','1@abc.com',20,1000

    UNION

    SELECT '2','aaa','1@abc.com',20,1000

    UNION

    SELECT '3','bbb','1@abc.com',20,1000

    SELECT ID, tName, Email, Age, Salary, recID

    FROM( SELECT ID, tName, Email, Age, Salary,

    ROW_NUMBER() OVER (PARTITION BY tName, Email ORDER BY tname, email) AS RecID

    FROM #Temp

    ) AS d

    WHERE RecID = 1

    DROP TABLE #temp

    [EDIT] This solution only works in SQL 2005.. (this forum.. *wink wink*) [/EDIT]

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Sarvesh, I am sure you are using SQL Server 2000 as your database. The syntax is not supported in SQL Server 2000.

    Prasad Bhogadi
    www.inforaise.com

  • g.sarvesh (10/10/2007)


    No, i m using SQL SERVER 2000

    You are posting your questions to the 2005 TSQL forum. Any answers are likely to contain syntax that won't work in 2000. That's exactly what's happening here. Note the little bar at the top of the screen that says "SQL Server 2005." for the forum. Scroll down a little on the forums page and you'll find the 2005 forum where people will provide syntax specific to your version. Just in case, here's the link 2000/7 TSQL

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • Jason Selburg (10/10/2007)


    You want row_number() not count(*) ....

    Why?

    ALL groups of records always start with 1, both distinct and non-distinct groups...


    N 56°04'39.16"
    E 12°55'05.25"

  • No, the count(*) will give you the count of records in that group, not an incremental row number....

    Run your script and you'll see... *grin* :hehe:

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg

Viewing 15 posts - 1 through 15 (of 20 total)

You must be logged in to reply to this topic. Login to reply