Using an Alias column in the where clause in ms-sql 2000 - sql

I know you cannot use a alias column in the where clause for T-SQL; however, has Microsoft provided some kind of workaround for this?
Related Questions:
Unknown Column In Where Clause
Can you use an alias in the WHERE clause in mysql?
“Invalid column name” error on SQL statement from OpenQuery results

One workaround would be to use a derived table.
For example:
select *
from
(
select a + b as aliased_column
from table
) dt
where dt.aliased_column = something.
I hope this helps.

Depending on what you are aliasing, you could turn it into a user defined function and reference that in both places. Otherwise your copying the aliased code in several places, which tends to become very ugly and means updating 3+ spots if you are also ordering on that column.

Related

Ambiguous column name for order by the selected column

I never notice below before:
SELECT A,*
FROM Table
ORDER BY A
It gives me the Ambiguous column name error. The * of course contains A, but will sql server take the same column A in above query as two different columns? Is this the reason behind that?
it is because the output has the same name if you did
SELECT A AS A_STANDS_ALONE,*
FROM Table
ORDER BY A
It will work
If you are looking for yes/no answer, then 'YES', you are right.
SQL Selects A column twice, one by explicit and one by the implicit * call.
SQL doesn't understand which one you want to sort by.
The first comment to your post shows that you can use an alias for the column.

Why I must put "AS" clause at the end of subquery.?

I saw this thread:
SELECT
MAX(totalcontracts) AS highest_total
FROM ( SELECT
SuperVisor
,COUNT(ContractNo) AS totalcontracts
FROM Contract
GROUP BY SuperVisor
) AS t
If I delete "as t" line that return me an error. but when I put it back that work fine.
This is temporarly so why do I required to put a name to that subtable?Any sense in that?
Thank you.
In standard SQL a derived table (a subquery you select data from) needs a name:
FROM (<subquery>) [AS] <name>`
(Some DBMS, however, are not compliant with the standard. In Oracle for instance it is not mandatory to give a derived table an alias, but the optional AS is forbidden when you specify one. Other DBMS on the other hand may require the AS although the SQL standard specifies it as optional.)
No, there isn't a real logic necessity here.
Oracle for instance does not force it
select * from (select * from dual);
Nor SQLite
select * from (select 1)
As #jarlh mentioned, if you're using some database systems, in order to use the result from any nested subquery you need to give that result-set a name (or alias), which is why you need to put a name / letter / character after you close the paranthesis.
The AS keyword is not mandatory, but the name is.

Inserting new rows into table-1 based on constraints defined on table-2 and table-3

I want to append new rows to a table-1 d:\dl based on the equality constraint lower(rdl.subdir) = lower(tr.n1), where rdl and tr would be prospective aliases for f:\rdl and f:\tr tables respectively.
I get a function name is missing ). message when running the following command in VFP9:
INSERT INTO d:\dl SELECT * FROM f:\rdl WHERE (select LOWER(subdir)FROM f:\rdl in (select LOWER(n1) FROM f:\tr))
I am using the in syntax, instead of the alias based equality statement lower(rdl.subdir) = lower(tr.n1) because I do not know where to define aliases within this command.
In general, the best way to get something like this working is to first make the query work and give you the results you want, and then use it in INSERT.
In general, in SQL commands you assign aliases by putting them after the table name, with or without the keyword AS. In this case, you don't need aliases because the ones you want are the same as the table names and that's the default.
If what you're showing is your exact code and you're running it in VFP, the first problem is that you're missing the continuation character between lines.
You're definitely doing too much work, too. Try this:
INSERT INTO d:\dl ;
SELECT * ;
FROM f:\rdl ;
JOIN f:\tr ;
ON LOWER(rdl.subdir) = LOWER(tr.n1)

SQL - Where clause: comparing a column with multiple values

Is there any generic command or syntax in SQL to allow one to have multiple values in a where statement without using OR?
OR just gets tedious when you have many values to choose from and you only want say half of them.
I want to return only columns that contain certain values. I am using Cache SQL, but as I said, a generic syntax might be helpful as well because most people are unfamiliar with Cache SQL. Thanks!
You should use IN:
... where column_name in ('val1', 'val2', ...);
Use the 'IN' clause.
SELECT * FROM product WHERE productid IN (1,2,3)
I believe you are looking for the IN clause.
Determines whether a specified value matches any value in a subquery or a list.

SQL with LIMIT1 returns all records

I made a mistake and entered:
SELECT * FROM table LIMIT1
instead of
SELECT * FROM table LIMIT 1 (note the space between LIMIT and 1)
in the CLI of MySQL. I expected to receive some kind of parse error, but I was surprised, because the query returned all of the records in the table. My first thought was "stupid MySQL, I bet that this will return error in PostgreSQL", but PostgreSQL also returned all records. Then tested it with SQLite - with the same result.
After some digging, I realized that it doesn't matter what I enter after the table. As long as there are no WHERE/ORDER/GROUP clauses:
SELECT * FROM table SOMETHING -- works and returns all records in table
SELECT * FROM table WHERE true SOMETHING -- doesn't work - returns parse error
I guess that this is a standardized behavior, but I couldn't find any explanation why's that. Any ideas?
Your first query is equivalent to this query using a table alias:
SELECT * FROM yourtable AS LIMIT1
The AS keyword is optional. The table alias allows you to refer to columns of that table using the alias LIMIT1.foo rather than the original table name. It can be useful to use aliases if you wish to give tables a shorter or a more descriptive alias within a query. It is necessary to use aliases if you join a table to itself.
From the SQL lite documentation:
This is why I want DB engine to force the usage of keyword AS for alias names
http://beyondrelational.com/modules/2/blogs/70/posts/10814/should-alias-names-be-preceded-by-as.aspx
SELECT * FROM table LIMIT1;
LIMIT1 This has taken as alias by SQL, cause LIMIT1 is not a reserved literal of SQL.
Something after table name and that is not a reserved keyword always taken as an table alias by SQL.
SELECT * FROM table LIMIT 1;
When you used LIMIT just after the table name, SQL found that as a reserved keyword and worked for it as per the behavior. IF you want to use reserved key words in query It can be done by putting reserved literals in quotes. like..
SELECT * FROM table `LIMIT`;
OR
SELECT * FROM table `LIMIT 1`;
Now all words covered under `` quotes will treated as user defined.
Commonly we did mistake with date, timestamp, limit etc.. keywords by using them as column names.