How do I use a WHERE on a calculated column - sql

I have a calculated column in my sql query and I want to use it in my where clause, normally I use the table names and then what I want to filter. In this case I don't have that option
this is in my SELECT statement, Its a basic margin calculation SELL-COST/SELL*100 to give me the margin.
CONVERT(DECIMAL(10,2),(T_CUSTOMERPRICELISTBASESTANDARDRULE_PRICEDEFINITION.C_NETPRICE - T_PRODUCT_PURCHASING.C_LISTPRICEACTUAL)/T_CUSTOMERPRICELISTBASESTANDARDRULE_PRICEDEFINITION.C_NETPRICE ) * 100 Percentage
what I would like to do is use the WHERE clause to filter the above statement anything below 15

You need to use a table expression to name that column officially. Then the outer query can use it as needed. For example:
select *
from (
select a, b, a + b as c from t
) x
where c > 10 -- "c" exists in the outer query

Related

Transpose a single row multiple columns into multiple rows single colum

I have a table valued function in sql server which returns multiple rows and single column such as below
1
2
3
I use the syntax select * from dbo.function to use the values returned by this function in where clause of my queries.
Now apart from the value returned by the function I want to put certain hard coded values in that where clause.
For example :
Select * from dbo.table where ID in (Select * from dbo.function + **I want to add some more values here**)
So that if function returns
1
2
3
I want to add lets say
4
5
in that list such that final query becomes as follows :
select * from dbo.table where ID in (1,2,3,4,5)
Use or:
Select *
from dbo.table
where ID in (Select * from dbo.function) or
ID in (4, 5)
Although you could mangle the subquery using union all, the above makes the query easier to follow (in my opinion). Also, in the event that "function" is really a table, it is easier for the optimizer to recognize appropriate indexes.

how do i filter a column with multiple values

how do i filter a column col1 with multiple values
select * from table where col1=2 and col1=4 and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'
i tired
select * from table where col1=2 or col1=4 and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'
the result of both queries is incorrect
the first one gives zero results
second one gives 3 results
the correct is 2 results
the sql will run against the sqlite db.
AND is evaluated before OR, so your query is equivalent to:
select *
from table
where col1=2 or (col1=4 and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1')
You need to explicitly group the conditions when mixing AND and OR:
select *
from table
where (col1=2 or col1=4) and userID='740b9738-63d2-67ff-ba21-801b65dd0ae1'

Is it possible to rewrite a NOT IN query to use indexes?

Sqlite does not support the use of indexes in queries based around a NOT IN clause.
Is it possible to logically rewrite a query like the following in such a way that it will only use the operators listed at the link above?
The query:
Select *
From table
Where table-column not in (
Select table-column
From table2);
The operators listed as being able to use an index:
column = expression
column > expression
column >= expression
column < expression
column <= expression
expression = column
expression > column
expression >= column
expression < column
expression <= column
column IN (expression-list)
column IN
(subquery)
column IS NULL
Use a LEFT JOIN as described in section 6.
SQLFiddle with sample data here. Expand View Execution Plan to confirm that the original query does a table scan, while the LEFT JOIN query uses the index.
Select A.*
From table a
left join table2 b
on a.table-column = b.table-column
WHERE b.table-column is null
SELECT
*
FROM
Table1
LEFT JOIN Table2
ON Table1.table-column = Table2.table_column
WHERE
Table2.table_column IS NULL

How to Sum One Column over Three Rows?

I have table that I want to sum 3 rows of one column how can I do that?
If I understand your question correctly - you should use the SUM() function
SELECT SUM(the_column)
FROM the_table
WHERE condition-to-get-your-three-rows
A "WHERE" clause will allow to specify which 3 rows you want to be summed up. Some examples of "WHERE" clauses are below:
WHERE row_id < 3
or
WHERE active = 'Yes'
So the above statement may look like
SELECT SUM(the_column)
FROM the_table
WHERE active = 'Yes'
SELECT
SUM(table.column1) + SUM(table.column2) + SUM(table.column3)
FROM table

Returning more than one value from a sql statement

I was looking at sql inner queries (bit like the sql equivalent of a C# anon method), and was wondering, can I return more than one value from a query?
For example, return the number of rows in a table as one output value, and also, as another output value, return the distinct number of rows?
Also, how does distinct work? Is this based on whether one field may be the same as another (thus classified as "distinct")?
I am using Sql Server 2005. Would there be a performance penalty if I return one value from one query, rather than two from one query?
Thanks
You could do your first question by doing this:
SELECT
COUNT(field1),
COUNT(DISTINCT field2)
FROM table
(For the first field you could do * if needed to count null values.)
Distinct means the definition of the word. It eliminates duplicate returned rows.
Returning 2 values instead of 1 would depend on what the values were, if they were indexed or not and other undetermined possible variables.
If you are meaning subqueries within the select statement, no you can only return 1 value. If you want more than 1 value you will have to use the subquery as a join.
If the inner query is inline in the SELECT, you may struggle to select multiple values. However, it is often possible to JOIN to a sub-query instead; that way, the sub-query can be named and you can get multiple results
SELECT a.Foo, a.Bar, x.[Count], x.[Avg]
FROM a
INNER JOIN (SELECT COUNT(1) AS [Count], AVG(something) AS [Avg]) x
ON x.Something = a.Something
Which might help.
DISTINCT does what it says. IIRC, you can SELECT COUNT(DISTINCT Foo) etc to query distinct data.
you can return multiple results in 3 ways (off the top of my head)
By having a select with multiple values eg: select col1, col2, col3
With multiple queries eg: select 1 ; select "2" ; select colA. you would get to them in a datareader by calling .NextRecord()
Using output parameters, declare the parameters before exec the query then get the value from them afterwards. eg: set #param1 = "2" . string myparam2 = sqlcommand.parameters["param1"].tostring()
Distinct, filters resulting rows to be unique.
Inner queries in the form:
SELECT * FROM tbl WHERE fld in (SELECT fld2 FROM tbl2 WHERE tbl.fld = tbl2.fld2)
cannot return multiple rows. When you need multiple rows from a secondary query, you usually need to do an inner join on the other query.
rows:
SELECT count(*), count(distinct *) from table
will return a dataset with one row containing two columns. Column 1 is the total number of rows in the table. Column 2 counts only distinct rows.
Distinct means the returned dataset will not have any duplicate rows. Distinct can only appear once usually directly after the select. Thus a query such as:
SELECT distinct a, b, c FROM table
might have this result:
a1 b1 c1
a1 b1 c2
a1 b2 c2
a1 b3 c2
Note that values are duplicated across the whole result set but each row is unique.
I'm not sure what your last question means. You should return from a query all the data relevant to the query. As for faster, only benchmarking can tell you which approach is faster.