我有一张类似
的桌子 ID NAME TL_NUM TL_NAME MANAGER_NUM
===============================================
1 F-23 2 T-2 1
2 B-23 3 T-3 2
3 F-24 2 T-2 1
4 F-25 1 T-1 2
5 F-26 2 T-2 2
6 F-27 1 T-1 2
现在我想获得如下结果集
TL_NUM TL_NAME MANAGER_NUM
================================
2 T-2 1
3 T-3 2
1 T-1 2
2 T-2 2
我尝试使用以下查询
select DISTINCT TL_NUM, TL_NAME,MANAGER_NUM
from dataTable
使用此查询时,它为我提供了正确的结果集,但上述查询却没有:
select DISTINCT TL_NUM, TL_NAME
from dataTable
,但是在我的结果集中没有返回正确的“ MANAGER_NUM”。谁能让我知道确切的问题是什么,我相信是由于DISTINCT造成的?
答案 0 :(得分:1)
您的查询看起来不错并且可以正常工作,但是根据输出结果排序是不正确的。我已经测试了该查询及其正常工作。
def isPos(ea: EitherAcc, t: Int): EitherAcc =
ea.filterOrElse(_ => t > 0, s"$t is not positive")
答案 1 :(得分:0)
经过测试,它会返回正确的值-您是否正确查看了这些值?这是用于创建表的工作版本的测试脚本:
CREATE TABLE #dataTable (id int identity, Name varchar(10), TL_NUM smallint, TL_NAME varchar(10), MANAGER_NUM smallint)
INSERT INTO #datatable (Name, TL_NUM, TL_NAME, MANAGER_NUM)
VALUES
('F-23', 2, 'T-2', 1),
('B-23', 3, 'T-3', 2),
('F-24', 2, 'T-2', 1),
('F-25', 1, 'T-1', 2),
('F-26', 2, 'T-2', 2),
('F-27', 1, 'T-1', 2)
然后我使用了您的查询:
select DISTINCT TL_NUM, TL_NAME ,MANAGER_NUM
from #dataTable
并设置了此返回值:
---------------------------------
|TL_NUM |TL_NAME |MANAGER_NUM|
---------------------------------
|1 |T-1 |2 |
|2 |T-2 |1 |
|2 |T-2 |2 |
|3 |T-3 |2 |
---------------------------------
主要区别在于返回的行的顺序,但是它们以正确的值返回。
答案 2 :(得分:0)
在下面查看我的小提琴:
DECLARE @tmp TABLE ( ID INT,NAME NVARCHAR(100),TL_NUM INT,TL_NAME NVARCHAR(100),MANAGER_NUM INT)
INSERT INTO @tmp
VALUES
(1, 'F-23', 2, 'T-2', 1)
,(2, 'B-23', 3, 'T-3', 2)
,(3, 'F-24', 2, 'T-2', 1)
,(4, 'F-25', 1, 'T-1', 2)
,(5, 'F-26', 2, 'T-2', 2)
,(6, 'F-27', 1, 'T-1', 2)
SELECT DISTINCT t.TL_NUM ,t.TL_NAME ,t.MANAGER_NUM
FROM @tmp AS t
ORDER BY t.MANAGER_NUM
,t.TL_NAME DESC
答案 3 :(得分:0)
您可以这样做:
CREATE TABLE TAB1 ( ID INT,NAME NVARCHAR(100),TL_NUM INT,TL_NAME
NVARCHAR(100),MANAGER_NUM INT)
INSERT INTO TAB1
VALUES
(1, 'F-23', 2, 'T-2', 1)
,(2, 'B-23', 3, 'T-3', 2)
,(3, 'F-24', 2, 'T-2', 1)
,(4, 'F-25', 1, 'T-1', 2)
,(5, 'F-26', 2, 'T-2', 2)
,(6, 'F-27', 1, 'T-1', 2)
SELECT DISTINCT t.TL_NUM ,t.TL_NAME ,t.MANAGER_NUM
FROM TAB1 AS t;