avoid Previous posted question.
I am using inner join to join the table, lotid
and date
is from test
table and worker
is from test1
table. I am using group by
but it is not working because worker and date column is different
lotid worker date
1234 abc 02/02/2106
1234 xyz 03/03/2016
but in the output I should get only 1 lotid
lotid worker date
1234 abc 02/02/2016
how to achieve that?
I make simple table and simple query use DENSE_RANK() below
CREATE TABLE #test (lotid NVARCHAR(10), workDate DATETIME)
CREATE TABLE #test1 (lotid NVARCHAR(10),worker NVARCHAR(10) )
INSERT INTO #test VALUES ('1234','2016-02-02'),('1234','2016-03-03')
INSERT INTO #test1 VALUES ('1234','abc'),('1234','xyz')
SELECT *
FROM (
SELECT DISTINCT t.*
,DENSE_RANK() OVER (PARTITION BY t.lotid ORDER BY t.workDate ASC) AS [r]
FROM #test t
INNER JOIN #test1 t1 ON t.lotid = t1.lotid
) q
WHERE q.r = 1
DROP TABLE #test
DROP TABLE #test1
and result is :
lotid | workDate | r
1234 | 2016-02-02 00:00:00.000 | 1
Thanks for all your helps folks but I figured out the solution,
select lotid,
max(worker) as worker
max(trsdate) as max_date
from test
inner join test1
on test.lotid=test1.lotid
group by lotid
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.