SQL TOP 1 issue when no record is found - sql

Hope you can help me with this SQL issue I am unable to solve:
I have a price list file with multiple entries per item number like this:
ItemNo
ValidFrom
ValidTo
Price
Now when I try to generate a list with prices valid today, I use the statement below to get a decent report. I only have a problem when no valid price was found, i.e. for TODAY there is no record. In this case the first TOP 1 subquery still returns a (random?) value and destroys my report.
I assume that TOP 1 will always return one value by definition, or is there a trick which can prevent this?
Really appreciate your help! (and please excuse the german terms in the query...)
SELECT
MITBAL.MBITNO as 'Artikel', MITMAS.MMITDS as 'Bezeichnung',
MITBAL.MBBUYE as 'Disponent', MITMAS.MMNEWE as 'Gewicht',
MITBAL.MBSUNO as 'Lieferant', MMITTY as 'Typ',
m9ucos as 'Std.-Kosten', idsunm as 'Lieferantenname',
iicucd as 'WSL', MBEOQT as 'EOQ',
(select count(*)
from RCE12_Staging.dbo.mpagrl
left outer join RCE12_Staging.dbo.mpagrh on aicono = ahcono
and aiagnb = ahagnb and aisuno = ahsuno
where aicono = 2
and aiobv1 = mbitno and aisagl = 20
and aisuno between '400000' and '599999' and aigrpi = 40
and aiuvdt >= CONVERT(char(8), GETDATE(), 112) and ahpast = '40') as 'Anz.Lief',
(select TOP 1 ajpupr
from RCE12_Staging.dbo.mpagrp
left outer join RCE12_Staging.dbo.mpagrh on ajcono = ahcono
and ajagnb = ahagnb and ajsuno = ahsuno
left outer join RCE12_Staging.dbo.mpagrl on ajcono = aicono
and ajagnb = aiagnb and ajsuno = aisuno
where ajcono = 2
and ajobv1 = mbitno and ajsuno = mbsuno
and ajmapr = 1 and ajgrpi = 40
and ajfvdt < CONVERT(char(8), GETDATE(), 112) and ahpast = '40'
and ahfvdt < CONVERT(char(8), GETDATE(), 112)
and ahuvdt >= CONVERT(char(8), GETDATE(), 112)
and aisagl = 20 and aigrpi = 40
and aifvdt < CONVERT(char(8), GETDATE(), 112)
and aiuvdt >= CONVERT(char(8), GETDATE(), 112)
order by ajfvdt desc) as 'EK-Preis',
(select TOP 1 ajfrqt
from RCE12_Staging.dbo.mpagrp
left outer join RCE12_Staging.dbo.mpagrh on ajcono = ahcono
and ajagnb = ahagnb and ajsuno = ahsuno
left outer join RCE12_Staging.dbo.mpagrl on ajcono = aicono
and ajagnb = aiagnb and ajsuno = aisuno
where ajcono = 2 and ajobv1 = mbitno
and ajsuno = mbsuno and ajmapr = 1 and ajgrpi = 40
and ajfvdt < CONVERT(char(8), GETDATE(), 112) and ahpast = '40'
and ahfvdt < CONVERT(char(8), GETDATE(), 112)
and ahuvdt >= CONVERT(char(8), GETDATE(), 112) and aisagl = 20
and aigrpi = 40
and aifvdt < CONVERT(char(8), GETDATE(), 112)
and aiuvdt >= CONVERT(char(8), GETDATE(), 112)
order by ajfvdt desc) as 'EK-Menge'
FROM
RCE12_Staging.dbo.MITBAL MITBAL
left outer join
RCE12_Staging.dbo.MITFAC MITFAC ON mbcono = m9cono and mbitno = m9itno and m9faci = 'DFP'
left outer join
RCE12_Staging.dbo.MITMAS MITMAS ON mbcono = mmcono and mbitno = mmitno
left outer join
RCE12_Staging.dbo.CIDMAS CIDMAS ON mbcono = idcono and mbsuno = idsuno
left outer join
RCE12_Staging.dbo.CIDVEN CIDVEN ON mbcono = iicono and mbsuno = iisuno
WHERE
MITBAL.MBCONO = 2
AND MITBAL.MBWHLO = '200' AND MBSTAT = '20' AND MBPUIT = '2'
and MBITNO like '479200222%'
ORDER BY MBITNO

The 'top 1' will return the first row from the dataset if the dataset you have requested is empty then top 1 will return an empty dataset with no rows and no 'Random data'.
You should probably check the number of rows returned if you need the data for later in the query or initialise a variable and use that.

Related

Microsoft SQL Server: where logic - Column stored as an integer but represents a date in my table

I'm running into an issue with my Where clause;
AND CAST(h.chinto as DATETIME) >= DATEADD(DAY, -7, GetDate())
chinto is stored as an integer in the format YYYYMMDD and represents a date. Im trying to write a where clause to sort my results based on the last 30 days ect.
Ive tried try_convert to change the data type to a date but I cant seem to get it right. Always running into an the error: Arithmetic overflow error converting expression to data type datetime.
SELECT top 100
[ClaimNumber] = MAX(RIGHT('00000000' + convert(varchar(8),h.chclno),8) + RIGHT('00' + convert(varchar(8),h.chwkno),2)),
[EmployeeSSN] = RTRIM(MAX(RIGHT('000000000' + CONVERT(VARCHAR(9),m.edssno),9))),
[EmployeeID] = RTRIM(MAX(ee.edmemb)),
[EmployeeLastName] = RTRIM(MAX(m.edlnmk)),
[EmployeeFirstName] = RTRIM(MAX(m.edfnam)),
[PatientSSN] = RTRIM(MAX(RIGHT('000000000' + CONVERT(VARCHAR(9),e.eessno),9))),
[PatientDOB] = RTRIM(MAX(m.edbrdt)),
[PatientGender] = RTRIM(MAX(m.edsexx)),
-- m.edbrdt as 'PatientDOB',
--m.edsexx as 'PatientGender',
[FirstDateOfService] = MAX(left(convert(char(8),h.chinfr),4) + '-' + substring(convert(char(8),h.chinfr),5,2) + '-' + right(convert(char(8),h.chinfr),2) ),
[LastDateOfService] = MAX(left(convert(char(8),h.chinto),4) + '-' + substring(convert(char(8),h.chinto),5,2) + '-' + right(convert(char(8),h.chinto),2) ),
[ProviderName] = MAX(ps.pmname),
[ProviderAddress1] = MAX(CASE WHEN sn.npinpi = nps.NPI THEN RTRIM(nps.Prov_First_Line_Business_Practice_Location_Address) ELSE RTRIM(ps.pmadr1) COLLATE SQL_Latin1_General_Cp1_CS_AS END ),
[ProviderAddress2] = MAX(CASE WHEN sn.npinpi = nps.NPI THEN RTRIM(ISNULL(nps.Prov_Second_Line_Business_Practice_Location_Address,'')) ELSE RTRIM(ps.pmadr2) COLLATE SQL_Latin1_General_Cp1_CS_AS END ),
[ProviderCity] = MAX(CASE WHEN sn.npinpi = nps.NPI THEN RTRIM(ISNULL(nps.Prov_Business_Mailing_Address_City_Name,'')) ELSE RTRIM(ps.pmadr2) COLLATE SQL_Latin1_General_Cp1_CS_AS END ),
[ProviderState] = MAX(CASE WHEN sn.npinpi = nps.NPI THEN RTRIM(nps.Prov_Business_Practice_Location_Address_ST_Name) ELSE RTRIM(ps.pmstat) COLLATE SQL_Latin1_General_Cp1_CS_AS END ),
[ProviderZIP] = MAX(CASE WHEN sn.npinpi = nps.NPI THEN LEFT(nps.Prov_Business_Practice_Location_Address_Postal_CD,5) ELSE LEFT(ps.pmzpcd,5) COLLATE SQL_Latin1_General_Cp1_CS_AS END ),
--[ProviderTIN] ,
--[ProviderTINSuffix] ?,
[DiagnosisCode1] = RTRIM(MAX(h.chdgcd)),
[DiagnosisCode2] = RTRIM(MAX(ISNULL(h.chadgc_1,''))),
[DiagnosisCode3] = RTRIM(MAX(ISNULL(h.chadgc_2,''))),
[DiagnosisCode4] = RTRIM(MAX(ISNULL(h.chadgc_3,''))),
[DiagnosisCode5] = RTRIM(MAX(ISNULL(h.chadgc_4,''))),
[DiagnosisCode6] = RTRIM(MAX(ISNULL(h.chadgc_5,''))),
[DiagnosisCode7] = RTRIM(MAX(ISNULL(h.chadgc_6,''))),
[DiagnosisCode8] = RTRIM(MAX(ISNULL(h.chadgc_7,''))),
[DiagnosisCode9] = RTRIM(MAX(ISNULL(h.chadgc_8,''))),
--[ProcedureCode1?] = RTRIM(MAX(d.cdproc)) ---> clmdet missing prod code(s)/date(s) joined ACO.dbo.clmubf p
[ProcedureCode1] = RTRIM(MAX(ISNULL(p.cwprcd_1, ''))),
[ProcedureDate1] = RTRIM(MAX(ISNULL(p.cwprdt_1, ''))),
[ProcedureCode2] = RTRIM(MAX(ISNULL(p.cwprcd_2, ''))),
[ProcedureDate2] = RTRIM(MAX(ISNULL(p.cwprdt_2, ''))),
[ProcedureCode3] = RTRIM(MAX(ISNULL(p.cwprcd_3, ''))),
[ProcedureDate3] = RTRIM(MAX(ISNULL(p.cwprdt_3, ''))),
[ProcedureCode4] = RTRIM(MAX(ISNULL(p.cwprcd_4, ''))),
[ProcedureDate4] = RTRIM(MAX(ISNULL(p.cwprdt_4, ''))),
[ProcedureCode5] = RTRIM(MAX(ISNULL(p.cwprcd_5, ''))),
[ProcedureDate5] = RTRIM(MAX(ISNULL(p.cwprdt_5, ''))),
[ProcedureCode6] = RTRIM(MAX(ISNULL(p.cwprcd_6, ''))),
[ProcedureDate6] = RTRIM(MAX(ISNULL(p.cwprdt_6, ''))),
[TotalCharge] = SUM(d.cdtchg),
--[TotalCharge] = SUM(c.dichga?) --> drghis table missing from ACO.dbo, has all the info I need (clmdet d missing columns)
--[AllowableCharge] = ?,
[TotalIneligible] = SUM(d.cdinea),
--[TotalEligible] =,
[TotalDiscount] = SUM(d.cddisc),
[TotalDeductible] = SUM(d.cddeda),
[TotalCoPay] = SUM(d.cdcopa),
[TotalCoInsurance] = SUM(d.cdleva_1)
--[TotalPaid] = SUM(d.cdcoba?)
--[ClaimType]
FROM
ACO.dbo.clmhdr h
INNER JOIN ACO.dbo.clmdet d ON h.chclno = d.cdclno AND h.chwkno = d.cdwkno
INNER JOIN ACO.dbo.elgdep m ON h.chemno = m.ednmbr AND h.chdpcd = m.eddpcd
INNER JOIN ACO.dbo.elgemp e ON m.ednmbr = e.eenmbr
INNER JOIN ACO.dbo.elgdep ee ON e.eenmbr = ee.ednmbr AND ee.eddpcd = 'e'
INNER JOIN ACO.dbo.clmubf p ON h.chclno = p.cwclno
--INNER JOIN ACO.dbo.drghis? c ON h.chclno = c.diclno
LEFT OUTER JOIN ACO.dbo.prvmst pp ON d.cdpvno = pp.pmnmbr
LEFT OUTER JOIN ACO.dbo.prvmst ps ON d.cdspno = ps.pmnmbr
LEFT OUTER JOIN ACO.dbo.prvnpi sn ON d.cdspno = sn.npinmbr
LEFT OUTER JOIN ACO.dbo.prvnpi pn ON d.cdpvno = pn.npinmbr
LEFT OUTER JOIN [RIMSReport].[dbo].[NPI] nps ON sn.npinpi = nps.NPI
LEFT OUTER JOIN [RIMSReport].[dbo].[NPI] npp ON pn.npinpi = npp.NPI
WHERE
h.chgpno = 'CTT0001'
AND CAST(h.chinto as DATETIME) >= DATEADD(DAY, -7, GetDate())
GROUP BY
h.chclno, h.chinto
--m.edsexx, m.edbrdt
Casting int to date isn't allowed, but you can cast from string instead.
Try this: select cast(cast(h.chinto as varchar(10)) as date)

Need query to start at the beginning of the month

SELECT DISTINCT
ATB.AcountCountDesc,
TB.LastFirstName,
N.EMAIL,
TB.AccountNumber,
TB.OpenShareCount,
TB.MemberOpenDate,
TB.OpenMemberCount,
TB.OpenShareBalance,
SH.ShareType,
FORMAT(SH.ShareOpenDate, 'MM/dd/yyyy') AS "ShareOpenDate",
SH.ShareCreatedByUser,
SH.ShareCreatedByUserName,
SH.ShareBranchName,
SH.ShareBranch,
cast(month(SH.ShareOpenDate) as varchar) + '/' + cast(year(SH.ShareOpenDate) as varchar) as 'Open Period',
CONCAT(SH.ShareCreatedByUser, '-',SH.ShareCreatedByUserName) 'Opened By'
FROM
arcu.vwARCUOperationMemberTrialBalance as TB
INNER JOIN arcu.vwARCUOperationMemberAccountTrialBalance as ATB ON TB.MemberSuppID = ATB.MemberID
and TB.ProcessDate = ATB.PDate
and TB.MemberStatus = 0 -- Account count for open Members only
and TB.AccountStatus <> 1
INNER JOIN arcu.vwARCUShare AS SH ON TB.ProcessDate = SH.ProcessDate
AND TB.AccountNumber = SH.AccountNumber
INNER JOIN NAME AS N ON TB.AccountNumber = N.PARENTACCOUNT
WHERE
ATB.AcountCountDesc = 1
AND TB.OpenShareCount >= 1
AND SH.ShareType = '00'
AND SH.ShareID != '40'
AND SH.ShareOpenDate >= DATEADD(s, 1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), -2))
AND TB.MemberOpenDate = SH.ShareOpenDate
AND N.EMAIL <> ''
AND N.EMAIL is not null
order by MemberOpenDate Desc;
Issue: the MemberOpen Date starts on 08/02/2021.
Desired outcome: I need it to start on 08/01/2021
What I have tried: I have tried declaring a variable and calculating the start of the month by using datediff and still get the 08/02/2021 outcome.
I tried changing the => to = or <= and the issue persists. Please help.
You could calculate the 1st of the month using EOMONTH something like this
SELECT DISTINCT ATB.AcountCountDesc,TB.LastFirstName,N.EMAIL,TB.AccountNumber,TB.OpenShareCount,TB.MemberOpenDate,
TB.OpenMemberCount,TB.OpenShareBalance,SH.ShareType,FORMAT(SH.ShareOpenDate,'MM/dd/yyyy') AS "ShareOpenDate",
SH.ShareCreatedByUser,SH.ShareCreatedByUserName,SH.ShareBranchName,SH.ShareBranch,cast(month(SH.ShareOpenDate)
as varchar) + '/' + cast(year(SH.ShareOpenDate) as varchar)as 'Open Period', CONCAT(SH.ShareCreatedByUser,'-',SH.ShareCreatedByUserName)
'Opened By'
FROM arcu.vwARCUOperationMemberTrialBalance as TB
JOIN arcu.vwARCUOperationMemberAccountTrialBalance as ATB
ON TB.MemberSuppID = ATB.MemberID
and TB.ProcessDate = ATB.PDate
and TB.MemberStatus = 0 -- Account count for open Members only
and TB.AccountStatus <> 1
JOIN arcu.vwARCUShare AS SH
ON TB.ProcessDate = SH.ProcessDate
AND TB.AccountNumber = SH.AccountNumber
JOIN NAME AS N ON TB.AccountNumber = N.PARENTACCOUNT
WHERE
ATB.AcountCountDesc = 1 AND
TB.OpenShareCount >= 1 AND
SH.ShareType = '00' AND
SH.ShareID !='40' AND
SH.ShareOpenDate >= (select dateadd(day, 1, dateadd(month, -1, eomonth(getdate())))) AND
TB.MemberOpenDate = SH.ShareOpenDate AND
N.EMAIL <> '' AND
N.EMAIL is not null
order by MemberOpenDate Desc;

WHERE returning entire table

Question: How do I ignore WHERE param if value = '0' or NULL
One of the columns returned is showing 0 which is causing problems in my query.
The problem I am having is in the p.author value. Sometimes it matches up with a pe.system_id, but sometimes it is 0 or NULL. If p.author is 0. If I comment the and param out, then it returns a row for each entry in docsadm.people pe1 or nothing because no entry = 0
SELECT
p.docnumber, p.docname, p.abstract, d.type_id, a.application,
pe1.full_name as 'Author', pe.full_name as 'Registered by',
convert(varchar(10), p.pif_letter_date + 0.4166666,111) as 'Date written',
convert(varchar(10), p.creation_date + 0.4166666,111) as 'Registered on',
convert(varchar(10), p.last_edit_date + 0.4166666,111) as 'Last edit date',
f.pd_filept_no, f.pd_file_name, f.pd_title, sm.pif_marking, s.pd_section_code,
p.status as 'Availability', p.related, p.kmc_old_docno
FROM
docsadm.profile p, docsadm.pd_file_part f, docsadm.people pe,
docsadm.people pe1, docsadm.apps a, docsadm.documenttypes d,
docsadm.pif_sec_mark sm, docsadm.pd_section s
WHERE
p.DOCNUMBER IN ('5451326')
AND p.pd_file_part = f.system_id
AND p.typist = pe.system_id
AND p.author = pe1.system_id --Problem Line
AND p.application = a.system_id
AND p.documenttype = d.system_id
AND f.kmc_ptto_sec_mark = sm.system_id
AND f.pd_pttosec_link = s.system_id
AND p.item_type IN ('D','E','M','F')
ORDER BY
5, 4, 1
There are a number of ways but the way I prefer is to use the JOIN .. ON syntax and make tables with optional data part of a LEFT OUTER JOIN (LEFT JOIN).
SELECT p.docnumber,
p.docname,
p.abstract,
d.type_id,
a.application,
pe1.full_name as 'Author',
pe.full_name as 'Registered by',
convert(varchar(10), p.pif_letter_date + 0.4166666,111) as 'Date written',
convert(varchar(10), p.creation_date + 0.4166666,111) as 'Registered on',
convert(varchar(10), p.last_edit_date + 0.4166666,111) as 'Last edit date',
f.pd_filept_no,
f.pd_file_name,
f.pd_title,
sm.pif_marking,
s.pd_section_code,
p.status as 'Availability',
p.related,
p.kmc_old_docno
FROM docsadm.profile p
JOIN docsadm.pd_file_part f
ON p.pd_file_part = f.system_id
JOIN docsadm.people pe
ON p.typist = pe.system_id
JOIN docsadm.apps a
ON p.application = a.system_id
JOIN docsadm.documenttypes d
ON p.documenttype = d.system_id
JOIN docsadm.pif_sec_mark sm
ON f.kmc_ptto_sec_mark = sm.system_id
JOIN docsadm.pd_section s
ON f.pd_pttosec_link = s.system_id
LEFT
JOIN docsadm.people pe1
ON p.author = pe1.system_id --Problem Line
WHERE p.DOCNUMBER in ('5451326')
and p.item_type in ('D','E','M','F')
ORDER by 5, 4, 1
...
and ( (p.author is null) or (p.author='0') or (p.author=pe1.system_id) )
...
...work?
If p.author is null or p.author=0 it will turn that "and ..." into "and true"

does sequence of where condition changes the result in sql server

I am getting a unique problem in one query .
insert into #tempclasssearch
SELECT cc.Id, B.ScheduleId, 10, Lc.Id LocationId, Lc.GroupId, cc.CategoryId,CCSC.SubCategoryId,cc.RegistrationId
from Event CC Inner join Batches B
On Cc.Id = B.ClassCreativeId
Inner Join Venues V On B.VenueId = V.ID
Inner Join loc Lc On V.LocId = Lc.id
Inner join EventSubCategories CCSC on CC.Id = CCSC.EventId
inner join PackageSubscriptions p on p.userId = cc.userId
Where
p.SubscriptionStatus = 1 and
((#GroupId = 0) or (#GroupId is not null and Lc.GroupId = #GroupId))
and ((#LocationId = 0) or (#LocationId is not null and Lc.Id = #LocationId))
and ((#TargetCityId is null) or (#TargetCityId is not null and Lc.TargetCityId = #TargetCityId))
and ((#CategoryId = 0) or (#CategoryId is not null and cc.CategoryId = #CategoryId))
and ((#SubCategoryId = 0) or (#SubCategoryId is not null and ccsc.SubCategoryId = #SubCategoryId))
and cc.UserStatus = 1 and cc.PublishStatus = 3 and B.StartDate >= #StartDateTime -- and B.StartDateTime > #StartDateTime
and ((#EndDateTime is null) or (#EndDateTime is not null and B.StartDate <= #EndDateTime
and p.StartDateTimeUtc <= GETUTCDATE()
and p.EndDateTimeUtc >= GETUTCDATE()
The problem is that if I place this condition p.SubscriptionStatus = 1 at the end then it doesn't execute this condition while if I place it as first condition it does.
I don't know what is happening :(
let me know if you guys have any idea ....
Parenthesis missing at the end
and ((#EndDateTime is null) or (#EndDateTime is not null and B.StartDate <= #EndDateTime

Flag parameter doesn't work correctly (SQL)

I have an "AcctMgr_Flag" that designates a person as the account manager. This is on the table Company_Team. If I send parameter #acctmgr as 'true', I want to return only Activities where #member is the account manager. If #acctmgr is not true, I do not care whether AcctMgr_Flag is true or not.
Every Activity in SO_Activity has an "Assigned_To" column which designates a member_recid. Every Member in Company_Team has an AcctMgr_Flag and Company_RecID. Every Member in v_rpt_Member has a member_recid and a Company_recid.
Here is my code
SELECT v_rpt_Company.Company_Name, SO_Activity.Subject, SO_Activity.Notes,
SO_Activity.Date_Closed, SO_Activity.Last_Update, v_rpt_Member.Member_ID,
v_rpt_ActivityType.SO_Activity_Type_Desc,
v_rpt_ActivityStatus.SO_Act_Status_Desc
FROM v_rpt_Company
LEFT OUTER JOIN SO_Activity
ON v_rpt_Company.Company_RecID = SO_Activity.Company_RecID
LEFT OUTER JOIN v_rpt_Member
ON SO_Activity.Assign_To = v_rpt_Member.Member_ID
LEFT OUTER JOIN Company_Team
ON v_rpt_Member.Member_RecID = Company_Team.Member_RecID AND
v_rpt_Company.Company_RecID = Company_Team.Company_RecID
LEFT OUTER JOIN v_rpt_ActivityType
ON SO_Activity.SO_Activity_Type_RecID=v_rpt_ActivityType.SO_Activity_Type_RecID
LEFT OUTER JOIN v_rpt_ActivityStatus
ON SO_Activity.so_act_status_recid = v_rpt_ActivityStatus.SO_Act_Status_RecID
WHERE (Company_Team.AcctMgr_Flag =
CASE WHEN #acctmgr = 'true' THEN 1 ELSE Company_Team.AcctMgr_Flag END) AND
(SO_Activity.Assign_To = #member) AND
(v_rpt_ActivityStatus.SO_Act_Status_Desc =
CASE WHEN #act_status IS NULL
THEN v_rpt_ActivityStatus.so_act_status_desc ELSE #act_status END) AND
(v_rpt_Company.Company_RecID =
CASE WHEN #company = '' THEN v_rpt_Company.Company_RecID ELSE #company END) AND
(SO_Activity.Last_Update >= CONVERT(datetime, #date_start, 101)) AND
(SO_Activity.Last_Update <= CONVERT(datetime, #date_end, 101))
GROUP BY v_rpt_Company.Company_Name, SO_Activity.Subject, SO_Activity.Notes,
SO_Activity.Date_Closed, SO_Activity.Last_Update, v_rpt_Member.Member_ID,
v_rpt_ActivityType.SO_Activity_Type_Desc,
v_rpt_ActivityStatus.SO_Act_Status_Desc
ORDER BY v_rpt_Company.Company_Name, SO_Activity.Last_Update DESC
Instead of
(Company_Team.AcctMgr_Flag = CASE WHEN #acctmgr = 'true' THEN 1 ELSE Company_Team.AcctMgr_Flag END)
Put this
(#acctmgr != 'true' or Company_Team.AcctMgr_Flag = 1)