3

Can "Distinct" key Word be used twice in a single Select Query? like wise:

select DISTINCT(trackid), DISTINCT(table_name) 
from jos_audittrail 
where live = 0 AND operation = UPDATE

Thanks

4 Answers 4

5

No, By Default Distinct works on all the columns you are selecting. eg.

select DISTINCT trackid, table_name 
from jos_audittrail 
where live = 0 AND operation = UPDATE

This will select all distinct trackid and table name combination

EDIT

For retrieving distinct records other than this you can use the answer given by davek. It will work.

You can use group by to do this work as group by is being applied on both the columns being provided so no aggregate function is needed.

    SELECT trackid, table_name FROM jos_audittrail 
    WHERE live = 0 AND operation = 'UPDATE' 
    GROUP BY trackid, tablename
0
2
select trackid
, table_name
, count(*)
from jos_audittrail 
where live = 0 AND operation = UPDATE
group by trackid, table_name
order by trackid, table_name

would give you distinct combinations of the two.

2

No you can't use that, it will throw an error, but there are other alternatives where you can get your desired results.

1
  • No error will be thrown, distinct works with multiple columns but the syntax given by OM is incorrect and if you are talking about that then it will give an error Apr 30, 2010 at 11:15
0

the easiest way to find this it is to just run the query. i just tried, and it didn't work.

however, you can use two columns in a GROUP BY -- just do this:

select trackid, table_name from jos_audittrail where live = 0 and operation = 'UPDATE' group by trackid, tablename

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.