2013-05-08 65 views
-1

下面的代碼工作正常,直到我嘗試按「Prior_Usage」列排序查詢時出現語法錯誤。任何幫助將大大appricated,因爲我花了幾個小時尋找解決方案如何按子查詢字段排序查詢?

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

爲什麼不顯示*不*工作的SQL? – 2013-05-08 00:18:42

+0

你在哪裏放ORDER BY? – 2013-05-08 01:48:45

回答

0

沒有答案。一個問題,因爲我不能評論的原因。

什麼是你正在得到的錯誤,以及你爲什麼按[數量] * [重量]進行分組,如果你在SELECT中列出了兩次。刪除其中一個,因爲他們都做同樣的事情,但用不同的標籤。

你在哪裏/如何將Prior_Usage放入GROUP BY中?

只是幾個問題。抱歉。在括號裏也有點過分。