Better way of writing a nested query in SQl - sql

I wrote a piece of code and I was wondering if there is a more elegant, more efficient way of writing it.
SELECT act1.ACTIVITY_DETAIL_ID FROM CPMS.ACTIVITY_TRANSACTION act1
WHERE act1.USER_CREATED ='CPMSAutoRecovery'
AND act1.TRANSACTION_GROUP_ID =23 --commitment, not entitled
GROUP BY act1.ACTIVITY_DETAIL_ID HAVING COUNT(*)>1
AND act1.ACTIVITY_DETAIL_ID IN (
select ad.ACTIVITY_DETAIL_ID from CPMS.activity_detail ad, CPMS.PARTY_ROLE pr
where pr.ACTIVITY_REGISTRY_ID = ad.activity_registry_id
AND pr.PARTY_ROLE_type_ID=2
AND pr.PARTY_ID IN (
select distinct i.PARTY_ID FROM CPMS.INDIVIDUAL i, CPMS.PARTY_ROLE pr where
I.PARTY_ID = PR.PARTY_ID AND i.EXPIRY_DATE is null
)
)
order by act1.ACTIVITY_DETAIL_ID;
this is the key dependencies of the tables:
INDIVIDUAL
----------
PARTY_ID
PARTY_ROLE
------------
ACTIVITY_REGISTRY_ID
PARTY_ID
ACTIVITY_DETAIL
-----------------
ACTIVITY_DETAIL_ID
ACTIVITY_REGISTRY_ID
ACTIVITY_TRANSACTION
----------------------
ACTIVITY_TRANSACTION_ID
ACTIVITY_DETAIL_ID
UPDATE:
I made it a JOIN
SELECT act1.ACTIVITY_DETAIL_ID FROM CPMS.ACTIVITY_TRANSACTION act1
JOIN CPMS.ACTIVITY_DETAIL ad ON act1.ACTIVITY_DETAIL_ID = ad.ACTIVITY_DETAIL_ID
JOIN CPMS.PARTY_ROLE pr ON ad.ACTIVITY_REGISTRY_ID = pr.ACTIVITY_REGISTRY_ID
JOIN CPMS.INDIVIDUAL i ON pr.PARTY_ID = i.PARTY_ID
WHERE act1.USER_CREATED ='CPMSAutoRecovery'
AND act1.TRANSACTION_GROUP_ID =23
AND pr.PARTY_ROLE_type_ID=2
AND i.EXPIRY_DATE is null
GROUP BY act1.ACTIVITY_DETAIL_ID HAVING COUNT(*)>1
order by act1.ACTIVITY_DETAIL_ID;

Related

Exclude those orders which has service code 'AAAA'

An order can have multiple services. As per the functionality, I have to remove those orders from the selection which have a service code 'AAAA'.
I mean if an order has two services code 'AAAA' and 'AAAB'. Then this must not be included in the output because this order has a Service code 'AAAA'. Please see the screenshot below for more info.
Query:
Select BO.OrderID ,OrderDate ,BackgroundServiceID , BST.BST_Name ,BST_Code from
BackgroundOrder BO
JOIN BackgroundOrderService BOS ON BO.OrderID = BOS.OrderID
JOIN ams.lkpBkgSvcType BST ON BST.BST_ID = BOS.BackgroundServiceID
Expected Output Query:
Select BO.OrderID ,OrderDate ,BackgroundServiceID , BST.BST_Name ,BST_Code from
BackgroundOrder BO
JOIN BackgroundOrderService BOS ON BO.OrderID = BOS.OrderID
JOIN ams.lkpBkgSvcType BST ON BST.BST_ID = BOS.BackgroundServiceID
Where BST.BST_Code = 'AAAB' AND BO.OrderID not in
(Select BO.OrderID from
BackgroundOrder BO
JOIN BackgroundOrderService BOS ON BO.OrderID = BOS.OrderID
JOIN ams.lkpBkgSvcType BST ON BST.BST_ID = BOS.BackgroundServiceID
Where BST.BST_Code = 'AAAA'
)
Please suggest some other ways to get the above output without using a not-in statement.
You can wrap your query in a cte and count the occurences of 'AAAA' per OrderId then filter where it's zero
with t as (
select BO.OrderID, OrderDate, BackgroundServiceID, BST.BST_Name, BST_Code,
Sum(case when bst_code='AAAA' then 1 else 0 end) over(partition by BO.OrderID) cnt=0
from BackgroundOrder BO
join BackgroundOrderService BOS on BOS.OrderID = BO.OrderID
join ams.lkpBkgSvcType BST on BST.BST_ID = BOS.BackgroundServiceID
)
select OrderID, OrderDate, BackgroundServiceID, BST_Name, BST_Code
from t
where cnt=0

How to check more than one unrelated conditions in SQL?

I have following query:
SQL> SELECT DISTINCT INSTRUCTORADDRESSMODPER.instructor_id, Instructor.instructor_name, InstructorRank.salary, Student.specification_id
2 FROM INSTRUCTORADDRESSMODPER
3 JOIN Student ON INSTRUCTORADDRESSMODPER.student_id = Student.student_id
4 JOIN Instructor ON INSTRUCTORADDRESSMODPER.instructor_id = Instructor.instructor_id
5 JOIN InstructorRank ON Instructor.instructor_rank = InstructorRank.instructor_rank
6 ORDER BY specification_id;
which has yielded following result:
I was trying to get result which shows same column values for instructors with same salary and same specification as highlighted in the figure. Now these two conditions require completely different checks and I don't even know how to get started.
You need something like this ?
SELECT instructor_id, instructor_name, instructor_name, specification_id
FROM (
SELECT DISTINCT INSTRUCTORADDRESSMODPER.instructor_id, Instructor.instructor_name, InstructorRank.instructor_name, Student.specification_id
, COUNT(distinct INSTRUCTORADDRESSMODPER.instructor_id)over(partition by InstructorRank.salary, Student.specification_id) cnt
FROM INSTRUCTORADDRESSMODPER
JOIN Student ON INSTRUCTORADDRESSMODPER.student_id = Student.student_id
JOIN Instructor ON INSTRUCTORADDRESSMODPER.instructor_id = Instructor.instructor_id
JOIN InstructorRank ON Instructor.instructor_rank = InstructorRank.instructor_rank
ORDER BY specification_id
)
WHERE cnt > 1
;
You can use the window function count as follows:
Select * from
(SELECT DISTINCT INSTRUCTORADDRESSMODPER.instructor_id, Instructor.instructor_name, InstructorRank.salary, Student.specification_id,
Count(1) over (partition by InstructorRank.salary, Student.specification_id) as cnt
FROM INSTRUCTORADDRESSMODPER
JOIN Student ON INSTRUCTORADDRESSMODPER.student_id = Student.student_id
JOIN Instructor ON INSTRUCTORADDRESSMODPER.instructor_id = Instructor.instructor_id
JOIN InstructorRank ON Instructor.instructor_rank = InstructorRank.instructor_rank)
Where cnt > 1
ORDER BY specification_id;
SQL> SELECT DISTINCT INSTRUCTORADDRESSMODPER.instructor_id,
Instructor.instructor_name,InstructorRank.salary, Student.specification_id
FROM INSTRUCTORADDRESSMODPER
WHERE 1=1
AND INSTRUCTORADDRESSMODPER.student_id = Student.student_id
AND INSTRUCTORADDRESSMODPER.instructor_id = Instructor.instructor_id
AND Instructor.instructor_rank = InstructorRank.instructor_rank
AND ORDER BY specification_id
AND ...;
With the above use, desired conditions can be added.

Can't order query correctly

A while ago I requested help to code a LEFT JOIN filtering in a particular way that the result postition the desired value in the first row.
Need to retrieve table's last inserted/updated record with some exclusions
The thing now is that there are many cases which are mixing data. The scenario is that on the same table we have 2 values that we need to organize on different columns. The PO_ID is unique, but can have 1 or more values on the other tables, and for this particular case 1 PO_ID has 3 SHIP_ID_CUS values. We only need 1 PO_ID per row (no duplicates) that is way we used the MAX() and GROUP BY.
Here is a piece of the code that I think cause issues.
select
z.po_id,
max(scdc.ship_id) as ship_id_cdc,
max(lscdc.ship_evnt_cd) as last_event_cdc,
max(lscdc.ship_evnt_tms) as event_tms_cdc,
max(scus.SHIP_ID) as ship_id_cus,
max(lscus.ship_evnt_cd) as last_event_cus,
max(lscus.ship_evnt_tms) as event_tms_cus
from TABLE.A z
left join (select distinct po_id, iltc.ship_id, s.ship_to_loc_code from TABLE.B iltc inner join TABLE.C s on iltc.ship_id=s.ship_id and iltc.ship_to_loc_code=s.ship_to_loc_code and s.ship_to_ctry<>' ') AS A ON z.po_id = a.po_id
left JOIN TABLE.C scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)
left JOIN TABLE.C scdc ON A.SHIP_ID = scdc.SHIP_ID AND A.SHIP_TO_LOC_CODE = scdc.SHIP_TO_LOC_CODE and scdc.loc_type = 'CDC' AND DAYS(scdc.shipment_tms)+10 >= DAYS(z.ship_tms)
left join
( select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
, row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM TABLE.D
WHERE LEFT(ship_evnt_cd, 1) <> '9') lscus
ON lscus.ship_id_856=scus.ship_id and scus.ship_to_loc_code=lscus.ship_to_loc_cd856 and lscus.rn = 1
left join
( select ship_id_856, ship_to_loc_cd856, ship_evnt_cd, ship_evnt_tms, carr_tracking_num, event_srv_lvl
, row_number() over(partition by ship_id order by updt_job_tms desc) as RN
FROM TABLE.D
WHERE LEFT(ship_evnt_cd, 1) <> '9') lscdc
ON lscdc.ship_id_856=scdc.ship_id and lscdc.ship_to_loc_cd856=scdc.ship_to_loc_code and lscdc.rn = 1
WHERE
z.po_id = 'T1DLDC'
GROUP BY z.po_id
By searching that condition we get the following result
The problem is that if we search directly on the TABLE.D, the last event that we need (with last update record tms) is another one (X1) and somehow the date is incorrect.
What is even more weird, is that if we search for the ship_id_cus on the original query, we get the correct code but still with a wrong date...
WHERE
--z.po_id = 'T1DLDC'
scus.ship_id = 'D30980'
GROUP BY z.po_id
I tried other logic changes like modifying the left joins to search on a subquery.
left JOIN ( select * from TABLE.C order by updt_job_tms desc) scus ON A.SHIP_ID = scus.SHIP_ID AND A.SHIP_TO_LOC_CODE = scus.SHIP_TO_LOC_CODE and scus.loc_type = 'CUS' AND DAYS(scus.shipment_tms)+10 >= DAYS(z.ship_tms)
But this is also giving the same exact results by searching either by po_id or ship_id_cus
Any ideas or comment will be much appreciated.
Thanks
------------------------------------UPDATE-----------------------------------
Adding the result of the LEFT JOIN with the row_partition() including all the ship_id_cus for that po_id, and all the codes with the tms. None match here.
Based on all these, it should be the last ship_id_cus with X1 event/tms. If we exclude also the ones starting with 9, we would get the following result.
(I am not applying here ordering by ship_id_cus, which already described before that did not work either the way I implemented)
If you have a table: TBL1
ID APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC Y JOE 2019-01-13
ABC N ZACK 2018-12-23
ABC N SUE 2019-02-23
And you do SQL:
SELECT ID, MAX(APPROVED) AS APPROVAL
,MAX(APPROVER) AS APPROVED_BY , MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID
you will get result:
ID APPROVAL APPROVED_BY APPROVED_ON
====== ======== =========== =============
ABC Y ZACK 2019-02-23
which is correct to the code but is NOT what you want
Try the following:
SELECT T1.ID, T1.APPROVED, T1.APPROVER, T1.DATE_APPROVED
FROM TBL1 AS T1
INNER JOIN (SELECT ID, MAX(DATE_APPROVED) AS APPROVED_ON
FROM TBL1 GROUP BY ID
) AS T2
ON T1.ID =T2.ID
AND T1.DATE_APPROVED = T2.APPROVED_ON
Result:
ID APPROVED APPROVER DATE_APPROVED
====== ======== ======== =============
ABC N SUE 2019-02-23

Combining two tables including rows not found in other table, substituting values in column

I have two tables made from the following two queries:
SELECT t3.PatientID, SUM(t3.Fee) as total FROM
(SELECT t1.TestID, t2.PatientID, t1.Fee FROM
(SELECT Test.TestID, Test.Fee FROM MedicalTest AS Test) AS t1,
(SELECT T.TestID, T.PatientID FROM Take AS T) AS t2
WHERE t1.TestID = t2.TestID
ORDER BY t2.PatientID) AS t3
GROUP BY t3.PatientID
ORDER BY total DESC;
Which gives me a table of patient IDs and how much they have spent on tests, a portion of the table looks like this:
PATIENTID TOTAL
----------- ---------------------------------
99642131 550.00
99631255 440.00
99665378 430.00
99627956 310.00
99657423 280.00
99641125 260.00
99630025 230.00
99648682 230.00
My other query:
SELECT DISTINCT D.PatientID FROM Diagnose AS D
WHERE D.PhysicianID IN (
SELECT P.PhysicianID FROM Physician AS P
WHERE P.HName = 'Specific Hospital'
AND P.DName = 'Intensive Care Unit');
Returns to me a list of Patient IDs who are under the care of a specific physician. A portion of the table:
PATIENTID
-----------
99615376
99618797
99620783
99620882
99621221
I am trying to create a resultant table that contains the patient IDs from the second table as well as how much they have spent on medical tests from the first table. Some patients from the second table have not taken any tests in which case, I would like the table to simply give 0 for the total column, however, my attempt at combining the tables only gives me the patients who have taken tests:
SELECT t5.PatientID, t4.total FROM
(SELECT t3.PatientID, SUM(t3.Fee) as total FROM
(SELECT t1.TestID, t2.PatientID, t1.Fee FROM
(SELECT Test.TestID, Test.Fee FROM MedicalTest AS Test) AS t1,
(SELECT T.TestID, T.PatientID FROM Take AS T) AS t2
WHERE t1.TestID = t2.TestID
ORDER BY t2.PatientID) AS t3
GROUP BY t3.PatientID
ORDER BY total DESC) AS t4,
(SELECT DISTINCT D.PatientID FROM Diagnose AS D
WHERE D.PhysicianID IN (
SELECT P.PhysicianID FROM Physician AS P
WHERE P.HName = 'Specific Hospital'
AND P.DName = 'Intensive Care Unit')) AS t5
WHERE t5.PatientID = t4.PatientID;
Resultant table:
PATIENTID TOTAL
----------- ---------------------------------
99642131 550.00
99665378 430.00
99627956 310.00
How can I include the patients from table 2 that have not taken tests and enter 0 in their total column?
I managed to write the following query:
SELECT DISTINCT D.PatientID, COALESCE(total, '0') AS finalTotal
FROM Diagnose AS D
LEFT JOIN (
SELECT T.PatientID, SUM(M.Fee) AS total
FROM Take AS T, MedicalTest AS M
WHERE T.TestID = M.TestID
GROUP BY T.PatientID
) AS t1
ON t1.PatientID = D.PatientID
WHERE D.PhysicianID IN (
SELECT P.PhysicianID FROM Physician AS P
WHERE P.HName = 'Specific Hospital'
AND P.DName = 'Intensive Care Unit')
ORDER BY finalTotal DESC;
I use COALESCE(total, '0') with my LEFT JOIN to input 0 on the rows where no medical tests were done. This lead me to my desired output:
PATIENTID FINALTOTAL
----------- ------------------------------------------
99642131 550.00
99665378 430.00
99627956 310.00
99615376 0
99618797 0
99620783 0
99620882 0
99621221 0
99642157 0
99655482 0
99664061 0

SQL - Only show results for one column in first instance of a duplicated record

I am finding it difficult to explain exactly what I am trying to achieve so I think it best to show a visual representation.
Example of how my query results currently look
Example of how I want the results to look
The report I am running shows a list of every product within orders. Each product has its own cost assigned to it. Another column is a delivery charge, but this is a charge assigned to the order; not individual products. I want to be able to show the delivery charge against the first product in each order ONLY.
I have attempted, for far too long, to try and find an answer to this query but have had no luck. I don't know if it is even possible so assistance of any sort, or even just being pointed in the right direction, would be of great help.
Thanks
EDIT.
If it helps here is my query:
SELECT dbo.Orders.EncryptedOrderId,
dbo.OrderProduct.ProductID,
dbo.OrderProduct.QuantityPerRecipient,
dbo.OrderProduct.NumRecipients,
dbo.OrderProduct.TotalQuantity,
dbo.DocType.Name AS [Product Type],
dbo.ProductGroup_Culture.Name AS [Product Group],
RIGHT(CatalogNo, CHARINDEX('_', REVERSE('_' + CatalogNo)) -1) AS [HamptonsType],
FORMAT(dbo.Orders.DateOrderCreated, 'dd/MM/yyyy HH:mm:ss') AS 'DateOrderCreated',
CAST(REPLACE(dbo.Orders.ClearingResult, 'utf-8', 'utf-16') AS XML ).value('(/UserData//CostCenter/node())[1]', 'nvarchar(max)') AS [Cost Center],
dbo.Users.FirstName,
dbo.Users.LastName,
dbo.Users.CompanyName AS [Branch Name],
dbo.Users.Department AS Subsidiary,
dbo.Users.Custom1,
dbo.Users.Custom2,
dbo.Users.Custom3,
dbo.Users.Custom4,
dbo.Users.Custom5,
dbo.OrderProduct.TotalPrice,
dbo.Orders.ShippingCharges,
dbo.OrderProduct.OrderProductID,
dbo.FileSubmissionDocument.OriginalFileType,
COALESCE (dbo.FileSubmissionDocument.Title, dbo.Product_Culture.Name) AS [Product Name],
OPDV.FriendlyValue AS 'BCard Recipient'
FROM dbo.DocType
INNER JOIN dbo.Doc
ON dbo.DocType.DocTypeID = dbo.Doc.DocTypeID
INNER JOIN dbo.OrderProduct
ON dbo.Doc.ProductID = dbo.OrderProduct.ProductID
LEFT JOIN dbo.Product
ON dbo.Product.ProductID = dbo.Doc.ProductID
LEFT JOIN dbo.ProductGroupMembership
ON dbo.ProductGroupMembership.ProductID = dbo.Doc.ProductID
LEFT JOIN dbo.ProductGroup_Culture
ON dbo.ProductGroup_Culture.ProductGroupID = dbo.ProductGroupMembership.ProductGroupID
INNER JOIN dbo.Orders
ON dbo.OrderProduct.OrderID = dbo.Orders.OrderID
INNER JOIN dbo.Users
ON dbo.Orders.UserID = dbo.Users.UserID
INNER JOIN dbo.Product_Culture
ON dbo.OrderProduct.ProductID = dbo.Product_Culture.ProductID
INNER JOIN dbo.Store_Culture
ON dbo.Store_Culture.StoreID = dbo.Users.AssignedToStoreID FULL OUTER
JOIN dbo.FileSubmissionDocument
ON dbo.OrderProduct.OrderProductID = dbo.FileSubmissionDocument.SubOrderProductID - 1
LEFT JOIN (SELECT OP.OrderProductID,
OP.DialID,
OP.FriendlyValue
FROM OrderProductDialValue OP
LEFT JOIN Dial DI ON DI.DialID = OP.DialID
LEFT JOIN OrderProduct OT ON OT.OrderProductID = OP.OrderProductID
LEFT JOIN Product PR ON PR.ProductID = OT.ProductID
WHERE PR.ExternalID = 'BCName'
AND DI.UProduceDialName = 'Name') OPDV ON OPDV.OrderProductID = dbo.OrderProduct.OrderProductID
WHERE ('#CUSTOMERNAME' is null
OR '#CUSTOMERNAME' = ''
OR dbo.Store_Culture.Name LIKE '%' + '#CUSTOMERNAME' + '%')
AND dbo.OrderProduct.IsDraft = 0
AND dbo.Orders.IsCart=0
AND dbo.Orders.IsSaveForLater=0
AND (('#DATE' <= dbo.Orders.DateOrderCreated)
OR ('#DATE' IS NULL)
OR ('#DATE'=''))
AND ((DATEADD(day, 1, '#DATE') >= dbo.Orders.DateOrderCreated)
OR ('#DATE' IS NULL)
OR ('#DATE'=''))
AND dbo.Users.LastName NOT LIKE '%TEST%'
ORDER BY dbo.Orders.OrderID DESC, dbo.OrderProduct.OrderProductID DESC
The query runs through a reporting system on an online portal so the values that show as #CUSTOMERNAME or #DATE are variables based on values given at the time when the report is run.
this may help you
select orderid,
productid,
productvalue,
case ROW_NUMBER() over (partition by orderid order by orderid)
when 1 then deliverycharge
else null end as 'deliverycharge'
from ........
I assume your query looks like
select orderID
, productID
, productValue
, DeliveryCharge
from test_t
order by orderID
, productValue desc
;
and that you want delivery charges listed for the most expensive product of each order.
If supported by your rdbms, you can use the analytic RANK function
select orderID
, productID
, productValue
, DeliveryCharge
, CASE RANK() OVER ( PARTITION BY orderID ORDER BY productValue DESC ) WHEN 1 THEN DeliveryCharge ELSE NULL END r
from test_t
order by orderID
, productValue desc
;
If your rdbms does not support RANK, you can emulate it using a left join with a suitably aggregated copy of your table:
select t.orderID
, t.productID
, t.productValue
, rt.mdc DeliveryCharge
from test_t t
left join (
select orderID
, max(productValue) mp
, max(DeliveryCharge) mdc
from test_t
group by orderID
) rt
on (
rt.orderID = t.orderID
AND rt.mp = t.productValue
)
order by orderID
, productValue desc
;
The tacit assumption is that the delivery charge is by order (which seems reasonable as you wouldn't want to selectively drop it otherwise, right ?).
Moreover, both solutions will produce multiple rows containing the delivery charge per order if that order contains multipleproducts with the same productValue.
Tested on oracle 12c1;