Link to home
Start Free TrialLog in
Avatar of David11011
David11011Flag for United States of America

asked on

SQL substring group by

Here's one for ya. I'm baffled at this.

this query returns 2455 results
SELECT * FROM (
      SELECT	SUBSTRING(ORDERNO, 1, 6) AS ORDERNO,
		        SUM(DOLLARS) AS DOLLARS
      FROM TEMPORDR 
     GROUP BY ORDERNO 
  ) TBL1
WHERE DOLLARS >= 500.00
ORDER BY DOLLARS

Open in new window


and this query returns 2792 results:
SELECT * FROM (
SELECT	SUBSTRING(ORDERNO, 1, 6) AS ORDERNO
		,SUM(DOLLARS) AS DOLLARS
FROM TEMPORDR 
GROUP BY SUBSTRING(ORDERNO, 1, 6)
) TBL1
WHERE DOLLARS >= 500.00
ORDER BY DOLLARS

Open in new window


the order number look like this 368954*1, 368954*2, etc. The *1 denotes each level on an order so I want to ignore the suffix and group the order number part of the order number. i.e. 368954.

When I use the substring is should return less results because it should group the like terms. However it returns a lot more. Anyone know why?
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of David11011

ASKER

Holy Crap! I'm an idiot! thanks for your help teaching me some common sense lol.