尝试将子查询应用于表,以便能够对其进行分组。
主要查询
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
答案 0 :(得分:0)
由于使用的是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
Results :
| 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
Results :
| | fcfromno |
|---|----------|
| 1 | 1 |
答案 1 :(得分:0)
我最终没有使用联接或子查询。通过将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**;