[table].[field] vs table.field difference? - sql

I don't quite understand the difference between, for example:
SELECT [Products].[ID], [Products].[ProductName] FROM Products ORDER BY [ProductName];
compared with
SELECT Products.ID, Products.ProductName FROM Products ORDER BY ProductName;
Can someone give me some insight please? The query produces same result for me.

In a programming language a variable name must be unique throughout its context and cannot have Spaces.
VariableA is valid
variable b is not valid
variable_b is valid
Some languages are even case sensitive.
variableA is valid
VariableA is valid and different from variableA
On the other hand MS ACCESS allows free formatted names for it tables, queries, forms etc. This means you could have a table called
This is a fruit table
to identify "this is a fruit table" as one word/item Access somehow needs to evaluate/know that the variable name is one word or its a variable name and not a text. Therefore Access uses [] to encapsulate the word so it can evaluate its content.
if you follow a most desired coding style you would name your tables with prefix such as tbl_fruits, frm_fruits, qry_view_fruits which will help you as well as Access to understand what you are referring to.

If you use brackets around the names you can use reserved words like order for a table or colum name or names with spaces and such.
But you only need to apply the brackets on those but can apply it on all if you like.

Related

SQL query equality comparison with special character that is equal to everything

I am writing a python script that gets info from a database through SQL queries. Let's say we have an SQL array with information about some people. I have one query that can retrieve this information about a specific person whose name I pass as an argument to the query.
(" SELECT telephone FROM People_info WHERE name=%s " % (name))
Is it possible to pass as an argument a special character or something like that will return me the telephone for all the names? Meaning something that when I compare with every name the result will be equal? I want to use only one query for all the cases (either if I want the info about one person or all of them)
You can edit the SQL code in
SELECT telephone FROM People_info WHERE name=nvl(%s, name)
and pass null if you want to get all the records
Notice that this will never get the records where name is null, but I suppose this is not a problem.
You can use LIKE and the wild card % which matches no, one or any number of any characters.
SELECT telephone
FROM people_info
WHERE name LIKE '%';
However, it won't show records where name IS NULL.
Maybe the optimizer is smart enough to see, that this actually equivalent to a WHERE name IS NOT NULL and uses an index, if there is one. But maybe it don't see it, then this may come as higher price than necessary. So I'd rather change the WHERE clause (or completely omit it, if I wanted all records) in the application to what I actually want, then use such tricks.

How to query tables that have conflicting name?

i have a table called ORDER, when i try to query it using
select * from Order
i get the error 'invalid query'
how to do access this table please?
thank you
Step 1
Don't ever used reserved words for object (or column) names.
SQL 1992 Standard (search for <reserved word>)
SQL Server reserved words
MySQL reserved words
DB2 reserved words
Step 2
If you've inherited such a thing that you are not able to change, then you need to use "quoted identifiers".
I advocate
SELECT *
FROM "Order"
As it is a standard identifier, so will work better across platforms.
ORDER is a reserved word in SQL. Put double quotes around it:
select * from "Order"
And also, I personally think ORDERS is a better name. (Because several orders are stored in the table.)
Late edit: List of reserved words, in different versions of the SQL standard:
http://developer.mimer.com/standard/reservedwords/sql-reserved-words.tml
It's generally a good idea to not use reserved words for database object names (tables, views & etc.). Sometimes you just got to deal with it though. The below query should work for you.
select * from [Order]
Just put the brackets around the table name.
Hope this helps!

ms access - when I select a query in vba, column is empty

When i write a select statement in vba to grab a column from a query its empty.
i have a query thats joined by multiple tables.
For example if I call select query.specialcolumn from query where query.id=5 I get a blank back. However, If I view it in the query table I see data for ID=5 with data.
Straight SQL in design mode also produces blanks. Only when I view the query as a whole, I can see data.
Any ideas?
Sounds like you used "query" as the name for your saved query. And query is a reserved word, see Problem names and reserved words in Access. It's hard predict when reserved words as object names will create problems. And I'm not confident that name is the problem here. But I would rule it out first before investigating anything else.
Enclose query in square brackets everywhere it's referenced in the SQL.
select [query].specialcolumn from [query] where [query].id=5
The square brackets will inform the db engine that query is a database object rather than the reserved word.

How best to sum multiple boolean values via SQL?

I have a table that contains, among other things, about 30 columns of boolean flags that denote particular attributes. I'd like to return them, sorted by frequency, as a recordset along with their column names, like so:
Attribute Count
attrib9 43
attrib13 27
attrib19 21
etc.
My efforts thus far can achieve something similar, but I can only get the attributes in columns using conditional SUMs, like this:
SELECT SUM(IIF(a.attribIndex=-1,1,0)), SUM(IIF(a.attribWorkflow =-1,1,0))...
Plus, the query is already getting a bit unwieldy with all 30 SUM/IIFs and won't handle any changes in the number of attributes without manual intervention.
The first six characters of the attribute columns are the same (attrib) and unique in the table, is it possible to use wildcards in column names to pick up all the applicable columns?
Also, can I pivot the results to give me a sorted two-column recordset?
I'm using Access 2003 and the query will eventually be via ADODB from Excel.
This depends on whether or not you have the attribute names anywhere in data. If you do, then birdlips' answer will do the trick. However, if the names are only column names, you've got a bit more work to do--and I'm afriad you can't do it with simple SQL.
No, you can't use wildcards to column names in SQL. You'll need procedural code to do this (i.e., a VB Module in Access--you could do it within a Stored Procedure if you were on SQL Server). Use this code build the SQL code.
It won't be pretty. I think you'll need to do it one attribute at a time: select a string whose value is that attribute name and the count-where-True, then either A) run that and store the result in a new row in a scratch table, or B) append all those selects together with "Union" between them before running the batch.
My Access VB is more than a bit rusty, so I don't trust myself to give you anything like executable code....
Just a simple count and group by should do it
Select attribute_name
, count(*)
from attribute_table
group by attribute_name
To answer your comment use Analytic Functions for that:
Select attribute_table.*
, count(*) over(partition by attribute_name) cnt
from attribute_table
In Access, Cross Tab queries (the traditional tool for transposing datasets) need at least 3 numeric/date fields to work. However since the output is to Excel, have you considered just outputting the data to a hidden sheet then using a pivot table?

How do I perform a simple one-statement SQL search across tables?

Suppose that two tables exist: users and groups.
How does one provide "simple search" in which a user enters text and results contain both users and groups whose names contain the text?
The result of the search must distinguish between the two types.
The trick is to combine a UNION with a literal string to determine the type of 'object' returned. In most (?) cases, UNION ALL will be more efficient, and should be used unless duplicates are required in the sub-queries. The following pattern should suffice:
SELECT "group" type, name
FROM groups
WHERE name LIKE "%$text%"
UNION ALL
SELECT "user" type, name
FROM users
WHERE name LIKE "%$text%"
NOTE: I've added the answer myself, because I came across this problem yesterday, couldn't find a good solution, and used this method. If someone has a better approach, please feel free to add it.
If you use "UNION ALL" then the db doesn't try to remove duplicates - you won't have duplicates between the two queries anyway (since the first column is different), so UNION ALL will be faster.
(I assume that you don't have duplicates inside each query that you want to remove)
Using LIKE will cause a number of problems as it will require a table scan every single time when the LIKE comparator starts with a %. This forces SQL to check every single row and work it's way, byte by byte, through the string you are using for comparison. While this may be fine when you start, it quickly causes scaling issues.
A better way to handle this is using Full Text Search. While this would be a more complex option, it will provide you with better results for very large databases. Then you can use a functioning version of the example Bobby Jack gave you to UNION ALL your two result sets together and display the results.
I would suggest another addition
SELECT "group" type, name
FROM groups
WHERE UPPER(name) LIKE UPPER("%$text%")
UNION ALL
SELECT "user" type, name
FROM users
WHERE UPPER(name) LIKE UPPER("%$text%")
You could convert $text to upper case first or do just do it in the query. This way you get a case insensitive search.