eternal_noob Posted February 24, 2014 Share Posted February 24, 2014 Current Query: $sql= "SELECT SysproCompanyJ.dbo.InvWarehouse.StockCode, SUM(QtyOnHand - QtyAllocated) AS 'Value' FROM SysproCompanyJ.dbo.InvWarehouse WHERE SysproCompanyJ.dbo.InvWarehouse.QtyOnHand > '0' And Warehouse = 'SW' GROUP BY StockCode HAVING SUM(QtyOnHand - QtyAllocated) > 0"; Example of current output (If I were to exclude the Having Sum condition): +--------------+-------+ | Stock Code | Value | +--------------+-------+ | C-100-01A | 100 | | C-100-01B | -90 | | C-200-00A | 145 | | C-300-02C | -14 | | C-300-02D | 17 | +--------------+-------+ What I need (cut off the last character of the stock code and sum like stock codes) : +-------------+-------+ | Stock Code | Value | +-------------+-------+ | C-100-01 | 10 | | C-200-00 | 145 | | C-300-02 | 3 | +-------------+-------+ Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 24, 2014 Share Posted February 24, 2014 Without update the column? Quote Link to comment Share on other sites More sharing options...
Zane Posted February 24, 2014 Share Posted February 24, 2014 SELECT substring(invW.StockCode, 1, (len(invW.StockCode) - 1)) as sStockCode , SUM(invW.QtyOnHand - invW.QtyAllocated) AS 'Value' FROM SysproCompanyJ.dbo.InvWarehouse as invW WHERE invW.QtyOnHand > '0' And Warehouse = 'SW' GROUP BY substring(invW.StockCode, 1, (len(invW.StockCode) - 1)) HAVING SUM(invW.QtyOnHand - invW.QtyAllocated) > 0 Untested. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 24, 2014 Share Posted February 24, 2014 (edited) You should update your database to have a column without the letter. I would think you would want one column for the "code" and a separate one for the letter. Also, your query seems to have some inefficiencies. I don't see why you need the WHERE clause and the HAVING clause. Plus, the HAVING clause is using a separate calculation rather than the one that is included in the SELECT field list. Using Zane's example, I would think the query would look like this SELECT substring(invW.StockCode, 1, (len(invW.StockCode) - 1)) as sStockCode, SUM(invW.QtyOnHand - invW.QtyAllocated) AS 'Value' FROM SysproCompanyJ.dbo.InvWarehouse as invW WHERE Warehouse = 'SW' GROUP BY substring(invW.StockCode, 1, (len(invW.StockCode) - 1)) HAVING Value > 0 But again, I think it would be best to update the table structure. Edited February 24, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 24, 2014 Share Posted February 24, 2014 (edited) For gurus! MySQL follows the SQL standart using CHARACTER_LENGTH() and its aliases like CHAR_LENGTH() and LENGTH(). LEN() isn't a part of MySQL string functions PS: No need to substring same result twice b/s is already done in the select SELECT substring(invW.StockCode, 1, (len(invW.StockCode) - 1)) as sStockCode, SUM(invW.QtyOnHand - invW.QtyAllocated) AS 'Value' FROM SysproCompanyJ.dbo.InvWarehouse as invW WHERE Warehouse = 'SW' GROUP BY substring(invW.StockCode, 1, (len(invW.StockCode) - 1)) HAVING Value > 0 // to SELECT SUBSTRING(invW.StockCode, 1, (LENGTH(invW.StockCode) - 1)) as sStockCode, SUM(invW.QtyOnHand - invW.QtyAllocated) AS 'Value' FROM SysproCompanyJ.dbo.InvWarehouse as invW WHERE Warehouse = 'SW' GROUP BY sStockCode HAVING Value > 0 Edited February 24, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 24, 2014 Share Posted February 24, 2014 (edited) Also, I'm not realy sure is it possible to use more than one period(.) as separate table/column identifiers like in the example: FROM SysproCompanyJ.dbo.InvWarehouse as invW Edited February 24, 2014 by jazzman1 Quote Link to comment Share on other sites More sharing options...
Barand Posted February 24, 2014 Share Posted February 24, 2014 SysproCompanyJ.dbo.InvWarehouse looks more like a MSSQL tablename. Quote Link to comment Share on other sites More sharing options...
Zane Posted February 24, 2014 Share Posted February 24, 2014 PS: No need to substring same result twice b/s is already done in the select Although I didn't test the query at all, I've came across headaches in the past where I would get an invalid field error. or something something is ambigous. I'm not realy sure is it possible to use more than one period(.) as separate table/column identifiers We always had to use two dots in my DB Admin class... databaseName.tableName.fieldName Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 24, 2014 Share Posted February 24, 2014 According the docs, a FROM clause is an optional in MySQL and ONLY a table reference, no database, nor column. In his example above I see a column reference. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.