views:

38

answers:

2

I have a table with records as, in example data below a CO.Nr are TH-123,Th-456 and so on... I need to collect the data..

Nr.       CO.Nr           Employee       Resp            Description       Date

1         TH-123          ABC            NULL              HELLO           10.05.2010
2         TH-123          NULL           S14               NULL            18.05.2010
3         TH-123          DEF                              NULL            13.05.2010
4         TH-456          XYZ            NULL              NULL             1.07.2010
5         TH-456          NULL           S19               SOME             NULL
6         TH-456                                           TEXT            08.05.2010
7         TH-456                        NULL                               28.05.2010

For TH-123, If Nr. is maximum, that is the record i need to start with group by CO.Nr, so it is the record with Nr as 3, if the value in the other columns is NULL or space, go to a record above that is the record with Nr as 2, even if it has null value go to a record above that record with Nr. as 1 in this case. In the 3 records i need to take the maximum of date. For the above data, i need to have output as,

      CO.Nr           Employee       Resp            Description       Date

      TH-123          DEF            S14               HELLO            18.05.2010
      TH-456          XYZ            S19               TEXT             01.07.2010

Thanks in advance!

A: 

You can use a subselect to choose the record you want, then join on that. Something like the following for the employees one (I'll leave the rest of the columns as an exercise):

SELECT MyTable.[CO.Nr], Employees.Employee
FROM MyTable
LEFT OUTER JOIN (SELECT FIRST(Employee) as Employee, [CO.Nr]
                 FROM MyTable
                 WHERE Employee IS NOT NULL AND Employee <> ''
                 GROUP BY [CO.Nr]
                 ORDER BY [Nr.] DESC) Employees 
           ON MyTable.[CO.Nr] = Employees.[CO.Nr]
GROUP BY MyTable.[CO.Nr]

Or, if FIRST() is not a valid aggregate function, as mentioned in your comments, you can try subselects in your SELECT clause, like:

SELECT t.MyTable.[CO.Nr], 
       (SELECT TOP(1) x.Employee
        FROM MyTable x
        WHERE x.[CO.Nr] = t.[CO.Nr]
        AND x.Employee IS NOT NULL AND x.Employee <> ''
        ORDER BY [Nr.] DESC) as Employee
FROM MyTable t
GROUP BY t.[CO.Nr]
lc
Thanks for the response, when i execute this query, the error that is encountered is "'FIRST' is not recognized as a name of a built-in function." Then i have replaced FIRST with TOP 1 then the error is 'Employee' column is invalid in the select list because it is not in an aggregate function and not in the GROUP BY clause.
lucky
@lucky Fair enough, I've edited my answer and maybe you can use the second query?
lc
Thanku..working like a charm.In one select, for each field i need to write a select top 1...Is there any restriction or performance issue with nested selects?
lucky
@lucky Take a look at your `EXPLAIN` results and you can tell me. Make sure it's using indexes and not trying to do a full cross join first. That is, if you have indexes on `CO.Nr` and `Nr.`, you should be ok. (And on [Date] in the case that you're going to be selecting the maximum date)
lc
A: 

You can do it many ways

select [co.nr],
(select top(1) employee from mytable b where b.[co.nr]=a.[co.nr]  and 
                        employee is not null order by nr desc) as employee,
(select top(1) resp from mytable b where b.[co.nr]=a.[co.nr]  and 
                        resp is not null order by nr desc) as resp,
(select top(1) description from mytable b where b.[co.nr]=a.[co.nr]  and 
                        description is not null order by nr desc) as description,
(select max([date]) from mytable b  where b.[co.nr]=a.[co.nr]) as Date
from (
select distinct [co.nr] 
 from mytable ) as a
josephj1989
Thanku..working pretty well.In one select, for each field i need to write a select top 1...Is there any restriction or performance issue with nested selects?
lucky
Yes this will not perform very well depending on the number of records - because we have to do 4 extra selects based .But if you have an index on [co.nr],nr it should fix it. This can be achieved by a single scan through the table but have to use a more convoluted approach.
josephj1989