使用连接语句连接子查询
Joining a subquery with a join statement
正在尝试将子查询应用于 table,以便能够对其进行分组。
主查询
SELECT soitem.fsono, fpartno, soitem.fcfromno, finumber
FROM soitem
INNER JOIN somast on soitem.fsono = somast.fsono
WHERE somast.fsono='034313'
ORDER by soitem.fsono, soitem.finumber
子查询
SELECT min(sq.finumber), sq.fcfromno
FROM soitem sq
WHERE sq.fsono='034313'
GROUP BY sq.fcfromno
我想使用 min_finumber
进一步分组我的数据集。这应该显示为三行。我使用我的原始问题作为将子查询合并到我的整体代码中的方式。我的主要目标是能够成功地将 min_number
SELECT somast.fsono,
somast.fcustno,
somast.fcontact,
somast.fcustpono,
somast.fshipvia,
somast.forderdate,
somast.fduedate,
soship.fccompany,
soship.fcphone,
REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' ') AS Street,
soship.fccity,
soship.fcstate,
soship.fczip,
CAST(somast.fackmemo as CHAR(35)) as ShipCode,
somast.fordername,
somast.fcusrchr2,
somast.fcusrchr3,
somast.fcusrchr1,
somast.festimator,
soitem.fcfromno,
soitem.fcfromtype,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fpartno ELSE soitem.fcfromno END AS Item,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fquantity ELSE soitem.fquantity / soitem.fquoteqty END AS Qty,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END as Descr
FROM soitem INNER JOIN
somast ON soitem.fsono = somast.fsono LEFT OUTER JOIN
soship ON somast.fsono = soship.fcsono AND soship.fcenumber = ''
WHERE
(somast.fstatus <> 'Cancelled') AND (somast.fsocoord = 'IFP' OR
somast.fsocoord = '711')
Group By REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' '),
CAST(somast.fackmemo as CHAR(35)),
somast.fsono,
somast.fcustno,
fcontact,
fcustpono,
fshipvia,
forderdate,
somast.fduedate,
CASE WHEN soitem.fcfromtype<>'IFPKIT'
THEN soitem.fpartno
ELSE soitem.fcfromno END,
CASE WHEN soitem.fcfromtype<>'IFPKIT'
THEN soitem.fquantity
ELSE soitem.fquantity/soitem.fquoteqty END,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END,
soship.fccity,
soship.fcstate,
soship.fczip,
fordername,
fcusrchr2,
fcusrchr3,
fcusrchr1,
festimator,
soitem.fcfromno,
soitem.fcfromtype,
soship.fccompany,
soship.fcphone
ORDER BY somast.forderdate, somast.fsono, soitem.fcfromno
由于您使用的是 SQL Server 2008 R2,因此您可以访问所需的 window 功能。尝试类似的东西:
SQL Server 2017 架构设置:
CREATE TABLE soitem
(
fsono INT,
fpartno INT,
soitem INT,
finumber INT,
fcfromno INT
);
INSERT INTO soitem (fsono, fpartno, soitem, finumber, fcfromno)
VALUES (1, 1, 1, 1, 1), (1, 2, 2, 5, 1),
(2, 2, 2, 9, 2), (2, 2, 2, 2, 2),
(3, 2, 2, 1, 9);
CREATE TABLE somast (fsono int) ;
INSERT INTO somast (fsono)
VALUES (1), (2);
主查询:
SELECT
soitem.fsono,
soitem.fpartno,
soitem.fcfromno,
soitem.finumber,
/* THE BELOW PART IS YOUR "JOIN" */
MIN(soitem.finumber) OVER (PARTITION BY soitem.fsono, soitem.fcfromno) AS min_finumber
FROM
soitem
INNER JOIN
somast ON soitem.fsono = somast.fsono
WHERE
somast.fsono = '1'
ORDER BY
soitem.fsono, soitem.finumber
| fsono | fpartno | fcfromno | finumber | min_finumber |
|-------|---------|----------|----------|--------------|
| 1 | 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 5 | 1 |
您的主要查询:
SELECT soitem.fsono, fpartno, soitem.fcfromno, finumber
FROM soitem
INNER JOIN somast on soitem.fsono = somast.fsono
WHERE somast.fsono='1'
ORDER by soitem.fsono, soitem.finumber
| fsono | fpartno | fcfromno | finumber |
|-------|---------|----------|----------|
| 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 5 |
你的子查询:
SELECT min(sq.finumber), sq.fcfromno
FROM soitem sq
WHERE sq.fsono='1'
GROUP BY sq.fcfromno
| | fcfromno |
|---|----------|
| 1 | 1 |
我最终没有使用连接或子查询。我能够通过将 min(finumber)
作为 min_fin 添加到 SELECT
和 ORDER BY
来获得我需要的结果
select soitem.fsono,
somast.fcustno,
somast.fcontact,
somast.fcustpono,
somast.fshipvia,
somast.forderdate,
somast.fduedate,
soship.fccompany,
soship.fcphone,
REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' ') AS Street,
soship.fccity,
soship.fcstate,
soship.fczip,
CAST(somast.fackmemo as CHAR(35)) as ShipCode,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fpartno ELSE soitem.fcfromno END AS Item,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fquantity ELSE soitem.fquantity / soitem.fquoteqty END AS Qty,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END as Descr,
somast.fordername,
somast.fcusrchr2,
somast.fcusrchr3,
somast.fcusrchr1,
somast.festimator,
soitem.fcfromno,
soitem.fcfromtype,
**min(finumber)as min_fin**
from soitem INNER JOIN
somast ON soitem.fsono = somast.fsono
LEFT OUTER JOIN
soship ON somast.fsono = soship.fcsono AND soship.fcenumber = ''
where soitem.fsono='034313'
group by soitem.fsono, CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fpartno ELSE soitem.fcfromno END,
soitem.fcfromno, soitem.fcfromtype,
somast.fcustno,
somast.fcontact,
somast.fcustpono,
somast.fshipvia,
somast.forderdate,
somast.fduedate,
soship.fccompany,
soship.fcphone,
REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' '),
soship.fccity,
soship.fcstate,
soship.fczip,
CAST(somast.fackmemo as CHAR(35)),
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fquantity ELSE soitem.fquantity / soitem.fquoteqty END ,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END,
somast.fordername,
somast.fcusrchr2,
somast.fcusrchr3,
somast.fcusrchr1,
somast.festimator
order by soitem.fsono, **min_fin**;
正在尝试将子查询应用于 table,以便能够对其进行分组。
主查询
SELECT soitem.fsono, fpartno, soitem.fcfromno, finumber
FROM soitem
INNER JOIN somast on soitem.fsono = somast.fsono
WHERE somast.fsono='034313'
ORDER by soitem.fsono, soitem.finumber
子查询
SELECT min(sq.finumber), sq.fcfromno
FROM soitem sq
WHERE sq.fsono='034313'
GROUP BY sq.fcfromno
我想使用 min_finumber
进一步分组我的数据集。这应该显示为三行。我使用我的原始问题作为将子查询合并到我的整体代码中的方式。我的主要目标是能够成功地将 min_number
SELECT somast.fsono,
somast.fcustno,
somast.fcontact,
somast.fcustpono,
somast.fshipvia,
somast.forderdate,
somast.fduedate,
soship.fccompany,
soship.fcphone,
REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' ') AS Street,
soship.fccity,
soship.fcstate,
soship.fczip,
CAST(somast.fackmemo as CHAR(35)) as ShipCode,
somast.fordername,
somast.fcusrchr2,
somast.fcusrchr3,
somast.fcusrchr1,
somast.festimator,
soitem.fcfromno,
soitem.fcfromtype,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fpartno ELSE soitem.fcfromno END AS Item,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fquantity ELSE soitem.fquantity / soitem.fquoteqty END AS Qty,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END as Descr
FROM soitem INNER JOIN
somast ON soitem.fsono = somast.fsono LEFT OUTER JOIN
soship ON somast.fsono = soship.fcsono AND soship.fcenumber = ''
WHERE
(somast.fstatus <> 'Cancelled') AND (somast.fsocoord = 'IFP' OR
somast.fsocoord = '711')
Group By REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' '),
CAST(somast.fackmemo as CHAR(35)),
somast.fsono,
somast.fcustno,
fcontact,
fcustpono,
fshipvia,
forderdate,
somast.fduedate,
CASE WHEN soitem.fcfromtype<>'IFPKIT'
THEN soitem.fpartno
ELSE soitem.fcfromno END,
CASE WHEN soitem.fcfromtype<>'IFPKIT'
THEN soitem.fquantity
ELSE soitem.fquantity/soitem.fquoteqty END,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END,
soship.fccity,
soship.fcstate,
soship.fczip,
fordername,
fcusrchr2,
fcusrchr3,
fcusrchr1,
festimator,
soitem.fcfromno,
soitem.fcfromtype,
soship.fccompany,
soship.fcphone
ORDER BY somast.forderdate, somast.fsono, soitem.fcfromno
由于您使用的是 SQL Server 2008 R2,因此您可以访问所需的 window 功能。尝试类似的东西:
SQL Server 2017 架构设置:
CREATE TABLE soitem
(
fsono INT,
fpartno INT,
soitem INT,
finumber INT,
fcfromno INT
);
INSERT INTO soitem (fsono, fpartno, soitem, finumber, fcfromno)
VALUES (1, 1, 1, 1, 1), (1, 2, 2, 5, 1),
(2, 2, 2, 9, 2), (2, 2, 2, 2, 2),
(3, 2, 2, 1, 9);
CREATE TABLE somast (fsono int) ;
INSERT INTO somast (fsono)
VALUES (1), (2);
主查询:
SELECT
soitem.fsono,
soitem.fpartno,
soitem.fcfromno,
soitem.finumber,
/* THE BELOW PART IS YOUR "JOIN" */
MIN(soitem.finumber) OVER (PARTITION BY soitem.fsono, soitem.fcfromno) AS min_finumber
FROM
soitem
INNER JOIN
somast ON soitem.fsono = somast.fsono
WHERE
somast.fsono = '1'
ORDER BY
soitem.fsono, soitem.finumber
| fsono | fpartno | fcfromno | finumber | min_finumber |
|-------|---------|----------|----------|--------------|
| 1 | 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 5 | 1 |
您的主要查询:
SELECT soitem.fsono, fpartno, soitem.fcfromno, finumber
FROM soitem
INNER JOIN somast on soitem.fsono = somast.fsono
WHERE somast.fsono='1'
ORDER by soitem.fsono, soitem.finumber
| fsono | fpartno | fcfromno | finumber |
|-------|---------|----------|----------|
| 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 5 |
你的子查询:
SELECT min(sq.finumber), sq.fcfromno
FROM soitem sq
WHERE sq.fsono='1'
GROUP BY sq.fcfromno
| | fcfromno |
|---|----------|
| 1 | 1 |
我最终没有使用连接或子查询。我能够通过将 min(finumber)
作为 min_fin 添加到 SELECT
和 ORDER BY
select soitem.fsono,
somast.fcustno,
somast.fcontact,
somast.fcustpono,
somast.fshipvia,
somast.forderdate,
somast.fduedate,
soship.fccompany,
soship.fcphone,
REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' ') AS Street,
soship.fccity,
soship.fcstate,
soship.fczip,
CAST(somast.fackmemo as CHAR(35)) as ShipCode,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fpartno ELSE soitem.fcfromno END AS Item,
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fquantity ELSE soitem.fquantity / soitem.fquoteqty END AS Qty,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END as Descr,
somast.fordername,
somast.fcusrchr2,
somast.fcusrchr3,
somast.fcusrchr1,
somast.festimator,
soitem.fcfromno,
soitem.fcfromtype,
**min(finumber)as min_fin**
from soitem INNER JOIN
somast ON soitem.fsono = somast.fsono
LEFT OUTER JOIN
soship ON somast.fsono = soship.fcsono AND soship.fcenumber = ''
where soitem.fsono='034313'
group by soitem.fsono, CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fpartno ELSE soitem.fcfromno END,
soitem.fcfromno, soitem.fcfromtype,
somast.fcustno,
somast.fcontact,
somast.fcustpono,
somast.fshipvia,
somast.forderdate,
somast.fduedate,
soship.fccompany,
soship.fcphone,
REPLACE(REPLACE(CONVERT(VARCHAR(MAX), soship.fmstreet), CHAR(13), '|'), CHAR(10), ' '),
soship.fccity,
soship.fcstate,
soship.fczip,
CAST(somast.fackmemo as CHAR(35)),
CASE WHEN soitem.fcfromtype <> 'IFPKIT' THEN soitem.fquantity ELSE soitem.fquantity / soitem.fquoteqty END ,
CASE WHEN soitem.fcfromtype <> 'IFPKIT'
THEN CAST(soitem.fdesc as CHAR(35))
ELSE 'Store Set (Phantom)' END,
somast.fordername,
somast.fcusrchr2,
somast.fcusrchr3,
somast.fcusrchr1,
somast.festimator
order by soitem.fsono, **min_fin**;