Check presence of a column in multiple tables - kdb

I want to check if a column is present in multiple tables.
When I try for one table, it works.
`tickerCol in cols tradeTable / (output is 1b) hence working perfectly
`tickerCol in cols table2 / (output is 1b) hence working perfectly
but when I run
`ticker in cols #' (tradeTable;table2) / (output is 0b, expected output 11b)
for above example ticker column is present in both tables(tradeTable;table2).

The following works using each-both ':
`ticker in ' cols each (tradeTable; table2)
This will find the columns that are present in each of the tables and then perform a check on each of the column lists to find if `ticker is present in these lists.

Solution is already provided in another answer. Just trying to explain why your solution is not working.
Lets say we have 2 tables t1 (columns id and v1) and t2 (columns id and v2).
Now when we run:
q) cols#'`t1`t2
output will be a list of list:
(`id`v1;`id`v2)
This list has 2 entries where each entry is a list.
Now what you are doing is trying to find column in this list .
q) `id in (`id`v1;`id`v2) /output 0b
And since that list doesn't have id as an entry it returns 0b.
If you search `id`v1 which is a list you will get 1b matching first entry.
q) `id`v1 in (`id`v1;`id`v2) / output 1b
What you want here is to search your column name in each entry of that list. So the only thing you are missing in your expression is each-both. This will work:
q) `id in'cols#'`t1`t2 / output 11b
In your case it will be:
q) `ticker in ' cols#'`tradeTable`table2

Related

count t vs actual number in kdb select statement

I noticed the following
select (count t)#`test from t
Returns
flip (enlist `x)!enlist enlist `test`test`test
Vs
select 3#`test from t
Which returns
flip (enlist `x)!enlist `test`test`test
Similar with select (sum 1 2)#1 from t vs select(1 + 2)#1 from t etc
Anyone know the reason why key words in the select seems to cause the return to be a table with one row nested list containing x element vs a table with x rows?
It's because kdb recognises count and sum as aggregations and has special treatment for them (it enlists the result).
For example if you were to slightly change the count and sum to lambdas (which kdb won't recognise) you get the other results you expect:
q)select ({count x}t)#`test from t
x
----
test
test
test
q)select ({sum x}1 2)#1 from t
x
-
1
1
1
The reason kdb "recognises" certain common aggregations and auto-enlists them is because otherwise simple selects such as select sum a from tab would give a rank error as the sum returns an atom but a table column must be a list, e.g.
q)select {sum x}a from t
'rank
[0] select {sum x}a from t
^
/versus
q)select sum a from t
a
-
6
There's also a deeper reason which is to do with map/reduce aggregations over database partitions but that's beyond scope for this problem. The list of recognised aggregations is stored in the variable .Q.a0. See also https://code.kx.com/q/basics/qsql/#special-functions

Count the non-null columns in Tableau

I need to find how many aliases are present and what are they in my data. The columns storing the alias values are: ColA, ColB, ColC.
Sample Data Description:
"AAA" is also known as “a” & “b”,
“BBB” is also known as “c”,”d” & “e”.
I want to find the number of alias a Name have and display the values of alias too.
**Name** **ColA** **ColB** **ColC**
AAA a b
BBB c d e
CCC f g
Example:
for “AAA”, number of alias = 2 & values of alias are: “a” & “b”
for "BBB", number of alias = 3 & values of alias are: “c” & “d” & "e"
Easy.
First edit your data source to pivot the ColA, ColB and ColC fields. Hide [Pivot Field Names] and rename [Pivot Field Values] to [Alias]. In the top right of the data source page, filter to exclude rows with a null [Alias]. Then your data will appear to have two columns [Name] and [Alias]
The best way to count and display aliases, depends on your Tableau version. Prior to version 2020.2, you'd use SUM([Number of Records]), nowadays, use one of the generated Count() measures.

PostgreSQL query to find a particular pattern in a text column

I have a text column in my database table which contains values like
A/B
A/B/C
A/B/C/D
A/B/C/D/E
Now, I want to select only those rows where this column value contains maximum three occurrences of '/'.
For clarity - expected output should be:
A/B
A/B/C
A/B/C/D
Can anyone help me with such a query?
I think it would be easier to simply remove everything else and count the number of remaining characters:
where length(regexp_replace(the_column, '[^/]', '', 'g')) <= 3
Online example

Exclude Combination of Data Items From One Table From Another

I have a view, A, with 20 columns which forms my primary data. I have a table B which lists some of the columns from A and contains data I want to exclude from A.
For example table B will have 6 columns 2 of which are 'customer' and 'country' and contain the data 'HP' and 'America'. These columns exist in A. But I want to write a query that brings back data from A except where any rows that have a combination HP and America.
There are 6 columns and table B can have any combination of rows. Anywhere between 1 and all 6 rows could be filled in or there could be a row which has 5 columns filled in. Also another row with a different 5 columns filled in and so on.
I want to be prepared for any possible combination of the 6 rows and the query to search A for the combination and exclude any rows with that data from B.
I have tried this
SELECT *
FROM A T1
WHERE not EXISTS
(SELECT * FROM [dbo].[ExcludedItems] T2
WHere ReportNumber=1
AND
(
T1.job=ISNULL(T2.job,T1.job) and T1.CustomerName=ISNULL(T2.CustomerName,T1.CustomerName) and
T1.COUNTRY= ISNULL(T2.COUNTRY,T1.COUNTRY) and T1.CONTINENT=ISNULL(T2.CONTINENT,T1.CONTINENT) AND
T1.continer= ISNULL(T2.ContainerName, T1.continer) and T1.UnscheduledJob= ISNULL(T2.unscheduledJob, T1.UnscheduledJob) and
T1.[Price]= ISNULL(T2.Price, T1.Price) and
T1.[Haulage]= ISNULL(T2.[Haulage], T1.[Haulage]) and
T1.SiteAdress= ISNULL(T2.SiteAddress, T1.SiteAdress) and T1.Delta=ISNULL(T2.Delta, T1.Delta) and
T1.Cost= ISNULL(T2.Cost, T1.Cost)
)
)
The problem is the result set is not correct. I have tried with a smaller column sample and able to exclude the correct combination of Customer and Country but when I introduce a 3rd or 4th column combination I can eyeball the result set and immediately see its incorrect. Not sure if I have to use multiple NOT EXISTS for each possible combination, was hoping not to.
A constraint is A has to be a view not a table. Otherwise I would have used variables in some manner and wrapped the whole thing in a stored procedure.
Appreciate any help, fall back is to manually add to the code each time an item combination is supplied in B!

Datastage: Split multiple sequential rows under different columns

I've got this type of data in my Database. Imagine that File_Name is the column name and so I need to take all the rows (Under "File_name") and put them into different columns with different Names.
File_Name (Column Name)
File1 (First Row)
File2 (Second Row)
File3 (Third Row)
And I need to put them in another file like this:
File_Name1 (Column Name1) ,File_Name2 (Column Name2), File_Name3 (Column Name3)
File1 (Under First column), File2 (Under Second Column), File3 (Under Third column)
Is there a stage that can help me? I tried using the Pivot but I can't really figure how to set it with just one input column.
So assuming you just want a single result row from your input (that is what I understood from your question) I would use a Transformer (or Column Generator) to add an artificial column with a value of 1 for all rows.
You tried already with the Pivot Enterprise stage and with that additional column it will be possible to transform it into the result you need.