My company has a dual company structure which requires that data be kept separate. We have two datasets which are identical in structure. I commonly use MS Query to write SQL using one dataset, and when I have what I want, I simply add a UNION ALL statement and repeat the SQL but replace any dataset names with the second one. It always works fine tp give me a single query combining the data from both datasets.
However, I am retrieving some date fields using the datepart function to get the year and month of the records, and I use the AS statement to name the columns.
Example: Select datepart(yyyy,receiptdate) as "Year"
While it works in the original query with one dataset, when I create the 'UNION ALL' and add the additional SQL, the column name becomes blank. The same goes for some fields that I am summarizing and renaming to have a more concise name than SUM(QUANTITY_ORDERED) -- they turn blank after the UNION ALL.
Is there a trick to renaming columns when using UNION ALL, or other suggestions?
Thanks,
Mark
If you are referring to SQL SERVER (I'm honestly not sure what ms-query is), then it should be that your column aliases are done in the first statement of your union
For example the first example below leaves column name blank while the second example provides the correct column name;
-- Example 1
Select datepart(yyyy,CURRENT_TIMESTAMP)
UNION ALL
SELECT datepart(yyyy,DATEADD(YY, -1, CURRENT_TIMESTAMP)) as "Year"
-- Example 2
Select datepart(yyyy,CURRENT_TIMESTAMP) as "Year"
UNION ALL
SELECT datepart(yyyy,DATEADD(YY, -1, CURRENT_TIMESTAMP))
Related
The records of one column in my table have a letter/dash prefix (B-290151626). I need to remove the letter/dash without changing the rest of the record, and do this for 1700 rows.
This is for the Paradox database (yes I know it's old) and I have a simple SQL editor window to work with inside the application. I can select all the records I need to edit, grouped by their letter prefix.
Here's a view of the table:
UPDATE table
SET BookingID = SUBSTRING(BookingID , 3, LEN(BookingID ) - 2)
where substring(BookingID,0,1) ='B'
SQLite
There are multiple databases, one database for each time period (i.e. quarter). The column headers in each table are the same. Some of the columns. The data is identical between databases (e.g. ID, Name, Address, State, Website, etc). Some of the columns, the column header is the same but the
data in the column is different between databases.
The goal is to:
Select multiple columns from multiple databases, sum each column, convert the output from 000000000 to $000,000,000,000, adding three zero's to the output
(currently the data is represented in 000's).
Following is an iteration of queries that work, ending in the queries that fail.
Selecting one column from one database. This query works.
select dep
From AllReports19921231AssetsAndLiabilities;
output
"11005"
"34396"
"42244"
Adding a sum(columnName) method to this same query works.
select sum(dep)
From AllReports19921231AssetsAndLiabilities;
results: 3562807353
Attempting to sum(columnName) from multiple databases causes an error.
select sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities;
error:
ambiguous column name: dep: select sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities;
Using dot notation to attach a database to a column. Query works.
select AllReports19921231AssetsAndLiabilities.dep
From AllReports19921231AssetsAndLiabilities;
Output:
"11005"
"34396"
"42244"
However when I attempt to include dot notation and add sum(columnName) to the query, it fails.
select AllReports19921231AssetsAndLiabilities.sum(dep)
From AllReports19921231AssetsAndLiabilities;
I receive this error:
near "(": syntax error: select AllReports19921231AssetsAndLiabilities.sum(
What are correct ways to write this query?
The end goal is to select the same columns (e.g. col1, col2, col3, etc) from multiple databases (Q1, Q2, Q3, Q4).
Sum each column, add three zero's the output, then convert from 000000000 to $000,000,000,000
Note: There are 103 databases (i.e. one for each time period/quarter).
select AllReports19921231AssetsAndLiabilities.sum(dep),
AllReports19930331AssetsAndLiabilities.sum(dep),
AllReports19930630AssetsAndLiabilities.sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities,
AllReports19930630AssetsAndLiabilities;
The above query outputs an error:
near "(": syntax error: select AllReports19921231AssetsAndLiabilities.sum(
Your syntax is wrong :
select sum(AllReports19921231AssetsAndLiabilities.dep)
From AllReports19921231AssetsAndLiabilities
Learn to use aliases!
select sum(aal.dep)
From AllReports19921231AssetsAndLiabilities aal;
The query is much easier to write and to read. The table alias (whether the full table name or an abbreviation) is attached to the column name. In SQL, this results in a qualified column reference. The qualification specifies what table it is coming from.
The table alias is not attached to a function, because SQL does not currently allow tables to contain functions.
is it possible to use an array of elements as a select statement?
I know it is possiible to get rows based on static elements like this:
SELECT 405, CAST('4D6178' AS VARCHAR(32)), CAST('2017-01-01 00:00:00' AS TIMESTAMP) FROM rdb$databas
That will give you a table select with one row.
Now I would like to get this as table with n rows, but I don't know how to achieve this. Due to the fact that firebird doesn't allow multiple select statements I cannot only append n times a selec.
Info : Firebird 2.1
Use UNION ALL clause.
https://en.wikipedia.org/wiki/Set_operations_(SQL)#UNION_operator
Select x,y,z From RDB$DATABASE
UNION ALL
Select a,b,c From RDB$DATABASE
UNION ALL
Select k,l,m From RDB$DATABASE
Notice however that this should only be used for small data. Firebird query length is limited to 64KB and even if that would not be so - abusing this method to inject lots of data would not be good.
If you really need to enter lot of similar (same structure) data rows - use Global Temporary Tables
The following discussion hopefully would give you more insights:
https://stackoverflow.com/a/43997801/976391
This has been bothering me for quite some time but I do come across two ways of using select statement in SQL.
The first way is the obvious :
Select column_Name
from table_Name
And then there is a second one:
Select column_Name a
from table_Name
What is the difference in using the second one?
The second version is more properly written using as:
Select column_Name as a
This produces a result set with one column. The column is called a. Without the alias, the column would be called column_name.
Although the as is optional, I strongly recommend using it for column aliases.
Using an column alias comes handy when you want to change the table column name for display purpose, for an example think that you want to generate the excel sheet from your program that returns data set from sql server. You can concentrate only the excel generation part in your application [treat this is an common routine to generate multiple excels] and you can change your column names what ever you want using the mentioned method. Alias can be seen in below syntaxas
SELECT yourcolumnname abc
SELECT yourcolumnname [abc def]
SELECT yourcolumnname AS abc
SELECT yourcolumnname AS 'abc def'
SELECT yourcolumnname AS [abc def]
how to write a select query with one or two column excluded instead of list down required columns.
for example we have a table Table1 with 10 columns (col_1,col_2,col_2 ..... col_10)
and we just want select col_1 to col_8.
instead of writing like
Select col_1, col_2, col_3 .... col_8 from Table1
should we select like this
Select -(col_9, col_10) * from Table1
This is possible but you would have to use dynamic SQL or a stored procedure. It would still mean having to specify the start and end column using fixed column names and indexes and if the columns weren't in running order (colN, colN+1, etc.) then it would become messy.
So I suppose your answer it, just type it out. The benefit you will gain from doing something cleaver is small.
I hope this helps.
Any ideas?
I think we can select column name first then write into query.
1 Select column with excluded.
SHOW COLUMNS FROM _table WHERE FIELD NOT IN ('column1','column2')
2 Then, Use the column result we've got from above query to write to select query column.
SELECT {result_aboved} FROM _table WHERE 1
but I don't think it can be only once query for this case. you have to query twice times at least.