Results 1 to 7 of 7

Thread: Select unique values without using distinct

  1. #1
    Join Date
    Mar 2010
    Posts
    222

    Select unique values without using distinct

    Hello,
    I would like to retrieve unique values from a database without using the distinct keyword.
    Here is what I have tried till now, any help regarding this is appreciated.
    Code:
    SELECT prds.id,prds.nm,prds.short_name,prds.price,depts.nm AS 'dept_name' FROM prds,depts,prod_dept WHERE (prds.nm LIKE '%" . $search . "%' OR
    prds.short_name LIKE '%" . $search . "%' OR      
    prds.short_desc LIKE '%" . $search . "%' OR 
    prds.long_desc LIKE '%" . $search . "%' OR
    depts.nm LIKE '%" . $search . "%') AND 
    prod_dept.prod_id=prds.id AND prod_dept.dept_id = depts.id ORDER BY prds.nm ASC

  2. #2
    Join Date
    Nov 2009
    Posts
    359

    Re: Select unique values without using distinct

    Hello,
    I think you are asking for this
    Code:
    SELECT prds.id,prds.nm,prds.sh_nm,prds.price,min(depts.nm) AS 'dept_name'
    FROM prds,depts,prod_dept
    WHERE (prds.nm LIKE '%" . $search . "%' OR
    prds.sh_nm LIKE '%" . $search . "%' OR      
    prds.short_desc LIKE '%" . $search . "%' OR
    prds.long_desc LIKE '%" . $search . "%' OR
    depts.nm LIKE '%" . $search . "%') AND
    prod_dept.prod_id=prds.id AND
    prod_dept.dept_id = depts.id
    GROUP BY prds.id,prds.nm,prds.sh_nm,prds.price
    ORDER BY prds.nm ASC

  3. #3
    Join Date
    Nov 2009
    Posts
    583

    Re: Select unique values without using distinct

    Hello,
    I think the group by clause if the best choice in this situation. Also if you need to avoid the duplicate entries in your result then it can be done with the group by clause. Suppose you have a table A and it have a column X, the column C has the following values 1,2,3... etc.
    Then the following query will return the unique values
    Code:
    Select X From A Group By X;

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: Select unique values without using distinct

    Hello,
    I have not test the below query, but I think this can work
    Code:
    select test.deptno
    from sc.emp test
    sc.emp b
    where test.deptno b.deptno
    and test.rowid<>b.rowid

  5. #5
    Join Date
    Nov 2009
    Posts
    518

    Re: Select unique values without using distinct

    Hello,
    If you require unique values then I think you will need to use the distinct clause because it is more effective then using the group by clause
    Here are few examples

    Code:
    SELECT DISTINCTROW cpy 
    FROM cus 
    INNER JOIN Orders ON Cus.CustID = Orders.CustID;
    Code:
    SELECT DISTINCT cpy 
    FROM Cus
    INNER JOIN Orders ON Cus.CustID = Orders.CustID;

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Select unique values without using distinct

    Hello,
    Something like this can even work

    Code:
    SELECT CarID
    FROM TableName
    GROUP BY CarID;
    Or

    Code:
    SELECT DISTINCT CarID
    FROM TableName;

  7. #7
    Join Date
    Nov 2009
    Posts
    343

    Re: Select unique values without using distinct

    Hello,
    If you need distinct values then you can use the following

    Code:
    select x.depnum from emp x emp b
    where x.depnum b.depnum
    and x.rowid not in (select max(rowid) from emp)
    group by x.depnum;

Similar Threads

  1. Select unique values using xsl
    By Elizabeth Allen in forum Software Development
    Replies: 6
    Last Post: 17-05-2010, 10:27 AM
  2. Replies: 4
    Last Post: 14-05-2010, 12:16 AM
  3. SQL UNIQUE and DISTINCT
    By Taarank in forum Software Development
    Replies: 5
    Last Post: 10-05-2010, 10:40 PM
  4. How to get unique values with respective count from a list
    By hounds in forum Software Development
    Replies: 4
    Last Post: 06-03-2010, 09:54 PM
  5. SELECT UNIQUE and SELECT DISTINCT
    By TAARIQ in forum Software Development
    Replies: 3
    Last Post: 03-06-2009, 10:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,516,088.03157 seconds with 17 queries