How would I do this in SQL Server? (I know it won't run as written but it illustrates the question better than I can explain)
SELECT SQRT(number) WHERE number IN (4,9,16,25)
It would return multiple rows of course
you can use table value constructor
select sqrt(number)
from (
values (4),(9),(16),(25)
) as T(number)
or use union all
select sqrt(number)
from (
select 4 union all
select 9 union all
select 16 union all
select 25
) as T(number)
sql fiddle demo
You could create a derived table:
SELECT SQRT(number)
FROM (
SELECT 4 AS number
UNION ALL SELECT 9
UNION ALL SELECT 16
UNION ALL SELECT 25
) A
Related
I am trying to union some table. But I am having some issues with the query as I have the following error:
Queries in UNION ALL have mismatched column count; query 1 has 10 columns, query 2 has 12 columns at [3:1]
The query used is:
SELECT * FROM `table_1`
UNION ALL
SELECT * ,null, FROM `table_2`
UNION ALL
SELECT * FROM `table_3`
UNION ALL
SELECT * FROM `table_4`
UNION ALL
SELECT * , null, FROM `table_5`
Please someone have any suggestions?
In a UNION ALL all queries need to be the same number of columns.
You need to post the schema of all tables. But according to the error message you posted, this could help for the 2 first tables:
SELECT *, null FROM `table_1` -- assuming table_1 has 10 columns, add 1 more empty to match the table_2
UNION ALL
SELECT * FROM `table_2` -- assuming table_2 has 11 columns
-- make the same to the rest of queries
-- UNION ALL
-- SELECT * FROM `table_3`
-- UNION ALL
-- SELECT * FROM `table_4`
-- UNION ALL
-- SELECT * , null, FROM `table_5`
Update:
Adding a example of union all declaring the columns:
SELECT
col1_str,
col2_str,
col3_int,
col4_int
FROM `table_1`
UNION ALL
SELECT
col2_str,
FORMAT_DATE("%Y-%m-%d", col1_date) as col1_str, -- transform a date column to string
col3_int,
null
FROM `table_2`
I am trying to pull two tables from an Oracle SQL database, and want to join them sequentially, so they appear as if they are one list.
List one has items [1,2,3,4]
List two has items [a,b,c,d]
I want to output [1,2,3,4,a,b,c,d]
Any thoughts?
One option uses a UNION with a computed column:
SELECT val
FROM
(
SELECT val, 1 AS position FROM table1
UNION ALL
SELECT val, 2 AS position FROM table2
) t
ORDER BY
position, val;
Demo
Note that I assume that all data here is text. If not, e.g. the first table be numeric, then we would have to do a cast along the way. But, this is not the main focus of your question anyway.
SELECT id_1, value_column1 from table_1
UNION
SELECT id_2, value_column2 from table_2;
if the types of columns are different - make sure you cast/convert them to char() - the resulting type should be same.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm
use union, i think 1,2,3 as numeric value that why converted it on varchar as for union you have to same data type
with t1 as (
select 1 as id from dual union all
select 2 from dual union all
select 3 from dual union all
select 4 from dual
), t2 as (
select 'a' as item from dual union all
select 'b' from dual union all
select 'c' from dual union all
select 'd' from dual
)
select cast(id as varchar(20)) as id from t1
union
select * from t2
demo
output
1
2
3
4
a
b
c
d
I want to know query for the result
You can use PIVOT/UNPIVOT. Solution is here
You can achieve your expected result by below query,
SELECT *
FROM cardata
PIVOT(AVG(Price) FOR YEAR IN ([2010], [2011], [2012])) AS PivotTable;
So here you must have to use aggregate function to use Pivot.
You need to use PIVOT : SQL Fiddle
Data
create table cars(brand varchar(100),myear int,price bigint)
insert into cars
select 'audi',2010,5000000 union all
select 'audi',2011,5340000 union all
select 'audi',2012,5890000 union all
select 'bmw',2010,6000000 union all
select 'bmw',2011,6780000 union all
select 'bmw',2012,4450000 union all
select 'maruti',2010,4540000 union all
select 'maruti',2011,7800000 union all
select 'maruti',2012,9000000
Query
SELECT * FROM
cars
PIVOT
(
MAX(PRICE) FOR MYEAR IN ([2010],[2011],[2012])
)P
Sir I want to generate a series of integers say (from 1 to 1000) using query statement. BUT i do not want to use any Table nor want to create any Table.
I know this is possible in SQL Server but show the similar query for MS-Access 2003 too.
What you describe cannot be done in a single Access SQL query.
It you do not want to create a table. Then in Access I only think that you have this option left:
Select 1
Union All Select 2
Union All Select 3
Union All Select 4
Union All Select 5
Union All Select 6
Union All Select 7
Union All Select 8
Union All Select 9
Union All Select 10
Union All Select 11
--And so on
Sir I want to generate a series of integers say (from 1 to 1000) using query statement. BUT i do not want to use any Table nor want to create any Table.
I know this is possible in SQL Server but show the similar query for MS-Access 2003 too.
What you describe cannot be done in a single Access SQL query.
It you do not want to create a table. Then in Access I only think that you have this option left:
Select 1
Union All Select 2
Union All Select 3
Union All Select 4
Union All Select 5
Union All Select 6
Union All Select 7
Union All Select 8
Union All Select 9
Union All Select 10
Union All Select 11
--And so on