i have a dataset, dataset1:
i am trying to do:
select col1+col2, col3 from mytable
this does not work; however this is no problem:
select col1, col2, col3 from mytable
how can i concatenate fields inside of my dataset?
please note that col1, col2, col3 are all VARCHARS. i've tried the & operator as well.
please see the image below. i get the DEFINE QUERY PARAMETERS dialogue when it doesnt like my query:
Double quotes are not appropriate string delimiters in MS SQL. Try:
SELECT ppl.FirstName + ' ' + ppl.LastName AS ReferredBy
Putting your actual query in the question may help you get a faster response next time, since the concatenation is actually a red herring here.
Related
I am using the query function to generate content in Sheet 1 pulled from sheets 2-6. I've used this function in past, and its worked quite well – but only when I am pulling from 5 sheets. No more, no less. I do not understand this function as it was taken from an old template. I am interested in creating a new function to serve my purpose and learn how to decipher the meaning of the clauses. Please help.
This is the old function:
=query(query({Sheet2!A1:L;Sheet3!A1:L;Sheet4!A1:L;'Sheet5'!A1:L;Sheet6!A1:L},"select Col1, count(Col1), max(Col4) where Col1 <> 'Testing' group by Col1 order by max(Col4) desc",0), "select * where Col2 > 1",0)
What does each clause mean?
How can I alter it for greater/lesser number of sheets?
make sure this part is correct in your new attempt:
{Sheet2!A1:L; Sheet3!A1:L; Sheet4!A1:L; Sheet5!A1:L; Sheet6!A1:L}
all sheets need to be separated with ; and all ranges needs to have the same number of columns
I am trying to find the SQL equivalent of hash in bigquery.
SQL :
SELECT CAST(HASHBYTES('SHA2_256', CONCAT(
COL1, COL2, COL3
)) AS BINARY(32)) AS HashValue
Big Query:
SELECT SHA2_256(CONCAT(COL1, '', COL2 )) AS HashValue.
I can't find any examples where hashing is done on multiple columns. The datatype of the columns are different as well.
Any help is really appreciated.
You can see follow this change request
These are now implemented. Thanks again for sharing feedback on needing these ?> functions. Please see:
TO_HEX: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#to_hex
FROM_HEX: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#from_hex
2 related questions I found for you are:
Is it possible to hash using MD5 in BigQuery?
Random Sampling in Google BigQuery
Using Standard SQL (SHA256 function) you could cast all your fields to string, concatenate them and use the hash. Something like this:
SELECT SHA256(
CONCAT(
CAST(integer_field1 as STRING),
CAST(integer_field2 as STRING),
CAST(timestamp_field as STRING)
)
) as sha256_hash FROM `table`
I have an SSRS report that the user wants to be able to search upon multiple sites as supposed to just the one site that the report usually accepts.
If they don't enter a site in the parameter text box then the report searches for all sites.
I've handled this successfully within my SQL query in the where clause as shown below
WHERE
( ( LEN(#ProjectId) > 0 AND p.ProjectId = #ProjectId ) OR ( LEN(#ProjectId) =
0 ) )
This worked fine for searching just the one site or all sites if they didn't enter a value.
They want to be able to enter multiple sites and split them with a comma.
I handled this within my SSRS report as defining the parameter in my data set to be
=JOIN(Parameters!ProjectId.Value, ",")
However this doesn't work for blank values or null values as expected.
I've done some fancy expressions to try and get round this like
=IIF(ISNothing(Parameters!ProjectId.Value), "", JOIN(Parameters!ProjectId.Value))
To set the parameter passed to the SQL query to be blank and not to do the join if there isn't a value encountered.
Basically I want to know if there is a way I can have a multi set parameter split by commas but passed as a blank if a value is not chosen.
at the moment I've tried setting the parameter to allow multivalues which hasn't worked either I get the error message as shown below
First set up your multivalue parameter:
Then within your Dataset define a new parameter, in this instance I have called it #PCount:
#PCount is defined with the expression:
=IIF(Parameters!P1.Count=0 Or
(Parameters!P1.Count = 1 And Parameters!P1.Value(0) = ""),
0,
Parameters!P1.Count)
This will return 0 if no values are entered, or a blank value is entered. Then your query becomes something like:
SELECT Col1, Col2, Col3
FROM YourTable
WHERE ProjectID IN (#P1)
UNION ALL
SELECT Col1, Col2, Col3
FROM YourTable
WHERE #PCount = 0
You could do this with OR, as follows:
SELECT Col1, Col2, Col3
FROM YourTable
WHERE ProjectID IN (#P1)
OR #PCount = 0
But I think this is more likely to encounter performance issues.
I have a google spreadsheet sheet with several columns:
A: date
B: string
C: number
...
G: string (could be empty)
H: string (could be empty)
I would like to have a small table with the following:
Get the sum of C in table, where rows are the values of G (substring of it, as they are configured as this: CATEGORY:ITEM, I need grouping by CATEGORY only) and columns are monts
So far I've got only partial solutions with query (for example group by month(toDate(A)) etc) - I seem to be unable to use substring and charindex nor left to do string manipulation to remove the item part after the category nor to visualize those as the first clumns in the resulting rows...
Edit: just to clear up a bit: I need to alter the value in G for each row so that I can group by the altered value. I know it is possible to do with dates ( in my example -> group by month(toDate(A)) gives me access to each value in A column so the result is grouped correctly for each separate month). But it seems string manipulation is not allowed?
How do I do that for starters.
Thanks
Based on your desired output, can you try this on sheet 2:
=ArrayFormula(query({day(Sheet1!A2:A10)&text(month(Sheet1!A2:A10), " (mmm)"), Sheet1!B2:F10, regexextract(Sheet1!G2:G10, "(.+):")}, "select Col7, sum(Col3) group by Col7 pivot Col1"))
and see if this is getting somewhere ?
Or in case you prefer open ended ranges:
=ArrayFormula(query({day(Sheet1!A2:A)&text(month(Sheet1!A2:A), " (mmm)"), Sheet1!B2:F, iferror(regexextract(Sheet1!G2:G, "(.+):"))}, "select Col7, sum(Col3) where Col7 <>'' group by Col7 pivot Col1"))
been searching all over for a similar solution :-)
just in case you would like an alternative solution...
This works a treat based on my sheet H with data in column A to F
=transpose(split(textjoin("|",1,transpose({H!A1:F100})),"|"))
See... https://webapps.stackexchange.com/questions/90629/concatenate-several-columns-into-one-in-google-sheets
The course that I am currently doing uses brackets in its WHERE clauses like so:
SELECT bar
FROM Foo
WHERE (CurrentState = 'happy');
Is this standard sql ?
If not then why use them?
Doesn't seem to be used in the Date & Darwen book I have.
EDIT
Just to clarify - I'm referring to 1992 sql standards
Yes. You can use parenthesis to bind components of where clauses. This isn't necessary in your example, but if you had multiple and and or components, you might need parenthesis to either ensure correct order of operations or simply to self-document the query.
Example 1:
select *
from foo
where
(class='A' and subclass='B')
or (class='C' and subclass='D')
In example 1, the parens aren't strictly required because and binds more tightly than or, but if you had multiple or conditions tied by and you would need it to get correct results, as in example 2 below.
Example 2:
select *
from foo
where
(class='A' or class='B')
and (subclass='C' or subclass='D')
I use them in either case, because I don't like having to parse the sql in my head the same way the query optimizer does -- I'd rather be explicit about it and more quickly understand what the intent is.
They are optional. They make sense for more complex WHERE statements.
... WHERE (created > '2012-10' AND created < '2013-01')
OR (modified > '2012-10' AND modified < '2013-01')
No. They are only required to be used when you have AND on OR conditions in your statement to avoid shortcircuits, just like this:
SELECT...
FROM...
WHERE col1 > 1 AND col2 < 3 AND col3 >= OR col3 = 1 AND col5 < 1
The above query will give you unexpected result as it will not determine the correct condition it will take. A round brackets will help you segregate condition in this case,
SELECT...
FROM...
WHERE col1 > 1 AND col2 < 3 AND (col3 >= OR col3 = 1) AND col5 < 1
by the way, it should be single quotes ' ' not ‘ ’