Why is my table populating NULL instead of blank? - sql

I am trying to figure out why my Eclassification column is still populating NULL instead of a blank value when using the below query in a stored procedure. Originally I just had the Case statement but since that wasn't working I tried adding in the ISNULL as well, but my table will only populate Green or NULL.
update ft
set EClassification = case when ft2.hotelsabre = gh.sabre then ISNULL('Green',' ')
else ' ' end
from
fact.travel ft
left join ETL_Framework.Fact.Travel9999 ft2
on ft.OrigRecnum = ft2.RECNUM
Join ETL_Gooddata.ETL.GHCAnalysis gh
on ft2.hotelsabre = gh.sabre
where travel_type_id = 3
and
createdate >= #LoadDate
and invoicedate between #StartDate and #EndDate
--and datasourceid = #DataSourceID
and ft.datasourceid in (select d.DataSourceID from Dim.DataSource d
--left join ETL.LoadConfig c on d.DataSourceID = c.DataSourceID where case when d.DataSourceID in (2,3) then 1 else c.DataSourceID end = #DataSourceID)
left join ETL.LoadConfig c on d.DataSourceID = c.DataSourceID
where case when d.DataSourceID in (2,3) then 1
when d.DataSourceID in (11,14) then 11
else c.DataSourceID end = #DataSourceID)
When I select from the table that the above stored procedure updates, my EClassification column is only 'Green' or NULL, not 'Green' or blank.
SELECT TOP (1000) [FactTravelID]
,ft.[DataSourceID]
,[TravelTypeID]
,[TransactionID]
,[InvoiceDate]
,ft.[AHC]
,[AirMiscData1]
,[AirMiscData2]
,hotelsabre
,[EClassification]
,hotelsabre
FROM [ETL_Gooddata].[Fact].[Travel] ft
join ETL_Framework.Fact.Travel9999 ft2
on ft.OrigRecnum = ft2.recnum
where TravelTypeID = 3
and CreateDate > '2021-06-13'

StackOverFlow vets, still new to this. Be gentle...You're using a LEFT JOIN to ETL_Framework.Fact.Travel9999 and an INNER JOIN to ETL_Gooddata.ETL.GHCAnalysis and your ON clause is the same condition as your CASE statement evaluation. Try using a LEFT JOIN to [ETL_Gooddata].[ETL].[GHCAnalysis].
UPDATE ft
SET EClassification = CASE WHEN ft2.hotelsabre = gh.sabre THEN ISNULL('Green',' ') ELSE ' ' END
FROM [fact].[travel] ft
LEFT JOIN [ETL_Framework].[Fact].[Travel9999] ft2
ON ft.OrigRecnum = ft2.RECNUM
LEFT JOIN [ETL_Gooddata].[ETL].[GHCAnalysis] gh
ON ft2.hotelsabre = gh.sabre
WHERE travel_type_id = 3
AND createdate >= #LoadDate
AND invoicedate BETWEEN #StartDate AND #EndDate
AND ft.datasourceid IN (SELECT d.DataSourceID
FROM [Dim].[DataSource] d
LEFT JOIN [ETL].[LoadConfig] c
ON d.DataSourceID = c.DataSourceID
WHERE CASE WHEN d.DataSourceID in (2,3) THEN 1
WHEN d.DataSourceID in (11,14) THEN 11
ELSE c.DataSourceID END = #DataSourceID)

Related

The SQL Server query is taking too long to load the data

The below query is actually a view which is being used to display the cash payment report. But it is taking too much of time to load the data
SELECT
Billing_AccountPaymentDate.DueDate,
SUM(Billing_AccountCharge.NetAmount) AS NetCharges,
ISNULL(SUM(AccountChargePayment.SchoolPayments + AccountChargePayment.SchoolRemittances + AccountChargePayment.SchoolRemittancesPending), 0) / SUM(Billing_AccountCharge.NetAmount) AS PercentCollected,
SUM(Billing_AccountCharge.NetAmount - ISNULL(AccountChargePayment.SchoolPayments + AccountChargePayment.SchoolRemittances + AccountChargePayment.SchoolRemittancesPending, 0)) AS RemainingBalance,
Billing_AccountPaymentDate.RemittanceEffectiveDate,
Billing_Account.SchoolId,
ISNULL(SUM(AccountChargePayment.SchoolPayments), 0) AS SchoolPayments,
ISNULL(SUM(AccountChargePayment.SchoolRemittances), 0) AS SchoolRemittances,
ISNULL(SUM(AccountChargePayment.SchoolRemittancesPending), 0) AS SchoolRemittancesPending,
Billing_Account.SchoolYearId,
ISNULL(SUM(AccountChargePayment.SchoolPayments + AccountChargePayment.SchoolRemittances), 0) AS TotalReceipts
FROM
Billing_AccountCharge
INNER JOIN
Billing_AccountInvoice ON
Billing_AccountInvoice.AccountInvoiceId = Billing_AccountCharge.AccountInvoiceId
INNER JOIN
Billing_Account ON
Billing_Account.AccountId = Billing_AccountInvoice.AccountId
INNER JOIN
Billing_PaymentMethod ON
Billing_PaymentMethod.PaymentMethodId = CASE WHEN Billing_AccountInvoice.AutomaticPaymentEligible = 1 THEN Billing_Account.PaymentMethodId ELSE 3 END -- Send Statements
INNER JOIN
Billing_AccountPaymentDate ON
Billing_AccountPaymentDate.AccountPaymentMethodId = Billing_PaymentMethod.AnticipatedAccountPaymentMethodId AND
Billing_AccountPaymentDate.DueDate = Billing_AccountInvoice.DueDate AND
Billing_AccountPaymentDate.HoldForFee = Billing_Account.HoldPaymentForFee
INNER JOIN
Billing_ChargeItem ON
Billing_ChargeItem.ChargeItemId = Billing_AccountCharge.ChargeItemId
LEFT OUTER JOIN
(
SELECT
Billing_AccountChargePayment.AccountChargeId,
SUM(CASE WHEN Billing_AccountPayment.AccountPaymentTypeId = 9 THEN Billing_AccountChargePayment.Amount ELSE 0 END) AS SchoolPayments,
SUM(CASE WHEN Billing_AccountChargePayment.SchoolRemittanceId IS NOT NULL THEN Billing_AccountChargePayment.Amount ELSE 0 END) AS SchoolRemittances,
SUM(CASE WHEN Billing_AccountChargePayment.SchoolRemittanceId IS NULL AND Billing_AccountPayment.AccountPaymentTypeId <> 9 THEN Billing_AccountChargePayment.Amount ELSE 0 END) AS SchoolRemittancesPending
FROM
Billing_AccountChargePayment
INNER JOIN
Billing_AccountPayment ON
Billing_AccountPayment.AccountPaymentId = Billing_AccountChargePayment.AccountPaymentId
GROUP BY
Billing_AccountChargePayment.AccountChargeId
) AccountChargePayment ON
AccountChargePayment.AccountChargeId = Billing_AccountCharge.AccountChargeId
WHERE
Billing_AccountInvoice.AccountInvoiceStatusId <> 4 AND -- Voided
Billing_ChargeItem.RemitToSchool = 1
AND Billing_Account.[SchoolId] = 6 --hard code in a school with data
AND Billing_Account.[SchoolYearId] = 12 --hard code in a school year with data
GROUP BY
Billing_AccountPaymentDate.DueDate,
Billing_AccountPaymentDate.RemittanceEffectiveDate,
Billing_Account.SchoolId,
Billing_Account.SchoolYearId
HAVING
SUM(Billing_AccountCharge.NetAmount) <> 0
order by Billing_AccountPaymentDate.DueDate ASC
It looks like the inner query in the left join is taking too much of time, both the tables already have non clustered index, I tried taking those tables outside but the data is not accurate
Use CTE instead of subquery and do all calculation there instead of in Left outer join. Moreover, use with(nolock) whenever you fetch data. Still your query is taking much more time then You should implement proper indexing.
WITH PaymentData AS (
SELECT
acp.AccountChargeId,
SUM(
CASE
WHEN ap.AccountPaymentTypeId = 9 THEN acp.Amount
ELSE 0
END
) AS SchoolPayments,
SUM(
CASE
WHEN acp.SchoolRemittanceId IS NOT NULL THEN acp.Amount
ELSE 0
END
) AS SchoolRemittances,
SUM(
CASE
WHEN acp.SchoolRemittanceId IS NULL AND ap.AccountPaymentTypeId <> 9 THEN acp.Amount
ELSE 0
END
) AS SchoolRemittancesPending
FROM
Billing_AccountChargePayment acp WITH(NOLOCK)
INNER JOIN Billing_AccountPayment ap WITH(NOLOCK) ON acp.AccountPaymentId = ap.AccountPaymentId
GROUP BY
acp.AccountChargeId
)
SELECT
apd.DueDate,
SUM(ac.NetAmount) AS NetCharges,
COALESCE(
SUM(pd.SchoolPayments + pd.SchoolRemittances + pd.SchoolRemittancesPending),
0
) / SUM(ac.NetAmount) AS PercentCollected,
SUM(ac.NetAmount - COALESCE(pd.SchoolPayments, 0) - COALESCE(pd.SchoolRemittances, 0) - COALESCE(pd.SchoolRemittancesPending, 0)) AS RemainingBalance,
apd.RemittanceEffectiveDate,
a.SchoolId,
COALESCE(SUM(pd.SchoolPayments), 0) AS SchoolPayments,
COALESCE(SUM(pd.SchoolRemittances), 0) AS SchoolRemittances,
COALESCE(SUM(pd.SchoolRemittancesPending), 0) AS SchoolRemittancesPending,
a.SchoolYearId,
COALESCE(SUM(pd.SchoolPayments + pd.SchoolRemittances), 0) AS TotalReceipts
FROM
Billing_AccountCharge ac WITH(NOLOCK)
INNER JOIN Billing_AccountInvoice ai WITH(NOLOCK) ON ac.AccountInvoiceId = ai.AccountInvoiceId
INNER JOIN Billing_Account a WITH(NOLOCK) ON ai.AccountId = a.AccountId
INNER JOIN Billing_PaymentMethod pm WITH(NOLOCK) ON pm.PaymentMethodId = CASE
WHEN ai.AutomaticPaymentEligible = 1 THEN a.PaymentMethodId
ELSE 3
END
INNER JOIN Billing_AccountPaymentDate apd WITH(NOLOCK) ON
apd.AccountPaymentMethodId = pm.AnticipatedAccountPaymentMethodId AND
apd.DueDate = ai.DueDate AND
apd.HoldForFee = a.HoldPaymentForFee
INNER JOIN Billing_ChargeItem ci WITH(NOLOCK) ON ac.ChargeItemId = ci.ChargeItemId
LEFT OUTER JOIN PaymentData pd WITH(NOLOCK) ON ac.AccountChargeId = pd.AccountChargeId
WHERE
ai.AccountInvoiceStatusId <> 4 AND
ci.RemitToSchool = 1 AND
a.SchoolId = 6 AND
a.SchoolYearId = 12
GROUP BY
apd.DueDate,
apd.RemittanceEffectiveDate,
a.SchoolId,
a.SchoolYearId
HAVING
SUM(ac.NetAmount) <> 0
ORDER BY
apd.DueDate ASC;

How to write SQL query to ignore duplicate row with null value

My View shows Duplicate row which i don't want.
I am geting
1, YM
1, NULL
2, YM
2, NULL
With below Code
SELECT
dbo.Store.SID,
CASE WHEN dbo.Store.SID <> dbo.FileStore.SID THEN NULL
WHEN dbo.FileStore.MailSent = 'M' THEN 'YM'
WHEN dbo.FileStore.SID = dbo.Store.SID AND dbo.FileStore.FileType = 1 THEN 'Y'
ELSE NULL END AS FM
FROM
dbo.STORE
INNER JOIN dbo.FileStore ON dbo.Store.SID = dbo.FileStore.SID
I am looking for
1 YM
2 YM
You appear to want filtering. If I understand correctly:
SELECT s.SID,
(CASE WHEN fs.MailSent = 'M' THEN 'YM'
WHEN fs.FileType = 1 THEN 'Y'
END) AS FM
FROM dbo.STORE s INNER JOIN
dbo.FileStore fs
ON s.SID = fs.SID
WHERE fs.MailSent = 'M' OR fs.FileType = 1;
There is no reason to repeat the JOIN conditions in the CASE expression. You know they are true because of the JOIN.
SELECT
dbo.Store.SID
,CASE
WHEN dbo.Store.SID <> dbo.FileStore.SID THEN NULL --will never happen since it's an inner join
WHEN dbo.FileStore.MailSent = 'M' THEN 'YM'
WHEN dbo.FileStore.SID = dbo.Store.SID --will happen always since it's an inner join
AND dbo.FileStore.FileType = 1 THEN 'Y'
ELSE NULL -- this is the cause for Null, you have FileStore.MailSent <> 'M'
END AS FM
FROM dbo.STORE
INNER JOIN dbo.FileStore
ON dbo.Store.SID = dbo.FileStore.SID

using case for multiple parameters in where condition using sql

I have a query as below. I want to return the case value for each filename(alter.pdf, modify.pdf) in rows.
However I am getting only case value for alter.pdf.
SELECT CASE WHEN COUNT(P.[packageid]) > 0 THEN 1 ELSE 0 END AS [VALUE]
FROM [Table_1] AS P
INNER JOIN
[Table_2] AS F on F.FileID = P.FileID
WHERE F.FileName IN('alter.pdf','modify.pdf')
I think you are looking for something like this:
SELECT F.[FileName]
, sum(CASE WHEN F.FileName = 'alter.pdf' THEN 1
WHEN F.FileName = 'modify.pdf' THEN 1
ELSE 0
END AS) [VALUE]
FROM [Table_1] AS P
INNER JOIN [Table_2] AS F on F.FileID = P.FileID
WHERE F.FileName IN('alter.pdf','modify.pdf')
group by F.[FileName]

Else if in my select statement

I want to have an else if in my select statement i have already tried using case when but the results are not so accurate
heres a snippet of my scripts
select distinct t.MovementDate, t.ContractMovementID, t.GID,t.ReferenceGID,
p.refno, t.amount,convert(date,t.CreatedDate) as createdDat
from
LIF_MGM_T_Contract p
inner join LIF_MMS_T_PolicySuspense s with (nolock)
on s.ContractGID = p.GID
inner join LIF_TMS_T_FinancialTransaction t with (nolock)
on t.ContractGID = p.GID
where
p.refno = '1030642 - 1 - Mohahlaula'
and t.ReversedIndicator = 1
and t.counter = (select min(counter)
from LIF_TMS_T_FinancialTransaction t2 with (nolock)
where t2.ContractGID = p.GID
and t2.ReversedIndicator = 1
and t2.MovementDate = t.MovementDate )
So i want a else if t2.reversedindicator = 0 and date = getdate() return results.. Hope this makes sense

Combine 2 rows to the same column

Hey so my query right now is
ALTER PROCEDURE [SSRS].[VolumeCustomers]
#UserID int
AS
select
CaseTypeName,
COUNT(CaseNo) as CaseCount,
'Open' as indicator
FROM ORDERS.ApCase AC with (NOLOCK)
join ORDERS.CaseType CT (NOLOCK) on CT.CaseTypeID = AC.CaseTypeID
join WORKFLOW.WorkflowHistory WH (NOLOCK) on WH.EntityID = AC.CaseID and TableID = dbo.GetTableID('ApCase', 'ORDERS') and WH.Active = 1
inner join WORKFLOW.WorkflowStep WS (NOLOCK) on WS.WorkflowStepID = WH.WorkflowStepID and WS.NextStepID is null
where (AC.Active =1 and AC.CreatedDate >= DATEADD(day,-7,getdate()) and AC.CreatedDate < GETDATE())
Group By CaseTypeName
union
select
CaseTypeName,
COUNT(Caseno) as CaseCount,
'Closed' as indicator
FROM ORDERS.ApCase AC with (NOLOCK)
join ORDERS.CaseType CT (NOLOCK) on CT.CaseTypeID = AC.CaseTypeID
join WORKFLOW.WorkflowHistory WH (NOLOCK) on WH.EntityID = AC.CaseID and TableID = dbo.GetTableID('ApCase', 'ORDERS') and WH.Active = 1
join WORKFLOW.WorkflowStep WS (NOLOCK) on WS.WorkflowStepID = WH.WorkflowStepID and WS.NextStepID is not null
where (AC.Active =1 and AC.CreatedDate >= DATEADD(day,-7,getdate()) and AC.CreatedDate < GETDATE())
GROUP BY CaseTypeName
Order by CaseCount desc
and the out put is
Cytogenetics 2 All
Cytogenetics 1 Open
Flow Tech 1 All
Flow Tech 1 Open
Surgical 1 All
Surgical 1 Open
But i want the cytogenetics, flow tech, and surgical to all appear on the same row
example:
Cytogenetics 2 All 1 Open
Flow Tech 1 All 1 Open
Surgical 1 All 1 Open
How do I edit my query to reflect this?
Does this work?
SELECT A.*, B.CaseCount, B.indicator
FROM (<First Part of Union in Question>) AS A INNER JOIN
(<Second Part of Union in Question>) AS B ON A.CaseTypeName = B.CaseTypeName
SELECT
CaseTypeName,
COUNT(CASE WHEN WS.NextStepID IS NULL THEN Caseno END) AS CaseCountOpen,
COUNT(CASE WHEN WS.NextStepID IS NOT NULL THEN Caseno END) AS CaseCountClosed,
COUNT(CaseNo) AS CaseCountAll
FROM ORDERS.ApCase AC with (NOLOCK)
JOIN ORDERS.CaseType CT (NOLOCK)
ON CT.CaseTypeID = AC.CaseTypeID
JOIN WORKFLOW.WorkflowHistory WH (NOLOCK)
ON WH.EntityID = AC.CaseID
AND TableID = dbo.GetTableID('ApCase', 'ORDERS')
AND WH.Active = 1
JOIN WORKFLOW.WorkflowStep WS (NOLOCK)
ON WS.WorkflowStepID = WH.WorkflowStepID
WHERE AC.Active = 1
AND AC.CreatedDate >= DATEADD(day,-7,getdate())
AND AC.CreatedDate < GETDATE()
GROUP BY CaseTypeName
ORDER BY CaseCountAll DESC