Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm messing around with databases to learn them and I have the following basic SQL which works fine:

SELECT Name AS ProductName, StandardCost AS Cost, ListPrice AS Price
FROM SalesLT.Product
WHERE
ListPrice / 2 < StandardCost;


However, when I alter the SQL as below:

SELECT Name AS ProductName, StandardCost AS Cost, ListPrice AS Price
FROM SalesLT.Product
WHERE
ListPrice / 4 < StandardCost;


It's returning all records in the table even though I'm now dividing by 4. Any ideas?

Cheers.

Brian.
Posted

1 solution

Without your actual data, it's pretty difficult to be precise, but...
The operator precedence looks right - divide is done before <, so that should be right.
It may be that all ListPrice values are less than 4 times the standard cost: so try this:
SQL
SELECT Name AS ProductName, StandardCost AS Cost, ListPrice AS Price,
ListPrice / 2 AS LPBY2, ListPrice / 4 as LPBY4
FROM SalesLT.Product
WHERE
ListPrice / 4 < StandardCost;
And look at what the comparison is doing in each case.
If would probably also be worth reversing the test in each case and seeing what you get back:
SQL
...WHERE ListPrice / 2 >= StandardCost

SQL
...WHERE ListPrice / 4 >= StandardCost

That should help you work out why - but if not, let me know what results you do get.
 
Share this answer
 
Comments
BrianHamilton 24-Nov-13 8:30am    
That worked - I tried dividing by 2, 2.2, 2.5 ... and it turns out the code was working - my brain wasn't. Thanks for the help.

Brian.
OriginalGriff 24-Nov-13 8:37am    
I know the feeling well! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900