Home Forums SQL Server 7,2000 SQL Server Newbies attempting to recreate complex "multi-layer" Access queries in SSMS RE: attempting to recreate complex "multi-layer" Access queries in SSMS

  • Access lets you define a calculated column in a query and then use the calculated column name in other calculated columns. SQL Server will not allow this. In your calculation for [crateHC] you will have to replace [SumOfqtyReceived] with (SUM(qry_13winterPlant.qtyReceived)), and replace [TotalAC] with (qry_strawRanchTotAc.SumOfacres). And do the same where [SumOfqtyReceived] and [TotalAC] are used in the GROUP BY clause.

    Also, I hope (SUM(qry_13winterPlant.qtyReceived)) and (qry_strawRanchTotAc.SumOfacres) can never be zero since you are dividing by them.

    SELECT tlkpCompany.companyName

    , tblCycleRanch.hoops

    , tblCycleRanch.ranchNumber

    , tbNHC.[00002]

    , tbNHC.[30]

    , tbNHC.[40]

    , tbNHC.[60]

    , SUM(qry_13winterPlant.qtyReceived) AS SumOfqtyReceived

    , ( [30] + [40] + ( 0.6 * [60] ) ) / ( SUM(qry_13winterPlant.qtyReceived) / qry_strawRanchTotAc.SumOfacres ) AS crateHC

    , qry_strawRanchTotAc.SumOfacres AS TotalAC

    , tbNHC.Date

    FROM tlkpCompany

    INNER JOIN tblCycleRanch

    ON tlkpCompany.[companyNum] = tblCycleRanch.[companyNum]

    INNER JOIN qry_13winterPlant

    ON tblCycleRanch.ranchNumber = qry_13winterPlant.ranchNumber

    INNER JOIN qry_strawRanchTotAc

    ON tblCycleRanch.ranchNumber = qry_strawRanchTotAc.ranchNumber

    INNER JOIN tbNHC

    ON tblCycleRanch.ranchNumber = tbNHC.ranchNumber

    GROUP BY tlkpCompany.companyName

    , tblCycleRanch.hoops

    , tblCycleRanch.ranchNumber

    , tbNHC.[00002]

    , tbNHC.[30]

    , tbNHC.[40]

    , tbNHC.[60]

    , ( [30] + [40] + ( 0.6 * [60] ) ) / ( SUM(qry_13winterPlant.qtyReceived) / qry_strawRanchTotAc.SumOfacres )

    , qry_strawRanchTotAc.SumOfacres

    , tbNHC.Date ;