避免上一个发布的问题。
我使用内部联接加入表格,lotid
和date
来自test
表,worker
来自test1
表。我正在使用group by
,但它无法正常工作,因为工作日和日期列不同
lotid worker date
1234 abc 02/02/2106
1234 xyz 03/03/2016
但在输出中我应该只得到1个数据
lotid worker date
1234 abc 02/02/2016
如何实现这一目标?
答案 0 :(得分:0)
我使用简单的表和简单的查询使用下面的DENSE_RANK()
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
结果是:
lotid | workDate | [R
1234 | 2016-02-02 00:00:00.000 | 1
答案 1 :(得分:0)
感谢所有帮助人员,但我找到了解决方案,
select lotid,
max(worker) as worker
max(trsdate) as max_date
from test
inner join test1
on test.lotid=test1.lotid
group by lotid