How to query 'empty text' field in dremio? - dremio

I have certain rows in columns that have the phrase 'empty text' in the cell of a table in dremio.
When I try queries like:
select col1 from table1 where col1 is null
I get no results back for rows that have the text 'empty text' in them.
How do I query the 'empty text' fields or change the data type of empty fields to 'null' in dremio?

Use case statement to cast empty cells to null like so:
CASE WHEN column1= '' THEN NULL ELSE column1 END AS column1

Related

Join Table B and get values of different columns depending on values in a specific column from Table A

I want to join two tables via ID. The first table has all the IDs and a column with the column names from the other table. I did this with a case statement and if worked but I have like 50 differnet column names and more to come so I don't want to use the case statement. What can I do instead? I'm using Snowflake, this is what I have:
Table A:
tableA
Table B:
tableB
create table statements:
CREATE OR REPLACE TABLE
TABLE_A
(id INTEGER,
column_name VARCHAR);
INSERT INTO
test.TABLE_A
(id,column_name)
VALUES
(1,'column_1'),
(1,'column_2'),
(2,'column_2');
---------------------
CREATE OR REPLACE TABLE
TABLE_B
(id INTEGER,
column_1 VARCHAR,
column_2 VARCHAR);
INSERT INTO
TABLE_B
(id,column_1,column_2)
VALUES
(1,'value_1_1','value_1_2'),
(2,'value_2_1','value_2_2');
This is what I did but as I said this is bad for maintainance:
SELECT
a.ID,
a.COLUMN_NAME,
CASE
WHEN a.column_name = 'column_1'
THEN b.column_1
WHEN a.column_name = 'column_2'
THEN b.column_2
ELSE ''
END AS result
FROM
TABLE_A a
JOIN
TABLE_B b
ON
a.id = b.id
This gives me the following result:
result
How can I get this result without using the case statement or having to type the strings into any condition?

Query & importrange with a word that has symbol ' (example = James 'Lee)

I have issue to list the training program with the name that has symbol '. The participant name is James 'Lee
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets...", "'record'!A:Z"), "Select Col7,Col18 where Col1 = '"&C5&"' and Col1 is not null", 0)
enter image description here
I received the error message:
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "Lee "" at line 1, column 40. Was expecting one of: "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "and" ... "or" ...
Try
=QUERY( IMPORTRANGE ("..., "...."), "Select Col7, Col18 where Col1 = """&C5&"""", 0)
and see if that works?
Select Col7,Col18 where Col1 like '"&regexreplace(C5,"\'","\%")&"' and Col1 is not null
will remove the hyphen and replace it with a wild card character %.
The side effect is that it will also bring back results like 'James OtherLee'

Using IF conditional in SELECT statement of Google Sheets Query function

In order to cleanse some outlying bad data I need to implement a conditional statement into the SELECT statement of a query in Google Sheets, but GS does not want to cooperate
My attempted statement:
=QUERY('JSON Data'!A1:AS,"Select A, IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) where B is not null GROUP BY A pivot B label IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) 'ratio' ",1)
Error thrown:
Unable to parse query string for Function QUERY parameter 2:
PARSE_ERROR: Encountered " "(" "( "" at line 1, column 13. Was
expecting one of: "where" ... "group" ... "pivot" ... "order"
... "skipping" ... "limit" ... "offset" ... "label" ... "format" ...
"options" ... "," ... "" ... "+" ... "-" ... "/" ... "%" ... "" ...
"/" ... "%" ... "+" ... "-" ...
Note that the following statement works:
Select A, SUM(AP)/SUM(AS) where B is not null GROUP BY A pivot B label SUM(AP)/SUM(AS) 'ratio'
The following statement does not work (testing to see if labelling the field has an effect):
Select A, IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) where B is not null GROUP BY A pivot B
The sheet is here: https://docs.google.com/spreadsheets/d/1raQI22n3J08nKCHnz2LxRCXRP3dDnYJtGCEb8o4VUWY/edit#gid=1466522015
How to (Can I) execute a proper IF/THEN in the SELECT portion of a Google Sheets QUERY statement?
Try to process the if then before the query. See if this works
=ArrayFormula(QUERY({'JSON Data'!A1:B, IF('JSON Data'!AP1:AP/'JSON Data'!AS1:AS > 1, 1, 'JSON Data'!AP1:AP/'JSON Data'!AS1:AS) } ,"Select Col1, sum(Col3) where Col3 is not null GROUP BY Col1 pivot Col2",1))

Pivot Table Conditional Sum or Count

I have a data tab with following columns:
State => string
City => string
Person_ID => int
Status => values(0/1)
OnLeave => values(0/1)
Need to create a Pivot table like below:
Report Filter: State
Row Labels: City
Values: count(Person_ID), sum(Status), sum(OnLeave)
I want to do a conditional count for count(Person_ID) where this should count Person_ID only when OnLeave is 0.
Tried using Calculated Field, it doesn't work since its applied at the pivot table level instead of Data Row. Is there a way to do it directly in Pivot table?
Add a column to the source data with an IF statement that evaluates the OnLeave column and returns a 1 when true. Then sum that column in the pivot table.
add to report filter: onLeave column, select 0 as filter

Can I have some attributes in my model without having these as database columns but map these instead?

My use case is as follows:
I am reading a csv file and I want to get the column headers there as a hash.
Then Im saving the row-values of the these column names into a database.
What I want to do is:
incoming CSV file hash-keys = [Col1, Col2, Col3]
I want to map the values of these to the following database columns: [colA, colB, colC]
I don't want my database columns to be named Col1, Col2, Col3 but I want these as model attributes.
How do I make a model where some attributes are not database columns?
Thanks
Write the database tables as Row ->* Field (row.has_many fields).
Then in your Row class override method_missing, and inside that look up the target field via the missing name. So if Field.name == 'foo', then row.foo will fetch this record and return its value. When you call row.foo = 'bar', that will pass :'foo=' to method_missing; you can detect the = and assign a value to the target field. Googling for 'ActiveRecord method_missing has_many' might hit some code that already does this.
Here's what you do with each column name.
col = 'Col20'
To convert it to lowercase + letters format that you want, first get the number:
number = col.match(/(\d+)/).try(:[],1).to_i
=> "20"
The above will attempt to find a number and then if there's a match, it will try to get that number.
Then convert (1=A, 20=T, ...):
new_col = col.downcase.sub( /\d+/, (number+64).chr )
=> "colT"
Then define such a property on the object you want it on like this:
my_object.class_eval { attr_accessor new_col }
And assign a value like this:
my_object.colT = "New Value"
Or dynamically:
my_object.send "#{new_col}=", "New Value"
And get its existing value by calling it by name:
my_object.colT
=> "New Value"
Or dynamically:
my_object.send new_col
=> "New Value"

Resources