retriveing data using join in an sql query and reprenting it in jdbc template - sql

I have 3 tables:
REPOTRANSSMISSION TABLE column are
REPO_TRANSMISSION_ID,
G3_SESSION_ID,
CLIENT_NM,
ASSESSMENT_SESSION_ID,
PACKAGE_SESSION_ID,
TEST_SESSION_ID,
SCORE_SESSION_ID,
REPO_TRANSMISSION_STATE_CD,
REPO_TRANSMISSION_DATA_TX,
REPO_TRANSMISSION_LEVEL_CD,
CREATE_DT,
LAST_MODIFIED_DT.
here REPO_TRANSMISSION_ID is the primary key and REPO_TRANSMISSION_STATE_CD is the foregin key
2nd table REPO_ TRANSSMISSION_REQ_LOG column are
REPO_TRANSMISSION_REQ_LOG_ID
REPO_TRANSMISSION_ID
REQUEST_TX
RESPONSE_TX
ERROR_TX
CREATE_DT
LAST_MODIFIED_DT
here PK_REPO_TRANSMISSION_REQ_LOG is the primary key, REPO_TRANSMISSION_ID is foregin key
3rd tables REPO TRANSSMISSION STATE column are
REPO_TRANSMISSION_STATE_CD
REPO_TRANSMISSION_STATE_DS
CREATE_DT
LAST_MODIFIED_DT
and
REPO_TRANSSMISSION_STATE_CD values are TRANS_RESP,
RECON_REQ,
RECON_ERR,
RECON_RETRY,
RECON_RESP
here PK_REPO_TRANSMISSION_STATE_cd is the primary key
I have to retrieve the repo_transsmission_Id when the repotransmission_state_cd value is above 4 and I have to join the 1 st and 2nd table.
How I will write the sql query?

Do you just want to see how a query would look like to give you the results you need?
It would be something like this:
SELECT tr.repo_transmission_id
FROM REPOTRANSSMISSION tr
JOIN REPO_TRANSSMISSION_REQ_LOG lg ON (tr.REPO_TRANSMISSION_ID = lg.REPO_TRANSMISSION_ID)
WHERE tr.REPO_TRANSMISSION_STATE_CD > 4;

Related

Oracle SQL Subquery - Usage of NOT EXISTS

I used a query to find a list of Primary Keys. One Primary key per each ForiegnKey in a table by using below query.
select foreignKey, min(primaryKey)
from t
group by foreignKey;
Let us say this is the result : 1,4,5
NOw I have another table - Table B that has list of all Primary keys. It has 1,2,3,6,7,8,9
I want a write a query using the above query So that I get a subset of the original query(above) that does not exist in Table B. I want 4 and 5 back with the new query.
Use a having clause:
select foreignKey, min(primaryKey)
from t
group by foreignKey
having min(primarykey) not in (select pk from b);
You should also be able to express this as not exists:
having not exists (select 1
from b
where b.pk = min(t.primaryKey)
)

Joining 3 SQL tables without foreign keys relationships

I want to join 3 SQL tables into one. 2 of them are linked by a foreign key but the 3rd isn't.
The 3 tables schemes are:
PaysSociete
Employe
Ligne
I want to join the 3 of them into a unique table called User with the following columns:
Pays: PaysSociete.LibellePaysSociete
Societe: Employe.SOCIETE
Utilisateur: Ligne.UtilisateurLigne
Matricule: Employe.Matricule
NumLigne: Ligne.Numero
EmpMetier: Employe.SectionIris
In the table Ligne, the column CodeSociete corresponds to the idcolumn in the table PaysSociete (as a foreign key)
The column Societe of the table Employe corresponds to the CodePaysSociete column in PaysSociete (but it's not a foreign key because it isn't unique)
Here is my request:
select
case
when p.PaysSocieteId=1 then 'CountryCode1'
when p.PaysSocieteId=2 then 'CountryCode2'
when p.PaysSocieteId=3 then 'CountryCode3'
end Pays,
p.CodePaysSociete Societe,
l.UtilisateurLigne Utilisateur,
e.Matricule Matricule,
l.Numero NumLigne,
e.SectionIris EmpMetier
from Ligne as l
join PaysSociete as p on l.CodeSociete=CONVERT(varchar(10), p.PaysSocieteId)
join Employe as e on e.Societe = p.CodePaysSociete
But i'm getting a bad result with duplicated User.Matricule and User.EmpMetier columns.
How can I fix it?
Please help!!!
You can join tables on any column that corresponds to an other column. A foreign key is not necessary for a join.
SELECT *
FROM Ligne
JOIN PaysSociete ON Ligne.CodeSociete = CONVERT(varchar(10), PaysSociete.id)
JOIN Employe ON Employe.Societe = PaysSociete.CodePaysSociete
Problem solved!
The column ReferentielId of Ligne handled datas from column EmployeId of Employe.
A simple junction works.
Sorry for the trouble.

How do I delete record through a Join

I have an Access database where I wish to delete a record from a table using its referential entigrity to another table. For example I have the following two tables;
CI_Aliases with fields - CI_Ref (Primary Key) with a value of 3
and Aliase_ID (Foreign Key) with a value of 5
Aliases_Table with fields - Aliase_ID (Primary Key) with a value of 5
and Aliase with a value of "AMSS"
I have tried the following DELETE statement but I get the message "Cannot delete records from the specified table" - what am I doing wrong?
DELETE FROM Aliases_Table a
INNER JOIN CI_Aliases c
ON a.Aliase_ID = c.Aliase_ID
WHERE c.CI_Ref = 3
I should confirm that it is the record in the Aliases_Table i wish to delete but using the CI_Aliase primary key of "3"
Use the alias first, then the FROM clause. The syntax is a bit unintuitive
DELETE a.*
FROM Aliases_Table a
WHERE a.Aliase_ID IN (
SELECT
c.Aliase_ID
FROM CI_Aliases c
WHERE c.CI_Ref = 3
)

Insert data from one table to other using select statement and avoid duplicate data

Database: Oracle
I want to insert data from table 1 to table 2 but the catch is, primary key of table 2 is the combination of first 4 letters and last 4 numbers of the primary key of table 1.
For example:
Table 1 - primary key : abcd12349887/abcd22339887/abcder019987
In this case even if the primary key of table 1 is different, but when I extract the 1st 4 and last 4 chars, the output will be same abcd9887
So, when I use select to insert data, I get error of duplicate PK in table 2.
What I want is if the data of the PK is already present then don't add that record.
Here's my complete stored procedure:
INSERT INTO CPIPRODUCTFAMILIE
(productfamilieid, rapport, mesh, mesh_uitbreiding, productlabelid)
(SELECT DISTINCT (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4)))
productnummer,
ps.rapport, ps.mesh, ps.mesh_uitbreiding, ps.productlabelid
FROM productspecificatie ps, productgroep pg,
product p left join cpiproductfamilie cpf
on (CONCAT(SUBSTR(p.productnummer,1,4),SUBSTR(p.productnummer,8,4))) = cpf.productfamilieid
WHERE p.productnummer = ps.productnummer
AND p.productgroepid = pg.productgroepid
AND cpf.productfamilieid IS NULL
AND pg.productietype = 'P'
**AND p.ROWID IN (SELECT MAX(ROWID) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))**
AND (CONCAT(SUBSTR(p.productnummer,1,2),SUBSTR(p.productnummer,8,4))) not in
(select productfamilieid from cpiproductfamilie));
The highlighted section seems to be wrong, and because of this the data is not picking up.
Please help
Try using this.
p.productnummer IN (SELECT MAX(productnummer) FROM product
GROUP BY (CONCAT(SUBSTR(productnummer,1,4),SUBSTR(productnummer,8,4))))

SQL Server : show foreign key constraints tied to a single record

This probably is a bit complicated but is there anyway or already a script out there that could show you all foreign key constraints tied to a single table row.
What I mean by this is say you have the following DB structure:
TABLE 1
column a
column b
TABLE 2
column c
column d (foreign key constraint to 1.a)
TABLE 3
column e
column f (foreign key constraint to 2.c)
TABLE 4
column g (foreign key constraint to 3.e)
column h
Then, you have 2 rows in Table 1. One of the rows is constrained through table 2, then further to table 3, BUT not further to table 4 (IDs tied throughout tables 1-3).
I would like to simply query one of the rows in Table 1 and have it tell me that for that row there are ties that go to Table 2, and then those rows have ties to Table 3. Using this 'query' on the second row in Table 1 would simply just return nothing as there are no foreign keys that are tying that row down.
Something like this would be immensely useful when it comes to tracking down what tables/rows are currently using a particular starting row.
Thanks!
I think what you're looking for can be accomplished by:
SELECT a, t2=COUNT(d), t3 = COUNT(f), t4 = COUNT(g)
FROM [1] LEFT JOIN [2] ON 1.a=2.d
LEFT JOIN [3] ON 2.c = 3.f
LEFT JOIN [4] ON 4.g = 3.e