标题可能听起来很愚蠢,但我不知道还有什么可以命名。事先对此抱歉。
对我的问题。我发现这个旧查询需要修改。
SELECT t.batchid, t.SQLidentity, t.qvardenum, t.kassorsakskod, t.editint, t.rowcreatedby
FROM dbo.qdin t
INNER JOIN (
SELECT batchid, max(sqlidentity) AS MaxId
FROM dbo.qdin
WHERE qparamid = 1
GROUP BY batchid)
tm on t.batchid = tm.batchid AND t.sqlidentity = tm.MaxId
我需要添加列tiq.qparamgrupp以及它的Where子句。我设法加入表格并添加列,但我似乎无法添加Where子句。
这是我的查询。
SELECT t.batchid,
t.sqlidentity,
t.qvardenum,
t.kassorsakskod,
t.editint,
t.rowcreatedby,
tiq.qparamgrupp
FROM dbo.qdin t
INNER JOIN dbo.tiq
ON tiq.aonr = T.aonr
AND tiq.aopos = T.aopos
AND tiq.dummyuniqueid = T.conndummyuniqueid
INNER JOIN (SELECT batchid,
Max(sqlidentity) AS MaxId
FROM dbo.qdin
WHERE qparamid = 1
GROUP BY batchid) tm
ON t.batchid = tm.batchid
AND t.sqlidentity = tm.maxid
WHERE tiq.qparamgrupp = 2
并且它什么都不返回。
修改的 这是内容的一部分。
qdin.batchid qdin.qparamid tiq.qparamgrupp qdin.qvardenum
(03-2020 10471) 1 2 5,26
(03-2020 10471) 1 3 5,23
(03-2020 10471) 2 3 3290
(03-2020 10471) 2 2 3400
(03-2020 10471) 3 4 0
(03-2020 10471) 4 2
(03-2020 10471) 4 3
(03-2020 10471) 5 3
(03-2020 10471) 5 2
如果我在没有" WHERE tiq.qparamgrupp = 2"的情况下运行查询我明白了
qdin.batchid qdin.qparamid tiq.qparamgrupp qdin.qvardenum
(03-2020 10471) 1 3 5,23
当我真的想要这个。
qdin.batchid qdin.qparamid tiq.qparamgrupp qdin.qvardenum
(03-2020 10471) 1 2 5,26
EDIT2 如果我运行这个
SELECT t.batchid, t.qparamid,
t.sqlidentity,
t.qvardenum,
tiq.qparamgrupp
FROM dbo.qdin t
INNER JOIN dbo.tiq
ON tiq.aonr = T.aonr
AND tiq.aopos = T.aopos
AND tiq.dummyuniqueid = T.conndummyuniqueid
AND tiq.qparamgrupp = 2
WHERE t.batchid = '03-2020 10471' AND t.qparamid = 1 AND tiq.qparamgrupp = 2
我明白了
batchid qpramid sqlidentity qvardenum qparamgrupp
03-2020 10471 1 32278 5.2600000 2
哪个好。但问题是当我运行另一个" batchid"并且有2条记录与#34; qparamid"和" qparamgrupp"。这就是为什么我有" max(sqlidentity)"因为我想要最后一条记录。
例如
SELECT t.batchid, t.qparamid,
t.sqlidentity,
t.qvardenum,
tiq.qparamgrupp
FROM dbo.qdin t
INNER JOIN dbo.tiq
ON tiq.aonr = T.aonr
AND tiq.aopos = T.aopos
AND tiq.dummyuniqueid = T.conndummyuniqueid
AND tiq.qparamgrupp = 2
WHERE t.batchid = '03-2020 10470' AND t.qparamid = 1 AND tiq.qparamgrupp = 2
给我这个
batchid qpramid sqlidentity qvardenum qparamgrupp
03-2020 10470 1 32112 5.2300000 2
03-2020 10470 1 32235 5.3000000 2
有没有更简单的方法或其他方式来写这个,所以我只得到
03-2020 10470 1 32235 5.3000000 2
?
提前致谢
上外
答案 0 :(得分:0)
试试这个:
SELECT t.batchid,
t.sqlidentity,
t.qvardenum,
t.kassorsakskod,
t.editint,
t.rowcreatedby,
tiq.qparamgrupp
FROM dbo.qdin t
INNER JOIN dbo.tiq
ON tiq.aonr = T.aonr
AND tiq.aopos = T.aopos
AND tiq.dummyuniqueid = T.conndummyuniqueid
AND tiq.qparamgrupp = 2
INNER JOIN (SELECT batchid,
Max(sqlidentity) AS MaxId
FROM dbo.qdin
WHERE qparamid = 1
GROUP BY batchid) tm
ON t.batchid = tm.batchid
AND t.sqlidentity = tm.maxid
答案 1 :(得分:0)
我设法解决了这个问题,只是想发布我的答案。感谢你的帮助。
SELECT t.batchid,
t.qparamid,
t.sqlidentity,
t.qvardenum,
tiq.qparamgrupp
FROM dbo.qdin t
INNER JOIN dbo.tiq
ON tiq.aonr = T.aonr
AND tiq.aopos = T.aopos
AND tiq.dummyuniqueid = T.conndummyuniqueid
WHERE
t.sqlidentity = (SELECT
Max(sqlidentity) AS MaxID
FROM dbo.qdin t
JOIN dbo.tiq
ON tiq.aonr = T.aonr
AND tiq.aopos = T.aopos
AND tiq.dummyuniqueid = T.conndummyuniqueid
WHERE t.batchid = '03-2020 10470' AND t.qparamid = 1 AND
tiq.qparamgrupp = 2)