tis my first question so sorry if its not well structured, I have looked for the answer for a while but no joy so here goes..
basically I have 20 columns and want to take the result of adding columns (a+b) (b+c) etc and make this the value of my new columns,
when i do a simple select statement the values appear as expected but i cant seem to get them to appear into a new table
the columns are varchars
this is one of the 20 select queries
((accidentlogs.before_T18/16-accidentlogs.before_T19/16)/21.954),
It seems like such an easy function and it probably is but stick a fork in me on this one
You can use the result of a SELECT Statement as a value for an INSERT Statement. The exact syntax may vary for the SQL Dialect you use (Oracle, Postgres, MySql...)
This is the code for postgres:
INSERT INTO table (field1, field2...) SELECT 'value1', 'value2'...
Related
I know that you can use DISTINCT with multiple columns and not use it more than once in a single SQL query, but what is the reason for this ? I could think of the case that the output of SQL is rows and applying distinct on 2 columns seperately would result in a conflict of which rows to output but is there any more to it? Perhaps someone could clarify this.
This is common for many SQL implementations(I have tried MySQL, PostgreSQL and SQLite). An example query would be like
SELECT DISTINCT(column1), DISTINCT(column2) FROM table;
SAS has a sum(of col1 - coln ) function which finds the sum of all the values from col1, col2, col3...coln. (ie, you don't have to list out all the column names, as long as they are numbered consecutively). This is a handy shortcut to find a sum of several (suitably named) variables.
Question - Is there a DB2/SQL equivalent of this? I have 50 columns (they are named col1, col2, col3....col50 and I need to find the sum of them.
ie:
select sum(col1, col2, col3,....,col50) AggregateSum
from foo.table
No, DB2 has no such beast, at least to my knowledge. However, you can dynamically create such a query by first querying the database metadata to extract the columns for a given table.
From memory, DB2 has a sysibm.syscolumns table which basically contains the column information that you could use to construct a query on the fly.
You would first use a query like:
select column for sysibm.syscolumns
where schema = 'foo' and tablename = 'table'
and column like 'col%'
(the column names may not match exactly but, since they're not the same on the differing variants of DB2 (DB2/z, DB2/LUW, iSeries DB2, etc) anyway, that hardly matters).
Then use the results of that query to construct your actual query:
select col1+col2+...+colN AggregateSum from foo.table
where the col1+col2+...+colN bit has been built from the previous query.
If, as you mention in a comment, you only want the eighteen "highest" columns (e.g., if columns 1 thru 100 exist, you only want 83 thru 100), you can modify the first query to do that, with something like:
select column for sysibm.syscolumns
where schema = 'foo' and tablename = 'table'
and column like 'col%'
order by column desc
fetch first 18 rows only
but, in that case, you may want to call the columns col0001, col0145 and so on, or make the sorting able to handle variable width numbers.
Although it may be easier (if you can't change the column names) to get all the columns colNNN, sort them yourself by the numeric (not string) value after the col, and throw away all but the last eighteen when constructing the second query).
Both these options will return only eighteen rows maximum.
But you may also want to think, in that case, about moving the variable data to another table, if that's possible in your situation. If you ever find yourself maintaining an array within a table, it's usually better to separate that out.
So your main table would then be something like:
main_id primary key
other_data
and your auxiliary table would be akin to:
main_id foreign key to main(main_id)
sequence_nm
other_data
primary key (main_id, sequence_num)
That would allow you to have sparse data if needed, and also to add data without having to change the schema of the main table. The query to get the latest eighteen results would be a little more complicated but still a relatively simple join of the two tables.
In SQL Server, I can do this with the help of Cursor (to loop through each row in the result set and build my string). But I don't know how to do the same in SQLite, this task is not like the so-called Pivoting which as far as I know, we have to know the exact rows we want to turn to columns. My case is different, I want to select all the values of a specified column into a string (and then can select as a column of 1 row), this column can have various values depend on the SELECT query. Here is a sample of how it looks:
A | B
------------
1 | 0
8 | 1
3 | 2
... ....
I want to select all the values of the column A into a string like this "183..." (or "1,8,3,..." would be fine).
This can be used as a column in another SELECT, I have to implement this because I need to display all the sub-infos (on each row of column A) as a comma-separated list in another row.
I don't have any idea on this, it seems to need some procedural statements (such as placed in a procedure) but SQLite limits much on how I can do with loop, variable declaration,...I'm really stuck. This kind of task is very common when programming database and there is no reason for me to refuse doing it.
Please help, your help would be highly appreciated! Thanks!
If you're just trying to get all the values from Column A into a single record, then use GROUP_CONCAT:
select group_concat(a, ',')
from yourtable
SQL Fiddle Demo
The main problem is that you are think like an application programmer. SQL does a lot of things for you.
SELECT *
FROM tableA
WHERE A IN (SELECT A
FROM tableB)
No need to resort to cursors, stored procedures and multiple queries.
I want to use select * to select all the fields from a table but I also want to use an alias on just one field. Is it possible? If so what would the syntax be.
Example
select *,
item1 as hats
from factory
I have not been able to make anything like this work, Thanks!
I just tried
select *, item1 as hats from factory
on mysql and postgres and on both DBMS it runs fine. Only take into consideration that you get two columns with item1. One column named item1 and one named hats.
That should work, but keep in mind that your item1 column will be duplicated!
Suppose your table has the columns (id, item1, item2) then your proposed select will return (id,item1,item2,hats).
It's valid in MS Sql Server as well but the column you are alias'ng will be duplicated.
Tried the query in Sql server 2005 and it did worked.
select *,col2 as 'customcol' from table1
Yes, it is possible to use that format however, use of Select * is not recommend. It is better to enumerate the columns you want. Some database products may balk if the list of columns in combination with your aliased column produces a duplicate column name. However, again, you can solve this by enumerating the columns.
Is there a another way to subtract the smallest value from all the values of a column, effectively offset the values?
The only way I have found becomes horribly complicated for more complex queries.
CREATE TABLE offsettest(value NUMBER);
INSERT INTO offsettest VALUES(100);
INSERT INTO offsettest VALUES(200);
INSERT INTO offsettest VALUES(300);
INSERT INTO offsettest VALUES(400);
SELECT value - (SELECT MIN(value) FROM offsettest) FROM offsettest;
DROP TABLE offsettest;
I'd like to limit it to a single query (no stored procedures, variables, etc) if possible and standard SQL is preferred (although I am using Oracle).
I believe this works as of ANSI 1999.
SELECT value - MIN(value) OVER() FROM offsettest;
It would have helped you see your actual query, though, since depending on whether you need to manipulate more than one column this way, and the various minimums come from different rows, there may be more efficient ways to do it. If the OVER() works for you, then fine.