PK带来多个结果,但显示相同的PK数

时间:2013-12-04 16:46:42

标签: sql sql-server

我试图获得一个“细分”的名称但是当我加入PK时它会带来近1k的结果

我需要第二个查询的第一个查询(注意我试图让它在第一个查询中工作但是为了帮助找出它为什么这样做我在两个查询中这样做)

DECLARE 
@vnuID int = 1212,
@StartDate datetime = '18/11/2013 00:00:00',
@EndDate datetime = '18/11/2013 11:59:59',
@Users int = 538

SELECT DISTINCT trID,
    CAST(trFinish - trStart AS TIME(2)) AS SegmentTime,
    CAST(CAST(@StartDate AS DATE) AS Varchar(50)) + ' - ' + CAST(CAST(@EndDate AS DATE) AS Varchar(50)) AS DateRage
    --,segName
FROM 
    tblTrace
    INNER JOIN tblUsers ON usrID = tr_usrID
    INNER JOIN VS.dbo.tblSegments ON seg_vnuID = tr_vnuID
WHERE 
    (tblTrace.trFinish IS NOT NULL)
    AND (tblTrace.trObjectType LIKE 'Segment%') 
    AND (tblTrace.tr_vnuID = @vnuID) 
    AND (tblTrace.trStart BETWEEN @StartDate AND @EndDate)
    AND (tblUsers.usrEmail NOT LIKE '%@test%')
    AND usrID IN (@Users)
GROUP BY trID, trFinish, trStart--, segName

这是为了找到段名,但会带来很多结果

SELECT trID, segName 
FROM VS.dbo.tblSegments
    INNER JOIN tblTrace ON tr_vnuID = seg_vnuID
WHERE trID = 224663

知道为什么吗?

1 个答案:

答案 0 :(得分:1)

VS.dbo.tblSegments表格中有多行与指定seg_vnuID行上的tr_vnuID具有相同的tblTrace