I would like to know a way of adding one additional column to a BigQuery table, that will populate all the rows for this newly created column with specific constant value.
I know how to create column with NULL values:
ALTER TABLE project_id.dataset.table
ADD COLUMN A STRING
But my goal is to also add the ingestion time with CURRENT_TIMESTAMP() function. Is it even possible with one command? Or maybe I need to apply subsequently some second command?
Seems like a solution is to use another query after the mentioned one:
UPDATE project_id.dataset.table SET A = CAST(CURRENT_TIMESTAMP() AS STRING) WHERE 1=1
Assume I have a query that returns a result set of columns A and B from table First_Table. I want to limit the result set to those columns if the value of column X in table Second_Table is 0, and I want to add column C from table First_Table if the value of column X is 1.
The problem is easily resolved using a Python for example whereby I just have a variable as an empty string if value in column X is 0 or it would be equal to the string 'First_Table.ColumnC AS [Dynamic Value],', and I just format the sql in the script accordingly.
If Else solution is not an elegant way because I have multiple columns to add dynamically depending on multiple values...
I am just looking for some ideas on directions.. I have been looking at this for a while, might be bogged up
Dynamic sql is the best way to resolve this as suggested in the comments.
In application I am working on.
I have to input from user through excel and first put it in temporary sql table & then from temporary table to final target table.
My query is failing while putting data from temporary table to target table.
Because some values present in temporary table are out of range of columns in target table.
How can I check if values present in temporary table are within range of column of target table?
I have to check like this
20 < len(temporary_table.column1) < 50
or is there any better way
If you are using SQL server you can use below query for data checking.
temporary_table.column1 between 20 and 50
If you are looking based on the column max length. For example, your columns have datatype varchar(100) then you can use the condition like this
where len(temporary_table.column1)<=100
Extending on the above answer you can just use col_length instead of hard coding the value on the target column. This makes it more automated and less prone to mistakes (entering a value mistakenly)
where len(temporary_table.column1) <= COL_LENGTH ( 'target_table' , 'column1' )
So, I learnt the joys(/s) of INNER JOIN today. Now I need to copy select records from certain specific columns into my main column. Once again, I'll be using the following format:
Please bear in mind that I'm working with 300,000 records. Copy/pasting one by one is not practical.
Tables:
MainTab
CritTab
Key column:
MainTab:- LINK
CritTab:- FORNLINK
Columns to be copied
CritTab.DATE
CritTab.CODE
Criteria
WHERE CritTab.[CODE] = <This kinda code> OR <This other code>
AND CritTab.[FORNLINK] = MainTab.[LINK]
So, to clarify: I need to copy specific columns from CritTab to MainTab. As not every record in MainTab will have a corresponding CritTab entry, I cannot simply copy and paste the entire column, as some records won't match up.
Is it possible to do this with a query in Access?
I'm not sure if you need the columns added or not, but if you do run these first. I am changing the column name because you should never use keywords as a column, table or variable name.
ALTER TABLE MainTab ADD COLUMN DATENAME DATETIME
ALTER TABLE MainTab ADD COLUMN CODENAME (WHATEVER DATA TYPE IT IS IN CritTab)
I am moving the JOIN operator from the where clause to the FROM clause. This usually makes it easier to understand how the data is being built and clutters the WHERE a bit less in my opinion. Note: I am doing the same sort of syntax as the article Mike posted in the comments.
UPDATE MainTab INNER JOIN CritTab ON CritTab.[FORNLINK] = MainTab.[LINK]
SET MainTab.DATENAME = CritTab.[Date], MainTab.CODENAME = CritTab.[CODE]
WHERE CritTab.[CODE] = <This kinda code> OR CritTab.[CODE] = <This other code>
I have created a lookup table in Access to provide the possible values for a column. Now I need to update this column with the data it had before I converted the column. I am unable to figure out a SQL Query that will work. I keep getting the error "An UPDATE or DELETE query cannot contain a multi-valued field." My research has suggested that I just need to set the value of the column but this always updates 0 records:
UPDATE [table_name] SET [column_name].Value = 55 WHERE [table_name].ID = 16;
I know this query will work if I change it to update a text column, so it is definitely a problem with just this column.
If you're adding a value to your multi-valued field, use an append query.
INSERT INTO table_name( [column_name].Value )
VALUES (55)
WHERE ID = 16;
If you want to change one particular value which exists in your multi-valued field, use an UPDATE statement. For example, to change the 55 to 56 ...
UPDATE [table_name]
SET [column_name].Value = 56
WHERE [column_name].Value = 55 And ID = 16;
See Using multivalued fields in queries for more information.
I have figured this out! It certainly was counter-intuitive! You have to use an INSERT statement to do the update.
-- Update a record with a multi-valued field that has no value
INSERT INTO [table_name] ( [[column_name].[Value] )
VALUES(55)
WHERE [table_name].ID = 16;
This confused me because I was expecting an UPDATE statement. I think it actually inserts a record into a hidden table that is used to associate multiple values with this column.
I am working with Sharepoint, I created the tables as multi-value fields, ran into the error with my INSERT INTO statement, went back to Sharepoint to change to non-multi-value fields, but that didn't fix it.
Recreated the table without using multi-value fields, and the INSERT INTO worked just fine.
do not use the .value part
UPDATE [table_name] SET [column_name] = 55 WHERE [table_name].ID = 16;
INSERT INTO Quals (cTypes.[value])
SELECT Quals_ContractTypes.ContractType
FROM Quals_ContractTypes
WHERE (Quals.ID = Quals_ContractTypes.ID_Quals);
I gotta say I didn't understand very well your problem but I saw something strange in your query. Try this:
UPDATE [table_name] SET [column_name]= 55 WHERE [table_name].ID = 16;
UPDATE:
Look at this link: it has an example
UPDATE Issues
SET Issues.AssignedTo.Value = 10
WHERE (((Issues.AssignedTo.Value)=6)
AND ((Issues.ID)=8));
NOTES
You should always include a WHERE
clause that identifies only the
records that you want to update.
Otherwise, you will update records
that you did not intend to change. An
Update query that does not contain a
WHERE clause changes every row in the
table. You can specify one value to
change.
The Multi-Valued field refers to Access databases that have tables with columns, that allow you to select multiple values, like a Combo Checkbox list.
THOSE are the only Access types that SQL cannot work with. I've tested all Access lookup possibilities, including hard-coded values, and lookup tables. They work fine, but if you have a column that has the Allow Multiple select options, you're out of luck. Even using the INSERT INTO as mentioned below, will not work as you'll get a similar but different error, about INSERTing into multi-valued fields.
As mentioned it's best to avoid using such tables outside of Access, and refer to a table specifically for your external needs. Then write a macro/vba script to update the real tables with the data from the "auxiliary" table.