How to add an additional fake null column to select result in Informix? - informix

I need to select an additional column which does not exist in the current table in order to unload the data in the correct format.
Suppose I have two different tables as follows.
**
tab1
----
col1 col2 col3
tab2
---
col1 col2
**
Suppose I need to unload 1000 records from tab2 and need to load them into tab1. But there is a mismatch when considering the number of columns. But I'm ok to enter null values for col3 when loading the data into tab1
So my unload command is as follows
unload to data.unl select col1, col2, null as col3 from tab2;
Then I can load the contents of data.unl into tab1. But my problem is that I get a syntax error when I'm trying to unload the data in that way. Please correct me. Someone can argue that I can use a simple insert command with selecting the data from the tab2. But in my actual use-case it is not possible because data volume is so high and I hope to use the ipload.
Here is my sample error:
nwn#nwnhost$ echo "select CURRENT as col1,null as col2 from sysdual" | dbaccess sysmaster
Database selected.
201: A syntax error has occurred.
Error in line 1
Near character position 29
Database closed.

Cast the NULL to the desired type:
SELECT CURRENT AS col1, NULL::INTEGER AS col2
FROM sysmaster:"informix".sysdual;

Related

Google Sheets - Trying to use IMPORTRANGE to get student enrollments by school by date

I made columns that simply lists all the schools for each student under a date column header. I only need to count the schools - I don't want to see any student data. I have a form that needs to be submitted to the state and there is a cell with the date we are submitting. I want to use an IMPORTRANGE to go to the other spreadsheet and find the column where the date matches the form date and pull back the count of schools in that column. I tried using INDEX/MATCH, etc., and while I have those working on the form for other data already on my spreadsheet - I can't seem to get them to work on the 'outside' spreadsheet. Any help is appreciated! I'm attaching an example spreadsheet with more explanation. I hope it's clear. Thank you.
Example Document
ztiaa has provided a good solution. I will suggest another which pivots the data differently. I feel that having the dates go across columns is going to quickly become unwieldy. So this approach leaves the dates running down the right side with the schools running as column headers. My solution also includes exceptions such as snow days, as those will likely be important to see at a glance as well. Just be careful to use the same notation for such exceptions. For instance, always use "snowday" (not, eg., "snow day") or "prep day" (not, e.g., "prep").
In Sheet1, use IMPORTRANGE to bring in the data exactly as you have it listed in your sample sheet (i.e., dates at top, school names or exceptions under each date header).
Then, in a new sheet, place the following formula in cell A1:
=ArrayFormula({QUERY(SPLIT(FLATTEN(FILTER(Sheet1!1:1&"~"&INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!1:1<>"")),"~",1,0),"Select Col1, COUNT(Col1) WHERE Col2 Is Not Null GROUP BY Col1 PIVOT Col2 FORMAT Col1 'mmm dd'"),TRANSPOSE({"District Totals",MMULT(SEQUENCE(1,ROWS(Sheet1!A2:A),1,0),IF(FILTER(INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!1:1<>"")="",0,1))})})
This assumes that the first sheet is, in fact, named Sheet1. If it is not, you'll need to change every instance of that sheet name in the formula.
It will probably further aid visual ease if you freeze Row 1 and Column 1 (View > Freeze > 1 row / 1 column).
ADDENDUM (after seeing realistic copy of OP's sheet)
=ArrayFormula({TRANSPOSE(QUERY(SPLIT(FLATTEN(FILTER(Sheet1!1:1&"~"&INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")),"~",1,0),"Select Col1, COUNT(Col1) WHERE Col2 Is Not Null GROUP BY Col1 PIVOT Col2 FORMAT Col1 'mmm dd'"));{"District Totals",MMULT(SEQUENCE(1,ROWS(Sheet1!A2:A),1,0),IF(FILTER(INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")<>"",1,0))}})
If you prefer to see the name of the exception (e.g., "snowday") rather than the count of 1 for each exception in the "District totals" line:
=ArrayFormula({TRANSPOSE(QUERY(SPLIT(FLATTEN(FILTER(Sheet1!1:1&"~"&INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")),"~",1,0),"Select Col1, COUNT(Col1) WHERE Col2 Is Not Null GROUP BY Col1 PIVOT Col2 FORMAT Col1 'mmm dd'"));{"District Totals",IF(MMULT(SEQUENCE(1,ROWS(Sheet1!A2:A),1,0),IF(FILTER(INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")<>"",1,0))=1,FILTER(Sheet1!2:2,Sheet1!2:2<>""),MMULT(SEQUENCE(1,ROWS(Sheet1!A2:A),1,0),IF(FILTER(INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")<>"",1,0)))}})
If you want exceptions (e.g., "snowday") to always appear at the bottom instead of in alphabetical order within the school-name list:
=ArrayFormula({TRANSPOSE(QUERY(SPLIT(FLATTEN(FILTER(Sheet1!1:1&"~"&IF(REGEXMATCH(UPPER(INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A))),INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A))),,"‡")&INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")),"~",1,0),"Select Col1, COUNT(Col1) WHERE Col2 Is Not Null AND Col2 <> '‡' GROUP BY Col1 PIVOT Col2 FORMAT Col1 'mmm dd'"));{"District Totals",IF(MMULT(SEQUENCE(1,ROWS(Sheet1!A2:A),1,0),IF(FILTER(INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")<>"",1,0))=1,FILTER(Sheet1!2:2,Sheet1!2:2<>""),MMULT(SEQUENCE(1,ROWS(Sheet1!A2:A),1,0),IF(FILTER(INDIRECT("Sheet1!2:"&ROWS(Sheet1!A:A)),Sheet1!2:2<>"")<>"",1,0)))}})
Try this out
=ArrayFormula({query(split(flatten(text(A1:E1,"mmm/d")&"❄️"&A2:E),"❄️"),"select Col2, count(Col2) where not Col2 matches '|Snowday' group by Col2 pivot Col1");{"District Total",transpose(MMult(transpose(N(filter(A2:E,not(RegexMatch(A2:E2,"Snowday")))<>"")),sequence(rows(A2:E))^0))}})

issue with querying of multiple tabs inside same sheet

i am trying to query number of response, country name and month from one tab but i also need it to display the name of country corresponding to country number which is in a different tab
this is my statement
=QUERY({CSAT!$A$2:$F$34;Countries!$A$2:$B$11},"SELECT Col1, Col2, Col3, Col5 WHERE Col1 = 'Jun-21' ON Col2 = Col3 ")
i keep getting an error saying that there are missing values on lateral array
Number of columns from both sheets must be the same if you want to append them in the query. Using the query with different number of columns (6 vs 2) will result into:
If we get 2 columns from both sheets:
=QUERY({CSAT!$A$2:$B$34;Countries!$A$2:$B$11})
Error is not shown:

Query Importrange Issue

I am not sure why this functionality stopped working, but I am sure it has to do with inconsistent back end data or how the query condition "CONTAINS" needs to be changed. The IMPORTRANGE portion works just fine, but will not always pull data into the front end sheet. The query portion looks like this
SELECT Col3, Col2, Col1 WHERE Col2 CONTAINS "&'Job Number'!A1&" ORDER BY Col3 ASC,1
Column 2 contains job numbers that are xxxxx with another 3 digit code appended to end of it. It will only populate temporarily if I manually go into the sheet and edit the IMPORTRANGE range values. If I close the spreadsheet and open it again it will not populate. Does the data in Column 2 need to be a consistent datatype throughout the column or it will break the query?
Unfortunately we do not have a test sheet, so we can not know what the expected results would be.
Nevertheless, your formula syntax is not correct.
The correct syntax would be:
SELECT Col3, Col2, Col1 WHERE Col2 CONTAINS '"&"JOB"&A1&"' ORDER BY Col3 ASC,1
Please notice the syntax '"&"JOB"&A1&"'
(If you still have issues please share a test sheet and let us know.)

dbaccess control output format regardless of result size

Using Informix dbaccess e.g.
select col1, col2, col3 from MyTable
may output results like this
col1 col2 col3
100 The first row blah blah
101 The second row more blah blah
or it may output results like this
col1 100
col2 The first row
col3 blah blah
col1 101
col2 The second row
col3 more blah blah
I need to make it choose one output format and stick with it so that I can parse it in a regular way.
Can I make it always output in either one style or the other?
I can use unload to and get a regular a|b|c format, which is nice, but
I require the column names to be outputted
I do not control the query, this has to work for an arbitrary SELECT, so I do not know the column names in advance
How can I force the output to be
always of a certain (any) fixed format
which includes the column names
?

How to use QUERY in Google Sheets to return the row with the maximum date?

I am trying to use QUERY to copy the most recent data in a category to another sheet. See example here. The first sheet has the data I want to copy with the category (row 1 and 3) and the date the data was gathered (row 5).
On the second tab, I am trying to copy over only data that has the tag 6.Portions.B in row 1, Summative in row 3, and the most recent date in row 5.
I have successfully used the QUERY command and double transpose to have only 6.Portions.B and Summative data be copied to the second sheet. However, I am unable to get the QUERY command to show only the most recent date. I am trying to use the following:
=transpose(query(transpose(Data!$1:$15), "select Col5, Col6, Col7, Col8, Col9, Col10, Col11, Col12, Col13, Col14, Col15 where Col1 starts with """&C$2&""" and Col3 = 'Summative' and Col5 = max(Col5)"))
It is the and Col5 = max(col5) that isn't working (everything else is fine). Is there some way to further filter by only the most recent date? I have tried using the Filter command, but my range size varies unpredictably based on other factors not shown here, and I haven't been able to get that to work without knowing the exact size of the range.
Sort by the date column in descending order and limit the number of returned rows to 1:
select ... where ... order by Col5 desc limit 1
Strictly speaking, "the row with the maximum date?" is not a well-defined concept: multiple rows may have the same date. If this happens, query will pick one of such rows.

Resources