In Excel if I have say numbers 1, 2 and 3 in columns A, B and C. I can write a formula in column D "=A+B" and then a formula in column E "=D+C".
Basically, I can use the result of a calculated column in the same row.
Can I achieve something similar in SQL with a single line of query.
For example, something like
SELECT A, B, C, A+B as D, D+C as E
FROM TABLE1
Result: 1, 2, 3, 3, 6
You can use calculated columns when create table as
CREATE TABLE tbl(id int, A int, B int, C int, D as A+B, E as A + B + C);
insert tbl(A, B, C) values (1, 2, 3)
Or use
SELECT A, B, C, A+B as D, + A+B + C as E
FROM TABLE1
Related
I want to transfer data into the table 2 using the data contain in table 1. The two tables have the following schema :
Table 1 :
column A, column B, column C
Table 2 :
column A, column B, column C, column D, column E
The result I want in table 2 is the following :
Table 2 :
A values of Table 1, B values of Table 1, C values of Table 1, NULL (for D values), NULL (for E values)
Is there an HQL command that can do this job ?
INSERT INTO TABLE2
SELECT A, B, C, NULL, NULL FROM TABLE1;
Can anyone please help me on this?
I want to generate a alpha numeric series like A.1, A.2, A.3, A.4, B.1, B.2 which should increment automatically if I add new row.
I have a columns A and B which will be look like below.
A B
-----
1 A
1 A
1 A
1 A
2 B
2 B
3 C
3 C
3 C
The result must be look like below:
A B C
-----------
1 A A.1
1 A A.2
1 A A.3
1 A A.4
2 B B.1
2 B B.2
3 C C.1
3 C C.2
3 C C.3
The below query can return your expected result.
SELECT A, B, B + '.' + CAST(ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS VARCHAR) AS C
FROM TableName
Generating the Alpha numeric series is difficult. Using ROW_NUMBER() in computed column is also not possible. So for your case view is the right choice to achieve your expectation:
CREATE VIEW dbo.vw_MyAlphaNumbericOrder AS
SELECT A, B, B + '.' + CAST(ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS VARCHAR) AS C
FROM dbo.TableName
So when ever you are inserting a new record, then SELECT * FROM dbo.vw_MyAlphaNumbericOrder will return with the alpha numeric series as column C.
Sample execution with the given sample data:
DECLARE #TestTable TABLE (A INT, B VARCHAR (2));
INSERT INTO #TestTable (A, B) VALUES
(1, 'A'),
(1, 'A'),
(1, 'A'),
(1, 'A'),
(2, 'B'),
(2, 'B'),
(3, 'C'),
(3, 'C'),
(3, 'C');
SELECT A, B, B + '.' + CAST(ROW_NUMBER() OVER (PARTITION BY B ORDER BY A) AS VARCHAR) AS C
FROM #TestTable
We can achieve this by below statement:
Select A,B,concat(B,'.',cast(row_number() over(partition by B order by B) as char(32))) as C
1.row_number() over (Partition by)-- will generate new row no. in each category of column B.
2.Cast -- will change the numeric value to character value where 32 specifies the length of data the field can hold
3. Concat-- will concatenate the required columns to create Alpha-numeric string
Let's assume I have a table (my_table) with four double columns A, B, C, and D. I'd like to create a new table that uses derived data from the existing table as such:
create table my_new_table as select A, B, C, D, A / (C + D), B / (C + D) from my_table;
Is there a way to define a local variable (e.g. my_var = C + D) that I could declare in the select statement and then use across the row, i.e.
create table my_new_table as select A, B, C, D, A / my_var, B / my_var from my_table;
Just wanted to know if this is feasible in Hive.
Yes, that is feasible, and here's an example of how to do it.
set my_var = C+D;
create table my_new_table as
select A, B, C, D, A / ${hiveconf:my_var}, B / ${hiveconf:my_var} from my_table;
Table A:
A|B|C|Version
1|2|3|1
1|2|3|2
I want table B to be
A|B|C|Version
1|2|3|2
Every row is the same as A except Version is increased by 1.
Let's say I only want to copy the rows from table A where version=1.
How do I do that?
insert into for "copying" + Where clause for only getting the one version "copied".
INSERT INTO tableB ( A, B, C, Version) VALUES
(SELECT * FROM tableA WHERE tableA.Version = 1);
INSERT INTO b( a, b, c, version )
SELECT a, b, c, version + 1
FROM a
WHERE version = 1
would work. Of course, since your WHERE clause limits you to just the rows where version = 1, you could just use a hard-coded 2 in your SELECT
INSERT INTO b( a, b, c, version )
SELECT a, b, c, 2
FROM a
WHERE version = 1
I have a SELECT inside a stored procedures which outputs data into this format.
A B C
rowkey 1 2 3
I need to transform it into something like this:
key value
rowkey A 1
rowkey B 2
rowkey C 3
How should I go about transforming it into this?
I am not allowed to touch the SELECT statement, so I should find a way to transform it perhaps creating a temporary table.
This ought to get you going if you're in SQL 2005 or higher...
You would store the result of your internal SELECT into a table variable, then transform it with something like the below:
DECLARE #test TABLE (rowkey int identity, A int, B int, C int)
INSERT INTO #test (A, B, C)
VALUES (1, 2, 3)
SELECT rowkey, [Key], [Value]
FROM
(SELECT rowkey, A, B, C
FROM #test) t
UNPIVOT
( [Value] FOR [Key] IN
(A, B, C)
) AS u