我如何在 INNER JOIN 查询中使用 Distinct
how i can use Distinct in Query with INNER JOIN
我想查询我的产品和因素以及要显示的因素项目。
在此查询中,我为用户显示了来自因素的报告。
我的查询是:
WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY FactoriTems.datesave ASC) AS rn,
FactoriTems.code, FactoriTems.replacement,
FactoriTems.suggcode, Factors.dateexport,
Productions.descriptions,
FactoriTems.countt, FactoriTems.price,
FactoriTems.countt * FactoriTems.price AS 'total',
Productions.country
FROM
Productions
INNER JOIN
FactoriTems ON Productions.code = FactoriTems.code
INNER JOIN
Factors ON Factors.gid = FactoriTems.gid
WHERE
(FactoriTems.gid = @gid)
)
SELECT *
FROM CTE
ORDER BY rn
这个查询是可以的,但是有问题,在Productions
table有些分类有两个或三个产品一个代码,当这个代码在factoritems
table 在我的结果集中显示两行!
结果是:
rn
code
dateexport
descriptions
countt
price
total
1
aaa
12/24/2021
...
100
2
200
2
bbb
12/24/2021
...
200
3
600
3
ccc
12/24/2021
...
100
2
200
4
ddd
12/24/2021
...
200
3
600
5
ddd
12/24/2021
...
100
2
200
6
eee
12/24/2021
...
200
3
600
现在如何才能只显示一行 'ddd'
产品代码?
我尝试使用 DISTINCT
但出现错误。
我希望输出如下:
rn
code
dateexport
descriptions
countt
price
total
1
aaa
12/24/2021
...
100
2
200
2
bbb
12/24/2021
...
200
3
600
3
ccc
12/24/2021
...
100
2
200
4
ddd
12/24/2021
...
200
3
600
5
eee
12/24/2021
...
200
3
600
谢谢
试试这个代码:
WITH CTE AS
(SELECT ROW_Number() over (PARTITION BY FactoriTems.code ORDER by FactoriTems.datesave ASC) as rn,FactoriTems.code,
FactoriTems.replacement, FactoriTems.suggcode,Factors.dateexport,
Productions.descriptions, FactoriTems.countt, FactoriTems.price,
FactoriTems.countt * FactoriTems.price AS 'total', Productions.country
FROM Productions
INNER JOIN FactoriTems ON Productions.code = FactoriTems.code
INNER JOIN Factors ON Factors.gid = FactoriTems.gid
WHERE (FactoriTems.gid = @gid))
SELECT * FROM CTE
WHERE CTE.rn = 1
ORDER BY rn
只是缺少内部查询中 code
列的 PARTITIN BY
子句。重写您当前的查询,例如
WITH CTE AS
(SELECT ROW_NUMBER() OVER (PARTITION BY ft.code ORDER BY ft.datesave) AS rn0,
ft.code,
ft.replacement,
ft.suggcode,
f.dateexport,
p.descriptions,
ft.countt,
ft.price,
ft.countt * ft.price AS total,
p.country
FROM Productions p
JOIN FactoriTems ft
ON p.code = ft.code
JOIN Factors f
ON f.gid = ft.gid
WHERE (ft.gid = @gid))
SELECT ROW_NUMBER() OVER (ORDER BY datesave,code) AS rn,
code, replacement, suggcode, dateexport, descriptions,
countt, price, total, country
FROM CTE
WHERE rn0 = 1
ORDER BY rn
为了过滤掉每个不同分组的重复项code
我想查询我的产品和因素以及要显示的因素项目。
在此查询中,我为用户显示了来自因素的报告。
我的查询是:
WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (ORDER BY FactoriTems.datesave ASC) AS rn,
FactoriTems.code, FactoriTems.replacement,
FactoriTems.suggcode, Factors.dateexport,
Productions.descriptions,
FactoriTems.countt, FactoriTems.price,
FactoriTems.countt * FactoriTems.price AS 'total',
Productions.country
FROM
Productions
INNER JOIN
FactoriTems ON Productions.code = FactoriTems.code
INNER JOIN
Factors ON Factors.gid = FactoriTems.gid
WHERE
(FactoriTems.gid = @gid)
)
SELECT *
FROM CTE
ORDER BY rn
这个查询是可以的,但是有问题,在Productions
table有些分类有两个或三个产品一个代码,当这个代码在factoritems
table 在我的结果集中显示两行!
结果是:
rn | code | dateexport | descriptions | countt | price | total |
---|---|---|---|---|---|---|
1 | aaa | 12/24/2021 | ... | 100 | 2 | 200 |
2 | bbb | 12/24/2021 | ... | 200 | 3 | 600 |
3 | ccc | 12/24/2021 | ... | 100 | 2 | 200 |
4 | ddd | 12/24/2021 | ... | 200 | 3 | 600 |
5 | ddd | 12/24/2021 | ... | 100 | 2 | 200 |
6 | eee | 12/24/2021 | ... | 200 | 3 | 600 |
现在如何才能只显示一行 'ddd'
产品代码?
我尝试使用 DISTINCT
但出现错误。
我希望输出如下:
rn | code | dateexport | descriptions | countt | price | total |
---|---|---|---|---|---|---|
1 | aaa | 12/24/2021 | ... | 100 | 2 | 200 |
2 | bbb | 12/24/2021 | ... | 200 | 3 | 600 |
3 | ccc | 12/24/2021 | ... | 100 | 2 | 200 |
4 | ddd | 12/24/2021 | ... | 200 | 3 | 600 |
5 | eee | 12/24/2021 | ... | 200 | 3 | 600 |
谢谢
试试这个代码:
WITH CTE AS
(SELECT ROW_Number() over (PARTITION BY FactoriTems.code ORDER by FactoriTems.datesave ASC) as rn,FactoriTems.code,
FactoriTems.replacement, FactoriTems.suggcode,Factors.dateexport,
Productions.descriptions, FactoriTems.countt, FactoriTems.price,
FactoriTems.countt * FactoriTems.price AS 'total', Productions.country
FROM Productions
INNER JOIN FactoriTems ON Productions.code = FactoriTems.code
INNER JOIN Factors ON Factors.gid = FactoriTems.gid
WHERE (FactoriTems.gid = @gid))
SELECT * FROM CTE
WHERE CTE.rn = 1
ORDER BY rn
只是缺少内部查询中 code
列的 PARTITIN BY
子句。重写您当前的查询,例如
WITH CTE AS
(SELECT ROW_NUMBER() OVER (PARTITION BY ft.code ORDER BY ft.datesave) AS rn0,
ft.code,
ft.replacement,
ft.suggcode,
f.dateexport,
p.descriptions,
ft.countt,
ft.price,
ft.countt * ft.price AS total,
p.country
FROM Productions p
JOIN FactoriTems ft
ON p.code = ft.code
JOIN Factors f
ON f.gid = ft.gid
WHERE (ft.gid = @gid))
SELECT ROW_NUMBER() OVER (ORDER BY datesave,code) AS rn,
code, replacement, suggcode, dateexport, descriptions,
countt, price, total, country
FROM CTE
WHERE rn0 = 1
ORDER BY rn
为了过滤掉每个不同分组的重复项code