我有一个tblData和一个tblUser。
我只想显示用户可以进行的交易。
事务由DisplayNum链接,如果DisplayNum中的某个事务与tblUser中的TransType不匹配,则用户无法在该DisplayNum中获取任何TransType。
答案 0 :(得分:1)
最后一个版本经过一些聊天后放在一起:
SELECT DISTINCT q2.Id, q3.SubQ, q1.DisplayNum, q1.TransType, q1.TotalTransTime, q1.UserId
FROM (
SELECT D.DisplayNum, HighestTransTime.TransType, SUM(D.TransTime) AS TotalTransTime, U.UserId
FROM tblData D
INNER JOIN tblUser U ON D.TransType=U.TransType
INNER JOIN
(
SELECT DISTINCT innerQuery.DisplayNum, TransType
FROM tblData
INNER JOIN
(
SELECT DisplayNum, MAX(TransTime) AS TransTime FROM tblData GROUP BY DisplayNum
) innerQuery ON tblData.DisplayNum = innerQuery.DisplayNum AND tblData.TransTime = innerQuery.TransTime
) HighestTransTime ON D.DisplayNum=HighestTransTime.DisplayNum
WHERE U.UserId = 10
AND D.TransType IN (SELECT TransType FROM tblUser WHERE tblUser.UserId = U.UserId)
AND D.DisplayNum NOT IN (SELECT DisplayNum FROM tblData WHERE TransType NOT IN (SELECT TransType FROM tblUser WHERE tblUser.UserId = U.UserId))
GROUP BY D.DisplayNum, HighestTransTime.TransType, U.UserId
) q1
INNER JOIN (SELECT DisplayNum, MAX(ID) AS ID FROM tblData GROUP BY DisplayNum) q2 ON q1.DisplayNum = q2.DisplayNum
INNER JOIN (SELECT SubQ, ID FROM tblData) q3 ON q2.ID=q3.ID
ORDER BY q2.ID
答案 1 :(得分:0)
据我所知,您可以在TransType
列上使用简单联接。
SELECT *
FROM dbo.tblData INNER JOIN dbo.tblUser ON
dbo.tblData .TransType = dbo.tblUser .TransType