使用join语句加入子查询
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用join语句加入子查询相关的知识,希望对你有一定的参考价值。
尝试将子查询应用于表以便能够对其进行分组。
主要查询
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,因此您可以访问所需的窗口功能。尝试类似的东西:
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
and 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**;
以上是关于使用join语句加入子查询的主要内容,如果未能解决你的问题,请参考以下文章