SQL Server : duplicate rows while changing a columns value - sql

I have a table T, say
1 | a
2 | a
I want to duplicate its rows while changing the value of the second column to b, so as to have
1 | a
2 | a
1 | b
2 | b
I came to
INSERT INTO T(col1, col2)
SELECT col1, 'b'
FROM T
but I get an error
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

Remove those extra parentheses in the SELECT :
INSERT INTO T(col1, col2)
SELECT col1, 'b' AS col2 FROM T;

Related

How to have a dummy value in a SQL select statement column?

So far I have a column that doesn't need values under the field, so I did: SELECT NULL AS Column1.
However, how do I query a column to actually have dummy values under it, say 'X' in all the rows for that column?
ex:
ID | Column2
1 | x
2 | x
3 | x
4 | x
The same way, just provide a value:
select id, 'X' as column2
from t;

sql - Only want rows with NULL in column if it isn't defined somewhere else as well

I have a table with possible NULL values in a column. I need to return the NULL values, but only if it isn't also defined somewhere else. Below, I want row F, but I do not want row B. We have some automation that attempts something but also has a fail over. We need to identify when both tries fail.
Column 1 | Column 2
A | 1
B | 1
B | null
C | 2
C | 1
D | 1
E | 2
F | null
F | null
G | 2
Simply do aggregation :
select col1, null as col2
from table t
group by col1
having max(col2) is null;
You can use not exists:
select t.*
from mytable t
where not exists (
select 1
from mytable t1
where t1.column1 = t.column1 and t1.column2 is not null
)
Or you can use window functions:
select column1, column2
from (
select t.*, max(column2) over(partition by column1) max_column2
from mytable t
) t
where max_column2 is null

How to do the equivalent of a double for loop append for SQL

I'm setting up this table in SQL (Presto, also new to sql) that has 2 columns, col1 and col2 These two columns are generated from two other existing tables table1 and table2. To keep things simple, let's say col1 from table1 has 3 values and col2 from table2 has 2 values. I want the table I want to create to look like this (let's call it table3, and I'll use col1.1 to denote the first value in that col and so on and so forth):
col1 | col 2
--------------------
col1.1 | col2.1
col1.1 | col2.2
col1.2 | col2.1
col1.2 | col2.2
col1.3 | col2.1
col1.3 | col2.2
I know how to do this in Python using Pandas like I did here (dummy example):
a = [1, 2, 3]
b = ['sam', 'john']
combined_lst = []
for i in a:
for j in b:
combined_lst.append({'col1': i, 'col2': j})
table = pandas.io.json.json_normalize(combined_lst)
print(table)
Table output:
col1 col2
0 1 sam
1 1 john
2 2 sam
3 2 john
4 3 sam
5 3 john
Basically it should be in the format of that table above. I've looked into trying out UNION ALL iteratively but I'm not too sure if I'm on the right track
I think you want a cross join:
select row_number() over (order by t1.col1, t2.col2) as id, t1.col1, t2.col2
from table1 t1 cross join
table2 t2;
The row_number() is in case the first column is supposed to be part of the data.

How to select distinct values on 2 columns in postgresql

I have a table with col A and col B. Col A and Col B can have repetitive values.
I want to select distinct values from Col A and Col B individually and populate them in 1 column as unique values. How do I do that?
Example
col_a | col_b
------+------
1 | 3
2 | 4
3 | 5
4 | 7
5 | 8
6 |
I want to extract the total unique values in a table that says 1,2,3,4,5,6,7,8. How do I do that?
You can use a UNION to combine two results with each column. A UNION will remove duplicates automatically:
select col_a as value
from the_table
union
select col_b
from the_table;
One simple approach is to use a union:
SELECT DISTINCT val
FROM
(
SELECT A AS val FROM yourTable
UNION ALL
SELECT B FROM yourTable
) t;
Demo

SQL - Select In from Array, and NOT in in same query

I have a userform built in VBA where my coworkers can enter multiple values that builds an array and places it into an IN statement, which works great. Problem is I need to also be able to display what values do not exist within the tables.
Example table
id | value
1 | value1
2 | value2
4 | value4
Then a query that could be generated would be
SELECT [id],[value] FROM [tablea] WHERE [id] IN (1,2,3,4)
Expected or desirable outcome would be as follows
id | value
1 | value1
2 | value2
3 | null
4 | value4
I've tried doing it like so;
SELECT [id],[value] FROM [tablea] WHERE [id] IN (1,2,3,4) AND [id] NOT IN (1,2,3,4)
since both arrays will be the same, this returns 0 of course.
I know I can do this with a union, and define the not in statement within the second union, but I'd like to do this without a union.. Any other thoughts?
This is on Microsoft SQL 2005
I unfortunately only have access to SELECT, since I'm performing queries either via VBA or Tableau. So I cannot create a derived table or have anything to reference other than the select statement.
You need a left join of some sort. One way would be to construct your query as:
select v.id, t.value
from (values (1), (2), (3), (4)
) v(id) left join
table t
on v.id = t.id;
Thanks to Joel Coehoorn for the tip towards using a CTE
I was able to accomplish this like so;
WITH numbers AS (
SELECT 1 AS num UNION ALL
SELECT 2 AS num UNION ALL
SELECT 3 AS num UNION ALL
SELECT 4 as num UNION ALL )
SELECT
COALESCE(id,num) as col1,
id as col2
FROM tablea
RIGHT JOIN numbers ON tablea.id = numbers.num
This would return
col1 | col2
1 | 1
2 | 2
3 | NULL
4 | 4