Query to get the line with the latest date - sql

SELECT T0.[ItemCode], T0.[PriceFOB], T0.[PriceAtWH], T0.[DocEntry], T0.[CardCode], T2.[DocDate], T3.[CardName] FROM IPF1 T0 LEFT JOIN OIPF T2 ON T2.[DocDate] = (SELECT MAX(T4.[DocDate]) FROM OIPF T4 WHERE T0.[DocEntry]=T4.[DocEntry]) INNER JOIN OCRD T3 ON T0.[CardCode] = T3.[CardCode] WHERE T3.[GroupCode] = '105'
I am using the following query listed above.
The issue rises where I have multiple lines of the same item listed but my goal is to get the one with the LATEST DocDate.
If someone can please help and explain what it is that I am doing wrong.
I have tried Order By, Group By but keep getting multiple errors (syntax mostly)
SELECT ItemCode, PriceAtWH, DocEntry, PriceFOB, CardCode FROM ( SELECT *, ROW_NUMBER() OVER (PARTITION BY T0.[ItemCode] ORDER BY T0.[DocEntry] DESC) AS ROWNUM FROM IPF1 T0) IPF1 WHERE ROWNUM = 1
This is the other query that I was able to come close to my solution but I need it to filter for only GroupCode = '105' from OCRD matching the CardCode
The Main goal is to filter out a Master DATA which is combined with IPF1 and OCRD(Only need to pull Group Code from this) and to filter the IPF1 with the Group Code first. Then proceed to do the Max Date or Max DocEntry ID for each item.

You could use a distinct to get one row per event even if they have different dates and use the MAX on the select
SELECT distinct T0.[ItemCode],
T0.[PriceFOB],
T0.[PriceAtWH],
T0.[DocEntry],
T0.[CardCode],
T3.[CardName],
MAX(T2.[DocDate] ) DocDate
FROM IPF1 T0
LEFT JOIN OIPF T2
ON T0.[DocEntry]=T2.DocEntry]
INNER JOIN OCRD T3
ON T0.[CardCode] = T3.[CardCode]
WHERE T3.[GroupCode] = '105'
GROUP BY T0.[ItemCode], T0.[PriceFOB], T0.[PriceAtWH], T0.[DocEntry], T0.[CardCode],T3.[CardName]

WITH T AS (SELECT T10.[DocEntry], T10.[CardCode], T10.[ItemCode], T10.[PriceFOB], T10.[PriceAtWH] FROM IPF1 T10 INNER JOIN OCRD T11 ON T10.[CardCode] = T11.[CardCode] WHERE T11.[GroupCode] = '105')
SELECT T.[DocEntry], T.[CardCode], T.[ItemCode], T.[PriceFOB], T.[PriceAtWH] FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY T.[ItemCode] ORDER BY T.[DocEntry] DESC) AS ROWNUM FROM T) T WHERE ROWNUM = 1
So I ended up solving my own problem.
Thank you Julissa for trying buy your query has an error where you are missing a "[" in front of DocEntry and also the query does not do what I want it do, multiple instances of the same item for all date still show up. Thank you for trying!

Related

SQL: Adding another MAX field to existing MAX Date Query

I have an existing query that is picking up the max date of the exdt field per below for every record for that particular item. I'm trying to add an additional MAX calculation to the sqn field, essentially picking up records with the highest exdt and sqn field together. I tried adding an additional "AND (a.sqn = (SELECT MAX(sqn) to the WHERE and it seems to not like multiple selects.
SELECT a.item, a.cpc, dt.dsc, a.exdt, a.sqn
FROM dbo.ct AS a INNER JOIN dbo.dt ON a.cpc = dt.cpc
WHERE (a.exdt = (SELECT MAX(exdt) AS exdt FROM dbo.ct AS a
Any help would be appreciated!
Thanks.
The script to get records having max of both fields is the following
SELECT a.item, a.cpc, dt.dsc, a.exdt, a.sqn
FROM dbo.ct AS a INNER JOIN dbo.dt ON a.cpc = dt.cpc
WHERE
a.exdt = (SELECT MAX(exdt) AS exdt FROM dbo.ct )
and
a.sqn= (SELECT MAX(sqn) AS exdt FROM dbo.ct)
You can also use CTEas follows
with
cte_principal as
(SELECT a.item, a.cpc, dt.dsc, a.exdt, a.sqn
FROM dbo.ct AS a INNER JOIN dbo.dt ON a.cpc = dt.cpc),
cte_max_exdt as (SELECT MAX(exdt) AS exdt FROM dbo.ct),
cte_max_sqn as (SELECT MAX(sqn) AS exdt FROM dbo.ct)
select p.* from cte_principal p
inner join cte_max_exdt on p.exdt =cte_max_exdt.exdt
inner join cte_max_sqn pn p.sqn=cte_max_sqn.sqn
Perhaps you need subquery which is going to find max date for each item.
That is ct_max_date in my case
After that you join that subquery by max_date and item_Id
SELECT a.item, a.cpc, dt.dsc, a.exdt, a.sqn
FROM dbo.ct AS a
INNER JOIN dbo.dt ON a.cpc = dt.cpc
inner join (
select max(exdt) as max_date, ct.cpc
from dbo.ct
group by ct.dt
) as ct_max_date on a.cpc=ct_max_date.cpc and a.exdt=ct_max_date.max_date

Nesting queries on JOINS and top1 with ties

I am trying to use the result of the below SQL query-1 such that I can make another JOIN on this with my second query result to retrieve Fundsrc on the common ID - Project.
QUERY 1-
SELECT top 1 with ties
t.project, r.rel_value AS "FundSrc" ,r.date_to
from atsproject t
LEFT OUTER JOIN aglrelvalue r ON(t.client=r.client AND r.rel_attr_id='ZB18' AND r.attribute_id='B0' AND t.project=r.att_value)
WHERE r.date_To > '04/30/2020' and status='n'
ORDER BY row_number() over (partition by t.project order by t.project, r.rel_value)
I cannot put the JOIN inside the above query as it will mess with the result. Instead, if I can do a nesting on this then I think that should solve the issue.
My second query is -
SELECT
t.project,t.work_order as activity, r1.labor_funding_source2_fx AS "Designated Labour Funding"
FROM atsworkorder t
LEFT OUTER JOIN afxactlaborfund r1 ON( t.work_order = r1.dim_value AND t.client = r1.client AND r1.attribute_id = 'BF')
WHERE t.client='PC' and t.status = 'N'
The Output should be -
t.project,t.work_order from query 2 + Fundsrc from Query 1, with the common id on Project ID.
Any suggestions on this is highly appreciated.
You can wrap 'subqueries' in parenthesis and then join them.
Can you try this?:
SELECT *
FROM (
SELECT top 1 with ties t.project,
r.rel_value AS "FundSrc",
r.date_to
FROM atsproject t
LEFT OUTER JOIN aglrelvalue r
ON t.client=r.client
AND r.rel_attr_id='ZB18'
AND r.attribute_id='B0'
AND t.project=r.att_value
WHERE r.date_To > '04/30/2020' and status='n'
ORDER BY row_number() over (partition by t.project order by t.project, r.rel_value)
) AS TABLE_1
LEFT JOIN
(
SELECT t.project,
t.work_order as activity,
r1.labor_funding_source2_fx AS "Designated Labour Funding"
FROM atsworkorder t
LEFT OUTER JOIN afxactlaborfund r1
ON t.work_order = r1.dim_value
AND t.client = r1.client
AND r1.attribute_id = 'BF'
WHERE t.client='PC' and t.status = 'N'
) AS TABLE_2
ON TABLE_1.PROJECT = TABLE2.PROJECT
I am pretty sure an ORDER BY clause will not work within a subquery. Thus, this should probably work:
SELECT *
FROM (
SELECT t.project,
r.rel_value AS "FundSrc",
r.date_to,
row_number() over (partition by t.project order by t.project, r.rel_value) AS MY_RANKING
FROM atsproject t
LEFT OUTER JOIN aglrelvalue r
ON t.client=r.client
AND r.rel_attr_id='ZB18'
AND r.attribute_id='B0'
AND t.project=r.att_value
WHERE r.date_To > '04/30/2020' and status='n'
) AS TABLE_1
LEFT JOIN
(
SELECT t.project,
t.work_order as activity,
r1.labor_funding_source2_fx AS "Designated Labour Funding"
FROM atsworkorder t
LEFT OUTER JOIN afxactlaborfund r1
ON t.work_order = r1.dim_value
AND t.client = r1.client
AND r1.attribute_id = 'BF'
WHERE t.client='PC' and t.status = 'N'
) AS TABLE_2
ON TABLE_1.PROJECT = TABLE2.PROJECT
WHERE TABLE_1.MY_RANKING = 1
Note: On your formatting, wrap words within ` when they refer to code. They will look like this.
Wrap blocks of code within three of those (three at the beginning and at the end). It will look like the blocks of code above.

SQL Oracle/Joining two queries

I have two queries which I need to stitch together but I’m not sure how….
This first query pulls through the last three reconciled amounts for any chosen account from a table of accounts, reconciled amounts, periods, and any amount written off (if there was any)
SELECT *
FROM (
SELECT *
FROM (
SELECT gwod.account_id,
EXTRACT(month FROM gwod.charge_period_start) charge_period_month,
SUM(gwod.total_due_on_charge) total_due_on_charge,
SUM(gwod.amount_written_off) amount_written_off,
DENSE_RANK() over (PARTITION BY gwod.account_id
ORDER BY EXTRACT(month FROM
gwod.charge_period_start) DESC) rownumber
FROM Accounts_report gwod
WHERE account_id IN ('')
GROUP BY gwod.account_id,
EXTRACT(month FROM gwod.charge_period_start)
HAVING SUM (gwod.total_due_on_charge) <> 0) t1
WHERE t1.rownumber <=3)
PIVOT (MAX(charge_period_month) charge_period,
MAX(total_due_on_charge) total_due_on_charge,
MAX(amount_written_off) amount_written_off
FOR rownumber IN (1,2,3))
ORDER BY account_id
This query essentially gets me the list of accounts which I’m interested in from some additional tables...
WITH Account_Owners AS
(select gs.account_id, AP.SUPERVISOR
from Account_Info gs
Left join ACC_OWNERS AD
On gs.account_id = AD.ACCOUNT_NUMBER
Left Join Onwers_Info AP
On ad.owned_by = AP.ADNAME
group by account_id, AP.SUPERVISOR
)
SELECT distinct POLICY_INFO.ACCOUNT_ID, Count (POLICY_INFO.POLICY_NO) As
Active, a.supervisor
FROM POLICY_INFO
inner join Account_owners a on policy_info.account_id = a.account_id
WHERE Policy_Info.POLICY_STATUS = 'Active'
And policy_info.ACCOUNT_ID is not Null
And a.supervisor in ('David Smith')
GROUP BY Policy_Info.ACCOUNT_ID, a.supervisor
ORDER BY Policy_Info.ACCOUNT_ID
What I want to do is have the one query which pulls through the last three reconciled amounts (as per the first query) for all of the accounts of interest (as per the second query); I’m having trouble combining the two however into the single query however…
Add the first query as another set in the with clause and INNER JOIN it.Also DISTINCT might not be required in the final select as you are grouping any way. Try this and let me know if it's correct as it is very difficult for me to visualize data only with the query.
WITH Account_charges AS
(
SELECT *
FROM (
SELECT *
FROM (
SELECT gwod.account_id,
EXTRACT(month FROM gwod.charge_period_start) charge_period_month,
SUM(gwod.total_due_on_charge) total_due_on_charge,
SUM(gwod.amount_written_off) amount_written_off,
DENSE_RANK() over (PARTITION BY gwod.account_id
ORDER BY EXTRACT(month FROM gwod.charge_period_start) DESC) rownumber
FROM Accounts_report gwod
WHERE account_id IN ('')
GROUP BY gwod.account_id,
EXTRACT(month FROM gwod.charge_period_start)
HAVING SUM (gwod.total_due_on_charge) <> 0) t1
WHERE t1.rownumber <=3)
PIVOT (MAX(charge_period_month) charge_period,
MAX(total_due_on_charge) total_due_on_charge,
MAX(amount_written_off) amount_written_off
FOR rownumber IN (1,2,3))
),
Account_Owners AS
(select gs.account_id, AP.SUPERVISOR
from Account_Info gs
Left join ACC_OWNERS AD
On gs.account_id = AD.ACCOUNT_NUMBER
Left Join Onwers_Info AP
On ad.owned_by = AP.ADNAME
group by account_id, AP.SUPERVISOR
)
SELECT distinct POLICY_INFO.ACCOUNT_ID, Count (POLICY_INFO.POLICY_NO) As
Active, a.supervisor ,MAX(b.charge_period),MAX(b.total_due_on_charge),MAX(b.amount_written_off)
--use the proper column names.
FROM POLICY_INFO
inner join Account_owners a on policy_info.account_id = a.account_id
INNER JOIN Account_charges b ON policy_info.account_id = b.account_id
Where Policy_Info.POLICY_STATUS = 'Active'
And policy_info.ACCOUNT_ID is not Null
And a.supervisor in ('David Smith')
Group by Policy_Info.ACCOUNT_ID, a.supervisor
order by Policy_Info.ACCOUNT_ID;

Can this SQL Server CTE be expressed with a "simpler" basic query?

So that I can union all or join it to a larger outer query of some kind?
-- Get purchase order due date for first purchase order after today which completes a cumulative sum satisfying demand which currently exceeds inventory supply
with cte as
(
select
t1.itemcode as itemcode,
t1.dscription as itemname,
t1.quantity as quantity,
(select sum(t6.quantity)
from por1 t6
inner join opor t7 on t6.docentry = t7.docentry
where t7.docduedate <= t2.docduedate
and t7.docduedate > getdate()
and t6.itemcode = 'item001') as totalqty,
t2.docduedate as eta
from
por1 t1
inner join
opor t2 on t1.docentry = t2.docentry
where
t1.itemcode = 'item001'
and t2.docduedate > getdate()
)
select
cte.itemcode, cte.itemname,
convert(char(10), min(cte.eta), 101)
from
cte
where
totalqty > 55 -- I don't want this to be literal, the outer join would supply this as a calculated value
group by
cte.itemcode, cte.itemname
Re the outer query (which isn't included above because I haven't been able to join an outer query to the cte - I don't know that you can), I'd like it to calculate the amount, currently shown as 55, from the outer query and control which itemcodes to return based on a filtered list from an outer query instead of choosing just one literal itemcode (as above), for example.
I made various attempts to get totalqty using a having clause with no success.
Thanks in advance for your help.
Do you want to get the result of what TotalQty is, or do you only want to use it as a condition?
If it's the former, I would say the CTE way is the best. Otherwise, I would reframe it as a WHERE EXISTS clause on the select going into TotalQty, like WHERE EXISTS (SELECT SUM(T6.Quantity) [...] HAVING SUM(T6.Quantity) > 55).

inner join with two selects sql

I am trying to implement an inner join to compare values of two tables, however failing for some reason and the query is returning zero columns.
I have two tables security and security_his and trying to join them on columns SECURITY_ID and INVESTMENT_OBJECTIVE. Query is as follows
SELECT *
FROM SECURITY origin
INNER JOIN (
SELECT *
FROM SECURITY_HIS t2
WHERE DATED = (
SELECT MAX(DATED)
FROM SECURITY_HIS t1
WHERE t1.SECURITY_ID = t2.SECURITY_ID
)
) history ON origin.SECURITY_ID = history.SECURITY_ID
AND origin.INVESTMENT_OBJECTIVE = history.INVESTMENT_OBJECTIVE;
WITH cte as (
SELECT S.*,
row_number() over
(partition by S.SECURITY_ID ORDER BY SH.DATED DESC)
FROM SECURITY S
JOIN SECURITY_HIS SH
ON S.SECURITY_ID = SH.SECURITY_ID
AND S.INVESTMENT_OBJECTIVE = SH.INVESTMENT_OBJECTIVE
)
SELECT *
FROM cte
WHERE rn = 1
You have no GROUP BY on the innermost query, so only a single value, maxed over the entire table, is returned. However your query can also be simplified for easier understanding:
SELECT origin.*, history.Dated
FROM SECURITY origin
INNER JOIN (
SELECT
SECURITY_ID,
INVESTMENT_OBJECTIVE,
MaxDated = MAX(DATED)
FROM SECURITY_HIS t2
GROUP BY
SECURITY_ID,
INVESTMENT_OBJECTIVE
) history ON origin.SECURITY_ID = history.SECURITY_ID
AND origin.INVESTMENT_OBJECTIVE = history.INVESTMENT_OBJECTIVE