add a temporary column with a value - sql

I have a select statement like this
select field1, field2
from table1
What I want is to have a newfield with only value "example".
newfield does not exist in the table, but when I run this query it kinda makes a virtual column with the values of example field.

select field1, field2, 'example' as TempField
from table1
This should work across different SQL implementations.

You mean staticly define a value, like this:
SELECT field1,
field2,
'example' AS newfield
FROM TABLE1
This will add a column called "newfield" to the output, and its value will always be "example".

I'm rusty on SQL but I think you could use select as to make your own temporary query columns.
select field1, field2, 'example' as newfield from table1
That would only exist in your query results, of course. You're not actually modifying the table.

In this example, the TABLE registrofaena doesn't have the column called minutos. The query returns a column "minutos" with demora/60 as the content (the values represent the delay in minutes). The table is not modified in the process.
This is the query:
SELECT idfaena,fechahora,demora, demora/60 as minutos,comentario
FROM registrofaena
WHERE fecha>='2018-10-17' AND comentario <> ''
ORDER BY idfaena ASC;
This is the view:

select field1, field2, NewField = 'example' from table1

select field1, field2, '' as newfield from table1
This Will Do you Job

Related

Select rows with same id but different value in other column

Sample data:
I want to get row 1, 5, and 10. I hope you could help me, Thanks.
If you want to have distinct data based on different columnn data, you should use GROUP BY query.
If you have table called tbl which has column field1, field2 and field3 and you want to get all rows which has distinct data for all field in 'tbl', then your query should be like this :
SELECT field1, field2, field3
FROM tbl
GROUP BY field1, field2, field3

select distinct value in sql with 35 columns

I have table that contains 35 columns, how would I select only the distinct records from that table, this is what my query looks like:
`SELECT field1, field2, field3 etc... from table1 group by field1, field2, field3 etc...`
This gets me the unique results that I want but I have 35 columns, its too long to group all 35 rows - is there any efficient way of doing this:
by doing this, I get repeated results:
SELECT distinct * from table1
DISTINCT will be also faster:
Your query should looks like:
SELECT distinct field1, field2, field3 etc...
from table1
Use distinct once.
This will affect all columns.
You don't need GROUP BY if you use DISTINCT. But you have to list all the fields that you want to show, and for sure not the primary key.
SELECT DISTINCT field1, field2, field3 etc...
FROM table1

How to insert rows with structure from one table to another table?

What is the query in SQL Server to insert rows with structure from one table to another table?
A couple ways
SELECT INTO
SELECT
field1,
field2,
...
INTO Table1
FROM Table2
INSERT INTO
INSERT INTO Table1
field1,
field2,
...
SELECT
field1,
field2,
...
FROM Table2
Sounds like you want something like this:
INSERT INTO DestTable (Column1,COlumn2)
SELECT Column1, Column2
FROM SourceTable
Here is the documentation for the INSERT...EXECUTE statement that will allow you to do what you need.

Refer to other SQL SELECT statements aliased as tables in FROM clause

I have a very large query that follows the format below:
select ...
from ( select field1,
field2
from some_table ) table1,
( select field1,
field3
from other_table ) table2
where .....
Is is possible for me to refer to one of the tables "defined" in the from clause, lets say table1, in one of the other table definitions in the from clause?
For example:
select ....
from ( select field1,
field2
from some_table ) table1,
( select table1.field1,
field3
from other_table,
table1 ) table2
where .....
Disclaimer: What I am trying to do is not as simple as the example above. The example is simply to illustrate the idea.
WITH
table1 AS
(
SELECT field1, field2
FROM some_table
),
table2 AS
(
SELECT field1, field2
FROM other_table, table1
)
SELECT *
FROM table2
If you are using SQL 2005, you can use Common Table Expressions for doing what you are trying; Quassnoi gives us an example, in Oracle I don't know how to achieve it though

Generate field in MySQL SELECT

If I've got a table containing Field1 and Field2 can I generate a new field in the select statement? For example, a normal query would be:
SELECT Field1, Field2 FROM Table
And I want to also create Field3 and have that returned in the resultset... something along the lines of this would be ideal:
SELECT Field1, Field2, Field3 = 'Value' FROM Table
Is this possible at all?
SELECT Field1, Field2, 'Value' Field3 FROM Table
or for clarity
SELECT Field1, Field2, 'Value' AS Field3 FROM Table
Yes - it's very possible, in fact you almost had it!
Try:
SELECT Field1, Field2, 'Value' AS `Field3` FROM Table