I'm trying to filter-in only the rows from table Today which were not in table Last time (The first two columns are the identifiers).
These were my failing tries:
=IFNA(QUERY(ArrayFormula(if(countif(V8:V30&" "&W8:W30,Q8:Q30&" "&R8:R30)=0,Q8,"")),"select Col1 where Col1 <> ''"), "")
Tried to add an = in the criterion field.
=IFNA(QUERY(ArrayFormula(if(countif(V8:V30&" "&W8:W30,"="&Q8:Q30&" "&R8:R30)=0,Q8,"")),"select Col1 where Col1 <> ''"), "")
Use this formula
=FILTER(A3:D, NOT(COUNTIF(F3:F, A3:A)))
FRESH is values in A NOT in F
Reference:
FILTER
Related
Suppose I have 4 columns: A, B, C, D and one additional column: Index in a row.
Suppose I want to save a value of "50" but depending on the "Index" value of the row mysql will save 50 in either A, B, C, D. Say if Index=1 for that particular row, then 50 goes to column "A".
Is there a mysql query that will accomplish this all in one go? Or do I have to first read the index value, then make a switch statement with four different update queries to accomplish this?
Here is one way to do it:
UPDATE mytable
SET
A = CASE WHEN index = 1 THEN 50 ELSE A END,
B = CASE WHEN index = 2 THEN 50 ELSE B END,
C = CASE WHEN index = 3 THEN 50 ELSE C END,
D = CASE WHEN index = 4 THEN 50 ELSE D END
WHERE ...
The query works by doing conditional value assignment to each column, according to the value of index. When a column does not need to be updated, its original value is simply reassigned, hence turning the operation to a no-op.
I am trying to write a query that selects values from certain rows based on their parameters, then does some calculations and returns the result. I also need to update the rows that were selected. I can only think of ways to do both of these actions with separate queries, but is there a way to do both at once?
Example queries would be:
SELECT SUM(a.val1*b.val1)
FROM a, b
WHERE a.val2 = condition1 AND b.val2 = condition2;
UPDATE a
SET a.val3 = a.val1*b.val1
FROM a INNER JOIN b ON a.val2 = condition1 AND b.val2 = condition2;
Is there a way to combine them?
No. There is no syntax in SQL that allows you to retrieve values and update them in the same query. Use SELECT to retrieve values and UPDATE to change them.
You can use SELECT inside an UPDATE statement as part of the calucalation, but the result will not be the values of what has been updated, only the number of rows that were updated.
Your SELECT statement has a small mistake in it and should be as follows:
SELECT SUM(a.val1*b.val1) FROM a, b WHERE a.val2 = 1 and b.val2 = 1;
This only returns one row: the sum of the product of val1 columns in tables a and b where the val2 columns meet certain conditions.
It is not clear what you are trying to achieve by updating table a with the result of this. If you are looking to set val3 in table a with the product of val1 in tables a and b if the val2 in those tables meet certain criteria, the following might work, but you need to add a join between columns in both tables otherwise val3 will be set to the product of val1 in table a and all the val1 values in table b, which may not be what you want.
UPDATE a
SET a.val3 =
(
SELECT a.val1*b.val1
FROM b
WHERE b.key = a.key
AND b.val2 = condition2
)
WHERE a.val2 = condition1;
Nope :)
In SQL it is always different queries. You can write a function that will do 2 actions, but never one query.
This will have to be done in two steps - Select and Aggregate then Update.
I'm building a quality assurance sheet which automatically pulls values from one tab, to a separate one, for each agent.
I'll need to write one per agent. To simply the process, would it be possible to reference the first column selected without specfiyng it by letter?
So GROUP BY and LABEL could be X where X so I only need to edit the select row.
Below is the current query I am using.
=QUERY('Bimonthly Report 2017'!A:AN,
"SELECT D, sum(F)/count(F), sum(L)/count(L)
WHERE F >0
GROUP BY D
LABEL D 'Category'"
, 2)
It's possible. Just input the data as a array literal.
=QUERY({'Bimonthly Report 2017'!A:AN}, "SELECT Col4, sum(Col6)/count(Col6), sum(Col12)/count(Col12) WHERE Col6 >0 GROUP BY Col4 LABEL Col4 'Category'" , 2)
I saw recently (can't find it now) this syntax:
... LIKE CONCAT('%',col1,'%')
It is working for Selects but for update, it affects 0 rows
this is my query:
update locations set email = (
select col2 from vendoremail
where locations.city LIKE CONCAT('%',col1,'%')
AND locations.zip LIKE CONCAT('%',col1,'%')
)
here is a sample of col1 :
"455 N Cherokee St: Muskogee, OK 74403"
without the quotes
I hope I have given enough data to elicit an answer or two - thank you!
You have it backwards. You want to put the city and zip into the pattern.
update locations set email = (
select col2 from vendoremail
where col1 LIKE CONCAT('%', locations.city, '%', locations.zip, '%')
)
However, this may not always work properly. If you have two vendors in the same city+zip, the subquery will return 2 emails, but when you use a subquery as a value it has to return only 1 row. You can add LIMIT 1 to the subquery to prevent an error when this happens. But it will be selecting one of the vendors unpredictably -- maybe you should come up with a more reliable way to match the tables.
If col1 is = "455 N Cherokee St: Muskogee, OK 74403"
i think location.city is = Muskogee and locations.zip is = 74403
then the query should be
update locations
set email = (
select col2 from vendoremail
where col1 LIKE CONCAT('%',locations.city,'%')
AND col1 locations.zip LIKE CONCAT('%',locations.zip,'%')
)
e.g.
Table A (id - auto increment)
id labels names
1 a1,a3,b4 a1
2 a2,b5 a2
3 a1,b4 a2
What i want is to update names column such as "existing_names_data + ', B'" where labels like %b4%
I know following query works with integers not with strings, but i tried and failed anyways -
update TableA set names=names+ " B" where labels like '%b4%'
Is there any such query for strings?
Or, what should I do to get desired output?
Desired Output
id labels names
1 a1,a3,b4 a1, B
2 a2,b5 a2
3 a1,b4 a2, B
Thanks. Regards,
Use the CONCAT() function:
UPDATE foo SET bar = CONCAT(bar, ' B') WHERE foobar = 'barfoo';
..that said, you should really just use a different relational table and normalize your data rather than add comma/semicolon/space seperated columns.
Consider the following structure:
messages:
id
name
message_labels:
id
message_id (JOIN message_labels ON message_labels.message_id = message.id)
label
message_names:
id
message_id (JOIN message_names ON message_names.message_id = message.id)
name
For Strings you may use the || to concat strings:
update TableA set names=names ||' '|| 'B' where labels like '%b4%'