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 | +-------------+-------+ Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/ Share on other sites More sharing options...
jazzman1 Posted February 24, 2014 Share Posted February 24, 2014 Without update the column? Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470439 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. Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470442 Share on other sites More sharing options...
Psycho Posted February 24, 2014 Share Posted February 24, 2014 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. Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470450 Share on other sites More sharing options...
jazzman1 Posted February 24, 2014 Share Posted February 24, 2014 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 Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470489 Share on other sites More sharing options...
jazzman1 Posted February 24, 2014 Share Posted February 24, 2014 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 Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470495 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. Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470517 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 Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470528 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. Link to comment https://forums.phpfreaks.com/topic/286486-mysql-query-trim-last-character-and-group-by-similar-items/#findComment-1470541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.