Replacing a sql column value based on a column another table - mapping

I have table1 (col1,col2) and table2(col1,col2) as given below/
Now i need to replace values of col2 of table1 with corresponding value of col1 and col2 from table2. So that the final table should look like this. how can we do this in query??

I assume table1.col2 and table2.col2 have the same text type(?)
update table1 set table1.col2=table2.col2
from table1
join table2 on (table1.col2=table2.col1)

Related

How can I combine rows based on a column value in google sheets

If I have a table like this:
How can I combine (by addition) the rows based on the values in column A so I get this table as a result:
You need to create a Pivot Table. Please have a look https://support.google.com/docs/answer/1272900?hl=en&co=GENIE.Platform%3DDesktop
You can try:
={{A1:B1};query({A2:B},"select Col1,sum(Col2) where Col1<>'' group by Col1 order by sum(Col2) desc label sum(Col2) ''")}
OR
={{A1:B1};byrow(unique(filter(A2:A,A2:A<>"")),lambda(z,{z,sumif(A:A,z,B:B)}))}

Google sheet query select column from different spreadsheet

I have this query:
=Query({'Sheet request '!A2:Q;'Sheet sent '!A2:Q},"Select Col1,Col2,Col4,Col3 Where Col1 is not null",1)
How I am going to add this query so that I can have the column 12 from tab genseat?
=Query({genseat!A2:O},"Select Col12 Where Col1 is not null",1)
Since your first query generates 4 columns, you can only append a second query if you also generate 4 columns.
If you want 3 static columns of data in the second query, you could use something like:
=Query({genseat!A2:O},"Select Col12,'value1','value2','value3' Where Col1 is not null label 'value1' '','value2' '','value3' '' ",1)
Then you can add your queries together using {query1;query2} and put a third query around them to bring back records that are not null:
query({query1;query2},"where Col1 is not null",1)

How to count records based on subset of data in googlesheet?

I have a table of tickets assigned to users. The tickets are either in Open, Closed or Resolved state.
I'm able to create the list of owners using the formula:
=UNIQUE(D9:D20)
I'm also able to generate the number of total issues against each user using the formula:
=COUNTIF(D9:D20, A2)
But now I need to calculate the number of Open, Closed or Resolved issues for each individual users.
Any suggestions please?
=QUERY(C8:D,"select D,COUNT(D) where D<>'' group by D pivot C order by COUNT(D) desc",1)
then just make a totals column to the right of the three statuses and do a SUM() of each row
You may want to consider using this formula:
={{query({C1:D, C1:C},"Select Col2, count(Col1)
where Col2 is not null
group by Col2
pivot Col3 limit 0",1),"Total Issues"};
query(query({C1:D, C1:C},"Select Col2, count(Col1)
where Col2 is not null
group by Col2
pivot Col3",1),"Select Col1 offset 1",0),
ARRAYFORMULA(N(query(query({C1:D, C1:C},"Select Col2, count(Col1)
where Col2 is not null
group by Col2
pivot Col3",1),"Select Col2, Col3, Col4 offset 1",0))),
query(query({C1:D, C1:C},"Select Col2, count(Col1)
where Col2 is not null
group by Col2",1),"Select Col2 offset 1",0)}
Output:
Additional Reference:
Replace Blank Cells with 0 in Query Pivot in Google Sheets
The easiest way to do this is to create a pivot table.
I've got my final version set up like this...
Steps:
Delete the current summary table you have above your data table. Columns A:D can only contain data and the column headers.
Select columns A:D
Data > Pivot table - Existing sheet - Sheet1!F1
In the Pivot table editor panel on the right...
Rows - Add - Owner
Columns - Add - Status
Values - Add - Ticket # - Summarize by - COUNT
Filters - Add - Owner - Status - Uncheck (Blanks)
You can tweak it from there. As you add data to the bottom of the table, it will automatically update the pivot table. NOTE: If you add a new owner, you will need to update the Filter and place a check beside their name.

How to add 'on' clause to union?

I am currently successfully union-ing two tables and joining on a third.
select col1,col2
from table1
union
select col1,col2
from table2
join(select distinct(id), date from table3) on table3.id = table2.id
how do I only union table1 and table2 where table1.col3 is in table2.id?
In other words if there is a value in table1.col3, I want to get all the data from table2 where there is a match on table2.id and union the tables.
edit:
table1 (source table) table2 table3
id col1 col2 id col1 col2 id date col4
I want all records from table2
I want to get records from table1 where there is match between table2.col1 and table1.id
I want to get records from table1 where there is a match between table3.id and table1.id where date >='2018-12-01'
Notes: table1 and table2 are exactly the same, why i went with union.
Your requested UNION actually makes for a fairly simple query since it can be built from 3 separate simple queries from your requirements;
SELECT col1, col2 FROM table2
UNION ALL
SELECT col1, col2 FROM table1 WHERE id IN (SELECT col1 FROM table2)
UNION ALL
SELECT col1, col2 FROM table1 WHERE id IN (SELECT id FROM table3 WHERE date >= '2018-12-01')
Note that UNION ALL allows for duplicates (a single row may show up once for each query) while changing to UNION will remove all duplicates.
A runnable dbfiddle example

How to copy multiple row with single column update

I want to copy all the rows from my table with single column update.
Example.
Table County has 1000 rows and i want to copy all the rows with single column update.
What is the proper way to do that ?
This question describes how to use clone to copy your record.
Assuming the 1000 records are already in an Enumerable called counties, we end up with
counties.each { |county|
county_copy = county.clone
county_copy.col3 = update_function(county_copy.col3)
county_copy.save
}
Assuming I understand you correctly, I'd do something like
INSERT INTO NewTable (Col1, Col2, Col3)
SELECT Col1, Col2, UpdateFunction(Col3)
FROM County
Where Col3 is the column you want to update, and UpdateFunction is the function you wish to use to update the column.
EDIT: Of course this is SQL, not Rails - I didn't look close enough at the question's tags :-)
#krunalshah You can build a array of hashes and pass that array to
Country.create(array)
,though it will execute multiple insert queries.
Other option use
connection.execute(<<-SQL)
insert into country1 (col1, col2)
select col1, col2 from countries
SQL

Resources