HQL where clause query - hql

I have data, and I am trying to find out if data from one column affects data in another column. I can't put my real data up but an example is:
A 12345
A 23456
A 34567
A 45678
A 56789
B 12345
B 23456
B 34567
B 45678
B 56789
What I am trying to do is find where the data in column B (numbers) appears more than once in Column A (A or B).
I have roughly:
select *
from database
where number < 56790
and > 12344
and COLUMN A ('A;, 'B');
I have tried many variations of the Where clause but cant seem to get it sorted. Any ideas?
I am new to coding so sorry if this has been asked before but I couldn't find an answer.
Thanks in advance
I have the select and from sections ok in my query, its just this one bit I cant get.

Related

Google Sheet Date and Time Calculation Question

I have a column that has 34 records of Week Day, Month/Day, and Times. I am looking for two formulas that I can use in a table that will give me the count of weekdays and the time duration per day. Eventually, I would like to just copy and past new dates into column A and have the table automatically calculate. Here is my google sheet example. Is there a way to do this without creating helper columns? If not, no big deal. Anything to help automate the process will be helpful.
https://docs.google.com/spreadsheets/d/1C6N94QJyEgm-2yg2SEDOweIU2fk2h2DLydKb-nH-ObE/edit?usp=sharing
enter image description here
Take a look at the Punches tab in the sample sheet below. It shows what I was mentioning about breaking the columns up. Then, using the QUERY() function I was able to populate the table.
https://docs.google.com/spreadsheets/d/1qbLOjTdzISICTKyUp_jK6gZbQCt-OwtDYYy3HNJygeE/edit#gid=1181136581
G6 Formula
=if(isna(query($A$1:$C, "SELECT COUNT(B) WHERE B ='"&F2&"' LABEL COUNT(B) ''",1)),0,query($A$1:$C, "SELECT COUNT(B) WHERE B ='"&F2&"' LABEL COUNT(B) ''",1))
H6 Formula
=if(G2<>0,query($A$1:$C, "SELECT C WHERE B ='"&F2&"' ORDER BY C DESC LIMIT 1 LABEL C ''",1)-query($A$1:$C, "SELECT C WHERE B ='"&F2&"' ORDER BY C LIMIT 1 LABEL C ''",1),0)

Oracle: How to check if value exists in a collection type column?

Let say I've two tables,
Table A
PK SIZE
89733 5
83644 3
87351 8
84423 11
Table B
ID Table_A_PK
1 89733,83644,86455
2 87351,89542
3 84132
4 84566,84646
Note: Column Table_A_PK is of collection type, that's why it has many values.
I want to select value of size column of Table A if column PK value exits in Table B's column Table_A_PK
For this I tried this but it's not working and throwing an error
Select {a.SIZE}
from {A as a} where {a.PK}
in ({{ SELECT {b.Table_A_PK} FROM {B as b}
Actual Result: ORA-01722: invalid number
Expected Result
SIZE
5
3
8
First, collectiontypes are deprecated. If you use them by choice, prefer relations. They are much easier to work with.
I realized this once with the LIKE operator:
... WHERE Table_A_PK LIKE '%MYPK%'
However this is NOT best practice.
You might be able to use the Concat-Funktion to concatenate the % signs with the PK in the original table for a join. However I have not tried this.
SELECT {a.SIZE}
FROM {A AS a JOIN B AS b
ON {b.TABLE_A_PK} LIKE Concat('%', {a.pk}, '%') }
I would suggest to use Relation instead of CollectionType. If you are in the situation where you can't modify the itemType then you can search using LIKE operator
SELECT {a.SIZE}
FROM
{
B AS b JOIN A AS a
ON {b.TABLE_A_PK} LIKE CONCAT( '%', CONCAT( {a.PK} , '%' ) )
}

Sort entire Pivot table in Excel 2013 by column

I have a Pivot Table with many columns.
I want the Pivot the ability to sort one of the columns in a way that the whole column is sorted and not the relative position in the hierarchy.
Example:
NAME PRODUCT SUM
Joe A 400
Joe B 200
Joe B 300
Alice A 500
Alice A 200
Alice C 300
If I use the regular sort on the Sum column, I will get the data sorted partially.
Alice A 500
Alice A 300
Alice C 200
Joe A 400
Joe B 300
Joe B 200
As you can see, the Sum column is sorted only relevant to the Name column.
I want the whole column to be sorted.
Expected result should look something like this:
Alice A 500
Joe A 400
Alice C 300
Joe B 300
Alice A 200
Joe B 200
If you have more than one field in the row area of the pivot table, you cannot create a sort purely by value. The hierarchy cannot be ignored. That's how a pivot table works. Don't shoot the messenger.
So, essentially, you want to sort the SUM columns first and then the NAME column and last the PRODUCT column.
Have a look in the MSDN Sort data in a PivotTable, I think that should do the trick
Add a dummy column in the data set and create an index (1 through the number of rows). If you pull that into the left-most column in your pivot, you'll be able to sort by the field you want. You can hide that column for visual purposes.

How to know if a record DOESN'T exists on a table in Oracle

I'm dealing whit this for a couple of hours and I can't find the way to get the answer.
I've a table with a maximun of 4 records for a product (let's call it that way) for a diferent period (column name with a number). I'm trying to return the ones that DO NOT has a particular type of CONSUMPTION_TYPE_ID. But it doesn't work.
I'll explain it simple. I've a table with these fields (there are more, but these one are just fine)
product_id - CONSUMPTION_TYPE_ID - consumption_period
123 103 1
123 104 1
123 107 1
123 108 1
I need to return the ones that don't has one particular type of consumption, let's say that the type 107 is missing (the row doesn't exists), the select query should show the other 3 or any present. I don't mind doing the same select 4 times, I could also try to do a cursor for it and use loop to check every one. The point is, that the type of query with "not in" or "not exists" doesn't work. It gives me a result like the one given below, but when I query the "consumption_period" it shows me the missing "CONSUMPTION_TYPE_ID" and that's because the "not in" clause it's only hidding the results.
this is what I need.
select * from t1 where CONSUMPTION_TYPE_ID != 108;
product_id - CONSUMPTION_TYPE_ID - consumption_period
123 103 1
123 104 1
123 107 1
I hope you can help me with this. I'm stucked, it maybe simple, but I'm having one of those stucked times. Thanks in advance for any help
You probably should've posted that NOT EXISTS query that doesn't work, because that is the right way to do this.
If I got your requirements right: all products that do not have a record for a specific consumption_type_id.
SELECT DISTINCT product_id
FROM t1 t
WHERE NOT EXISTS
(SELECT 1 FROM t1
WHERE t.product_id = product_id
AND Consumption_Type_ID = ?)
The obvious answer here is to search for CONSUMPTION_TYPE_ID = 108 and have the surrounding code check for a lack of rows, rather than the existence of rows.
If you really need a row return for each consumption_type_id that's not in this table, then you should probably be selecting from the lookup table for consumption_type_id:
select *
from consumption_type ct
where not exists (select *
from t1
where t1.consumption_type_id = ct.consumption_type_id)
and ct.consumption_type_id = 108

more efficent way of reading data from two table and writing them in a new one using batch

I'm trying to write a spring batch to move data from two tables to a single table. I'm having a problem now and I thought of many ways to solve this problem but I'm still wondering if there is a more efficent solution to my problem?
Basically the problem is, I have two tables lets call them table A and table B and their structure is as the following:
table A
column 1A column 2A
======== ========
bmw 123555
nissan 123456777
audi 12888
toyota 9800765
kia 85834945
table B
column 1B column 2B
======== ========
12 caraudi
123456 carnissan
123 carbmw
0125 carvvv
88963 carbbn
what I'm trying to do is to create a table c from the batch's wrtier which holds all the data from table B (column 1B and column 2B)and column 1A only without losing any data from both tables and without writing duplicated data based on column 2A and column 1B. column A and column B have only one column in common (coulmn 1B == column 2A) but column 2A has a 3 digits suffix added to each id so if we do a join and compare I have to use a substr method and it will be very slow coz I have huge tables.
The other solution I thinked of is to have a reader for table A and write all results to tempA table without the suffix, then another reader that compare tables tempA and table B and write the data to table c as the following
table c
column 1A ( can be nullable because not all the records in column 2A exists in column 1B)
column 1B
column 2B
so the table will look like this
table C
column 1c column 2c column 3c
========= ========= =========
12 caraudi audi
123456 carnissan nissan
123 carbmw bmw
0125 carvv
88963 carbbn
9800765 toyota
85834945 kia
is this the bet way to solve the problem? or is there any other way that is more efficient?
thanks in advance!
Before giving up on a LEFT OUTER JOIN from tableA to tableB (or a FULL OUTER JOIN if your query conditions require it) consider using db2expln or the Visual Explain utility in IBM Data Studio to determine the cost of some alternative ways to perform a "begins with" match on VARCHAR columns:
ON a.col2a LIKE b.col1b || '___'
ON a.col2a >= b.col1b || '000' AND a.col2a <= b.col1b || '999'
If 1b is a CHAR column, you might need to trim off its trailing spaces before concatenating additional characters to it: RTRIM( b.col1b ) || '000'
Assuming column 1b is indexed, one prefix-based matching predicate or another is bound to make a join between those two tables less expensive than creating, populating, and joining to your own temp table. If I'm wrong (or there are other complicating factors) and a temp table ends up being the best option, be sure to use a declared global temporary table (DGTT) so you can avoid the logging overhead of populating it.

Resources