1

我正在尝试使用一个记录集的结果作为第二个记录集结果的基础。Q1 按我试图传递给另一个查询的部分为我提供正确的总库存。(当我作为Q2的一部分直接查询总和时,总和值不正确,似乎与预测表链接有关。是否可以创建子查询或直接传递信息。(我是一个介绍用户,所以对流程不太熟悉)Q1

oRS7.open "Select i.partNo, sum(i.InventoryQuantity) as 'Inv Qty in FG/FS' from invloc i Where i.InventoryLocation IN ('FG','FS') GROUP BY i.partno, i.InventoryLocation", oConn

第二季度

oRS.open "Select p.PartNo, p.Description, [NEED THE RESULTS TO INSERT HERE], sum(m.ForecastQuantity) as 'Current Month Plan', sum(m.ForecastQuantity)/4 as 'Weekly Avg Plan', sum(m.ForecastQuantity)/30 as '1 Day Avg Plan',(case when i.InventoryQuantity < sum(m.ForecastQuantity) then 'Y' else 'N' end)as 'Below Monthly Plan',(case when i.InventoryQuantity < sum(m.ForecastQuantity/4) then 'Y' else 'N' end)as 'Below Weekly Plan',(case when i.InventoryQuantity <= sum(m.ForecastQuantity/30) then 'Y' else 'N' end)as 'Below 1 Day Plan'
from parts p, invloc i, salesforecast m Where p.PartNo = i.PartNo and p.PartNo = m.PartNo and i.InventoryLocation='FS' and m.ForecastDate between '" & Date & "' and '" & Date+27 & "' 
Group by p.partno, p.Description, i.InventoryQuantity, Order by 'Below 1 Day Plan' desc, 'Below Weekly Plan' desc, 'Below Monthly Plan' desc, p.PartNo ", oConn

原始编码需要一个库存,但现在我需要将两个库存量相加,然后再将这些值与预测的量进行比较。这是从 MS SQL Server 的 asp.net 页面上提取的

请参阅下面的原始代码(需要将 FG 设为汇总库存)

<%@ Language="VBScript" %>
<%  Option Explicit %>
<%
    Response.Expires = 0
    Server.ScriptTimeout = 500000

    dim oConn, oRS, oRS2, oRS3, oRS4, oRS5, oRS6

    Set oConn = Server.CreateObject("ADODB.Connection")

    oConn.Open "Driver={SQL Server}; Server=ABSQL21; Database=DF_WHSE; Uid=ReadOnlyUser; Pwd=bionics;"

    Set oRS = Server.CreateObject("ADODB.Recordset")

    Set oRS2 = Server.CreateObject("ADODB.Recordset")

    Set oRS3 = Server.CreateObject("ADODB.Recordset")

    Set oRS4 = Server.CreateObject("ADODB.Recordset")

    Set oRS5 = Server.CreateObject("ADODB.Recordset")

    Set oRS6 = Server.CreateObject("ADODB.Recordset")
%>
<html>

<head>
<title><%=Month(Date) & Day(Date) & Year(Date)%></title>
<style>
body  {
    font-family: "Trebuchet MS", Arial, Helvetica, san-serif;;
    font-size: 12px;
}
table {
    font-size: 11px;
    border-collapse:collapse;
}
td  {
    border-bottom: 1px #ccc solid;
    border-right: 1px #a1b0f3 dashed;
}
th  {
    border-bottom: 2px #333 solid;
    border-right: 1px #a1b0f3 dashed;
    background-color:#1790C0;
    color: #fff;
}
input  {
    border: 0px;
}
a  {
    color: #1790C0;
    text-decoration: none;
}
a:hover  {
    color: #5A6D81;
    text-decoration: underline;
}
</style>
</head>

<body>
<center>
<img src="Logo.png">
<br>

<%
    oRS.open "Select p.PartNo, p.Description, (case when i.InventoryLocation='FG' then i.InventoryQuantity else 0 end) as 'Inv Qty in FG', " &_
                "sum(m.ForecastQuantity) as 'Current Month Plan', sum(m.ForecastQuantity)/4 as 'Weekly Avg Plan', sum(m.ForecastQuantity)/30 as '1 Day Avg Plan', " &_
                "(case when i.InventoryQuantity < sum(m.ForecastQuantity) then 'Y' else 'N' end)as 'Below Monthly Plan', " &_
                "(case when i.InventoryQuantity < sum(m.ForecastQuantity/4) then 'Y' else 'N' end)as 'Below Weekly Plan', " &_
                "(case when i.InventoryQuantity <= sum(m.ForecastQuantity/30) then 'Y' else 'N' end)as 'Below 1 Day Plan' " &_
                "from parts p, invloc i, salesforecast m Where p.PartNo = i.PartNo and p.PartNo = m.PartNo and i.InventoryLocation='FG' and m.ForecastDate between '" & Date & "' and '" & Date+27 & "' " &_
                "Group by p.partno, p.Description, i.InventoryQuantity, i.InventoryLocation Order by 'Below 1 Day Plan' desc, 'Below Weekly Plan' desc, 'Below Monthly Plan' desc, p.PartNo ", oConn

    Response.Write "<table cellpadding=2 border=0 cellspacing=4>"
    Response.Write "<tr align=center>"
    Response.Write "<th width=50></td>"
    Response.Write "<th width=130><b>Finished Part No</b></td>"
    Response.Write "<th width=140><b>Purchased Part No</b></td>"
    Response.Write "<th width=200><b>Description</b></td>"
    Response.Write "<th width=100><b>FG Inventory</b></td>"
    Response.Write "<th width=250><b>Forward 4 Week MRP Demand</b></td>"
    Response.Write "<th width=200><b>Comments</b></td>"
    Response.Write "</tr>"

    IF oRS.EOF THEN
        Response.Write "<tr align=center>"
        Response.Write "<td colspan=7>No MRP Warnings Found.</td>"
        Response.Write "</tr>"
    ELSE
    END IF

    DO WHILE NOT oRS.EOF

    Select case oRS("PartNo").Value

        case "7095316-36J"
        case "7095316-37H"
        case "7095316-49H"

        case else

            IF oRS("Below Monthly Plan").Value = "Y" THEN
                IF oRS("Below Weekly Plan").Value = "Y" THEN
                    IF oRS("Below 1 Day Plan").Value = "Y" THEN
                        Response.Write "<tr align=center>"
                        Response.Write "<td width=50 bgcolor=""#FF3366"">"
                        Response.Write "<td><font style=""color: #0098DB; font-weight: bold;"">" & oRS("PartNo").Value & "</font></td>"
                        Response.Write "<td>"

                        oRS2.open "Select Component from dbo.bomli_all where BomId='" & oRS("PartNo").Value & "' and Quantity = '1.0000' Order By Component ", oConn

                        Do While Not oRS2.EOF

                            oRS3.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS2("Component").Value & "' ", oConn

                            If oRS3("MakeBuyCode").Value = "B" Then
                                Response.Write "<font style=""font-size: 10px;"">" & oRS2("Component").Value & "</font><br>"
                            Else
                                oRS4.open "Select Component from dbo.bomli_all where BomId='" & oRS2("Component").Value & "' and Quantity = '1.0000' Order By Component ", oConn
                                Do While Not oRS4.EOF
                                    oRS5.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS4("Component").Value & "' ", oConn
                                    If oRS5("MakeBuyCode").Value = "B" Then
                                        Response.Write "<font style=""font-size: 10px;"">" & oRS4("Component").Value & "</font><br>"
                                    Else
                                    End If
                                    oRS5.Close
                                oRS4.MoveNext
                                Loop
                                oRS4.Close
                            End If
                            oRS3.Close

                        oRS2.MoveNext
                        Loop
                        oRS2.Close

                        Response.Write "</td>"
                        Response.Write "<td>" & oRS("Description").Value & "</td>"
                        Response.Write "<td><font color=#FF3366><b>" & oRS("Inv Qty in FG").Value & "</b></font></td>"
                        Response.Write "<td>" & oRS("Current Month Plan").Value & "</td>"
                        Response.Write "<td><input type=text size=30></td>"
                        Response.Write "</tr>"
                    ELSE
                        Response.Write "<tr align=center>"
                        Response.Write "<td width=50 bgcolor=""#FF6600"">"
                        Response.Write "<td><font style=""color: #0098DB; font-weight: bold;"">" & oRS("PartNo").Value & "</font></td>"
                        Response.Write "<td>"

                        oRS2.open "Select Component from dbo.bomli_all where BomId='" & oRS("PartNo").Value & "' and Quantity = '1.0000' Order By Component ", oConn

                        Do While Not oRS2.EOF

                            oRS3.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS2("Component").Value & "' ", oConn

                            If oRS3("MakeBuyCode").Value = "B" Then
                                Response.Write "<font style=""font-size: 10px;"">" & oRS2("Component").Value & "</font><br>"
                            Else
                                oRS4.open "Select Component from dbo.bomli_all where BomId='" & oRS2("Component").Value & "' and Quantity = '1.0000' Order By Component ", oConn
                                Do While Not oRS4.EOF
                                    oRS5.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS4("Component").Value & "' ", oConn
                                    If oRS5("MakeBuyCode").Value = "B" Then
                                        Response.Write "<font style=""font-size: 10px;"">" & oRS4("Component").Value & "</font><br>"
                                    Else
                                    End If
                                    oRS5.Close
                                oRS4.MoveNext
                                Loop
                                oRS4.Close
                            End If
                            oRS3.Close

                        oRS2.MoveNext
                        Loop
                        oRS2.Close

                        Response.Write "</td>"
                        Response.Write "<td>" & oRS("Description").Value & "</td>"
                        Response.Write "<td><font color=#FF3366><b>" & oRS("Inv Qty in FG").Value & "</b></font></td>"
                        Response.Write "<td>" & oRS("Current Month Plan").Value & "</td>"
                        Response.Write "<td><input type=text size=30></td>"
                        Response.Write "</tr>"
                    END IF
                ELSE
                    Response.Write "<tr align=center>"
                    Response.Write "<td width=50 bgcolor=""#FFFF66"">"
                    Response.Write "<td><font style=""color: #0098DB; font-weight: bold;"">" & oRS("PartNo").Value & "</font></td>"
                    Response.Write "<td>"

                        oRS2.open "Select Component from dbo.bomli_all where BomId='" & oRS("PartNo").Value & "' and Quantity = '1.0000' Order By Component ", oConn

                        Do While Not oRS2.EOF

                            oRS3.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS2("Component").Value & "' ", oConn

                            If oRS3("MakeBuyCode").Value = "B" Then
                                Response.Write "<font style=""font-size: 10px;"">" & oRS2("Component").Value & "</font><br>"
                            Else
                                oRS4.open "Select Component from dbo.bomli_all where BomId='" & oRS2("Component").Value & "' and Quantity = '1.0000' Order By Component ", oConn
                                Do While Not oRS4.EOF
                                    oRS5.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS4("Component").Value & "' ", oConn
                                    If oRS5("MakeBuyCode").Value = "B" Then
                                        Response.Write "<font style=""font-size: 10px;"">" & oRS4("Component").Value & "</font><br>"
                                    Else
                                    End If
                                    oRS5.Close
                                oRS4.MoveNext
                                Loop
                                oRS4.Close
                            End If
                            oRS3.Close

                        oRS2.MoveNext
                        Loop
                        oRS2.Close

                    Response.Write "</td>"
                    Response.Write "<td>" & oRS("Description").Value & "</td>"
                    Response.Write "<td><font color=#FF3366><b>" & oRS("Inv Qty in FG").Value & "</b></font></td>"
                    Response.Write "<td>" & oRS("Current Month Plan").Value & "</td>"
                    Response.Write "<td><input type=text size=30></td>"
                    Response.Write "</tr>"
                END IF
            ELSE
            END IF

    End select

    oRS.MoveNext
    Loop

    Response.Write "</table><br>"

    oRS.Close

    oRS.open "Select p.PartNo, p.Description, " &_
                "(case when i.InventoryLocation='FG' then i.InventoryQuantity else 0 end)as 'Inv Qty in FG', " &_
                "sum(p.OrderPoint)as '4 Weeks Safety Stock', " &_
                "sum(p.OrderPoint/4)as '1 Week Safety Stock', " &_
                "sum(p.OrderPoint/4/30)as '1 Day Safety Stock', " &_
                "(case when i.InventoryQuantity < sum(p.OrderPoint) then 'Y' else 'N' end)as 'Below Monthly Safety Stock', " &_
                "(case when i.InventoryQuantity < sum(p.OrderPoint/4) then 'Y' else 'N' end)as 'Below Weekly Safety Stock', " &_
                "(case when i.InventoryQuantity < sum(p.OrderPoint/30) then 'Y' else 'N' end)as 'Below 1 Safety Stock' " &_
                "from parts p, invloc i " &_
                "Where p.PartNo = i.PartNo " &_
                "and i.InventoryLocation='FG' " &_
                "and p.PlanningType='OPN' " &_
                "and p.OrderPoint > i.InventoryQuantity " &_
                "Group by p.partno, p.Description, i.InventoryQuantity, i.InventoryLocation " &_
                "Order by 'Below Monthly Safety Stock' desc, 'Below Weekly Safety Stock' desc, 'Below 1 Safety Stock' desc, p.PartNo", oConn



    IF oRS.EOF THEN
    ELSE
        Response.Write "<table cellpadding=2 border=0 cellspacing=4>"
        Response.Write "<tr align=center>"
        Response.Write "<th width=50></td>"
        Response.Write "<th width=130></td>"
        Response.Write "<th width=140></td>"
        Response.Write "<th width=200></td>"
        Response.Write "<th width=100></td>"
        Response.Write "<th width=250><b>Order Point Level:</b></td>"
        Response.Write "<th width=200></td>"
        Response.Write "</tr>"
    END IF

    DO WHILE NOT oRS.EOF


    Select case oRS("PartNo").Value

    case "6015213-001"
    case "5060050008"
    case "8044213-001"
    case "9050050011"
    case "5023015-001"
    case else

        IF oRS("Below Monthly Safety Stock").Value = "Y" THEN
                IF oRS("Below Weekly Safety Stock").Value = "Y" THEN
                    IF oRS("Below 1 Safety Stock").Value = "Y" THEN
                        Response.Write "<tr align=center>"
                        Response.Write "<td width=50 bgcolor=""#FF3366"">"
                        Response.Write "<td><font style=""color: #0098DB; font-weight: bold;"">" & oRS("PartNo").Value & "</font></td>"
                        Response.Write "<td>"

                        oRS2.open "Select Component from dbo.bomli_all where BomId='" & oRS("PartNo").Value & "' and Quantity = '1.0000' Order By Component ", oConn

                        Do While Not oRS2.EOF

                            oRS3.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS2("Component").Value & "' ", oConn

                            If oRS3("MakeBuyCode").Value = "B" Then
                                Response.Write "<font style=""font-size: 10px;"">" & oRS2("Component").Value & "</font><br>"
                            Else
                                oRS4.open "Select Component from dbo.bomli_all where BomId='" & oRS2("Component").Value & "' and Quantity = '1.0000' Order By Component ", oConn
                                Do While Not oRS4.EOF
                                    oRS5.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS4("Component").Value & "' ", oConn
                                    If oRS5("MakeBuyCode").Value = "B" Then
                                        Response.Write "<font style=""font-size: 10px;"">" & oRS4("Component").Value & "</font><br>"
                                    Else
                                    End If
                                    oRS5.Close
                                oRS4.MoveNext
                                Loop
                                oRS4.Close
                            End If
                            oRS3.Close

                        oRS2.MoveNext
                        Loop
                        oRS2.Close

                        Response.Write "</td>"
                        Response.Write "<td>" & oRS("Description").Value & "</td>"
                        Response.Write "<td><font color=#FF3366><b>" & oRS("Inv Qty in FG").Value & "</b></font></td>"
                        Response.Write "<td>" & oRS("4 Weeks Safety Stock").Value & "</td>"
                        Response.Write "<td><input type=text size=30></td>"
                        Response.Write "</tr>"
                    ELSE
                        Response.Write "<tr align=center>"
                        Response.Write "<td width=50 bgcolor=""#FF6600"">"
                        Response.Write "<td><font style=""color: #0098DB; font-weight: bold;"">" & oRS("PartNo").Value & "</font></td>"
                        Response.Write "<td>"

                        oRS2.open "Select Component from dbo.bomli_all where BomId='" & oRS("PartNo").Value & "' and Quantity = '1.0000' Order By Component ", oConn

                        Do While Not oRS2.EOF

                            oRS3.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS2("Component").Value & "' ", oConn

                            If oRS3("MakeBuyCode").Value = "B" Then
                                Response.Write "<font style=""font-size: 10px;"">" & oRS2("Component").Value & "</font><br>"
                            Else
                                oRS4.open "Select Component from dbo.bomli_all where BomId='" & oRS2("Component").Value & "' and Quantity = '1.0000' Order By Component ", oConn
                                Do While Not oRS4.EOF
                                    oRS5.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS4("Component").Value & "' ", oConn
                                    If oRS5("MakeBuyCode").Value = "B" Then
                                        Response.Write "<font style=""font-size: 10px;"">" & oRS4("Component").Value & "</font><br>"
                                    Else
                                    End If
                                    oRS5.Close
                                oRS4.MoveNext
                                Loop
                                oRS4.Close
                            End If
                            oRS3.Close

                        oRS2.MoveNext
                        Loop
                        oRS2.Close

                        Response.Write "</td>"
                        Response.Write "<td>" & oRS("Description").Value & "</td>"
                        Response.Write "<td><font color=#FF3366><b>" & oRS("Inv Qty in FG").Value & "</b></font></td>"
                        Response.Write "<td>" & oRS("4 Weeks Safety Stock").Value & "</td>"
                        Response.Write "<td><input type=text size=30></td>"
                        Response.Write "</tr>"
                    END IF
                ELSE
                    Response.Write "<tr align=center>"
                    Response.Write "<td width=50 bgcolor=""#FFFF66"">"
                    Response.Write "<td><font style=""color: #0098DB; font-weight: bold;"">" & oRS("PartNo").Value & "</font></td>"
                    Response.Write "<td>"

                        oRS2.open "Select Component from dbo.bomli_all where BomId='" & oRS("PartNo").Value & "' and Quantity = '1.0000' Order By Component ", oConn

                        Do While Not oRS2.EOF

                            oRS3.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS2("Component").Value & "' ", oConn

                            If oRS3("MakeBuyCode").Value = "B" Then
                                Response.Write "<font style=""font-size: 10px;"">" & oRS2("Component").Value & "</font><br>"
                            Else
                                oRS4.open "Select Component from dbo.bomli_all where BomId='" & oRS2("Component").Value & "' and Quantity = '1.0000' Order By Component ", oConn
                                Do While Not oRS4.EOF
                                    oRS5.Open "Select MakeBuyCode from dbo.parts where PartNo='" & oRS4("Component").Value & "' ", oConn
                                    If oRS5("MakeBuyCode").Value = "B" Then
                                        Response.Write "<font style=""font-size: 10px;"">" & oRS4("Component").Value & "</font><br>"
                                    Else
                                    End If
                                    oRS5.Close
                                oRS4.MoveNext
                                Loop
                                oRS4.Close
                            End If
                            oRS3.Close

                        oRS2.MoveNext
                        Loop
                        oRS2.Close

                    Response.Write "</td>"
                    Response.Write "<td>" & oRS("Description").Value & "</td>"
                    Response.Write "<td><font color=#FF3366><b>" & oRS("Inv Qty in FG").Value & "</b></font></td>"
                    Response.Write "<td>" & oRS("4 Weeks Safety Stock").Value & "</td>"
                    Response.Write "<td><input type=text size=30></td>"
                    Response.Write "</tr>"
                END IF
            ELSE
            END IF

    End select

    oRS.MoveNext
    Loop

    IF oRS.EOF THEN
    ELSE
        Response.Write "</table>"
    END IF

    oRS.Close


    Set oRS = Nothing
    oConn.Close
    Set oConn = Nothing

%>

</body>

</html>

`

4

2 回答 2

0

如果我只处理 SQL 部分:

你想混合这个:

Select i.partNo, sum(i.InventoryQuantity) as 'Inv Qty in FG/FS' 
from invloc i 
Where i.InventoryLocation IN ('FG','FS') 
GROUP BY i.partno, i.InventoryLocation

有了这个:

Select p.PartNo, p.Description, 
[NEED THE RESULTS TO INSERT HERE],  sum(m.ForecastQuantity) as 'Current Month Plan',
sum(m.ForecastQuantity)/4 as 'Weekly Avg Plan', 
sum(m.ForecastQuantity)/30 as '1 Day Avg Plan',
(case when i.InventoryQuantity < sum(m.ForecastQuantity) then 'Y' else 'N' end) as 'Below Monthly Plan',
(case when i.InventoryQuantity < sum(m.ForecastQuantity/4) then 'Y' else 'N' end)as 'Below Weekly Plan',
(case when i.InventoryQuantity <= sum(m.ForecastQuantity/30) then 'Y' else 'N' end)as 'Below 1 Day Plan'
from parts p, invloc i, salesforecast m 
Where p.PartNo = i.PartNo 
and p.PartNo = m.PartNo 
and i.InventoryLocation='FS' 
and m.ForecastDate between '" & Date & "' and '" & Date+27 & "' 
Group by p.partno, p.Description, i.InventoryQuantity
Order by 'Below 1 Day Plan' desc, 'Below Weekly Plan' desc, 
'Below Monthly Plan' desc, p.PartNo

您将第一个查询添加为子表并将其加入,但您需要注意重复计算问题:

Select p.PartNo, p.Description, 
-- Here's your column
IQ.[Inv Qty in FG/FS],  
------------------------
sum(m.ForecastQuantity) as 'Current Month Plan',
sum(m.ForecastQuantity)/4 as 'Weekly Avg Plan', 
sum(m.ForecastQuantity)/30 as '1 Day Avg Plan',
(case when i.InventoryQuantity < sum(m.ForecastQuantity) then 'Y' else 'N' end) as 'Below Monthly Plan',
(case when i.InventoryQuantity < sum(m.ForecastQuantity/4) then 'Y' else 'N' end)as 'Below Weekly Plan',
(case when i.InventoryQuantity <= sum(m.ForecastQuantity/30) then 'Y' else 'N' end)as 'Below 1 Day Plan'
from parts p, invloc i, salesforecast m ,
-- Here's your query from above, aliased as IQ
(
Select i.partNo, sum(i.InventoryQuantity) as 'Inv Qty in FG/FS' 
from invloc i 
Where i.InventoryLocation IN ('FG','FS') 
GROUP BY i.partno
) IQ
----------------------------
Where p.PartNo = i.PartNo 
and p.PartNo = m.PartNo 
and i.InventoryLocation='FS' 
and m.ForecastDate between '" & Date & "' and '" & Date+27 & "' 
--- Here is where we join
and IQ.partNo = p.partNo
Group by p.partno, p.Description, i.InventoryQuantity, 
-- don't forget to group by
IQ.[Inv Qty in FG/FS]
Order by 'Below 1 Day Plan' desc, 'Below Weekly Plan' desc, 
'Below Monthly Plan' desc, p.PartNo
于 2012-11-16T00:25:29.097 回答
0

这不能回答您的确切问题,但可能有助于解决一些加入问题。

注意: 这包含该行

AND m.liid = ????

预测表可能需要在此列上进行过滤或连接才能正确唯一。(这取决于数据)。在尝试关联实际值和预算时,这是一个常见问题。我希望这会有所帮助 - 回帖,我会进一步提供帮助。我怀疑如果我们使用 SQLFiddle 这会容易得多!

    SELECT p.PartNo, p.Description, 
    IQ.FG_Qty, Iq.FS_Qty,
    sum(m.ForecastQuantity) as 'Current Month Plan',
    sum(m.ForecastQuantity)/4 as 'Weekly Avg Plan', 
    sum(m.ForecastQuantity)/30 as '1 Day Avg Plan'
    FROM
    -- parts is unique on partNo
    parts p
    -- salesforecast is unique on partno,liid
    INNER JOIN
    salesforecast m
    ON P.partNo = m.partNo
    AND m.liid = ????
    INNER JOIN
    (
    -- The subtable IQ is unique on partNo
    Select 
    i.partNo, 
    sum(case when i.InventoryLocation = 'FG' then i.InventoryQuantity else 0 end) as [FG_Qty], 
    sum(case when i.InventoryLocation = 'FS' then i.InventoryQuantity else 0 end) as [FS_Qty]
    from invloc i 
    Where i.InventoryLocation IN ('FG','FS') 
    GROUP BY i.partno
    ) IQ
    ON p.PartNo = IQ.PartNo 
    AND m.ForecastDate between '" & Date & "' and '" & Date+27 & "' 
    --- Here is where we join
    GROUP BY p.partno, p.Description, IQ.FG_Qty, Iq.FS_Qty
于 2012-12-03T01:00:15.433 回答