BigQuery query from a nested field - google-bigquery

I have a table with a nested field externalIds.value
I want to run a query where 2 entries match values in this field. Here is the query I have, which doesn't return any data.
SELECT keyField, field2, example.field1, example.field2,
externalIds.type, externalIds.value FROM (FLATTEN([dataset1.table1], externalIds))
WHERE
externalIds.value = '157' AND
externalIds.value = 'Some test data'
;
If I run this query with only 1 WHERE clause (externalIds.value = '157') and then run a query WHERE keyField = "The value returned from the previous query", then I get two lines. One showing the externalIds.value as '157' and another result where it is 'Some test data'.
I'm not interested in displaying both values in the result. My priority is to get the keyField WHERE the .value is '157' AND 'Some test data'

Maybe something like that:
SELECT keyField, field2, example.field1, example.field2,
externalIds.type, externalIds.value FROM [dataset1.table1]
OMIT RECORD IF NOT(
SOME(externalIds.value = '157') AND
SOME(externalIds.value = 'Some test data'))

Related

WHERE JSON_Value Dynamically

I am working with the JSON_VALUE function and I need a kind of dynamic query
I have a column called Criteria and sometimes it has 1 value but sometimes it has 2 or 3 vales like:
Example of 1 value: $.IRId = 1
Example of 2 values: $.IROwner = 'james.jonson#domain.com' AND DaysTillDue < 10
So in order to read the values from a JSON column and taking the Criteria column I am using this logic:
DECLARE #CriteriaValue int
,#CriteriaStatement VARCHAR(50)
SELECT #CriteriaValue=SUBSTRING(Criteria, CHARINDEX('=',Criteria)+1, len(Criteria)) FROM #SubscriptionCriteria;
SELECT #CriteriaStatement= SUBSTRING(Criteria,0, CHARINDEX('=',Criteria)) FROM #SubscriptionCriteria;
SELECT #CriteriaValue,#CriteriaStatement
SELECT *
FROM [SAAS].[ObjectEvent]
WHERE
JSON_VALUE(JSONMessageData, #CriteriaStatement) = #CriteriaValue
That SQL code is taking only the Criteria Column with only 1 value ($.IRId = 1), but the idea is to have something that reads the criteria no matter the different filters and apply them into the final query. The idea I have is that the query would look like this:
SELECT *
FROM [SAAS].[ObjectEvent]
WHERE
JSON_VALUE(JSONMessageData, #CriteriaStatement1) = #CriteriaValue1 ADN JSON_VALUE(JSONMessageData, #CriteriaStatement2) = #CriteriaValue2 AND
JSON_VALUE(JSONMessageData, #CriteriaStatement3) = #CriteriaValue3
ETC
Any suggestion?

How to exclude SQL variable in output

I have a complex SQL query where I have a few cases that use END AS variableName. I then use variableName to do some logic and then create a new variable which I want in the output result. However when I run the query, all the END AS variableNames that I have used are also outputted in the results.
Is there a way that I can exclude these variables as I only want the final variable that uses these variableNames.
Thanks
EDIT, here is a query explaining my problem
SELECT DISTINCT
mt.src_id AS “SRC_ID”,
CASE
WHEN mt.cd = ‘TAN’ THEN
(
(
SELECT SUM(src_amt)
FROM source_table st
WHERE mt.id = st.id
AND st._cd = ‘TAN’
AND st.amt_cd = ‘ABL’)
)
END AS src_amt
FROM MAIN_TABLE mt
WHERE
mf.dt >= 2021-12-12
AND SRC_AMT > 10
I need SRC_AMT to be used as some sort of logic but when I run the query, it prints out in the output as it's own column. I want to ignore this variable
you can wrap the whole thing into a new select-statement:
select SRC_ID from ( <entire previous query here> )

Appending in middle of text in column data oracle sql

I need to append the text in middle of text .
1 EX :existing text is : "Monoclonal Anti-FLAG, Clone 6F7"
Result :"<HIDE>Monoclonal Anti-<HIDE>FLAG, Clone 6F7".
2 EX :existing text is : "Anti-100-KD subunit"
Result : "<HIDE> Anti-</HIDE>100-KD subunit"
I want to append <HIDE> tag where ever Anti and Monoclonal term is coming.
select * from table where regexp_like(colnanme, '^(Monoclonal Anti|Anti)')
Can you suggest how should i write this .for select i have written query .
The predicted certain regular expression solution.
-- Start test data
with test_data as
(select 'Monoclonal Anti-FLAG, Clone 6F7' as test_string from dual union all
select 'Anti-100-KD subunit' from dual)
-- End test data
select test_string,
REGEXP_REPLACE(test_string,'(Monoclonal Anti-|Anti-)','<HIDE>\1</HIDE>')
from test_data;
"TEST_STRING" "Result"
"Monoclonal Anti-FLAG, Clone 6F7" "<HIDE>Monoclonal Anti-</HIDE>FLAG, Clone 6F7"
"Anti-100-KD subunit" "<HIDE>Anti-</HIDE>100-KD subunit"
you could use replace and like
select replace(colname, 'Anti', '"<HIDE> Anti-</HIDE> )
from table where colname like '%Anti%'
and colname not like 'Monoclonal Anti%';
of for a combined situation you can use a case when for lead replace
select case when colname like '%Monoclonal Anti%'
then replace(colname, 'Monoclonal Anti', '"<HIDE> Monoclonal Anti-</HIDE> )
when colname like '%Anti%'
then replace(colname, 'Anti', '"<HIDE> Anti-</HIDE> )
else colname
end
from table where colname like '%Anti%';

Dynamic appending constraints in SQL query

I have this SQL
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
and <<either bdvalue or string value based on user selected value datatype>>
Here in the Query based on the devudp1.valueType I want to append below attribute
If the valueType is 3 then I want to append my above select clause with devudp1.bdvalue ='10', else it should be appended by devudp1.bdvalue = 'Hello'
So the above query when valueType is 3 will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.bdvalue = '10'
else it will look like
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND devudp1.stringValue = 'Hello'
Can anyone suggest me how to put this logic in place
Try this:
SELECT devudp1.deviceoid,devudp1.valueType
FROM DeviceUdpValues devUDP1
WHERE devudp1.udpname='TestUDP'
AND (
(<USER-SELECTED-VALUE> = 3 AND devudp1.bdvalue ='10') OR
(<USER-SELECTED-VALUE> <> 3 AND devudp1.stringvalue ='Hello')
)

echo out from the UNION sql?

<?php
$sql = mysql_query("
SELECT navn FROM member_film WHERE username = '$pusername'
UNION SELECT meetup FROM member_meetups WHERE byusername = '$pusername'
UNION SELECT title FROM member_tutorials WHERE username = '$pusername'");
$rowit = mysql_fetch_array($sql);
$number = mysql_num_rows($sql);
?>
<? echo $number; ?>
<? echo $rowit["navn"]; ?><br>
<? echo $rowit["meetup"]; ?><br>
So i have this.
Now, i have 3 columns where username is username in the database.
2 in member_film, 1 in member_meetups
The echo $number, shows it correct, that i have 3.
Now when i try to echo the "navn", it echo only 1, when there is 2 columns?
And when i try to echo "meetup" it says undefined index: meetup, like there's nothing to show..
but there is, mysql_num_rows founded it, else it would have been 2?
I think most of you misunderstood me..
I want to show what the user($pusername) have been posted, in member_film, member_meetups, member_tutorials.. And then if e.g the user have 2 columns with his username, i want to display the "navn" from the same column. Same with title on member_tutorials, and meetup on member_meetups.
An example on how i want it to look like:
Your posts: (which as i want, checks the three table if there's any columns with the user's username in "username" column.)
My new video, its so good (navn, which is from member_film)
My boring meetup with my parents (title, which is from member_meetup)
My great great video!(navn, which is from member_film)
UNION selects, are joining select statements into 1 result set, therefor have the same column names.
Correct SQL:
SELECT navn colName, 'member_film' tableName FROM member_film WHERE username = '$pusername'
UNION SELECT meetup colName, 'member_meetups' tableName FROM member_meetups WHERE byusername = '$pusername'
UNION SELECT title colName, 'member_tutorials' tableName FROM member_tutorials WHERE username = '$pusername'");
and in the php:
<? echo $rowit["colName"]; ?><br>
and to find out what table:
<? echo $rowit["tableName "]; ?><br>
A union query combines the results of several queries into one resultset. In order for the union to work all subqueries have to return the fields with the same data types for each position of the query or each named field.
So your original query will result in a result set with one field called "navn" (taken from the first sub query of the union). So to make it more readable you could either rename the field using As myName on each of the subqueries (sam munkes' solution).
However, from the way you are accessing the query I believe you want to have one row with all 3 values on it then you should try using this instead:
SELECT member_film, member_meetups.meetup, meber_tutorials.title
FROM member_film, member_meetups, member_tutorials
WHERE member_film.username = '$pusername' AND
member_film.username = member_meetups.byusername AND
member_film.username = member_tutorials.username
Edit after the comments:
SELECT CONVERT(varchar(100), navn) AS Comment FROM member_film WHERE username = '$pusername'
UNION
SELECT CONVERT(varchar(100), meetup) AS Comment FROM member_meetups WHERE byusername = '$pusername'
UNION
SELECT CONVERT(varchar(100), title) AS Comment FROM member_tutorials WHERE username = '$pusername'
This should now yield a table having one column named 'comment' and being of varchar(100). You can now access all values via the comment column. If you need to know from which table the information is taken, use Sam's answer. I inserted the convert to ensure that all entries are of the same data type and length, if they are in you DB you can leave this portion out and just use the names. If needed the length needs to be adjusted.
To make it clear, there is only one column with the name "comment" now.