How to find amounts greater than zero before a specified date - tsql

Currently I am trying to find the outstanding balance of an account before a certain specified date.
below is my query
SELECT
TRANS.AMTUNRES UNPAID,
TRANS.TRANTYPE,
TRANS.INCPROVCODE IPROV,
ACHOLDERACH.ACBALANCE BALANCE,
ACHOLDERANY.PREFORMATTEDINTERN ACCOUNTNAMECHART,
WHENPAY,
ChartNo
FROM TRANS
LEFT JOIN ACHOLDER ACHOLDERACH ON ACHOLDERACH.ACHOLDERID = TRANS.ACHOLDERID
LEFT JOIN ANYBODY ACHOLDERANY ON ACHOLDERANY.ANYBODYID = TRANS.ACHOLDERID
LEFT JOIN INVPAYRESOLN ON ACHOLDERACH.ACHOLDERID = INVPAYRESOLN.ACHOLDERID
left join anybody on trans.anybodyid = anybody.anybodyid
where TRANS.AMTUNRES <> '0.00' and
ACHOLDERACH.ACBALANCE <> '0.00' and
WhenPay between '01-01-2012' and '04-07-2016'
and chartNo = '123456789'

Related

Add sorting to this select [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
PostgreSQL 15.2
select
current.id as currentId,
current.date as currentDate,
current.position as currentPosition,
current.relevant_url as currentRelevantUrl,
current.restrictions_probability as currentRestrictionsProbability,
current.url_changed as currentUrlChanged,
current.identifier_id as currentIdentifierId,
current.phrase_id as phraseId,
previous.id as previousId,
previous.date as previousDate,
previous.position as previousPosition,
previous.relevant_url as previousRelevantUrl,
previous.restrictions_probability as previousRestrictionsProbability,
previous.url_changed as previousUrlChanged,
previous.identifier_id as previousIdentifierId,
semantics_clusters.id as clusterId,
semantics_clusters.name as clusterName,
site_pages.id as pageId,
site_pages.url as pageUrl,
site_pages.site_id as siteId,
site_sites.url as siteUrl,
semantics_core_phrases.frequency as frequency
from (
select
id,
date,
position,
relevant_url,
restrictions_probability,
url_changed,
identifier_id,
phrase_id
from overoptimisation
where identifier_id = 1) current
left join (
select
id,
date,
position,
relevant_url,
restrictions_probability,
url_changed,
identifier_id,
phrase_id
from overoptimisation
where identifier_id = 1) previous
on current.phrase_id = previous.phrase_id
inner join semantics_core_phrases on current.phrase_id = semantics_core_phrases.id
inner join semantics_clusters on semantics_core_phrases.cluster_id = semantics_clusters.id
inner join site_pages on semantics_clusters.page_id = site_pages.id
inner join site_sites on site_pages.site_id = site_sites.id
This select works. But I have failed to add sorting.
How can I add order by for these fields?
siteId
pageId
clusterId
frequency
I don't see any problem to add order by clause.
Basically your SQL is valid, you can check it, for example, here
If you add order by clause in the end, then it is valid too:
select
current.id as currentId,
current.date as currentDate,
current.position as currentPosition,
current.relevant_url as currentRelevantUrl,
current.restrictions_probability as currentRestrictionsProbability,
current.url_changed as currentUrlChanged,
current.identifier_id as currentIdentifierId,
current.phrase_id as phraseId,
previous.id as previousId,
previous.date as previousDate,
previous.position as previousPosition,
previous.relevant_url as previousRelevantUrl,
previous.restrictions_probability as previousRestrictionsProbability,
previous.url_changed as previousUrlChanged,
previous.identifier_id as previousIdentifierId,
semantics_clusters.id as clusterId,
semantics_clusters.name as clusterName,
site_pages.id as pageId,
site_pages.url as pageUrl,
site_pages.site_id as siteId,
site_sites.url as siteUrl,
semantics_core_phrases.frequency as frequency
from
(
select
id,
date,
position,
relevant_url,
restrictions_probability,
url_changed,
identifier_id,
phrase_id
from
overoptimisation
where
identifier_id = 1
) current
left join (
select
id,
date,
position,
relevant_url,
restrictions_probability,
url_changed,
identifier_id,
phrase_id
from
overoptimisation
where
identifier_id = 1
) previous on current.phrase_id = previous.phrase_id
inner join semantics_core_phrases on current.phrase_id = semantics_core_phrases.id
inner join semantics_clusters on semantics_core_phrases.cluster_id = semantics_clusters.id
inner join site_pages on semantics_clusters.page_id = site_pages.id
inner join site_sites on site_pages.site_id = site_sites.id
order by
siteId
The same way you can order by pageId, clusterId, frequency

The multi-part identifier could not be bound - SQL Server 2016

SELECT clm.CLCL_PAYEE_PR_ID, clm.SBSB_CK, clm.CLCL_ID, clm.clcl_id_adj_to,clm.clcl_id_adj_from, clm.CLCL_PAID_DT
FROM ODW.DW.fac_cmc_clcl_claim CLM
INNER JOIN ODW.DW.fac_cmc_meme_member MEME ON MEME.meme_ck = CLM.meme_ck
INNER JOIN ODW.DW.fac_cmc_mepe_prcs_elig MEPE ON MEPE.meme_ck = MEME.meme_ck
INNER JOIN ODW.DW.fac_cmc_mepr_prim_prov MEPR ON MEPE.meme_ck = MEPR.meme_ck AND CLM.clcl_prpr_id_pcp = MEPR.prpr_id
INNER JOIN ODW.DW.fac_cmc_sbsb_subsc SBSB ON MEME.sbsb_ck = SBSB.sbsb_ck
INNER JOIN ODW.DW.fac_cmc_prpr_prov PROV ON MEPR.prpr_id = PROV.prpr_id AND PROV.prpr_mctr_prty = 'RISK'
INNER JOIN ODW.DW.fac_cmc_prer_relation PRER ON PRER.prpr_id = MEPR.prpr_id
INNER JOIN ODW.DW.fac_cmc_plds_plan_desc PLDS ON MEPE.cspi_id = PLDS.cspi_id
INNER JOIN ODW.DW.fac_cmc_pdds_prod_desc PDDS ON MEPE.pdpd_id = PDDS.pdpd_id
WHERE CLM.clcl_paid_dt BETWEEN '2019-12-24 00:00:00.000' AND '2019-12-30 23:59:59.997'
AND CLM.clcl_cur_sts = '02'
AND CLM.clcl_cl_type = 'M'
AND CLM.clcl_cl_sub_type = 'H'
AND CLM.grgr_ck IN (46)
AND MEPR.grgr_ck IN (46)
AND MEPE.grgr_ck IN (46)
AND MEPE.mepe_elig_ind = 'Y'
AND CLM.clcl_low_svc_dt BETWEEN MEPE.mepe_eff_dt AND MEPE.mepe_term_dt
AND CLM.clcl_low_svc_dt BETWEEN MEPR.mepr_eff_dt AND MEPR.mepr_term_dt
AND SBSB.grgr_ck IN (46)
AND PRER.prer_prpr_entity = 'I'
AND PRER.prer_prpr_id IN ('64456546')
AND (PLDS.plds_desc LIKE '%risk%' OR PDDS.pdds_desc LIKE '%risk%');
This query runs in PROD with different variables which substitute the value of the hard coded values. It runs around 100 times per day in PROD and on some days some of the runs fail due to this error:
The multi-part identifier "PDDS.pdds_desc" could not be bound
Please note that all the joins are being done on views.
When I re-run the failed process, it succeeds the second time with no changes to the underlying query.
Can anyone suggest what could be the issue. Also, any performance optimization suggestions for this query query will be appreciated.
Thanks!

Highlighting duplicate values

I have the following script:
SELECT
Reference.ReferenceNumber AS [Reference.ReferenceNumber)
Reference.LastName
Reference.FirstName
Address.ReferenceNumber AS [Address.ReferenceNumber]
Address.Address1
Address.Address2
Address.Address3
Address.Address4
Address.ZipCode
Telephone.ReferenceNumber AS [Telephone.ReferenceNumber]
Telephone.TelephoneNumber
Email.ReferenceNumber AS [Email.ReferenceNumber]
Email.EmailAddress
FROM
Reference
INNER JOIN Address
ON Reference.ReferenceNumber = Reference.ContactNumber
LEFT OUTER JOIN Telephone
ON Reference.ReferenceNumber = Telephone.ReferenceNumber
LEFT OUTER Join Email
ON Reference.ReferenceNumber = Email.ReferenceNumber
This pulls through all customers, plus their addresses, email and phone numbers.
However some have duplicate surnames and addresses.
Please can you advise an amendment to the script that would show which records were duplicates? Ideally I'd like the output to show where the surname, address 1 and the zipcode were identical and restrict the output to the duplicate rows only.
Many thanks.
something like
SELECT
Reference.ReferenceNumber AS [Reference.ReferenceNumber),
case when vrln.c is null then Reference.LastName else 'dup: ' + Reference.LastName end as LastName,
Reference.FirstName,
Address.ReferenceNumber AS [Address.ReferenceNumber],
Address.Address1,
Address.Address2,
Address.Address3,
Address.Address4,
Address.ZipCode,
Telephone.ReferenceNumber AS [Telephone.ReferenceNumber],
Telephone.TelephoneNumber,
Email.ReferenceNumber AS [Email.ReferenceNumber],
Email.EmailAddress
FROM
Reference
INNER JOIN Address
ON Reference.ReferenceNumber = Reference.ContactNumber
LEFT OUTER JOIN Telephone
ON Reference.ReferenceNumber = Telephone.ReferenceNumber
LEFT OUTER Join Email
ON Reference.ReferenceNumber = Email.ReferenceNumber
left join (
select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
from
Reference rln
INNER JOIN Address add
ON rln.ReferenceNumber = add.ReferenceNumber
group by rln.LastName, add.Address1, add.ZipCode
having count(rln.LastName) > 1
) vrln on Reference.LastName = vrln.LastName and Address.Address1 = vrln.Address1 and Address.ZipCode = vrln.ZipCode
where vrln.c is not null
but, to find duplicates :
select rln.LastName, add.Address1, add.ZipCode, count(rln.LastName) as c
from
Reference rln
INNER JOIN Address add
ON rln.ReferenceNumber = add.ReferenceNumber
group by rln.LastName, add.Address1, add.ZipCode
having count(rln.LastName) > 1
is enough

Compare results with percentage with PRINT

I've queried the results I'm looking for, now I simply want to compare "OnTime" Y/N for a percentage "OnTime", but I'm having trouble comparing the alias and/or using the column name in the PRINT function. Any suggestions?
SELECT
shmast.fcnumber AS Customer,
shmast.fcbcompany AS Company,
somast.fsono AS SalesOrder,
somast.fduedate AS DueDate,
shmast.fshipno AS Shipper,
shmast.fshipdate AS ShipDate,
--DATEDIFF(DAY,somast.fduedate,shmast.fshipdate) AS 'Days',
CASE
WHEN shmast.fshipdate <= somast.fduedate THEN 'Y'
WHEN shmast.fshipdate > somast.fduedate THEN 'N'
END AS OnTime
FROM somast
JOIN shmast ON somast.fsono = shmast.fcsono
WHERE shmast.fcnumber = '000111'
PRINT (OnTime = 'Y') / COUNT (*)
/*GROUP BY
shmast.fcnumber,
shmast.fcbcompany,
somast.fsono,
somast.fduedate,
shmast.fshipno,
shmast.fshipdate*/
Giving it another shot after your description
SELECT
sum(CASE WHEN shmast.fshipdate<=somast.fduedate THEN 100.0 END)/count(*) AS OnTime
FROM somast
JOIN shmast ON somast.fsono = shmast.fcsono
WHERE shmast.fcnumber = '000111'

Returning distinct columns from left outer join in db2

SELECT
nzy.NZPYYD
,nzy.NZZSYG
,nzy.NZJRYG
,acn.ANITCD
FROM
ACNTRA acn
LEFT OUTER JOIN NZYTFL nzy
ON (
nzy.NZCNO1 = acn.ANCNO1
AND nzy.NZCNO2 = acn.ANCNO2
AND nzy.NZCNO3 = acn.ANCNO3
AND nzy.NZCNO4 = acn.ANCNO4
AND nzy.NZCNO5 = acn.ANCNO5
AND nzy.NZSLKI = acn.ANSLKI
AND nzy.NZDLTM = ''
)
WHERE
acn.ANDLTM = ''
AND acn.ANTKCD = '1029'
AND nzy.NZTXKB = 1
The problem here is it gives 2 rows result.I want to get one unique row from the result of left outer join .Any help?
If both rows are identical, try
SELECT DISTINCT
nzy.NZPYYD
,nzy.NZZSYG
,nzy.NZJRYG
,acn.ANITCD
If not, you can try to SUM(), CONCAT(), MAX() or whatever the column with different values.
Difficult to be more precise without a sample output.