Delete a specific value from a set of values in a column - HQL - hql

How can I write a HQL for deleting a specific value from a column ( column contains a set of values separated by comma)
Table1
ID Name Value
001 Rajesh 90,100,210,400
002 Suresh 100,400,300,66
003 Mahesh 200,500
004 Virat 400,578,57
I tried the following code but its wrong.
Session session=getSession();
Query query = session.createQuery("delete from Table1 where Value=:Value and Name=:Name");
query.setParameter("Value", "100");
query.setParameter("Name", "Rajesh");
query.executeUpdate();
I want to delete value- 100 from Name - Rajesh

Related

Oracle Merge - unable to get a stable set of rows in the source tables

I have a table Library
BranchNo BookShelfNo BookId BookSupplerNo
1234 4545 666
1234 4546 667
1234 4547 668
1234 4550 668
Table : BookSupplier
BookNo SupplierNo
666 9112
667 9897
667 9998
668 9545
I need to write an update statement where first I have to supply only the list of bookshelf, and the update query should find out
The BookId for that BookShelfNo in the Library Table.
Find SupplierNo in for that Book in the BookSupplier Table.
Update BookSupplierNo in the Library table.
Note : BookShelfNo is unique.
A BookId can have multiple SupplierNo and we can use any number
I had written something like this from Oracle SQL: Update a table with data from another table
MERGE INTO Library lib
USING
(
SELECT BookNo, SupplierNo FROM BookSupplier
)bs
ON(lib.BookId = bs.BookNo)
WHEN MATCHED THEN UPDATE SET
lib.BOOKSUPPLERNO = bs.SupplierNo
where lib.BookShelfNo in ('4545','4546')
It says,
ORA-30926: unable to get a stable set of rows in the source tables
I know it is because I have duplicates in BookSupplier column, but then I added distinct like this
MERGE INTO Library lib
USING
(
SELECT distinct BookNo, SupplierNo FROM BookSupplier
)bs
ON(lib.BookId = bs.BookNo)
WHEN MATCHED THEN UPDATE SET
lib.BOOKSUPPLERNO = bs.SupplierNo
where lib.BookShelfNo in ('4545','4546')
Now , it says
ORA-30926: unable to get a stable set of rows in the source tables
The diagnosis is correct, but the cure you tried makes no sense. The pairs (bookno, supplierno) are already distinct - what did you expect adding DISTINCT to do there?
Rather, the subquery should somehow get only one row (one single supplierno) for each bookno. For example:
......
using
(select bookno, max(supplierno) as supplierno from booksupplier group by bookno) bs
......

CONCATE UNIQUE VALUES BASED ON ID IN INFORMAITCA

SCENARO/input,
COL1 COL2
100 ABC
101 PQR
100 ABC
100 OPQ
101 HDR
101 PQR
Expected OUTPUT:
COL1 COL2
100 ABC,OPQ
101 PQR,HDR
This is one of classic string aggregation issue. Pls follow below steps.
using SRT order the data and get distinct. So, click enable distinct and then make sure you are ordering col1 first.
using expression transformation concat the COL2. Create 5 ports - in_out means input+output, in_ means input only, v_ means variable port and so on.
in_out_col1
in_col2
v_col2 = iif( in_out_col1=v_prev_col1, v_col2||',' ||in_col2,in_col2)
v_prev_col1 = in_out_col1
out_col2=v_col2
Create an AGG, group by col1. Create a new column max_col2 and assign the max value to it.
in_out_col1
in_col2
out_max_col2= MAX(in_col2)
Connect in_out_col1 to col1 and out_max_col2 to col2 for your desired data.

Sorting Matrix Columns in RDLC Report

I get the following query result:
EmployeeName payelement payelementValue payelementOrder
------------ ---------- --------------- ---------------
emp1 PE1 122 2
emp1 PE2 122 1
emp2 PE1 122 2
emp2 PE2 122 1
emp3 PE1 122 2
emp3 PE2 122 1
Which results in a report that looks like:
Employee Name PE2 PE1
emp1 122 122
emp2 122 122
emp3 122 122
I have created a matrix in rdlc report and and put the column field with the ->'payelement ' and the value field with ->'payelementValue' and set the rows field with ->'employeeName ' the problem now is that I want to sort the 'payelement' upon the field named 'payelementOrder' which represents the order for paylements in their actual table while I actually get them sorted alphabetically by defualt i.e.(PE1 then PE2). Any help would be greatly appreciated.
I Solved by this...
Go to the .rdlc... Check the Row Groups(which we will find in the left-bottom) under of that we will find the grouped column name (which we are having in the tables) then right click on it-> Go to Group properties... -> Go to sorting-> on the sort by give the column name which you want to sort according to and Click Ok.
And You are Done....
When you created a matrix you got a Column group. In the group properties of the column group you can set order by specific field (payelementOrder in your case)

How to group by a column and show as one row

I have a table with following structure
SubId Status
---------------------------------------
001 Active
001 Partially Active
While displaying this record, I need to display this like this
SubId Status
---------------------------------------
001 Active/Partially active
I tried using distinct, but this returns both rows.
Any idea how can I get only 1 row instead of 2. I know it would be simple. I just cannot find it.
select subid,
listagg(status, '/') within group (order by null) as status
from the_table
group by subid;

oracle connect by multiple parents

I am facing an issue using connect by.
I have a query through which I retrieve a few columns including these three:
ID
ParentID
ObjectID
Now for the same ID and parentID, there are multiple objects associated e.g.
ID ParentID ObjectID
1 0 112
1 0 113
2 0 111
2 0 112
3 1 111
4 1 112
I am trying to use connect by but I'm unable to get the result in a proper hierarchy. I need it the way it is showed below. Take an ID-parentID combo, display all rows with that ID-parentID and then all the children of this ID i.e. whose parentID=ID
ID ParentID ObjectID
1 0 112
1 0 113
3 1 111
4 1 112
2 0 111
2 0 112
select ID,parent_id, object_id from table start with parent_id=0
connect by prior id=parent_id order by id,parent_id
Above query is not resulting into proper hierarchy that i need.
Well, your problem appears to be that you are using a non-normalized table design. If a given ID always has the same ParentID, that relationship shouldn't be indicated separately in all these rows.
A better design would be to have a single table showing the parent child relationships, with ID as a primary key, and a second table showing the mappings of ID to ObjectID, where I presume both columns together would comprise the primary key. Then you would apply your hierarchical query against the first table, and join the results of that to the other table to get the relevant objects for each row.
You can emulate this with your current table structure ...
with parent_child as (select distinct id, parent_id from table),
tree as (select id, parent_id from parent_child
start with parent_id = 0
connect by prior id = parent_id )
select id, table.parent_id, table.object_id
from tree join table using (id)
Here's a script that runs. Not ideal but will work -
select * from (select distinct test.id,
parent_id,
object_id,
connect_by_root test.id root
from test
start with test.parent_id = 0
connect by prior test.id = parent_id)
order by root,id
First of all Thanks to all who tried helping me.
Finally i changed my approach as applying hierarchy CONNECT BY clause to inner queryw ith multiple joins was not working for me.
I took following approach
Get the hierarchical data from First table i.e. table with ID-ParentID. Select Query table1 using CONNECT BY. It will give the ID in proper sequence.
Join the retrieved List of ID.
Pass the above ID as comma seperated string in select query IN Clause to second table with ID-ObjectID.
select * from table2 where ID in (above Joined string of ID) order by
instr('above Joined string of ID',ID);
ORDER BY INSTR did the magic. It will give me the result ordered by the IN Clause data and IN Clause string is prepared using the hierarchical query. Hence it will obviously be in sequence.
Again Thanks all for the help!
Note: Above approach has one constraint : ID passed as comma separated string in IN Clause. IN Clause has a limit of characters inside it. I guess 1000 chars. Not sure.
But as i am sure on the data of First table that it will not be so much so as to cross limit of 1000 chars. Hence i chose above approach.

Resources