我找到了第一笔交易(分钟),但是当我添加“赢家”栏目时,我获得了他们的第一场胜利和第一场失利的一排。我只需要第一排,包括他们是赢还是输。我试过汇总获奖者列无济于事。如果可能的话,我宁愿不进行子查询。提前感谢您查看此内容。
SELECT
MIN(dbo.ADT.Time) AS FirstShowWager,
dbo.AD.Account, dbo.AD.FirstName,
dbo.AD.LastName, dbo.ADW.Winners
FROM
dbo.BLAH
WHERE
(dbo.ADT.RunDate = CONVERT(DATETIME, '2014-04-12
00:00:00', 102)) AND (dbo.ADW.Pool = N'shw')
GROUP BY
dbo.AD.Account,
dbo.AD.FirstName,
dbo.AD.LastName,
dbo.AD.RunDate,
dbo.ADW.Winners
ORDER BY
dbo.AD.Account
答案 0 :(得分:1)
听起来你并不关心winners列的价值,通过对获胜者进行分组,您可以获得多行,一个用于null,其他用于非null值。如果你不关心他们赢得的金额,只是如果他们赢了或输了,你可以做这样的事情,
SELECT
MIN(dbo.ADT.Time) AS FirstShowWager,
dbo.AD.Account, dbo.AD.FirstName,
dbo.AD.LastName, CASE WHEN dbo.ADW.Winners IS NULL THEN 0 ELSE 1 END
FROM
dbo.BLAH
WHERE
(dbo.ADT.RunDate = CONVERT(DATETIME, '2014-04-12
00:00:00', 102)) AND (dbo.ADW.Pool = N'shw')
GROUP BY
dbo.AD.Account,
dbo.AD.FirstName,
dbo.AD.LastName,
dbo.AD.RunDate,
dbo.ADW.Winners
ORDER BY
dbo.AD.Account
答案 1 :(得分:1)
select sorted.*
from
(
SELECT dbo.ADT.Time AS FirstShowWager,
dbo.AD.Account, dbo.AD.FirstName,
dbo.AD.LastName, dbo.ADW.Winners,
ROW_NUMBER ( ) OVER (partition by dbo.AD.Account,
dbo.AD.FirstName,
dbo.AD.LastName,
dbo.AD.RunDate
order by dbo.ADT.Time) as rowNum
FROM dbo.AD
WHERE dbo.ADT.RunDate = CONVERT(DATETIME, '2014-04-1200:00:00', 102)
AND dbo.ADW.Pool = N'shw'
) as sorted
where rowNum = 1
答案 2 :(得分:0)
通过
在select语句和组中添加此case语句而不是winners列 Case ( winners is NULL then 'Lose' else 'Win' end )
答案 3 :(得分:0)
通常这是通过派生表来完成的,该表选择您想要的记录,然后加入到所有分组字段的原始表中。
答案 4 :(得分:0)
您可以在内部查询中找到MIN,然后通过ID将其加入到ADW表中,以获取它们是否为赢家。
SELECT b.*, ADW.winner
FROM dbo.ADW ADW INNER JOIN (SELECT MIN(ADT.RunTime) AS FirstShowWager,
AD.Account, AD.FirstName,
AD.Lastnamne, AD.ADID
FROM dbo.AD AD INNER JOIN dbo.ADT ADT AD.adid = ADT.ADID
GROUP BY AD.Account, AD.Firstname, AD.Lastnamne, AD.ADID) b
ON ADW.ADID = b.ADID
假设:之间有一个外键 从ADT到AD表。 从ADW到AD表。