-4

How to select a distinct value in a query when there are more than one column when i am doing like this i am getting an error

 select distinct tempname,rundate from History_Table ORDER BY RunDate DESC

EDIT::

i have to show like this in my gridview

Name   Rundate 
Test   DDL(to show all the rundates)
Test1  rundate
Chandra sekhar
  • 169
  • 2
  • 7
  • 21

3 Answers3

2

DISTINCT give distinct rows

Seems you need tempname with one rundate (Last, First, etc). This can be achieved by using GROUP BY. Following example will give result tempname with last rundate

SELECT tempname,MAX(rundate) AS rundate FROM History_Table GROUP BY tempname

You can use ORDER BY as well.

SELECT tempname,rundate
FROM
(SELECT tempname,MAX(rundate) AS rundate  FROM History_Table GROUP BY tempname) AS SummaryTable
ORDER BY rundate
Prasanna
  • 4,583
  • 2
  • 22
  • 29
2

It looks like you want to concatenate your run dates into a single string. This can be done using sql-server's xml extensions:

SELECT  t1.TempName,
        STUFF(( SELECT  ', [' + CONVERT(VARCHAR, t2.RunDate, 103) + ']'
                FROM    History_Table t2
                WHERE   t1.TempName = t2.TempName
                ORDER BY t2.RunDate
                FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)'), 1, 2, '') [RunDates]
FROM    (   SELECT  DISTINCT TempName
            FROM    History_Table 
        ) t1

Example SQL Fiddle

A full explanation of how using XML to concatenate strings is contained in another answer here, with a little help from KM's answer to another question

Community
  • 1
  • 1
GarethD
  • 68,045
  • 10
  • 83
  • 123
  • This is what i want i think will check once, can i remove the [ ] ?? – Chandra sekhar Oct 26 '12 at 11:03
  • removing the brackets will have no effect on the query, nor will changing the date format from 103 (`dd/MM/yyyy`), I just added it to make the result clearer. – GarethD Oct 26 '12 at 11:08
  • but the thing is i have to fill a label and a dropdown using this query will it work?? – Chandra sekhar Oct 26 '12 at 11:14
  • Is this to add all run dates into another drop down (or similar) once someone has selected a rundate? IF this is the case you either want to use 2 queries and define relations within your dataset in your code behind, or use one query and a `Dictionary>` in your code behind to handle changes in run date. You may need to update the question, or ask a new one with full details of what you are actually trying to do both on the client side and the server side. – GarethD Oct 26 '12 at 11:24
  • I have to bind the dates into a DDL with the associated name.Yeah will ask another question – Chandra sekhar Oct 26 '12 at 11:28
  • This is my new question link http://stackoverflow.com/questions/13086153/binding-dates-to-dropdownlist-inside-gridview – Chandra sekhar Oct 26 '12 at 11:35
0

Sounds like you need two seperate querys. One to get the distinct tempnames and another to get the rundates. Your present query is correctly returning distinct combinations of the two.

Ben Thurley
  • 6,943
  • 4
  • 31
  • 54