如何通过子查询字段对查询进行排序?

时间:2013-05-08 00:09:08

标签: sql vba ms-access

下面的代码正常工作,直到我尝试按“Prior_Usage”列排序查询我得到语法错误。因为我花了几个小时寻找解决方案,所以任何帮助都会被大大加以评估

SELECT TOfferItem.id AS OfferID,
       TOfferItem.fkmanfitemid,
       tblManfItems_1.manf_code AS Code,
       tblManfItems_1.short_desc,
       tblCompany_1.companyname AS Manufacturer,
       tblManfItems_1.target_ounce_size AS Target_Size_Oz,
       tblgeneralitems.longdesc AS [Group],
       tblManfItems_1.quality AS Quality,
       tblitempacks.packdesc AS Pack,
       [quantity] * [weight] AS Avail_Weight,
       TOfferItem.price AS Cost,
       tblcity.city,
       tblstates.stateabbr,
       TOfferItem.note,
       Count(tblmanfitemfiles.id) AS CountOfFiles, 
       tblManfItemCustLINK_1.cust_review,
       tblManfItemCustLINK_1.rating,
       [quantity] * [weight] AS lbs,
       (SELECT SUM(podtl.totallbl)
        FROM tblorderstatus
        INNER JOIN (podtl
        INNER JOIN (pohdr
        INNER JOIN tblcompany ON pohdr.customer = tblcompany.pkcompanyid)
                              ON podtl.tranid = pohdr.tranid)
                              ON tblorderstatus.orderid = pohdr.tranid  
        WHERE (
              ( ( tblorderstatus.submitted ) = true )
              AND ( ( tblorderstatus.canceled ) = false )
              AND ( ( pohdr.customer ) = 1319 )
              AND ( ( podtl.fkmanfitemid ) = TOfferItem.fkmanfitemid ) )
        ) AS Prior_Usage
FROM   tblmanfitemcustlink AS tblManfItemCustLINK_1
       RIGHT JOIN ((tblcompany AS tblCompany_1
                    RIGHT JOIN (tblstates
                                RIGHT JOIN (tblcity
                                            RIGHT JOIN (tblgeneralitems
                                                        RIGHT JOIN (tblitempacks
                                                                    RIGHT JOIN (
                                                        tblofferiteminventory AS
                                                                   TOfferItem
                                            INNER JOIN tblmanfitems
                                                       AS
                                                       tblManfItems_1
                                                    ON
                                            TOfferItem.fkmanfitemid =
                                            tblManfItems_1.id)
                                                    ON tblitempacks.id =
                  tblManfItems_1.pack)
                  ON tblgeneralitems.itemid =
                  tblManfItems_1.commodity_id)
                  ON tblcity.cityid = TOfferItem.fkcityid)
                  ON tblstates.stateid = tblcity.state)
                  ON tblCompany_1.pkcompanyid = tblManfItems_1.fk_company_id)
                   LEFT JOIN tblmanfitemfiles
                          ON tblManfItems_1.id = tblmanfitemfiles.itemid)
               ON tblManfItemCustLINK_1.fkmanfitemid = tblManfItems_1.id
GROUP BY TOfferItem.id,
         TOfferItem.fkmanfitemid,
         tblManfItems_1.manf_code,
         tblManfItems_1.short_desc,
         tblCompany_1.companyname,
         tblManfItems_1.target_ounce_size,
         tblgeneralitems.longdesc,
         tblManfItems_1.quality,
         tblitempacks.packdesc,
         TOfferItem.price,
         tblcity.city,
         tblstates.stateabbr,
         TOfferItem.note,
         tblManfItemCustLINK_1.cust_review,
         tblManfItemCustLINK_1.rating,
         [quantity] * [weight]
HAVING ( ( ( TOfferItem.id ) IN ( 4280, 4287, 4288, 4292,
                                  4295, 4298, 4299, 4300,
                                  4301, 4384, 4401, 4404,
                                  4405, 4410, 4421, 4432,
                                  4435, 4455, 4458, 4459,
                                  4460, 4462, 4469, 4471,
                                  4472, 4473, 4474, 4475,
                                  4479, 4482, 4483, 4484,
                                  4487, 4489, 4494, 4498,
                                  4499, 4500, 4501, 4502,
                                  4503, 4504, 4506, 4508,
                                  4509, 4510, 4512, 4513,
                                  4514, 4515, 4516, 4517,
                                  4524, 4525, 4531, 4532,
                                  4533, 4534, 4535, 4536,
                                  4537, 4538, 4539, 4540,
                                  4541, 4542, 4543, 4544,
                                  4545, 4546, 4547, 4548,
                                  4549, 4550, 4551, 4552,
                                  4553, 4554, 4555, 4556,
                                  4557, 4558, 4560, 4561,
                                  4562, 4563, 4564, 4565, 4566 ) )
         AND ( ( [quantity] * [weight] ) > 0 ) ) 

1 个答案:

答案 0 :(得分:0)

不是答案。一个问题,因为我出于某种原因无法评论。

当您在SELECT中列出两次错误时,您获得的错误究竟是什么以及为什么按[数量] * [重量]进行分组。删除其中一个,因为它们都做同样的事情,但标签不同。

你在哪里/如何将Prior_Usage放在GROUP BY中?

只是几个问题。抱歉。那里的括号也有点过分。