How to copy multiple row with single column update - ruby-on-rails

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

Related

Query Importrange Issue

I am not sure why this functionality stopped working, but I am sure it has to do with inconsistent back end data or how the query condition "CONTAINS" needs to be changed. The IMPORTRANGE portion works just fine, but will not always pull data into the front end sheet. The query portion looks like this
SELECT Col3, Col2, Col1 WHERE Col2 CONTAINS "&'Job Number'!A1&" ORDER BY Col3 ASC,1
Column 2 contains job numbers that are xxxxx with another 3 digit code appended to end of it. It will only populate temporarily if I manually go into the sheet and edit the IMPORTRANGE range values. If I close the spreadsheet and open it again it will not populate. Does the data in Column 2 need to be a consistent datatype throughout the column or it will break the query?
Unfortunately we do not have a test sheet, so we can not know what the expected results would be.
Nevertheless, your formula syntax is not correct.
The correct syntax would be:
SELECT Col3, Col2, Col1 WHERE Col2 CONTAINS '"&"JOB"&A1&"' ORDER BY Col3 ASC,1
Please notice the syntax '"&"JOB"&A1&"'
(If you still have issues please share a test sheet and let us know.)

Use Query to display two columns and group rows based on one column's count?

I'm using an array to bring 2 columns of data together from 3 sheets.
There are duplicates in the second column, and I would like to group those duplicates together and display both Col1 and Col2, ordered alphabetically by Col1.
This is the formula I have right now:
=QUERY({'Sheet1!'A:B;'Sheet2!'A:B;'Sheet3!'A:B}, "Select Col1, count(Col2) where Col1 is not null group by Col1",1)
Which only displays Col1.
I've tried nesting QUERY, but I can't get it to work and can't find any direction anywhere online.
Here's an example sheet I made to show what I'm trying to do:
https://docs.google.com/spreadsheets/d/1_x0mXZC0ZjsHDCd6I0dDf9OI19lrzEcPYqfcMxuK74Y/edit?usp=sharing
In the example if an employee is listed twice the name may change but the email is consistent. I'm hoping to group by the email addresses and return only one name (it doesn't really matter which name).
I'm not sure if this is possible without formulas in more than one cell. Thank you either way!
#confuseddesk, try this array formula:
=ArrayFormula(QUERY({VLOOKUP(UNIQUE({Sheet2!B2:B;Sheet3!B2:B;Sheet4!B2:B}),{Sheet2!B2:B,Sheet2!A2:A;Sheet3!B2:B,Sheet3!A2:A;Sheet4!B2:B,Sheet4!A2:A},2,FALSE),UNIQUE({Sheet2!B2:B;Sheet3!B2:B;Sheet4!B2:B})},"Select * Where Col2 Is Not Null"))

How to select more than 2 columns in =QUERY()

I was trying to copy data from Sheet1 to Sheet2 using query. But I want to select MAX(A) and all the other columns where data exists (B,C,D...etc.) and group by B.
То clarify, in Sheet1 I have some similar rows which differ in col.A, which contains Timestamp, and a few other columns. I want to fetch only the latest record from each set of similar records.
So my Query is
=QUERY('Sheet1'!A3:S; "select MAX(A),B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S group by B")
It gives me error #Value! Unable to parse query string to FUNCTION query parameter 2 ADD_COL_TO_GROUP_BY_OR_AGG:C,...
The other columns need to be in the 'group by' to. See if this works
=QUERY('Sheet1'!A2:S, "select MAX(A),B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S group by B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S")
I used this function =QUERY('Sheet1'!$A$1:S;"select MAX(A) group by B label MAX(A) '' ";2) to populate the first column in Sheet2 and then for each column this function =ArrayFormula(LOOKUP($A$3:$A;'Sheet1'!$A$3:$A;'Имот'!B3:B)).
Basically the second function searches for match from the cell in column A in the same table with a cell in column A in the first table, gives the result of the current column and all this copies itslef in the whole column.
After that I copied the formula manually for each column. And my problem is already solved.

Google spreadsheet Query Error - column doesn't exist

Another problem with Google Spreadsheet API. I'm trying to use QUERY() function in order to get all customers from our database who are from Warsaw. Google API seems however to have a big problem with parsing my query. I've checked it few times and everything is OK. Tried semicolons, different apostrophes and column names—it still won't work. I type this code in the sheet cell:
=QUERY(IMPORTRANGE("0ArsOaWajjzv9dEdGTUZCWFc1NnFva05uWkxETVF6Q0E"; "Kuchnia polska!A3:G40"); "select B where E contains 'Warszawa'")
And get error like this:
Invalid query: Column [E] does not exist in table.
And I'm 110% sure that the column exists and the spreadsheet key is OK. ;)
If you are using the Query function with a direct reference to a rectangle of cells, then you use capital letters to refer to the columns. However, if you are using Query against the output of an array formula, such as the return from ImportRange, then you instead need to use Col1, Col2, ... to refer to the columns. So, your example query string would need to read:
"select Col2 where Col5 contains 'Warszawa'"
Working example:
=Query(ImportRange("0AtEH2Kw9zfIodEQ2dFNFam9sdEFEZlVnNEV3ZEZMZEE", "data!A:G"), "select Col3, count(Col4) group by Col3 order by count(Col4) desc label count(Col4) 'count'")
I've no idea why it doesn't just let you use names from a header row.
Alternatively you can try the FILTER function in this case and then you don't need to bother with SQL. :)
=FILTER(ImportRange("0ArsOaWajjzv9dEdGTUZCWFc1NnFva05uWkxETVF6Q0E"; "Kuchnia polska!B3:B40"); ImportRange("0ArsOaWajjzv9dEdGTUZCWFc1NnFva05uWkxETVF6Q0E"; "Kuchnia polska!e3:e40")="Warszava")
Although I admit that it's not so pretty because of the two importRange functions.
Hope it helped anyway.

What are the workarounds for returning variables number of columns from dynamic sql to entity framework?

So I have stored procedure which has some dynamic sql and it returns the calculated values. The returned result set though can be of variable columns.
So, now the problem is I want to add this procedure to entity framework 4 and I don't think its going to support it.So, now one way to do it would be creating temporary tables or table valued variables will kind of help but we don't want to go that way.
Have anyone of you come across this problem? What are the workarounds for such a situation. I basically want to return variable number of columns from a stored procedure and somehow map it to a complex type in entity framework ?
The workaround is that you have to modify your SP to always return all possible columns, and you use placeholders for any that aren't present. For example, if your SP returns col1, col2 and col3 in one scenario, and col1, col2, col4 and col5 in another scenario, then you need to always return col1, col2, col3, col4 and col5, and use dummy values (null or empty string, for example) for the columns that you don't have values for. E.g.
SELECT col1
, null as col2
, col3
...
The thing to remember about EF is that when it generates the complex type, it basically calls your SP and passes null for all the parameters. Whatever result set gets returned is what EF is going to use when creating the complex type.
We've found this one of the more frustrating things with EF.

Resources