how can i dynamically pass value to db2 search clause 'like' while fetching result from other table - search

Can someone help me how can i dynamically pass value to db2 search clause like while fetching result from other table.
I am trying this:
select * from table2 where file_name like '%(select file_name from table1)'
I've even tried CONTACT, using sysibm.sysdummy1 methods but no luck.

maybe, this help;
SELECT *
FROM table2
JOIN table1
ON table2.file_name LIKE CONCAT('%',table1.file_name)

Not having been shown the DDL for the files, nor any sample data and expected results from which a reader could determine if there might not be [other] considerations as implied obstacles, the following variation of the already-offered answer is more liberal in selecting what might be intended by the select * from table2 where file_name like '%(select file_name from table1)' from the OP; i.e. rather than effective predicates of ends-with [or a starts-with] the file-name value, the following achieves an effective predicate of contains the file-name value.
select /* t1.file_name, */ t2.*
from table2 as t2
inner join
table1 as t1
on t2.file_name like '%' concat rtrim(t1.file_name) concat '%'

Related

U-Sql not allowing non-equijoins

I have stumbled across a bit of an issue with U-SQL which for me is a problem I haven't yet found a workaround for.
It seems U-SQL doesnt support anything else but == in joins, so you can't put > or < in the join itself.
For the use case below which I have done in oracle:
create table trf.test_1(
number_col int
);
insert into trf.test_1 VALUES (10);
insert into trf.test_1 VALUES (20);
insert into trf.test_1 VALUES (30);
insert into trf.test_1 VALUES (60);
drop table trf.test_2;
create table trf.test_2(
number_col int
);
insert into trf.test_2 VALUES (20);
insert into trf.test_2 VALUES (30);
SELECT t1.number_col, t2.number_col
FROM trf.test_1 t1
LEFT JOIN trf.test_2 t2 ON t1.number_col < t2.number_col
;
I get the following:
How might I do that in u-sql without the < join?
I tried a cross join, but if you include the < in the where clause it just turns into an inner and you don't get the rows with the nulls.
Any ideas appreciated.
#t1 =
SELECT * FROM
( VALUES
(10),
(20),
(30),
(60)
) AS T(num_col);
#t2 =
SELECT * FROM
( VALUES
(20),
(30)
) AS T(num_col);
#result =
SELECT t1.num_col, t2.num_col AS num_col_2
FROM #t1 AS t1
CROSS JOIN #t2 AS t2
WHERE t1.num_col < t2.num_col;
#result2 =
SELECT t1.num_col, t2.num_col AS num_col_2
FROM #t1 AS t1
LEFT JOIN #result AS t2 ON t1.num_col == t2.num_col;
OUTPUT #result2
TO "/Output/ReferenceGuide/Joins/exampleA.csv"
USING Outputters.Csv();
Edit - I added the left join from the #t1 dataset back to the #result set which seems to work but would be interested if there are any better solutions out there. Seems a bit of a work around.
This is a known feature and discussed extensively in the article "U-SQL SELECT Selecting from joins".
Some quotes from that article:
Join Comparisons
U-SQL, like most scaled out Big Data Query languages
that support joins, restricts the join comparison to equality
comparisons between columns in the rowsets to be joined...
...
If one has a non-equality comparison or a more complex expression (such as a method invocation) in the comparison, one can move the comparison to the SELECT’s WHERE clause. Or the more complex expression can be placed in an earlier SELECT statement’s column and then that alias can be referred to in the join comparison.
Basically they don't scale particularly well on a distributed platform like ADLA.

How can I store the value of a subquery into a variable in Hana Studio?

I would like to know how can I store the value of subquery to use it in an operation after it recieve the value. For example:
Select IDTruck
, TruckPrice = (select "TruckPrice" from "Table1" where ("TruckID" = '123'))
, TruckUnit = (select "TruckUnit" from "Table2" )
, TruckPrice * TruckUnit as "PriceTotal"
from Table3
I just want to store the value and then use it in the operation so I don't have to do the select again.
I'm not sure why it should be necessary to store the values in variables for usage in your case. I think the calculation can be done also by joining just the data (assuming that table3 contains a reference to table1 and table2).
Your example above would also not work, because TruckPrice and TruckUnits are no atomar results.
So please try to refactor your statement to use joins.

Is it possible to chain subsequent queries's where clauses in Dapper based on the results of a previous query in the same connection?

Is it possible to use .QueryMultiple (or some other method) in Dapper, and use the results of each former query to be used in the where clause of the next query, without having to do each query individually, get the id, and then .Query again, get the id and so on.
For example,
string sqlString = #"select tableA_id from tableA where tableA_lastname = #lastname;
select tableB_id from tableB WHERE tableB_id = tableA_id";
db.QueryMultiple.(sqlString, new {lastname = "smith"});
Is something like this possible with Dapper or do I need a view or stored procedure to accomplish this? I can use multiple joins for one SQL statement, but in my real query there are 7 joins, and I didn't think I should return 7 objects.
Right now I'm just using object.
You can store every previous query in table parameter and then first perform select from the parameter and query for next, for example:
DECLARE #TableA AS Table(
tableA_id INT
-- ... all other columns you need..
)
INSERT #TableA
SELECT tableA_id
FROM tableA
WHERE tableA_lastname = #lastname
SELECT *
FROM #TableA
SELECT tableB_id
FROM tableB
JOIN tableA ON tableB_id = tableA_id

Maximo Conditional expression manager/Sql Query Builder

Can somebody help me in converting below mentioned query in to Maximo's where clause:
select distinct workorder.wonum from workorder inner join [assignment]
On workorder.wonum=[assignment].wonum
inner join amcrew
On amcrew.amcrew=[assignment].amcrew
inner join amcrewlabor
On amcrewlabor.amcrew=amcrew.amcrew
inner join labor
On amcrewlabor.laborcode=labor.laborcode
inner join person
on labor.laborcode=person.personid where amcrewlabor.laborcode='KELLYB'
KELLYB is PERSONID used here for just reference.
If you are using a custom search query in Maximo, you can try prepending your with in (your query)
For example, if you're in Maximo's work order tracking module, the application uses select * from workorder by default. Any time you add a search filter such as work order number (wonum), then the query appends to run a query as select * from workorder where wonum = '123' if 123 is the work order number you entered.
Your where clause might look something like this:
wonum in (
select distinct workorder.wonum
from workorder
join assignment on workorder.wonum=assignment.wonum
join amcrew on amcrew.amcrew=assignment.amcrew
join amcrewlabor on amcrewlabor.amcrew=amcrew.amcrew
join labor on amcrewlabor.laborcode=labor.laborcode
join person on labor.laborcode=person.personid
where amcrewlabor.laborcode='KELLYB'
)
The SQL that is generated in Microsoft Access will not necessarily work in Maximo without some modification.

Data in Excel with OLEDB connection from access don't update when use LIKE operator

When I use LIKE operator in a Query in Access and I create a OLEDB connection in Excel to import this data to Excel, the data don't update even after change the query with another filter. Is like if I had not changed the query with the new filter.
I can solve this taking this query and adding a INTO TempTable and linking this temptable in a OLEDB connection in excel. In this case the changes are reflected, but the query directly not.
Query
SELECT tb_fechamento_ddd.PERIODO,
tb_fechamento_ddd.DDD,
tb_fechamento_ddd.metrica,
tb_fechamento_ddd.categoria,
tb_fechamento_ddd.Qtd
FROM tb_fechamento_ddd
WHERE (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Like "*GSM*"
And (tb_fechamento_ddd.metrica) Not Like "*CDMA*"
And (tb_fechamento_ddd.metrica) Not Like "*LTE*"))
AND ((tb_fechamento_ddd.categoria)="Pre"))
OR (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Like "*GSM*"
And (tb_fechamento_ddd.metrica) Not Like "*CDMA*"
And (tb_fechamento_ddd.metrica) Not Like "*LTE*")
And (tb_fechamento_ddd.metrica) Like "Migra*")
AND ((tb_fechamento_ddd.categoria) Like "*Pré*"));
P.S: Sorry by excessive parenthesis, and duplicated filters in Where clause, but the access don't work Appropriately if I don't use this.
Based on post cited by HansUp:
I change every LIKE to ALIKE operator and change * of Access pattern by % of ANSI pattern.
Then:
SELECT tb_fechamento_ddd.PERIODO,
tb_fechamento_ddd.DDD,
tb_fechamento_ddd.metrica,
tb_fechamento_ddd.categoria,
tb_fechamento_ddd.Qtd
FROM tb_fechamento_ddd
WHERE (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Alike "%GSM%"
And (tb_fechamento_ddd.metrica) Not Alike "%CDMA%"
And (tb_fechamento_ddd.metrica) Not Alike "%LTE%"))
AND ((tb_fechamento_ddd.categoria)="Pre"))
OR (((tb_fechamento_ddd.PERIODO)>=#3/1/2014#)
AND ((tb_fechamento_ddd.DDD)="93")
AND (((tb_fechamento_ddd.metrica) Not Alike "%GSM%"
And (tb_fechamento_ddd.metrica) Not Alike "%CDMA%"
And (tb_fechamento_ddd.metrica) Not Alike "%LTE%")
And (tb_fechamento_ddd.metrica) Alike "Migra%")
AND ((tb_fechamento_ddd.categoria) Alike "%Pré%"));
Another bugs of Access Query on Excel that I get solutions:
Queries made with UNION don't is showed on list of queries on create a connection in Excel:
Create a SubQuery to "shelter" the query with Unions:
Select * from table_A
UNION ALLL
Select * from table_B
to
Select * from
(Select * from table_A
UNION ALLL
Select * from table_B) as SubQryUnion
Another bug:
If you use on field with a aggregate function a alias that have a same name that a field of table and use this alias in a expression field, the query doesn't is showed.
Select field1, field2, sum(table.qtd) as qtd, qtd/12 as yearmean
from table
group by field1, field2
Choice another name to aggregate field:
Select field1, field2, sum(table.qtd) as quantity, quantity/12 as yearmean
from table
group by field1, field2

Resources