Why my case column does not exist or not detected [duplicate] - sql

This question already has answers here:
Using alias in query and using it
(6 answers)
Why can't I use column aliases in the next SELECT expression?
(4 answers)
How to use alias column name in where clause in SQL Server
(5 answers)
Closed 23 days ago.
Here is my code, I use SAP B1 on SQL Server 2016.
SELECT
T0.[CANCELED], T0.[DocNum], T0.[DocDate], T0.[DocDueDate],
T0.[CardCode], T0.[CardName], T0.[DocTotal], T0.[PaidToDate],
T2.[DocDate],
MAX(T2.[DocDate]) AS LastPaymentDate,
CASE
WHEN T0.[PaidToDate] >= T0.[DocTotal] THEN 'Lunas'
WHEN T0.[PaidToDate] < T0.[DocTotal] THEN 'Belum Lunas'
END AS 'StatusPembayaran',
T3.[WhsCode],
CASE
WHEN T3.[WhsCode] = 'WH-OBRAL' THEN 'Harga Spesial'
ELSE 'Harga Normal'
END AS StatusHarga,
CASE
WHEN DATEDIFF(DAY,T0.DocDate,T0.DocDueDate) BETWEEN 0 AND 7 THEN T0.[DocDueDate] + 5
WHEN DATEDIFF(DAY,T0.DocDate,T0.DocDueDate) BETWEEN 8 AND 14 THEN T0.[DocDueDate] + 7
WHEN DATEDIFF(DAY,T0.DocDate,T0.DocDueDate) BETWEEN 15 AND 30 THEN T0.[DocDueDate] + 10
WHEN DATEDIFF(DAY,T0.DocDate,T0.DocDueDate) BETWEEN 31 AND 60 THEN T0.[DocDueDate] + 17
END AS MaxTolerance,
CASE
WHEN MaxTolerance <= LastPaymentDate THEN 'Safe'
ELSE 'Not Safe'
END AS 'PaymentCheck'
FROM
OINV T0
INNER JOIN
RCT2 T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN
ORCT T2 ON T1.[DocNum] = T2.[DocEntry]
INNER JOIN
INV1 T3 ON T0.[DocEntry] = T3.[DocEntry]
GROUP BY
T0.[CANCELED], T0.[DocNum], T0.[DocDate], T0.[DocDueDate],
T0.[CardCode], T0.[CardName], T0.[DocTotal], T0.[PaidToDate],
T2.[DocDate], T3.[WhsCode]
ORDER BY
T0.[DocNum] DESC
I get this error:
Invalid column name 'MaxTolerance'
I already use [] on the MaksimalToleransi but it don't change anything. i want to check which record that MaxTolerance is less than or equal to LastPaymentDate

Related

Need SQL Query to return data for a specific date using Microsoft SQL Server

I have an sql query that was created by an experienced team member who we lost unexpectedly. I was able to modify a different query created by that team member, but can't seem to figure this one out. The query is used to print out writers' surveys. I need it to print out surveys from 10/1/2022 - 12/31/2022 (4th quarter). When I run the query, it shows me surveys for 2023, which is not what I'm looking for. I only know very basic sql statements, so I'm having trouble modifying it to print out what I need it to and I'm the only one on my team who has some basic knowledge of sql. Help would be greatly appreciated.
Here's the query:
set datefirst 6 /* Saturday */
declare #StartDate as date = '1/1/' + convert(varchar(4),year(getdate()))
DROP TABLE IF EXISTS #WriterSurveys
DROP TABLE IF EXISTS #WriterResumes
Select * into #WriterSurveys
From
(
select
#StartDate as StartDate,
case month(#StartDate)
when 1 then 1
when 2 then 1
when 3 then 1
when 4 then 2
when 5 then 2
when 6 then 2
when 7 then 3
when 8 then 3
when 9 then 3
when 10 then 4
when 11 then 4
when 12 then 4
else 0
end as Qtr,
cd.WriterUserID,
u.lastname + ', ' + u.firstname as WriterName,
cfsa.SurveyAnswer,
case cfsa.SurveyAnswer
when 1 then 'Excellent'
when 2 then 'Very Good'
when 3 then 'Good'
when 4 then 'Fair'
when 5 then 'Poor'
else 'N/A'
end as SurveyAnswerText,
cf.ClientID,
c.lastname + ', ' + c.firstname as ClientName,
cf.SendDate as SurveySendDate,
cf.ReceivedDate as SurveyReceivedDate,
cd.ClientDocumentTypeID,
cd.IsResumeASAP,
cd.IsResumeOnly,
cd.IsRush
from ClientForm cf
Left outer join ClientDocument cd on cd.ClientID = cf.ClientID
left outer join ClientFormSurveyAnswer cfsa on cfsa.SurveyQuestionID = 202 and cfsa.ClientFormID = cf.ClientFormID
left outer join [User] u on u.UserID = cd.WriterUserID
left outer join Client c on c.ClientID = cf.ClientID
where cf.FormTypeID = 7
and cf.SendDate >= #StartDate
/*and cf.ReceivedDate is not null*/
) as WS
Select * into #WriterResumes
From
(
select
count(ClientID) as NumResumes,
max(WriterUserID) as WriterUserID
from
(
select
ClientID,
cd.ClientDocumentID,
cds.ClientDocumentWorkflowStepID,
convert(date,cds.MovedIntoStepOn) as Step3Date,
cd.WriterUserID
from ClientDocument cd
inner join ClientDocumentStep cds on cds.ClientDocumentID = cd.ClientDocumentID
left outer join [user] u on u.UserID = cd.CreatedBy
where cd.ClientDocumentTypeID = 5 and
cds.ClientDocumentWorkflowStepID = 4 and
convert(date,cds.MovedIntoStepOn) >= #StartDate
) as p1
group by WriterUserID
) as WR
/*
select
*
from #WriterSurveys
order by Qtr,
WriterName
*/
select
Qtr,
year(#StartDate) as 'Year',
WriterName,
/*sum(isnull(NumResults0,0)) as 'N/A',*/
sum(isnull(NumResults1,0)) as 'Excellent(1)',
sum(isnull(NumResults2,0)) as 'Very Good(2)',
sum(isnull(NumResults3,0)) as 'Good(3)',
sum(isnull(NumResults4,0)) as 'Fair(4)',
sum(isnull(NumResults5,0)) as 'Poor(5)',
isnull(
convert(decimal(10,2),convert(decimal(10,2),(
sum(isnull(NumResults1,0))*1 +
sum(isnull(NumResults2,0))*2 +
sum(isnull(NumResults3,0))*3 +
sum(isnull(NumResults4,0))*4 +
sum(isnull(NumResults5,0))*5)) /
convert(decimal(10,2),(sum(TotalResp))))
,0) as AverageRate,
isnull(
convert(decimal(10,2),convert(decimal(10,2),(
sum(isnull(NumResults1,0)) +
sum(isnull(NumResults2,0)) +
sum(isnull(NumResults3,0)) )) /
convert(decimal(10,2),(sum(TotalResp)))) * 100
,0) as '% E/VG/G',
sum(isnull(TotalResp,0)) as TotalResp,
sum(TotalSent) as TotalSent,
isnull(max(wr.NumResumes),0) as NumResumes
from
(
select
Qtr,
WriterUserID,
WriterName,
SurveyAnswer,
case when SurveyAnswer = 0 then count(clientid) end as NumResults0,
case when SurveyAnswer = 1 then count(clientid) end as NumResults1,
case when SurveyAnswer = 2 then count(clientid) end as NumResults2,
case when SurveyAnswer = 3 then count(clientid) end as NumResults3,
case when SurveyAnswer = 4 then count(clientid) end as NumResults4,
case when SurveyAnswer = 5 then count(clientid) end as NumResults5,
case when SurveyAnswer > 0 then count(clientid) end as TotalResp,
count(clientid) as TotalSent
from #WriterSurveys
group by Qtr,
WriterName,
WriterUserID,
SurveyAnswer
) as p1
left outer join #WriterResumes wr on wr.WriterUserID = p1.WriterUserID
/*where TotalResp > 0*/
group by Qtr,
WriterName
order by Qtr,
WriterName
On the top of the query you use GETDATE(), which returns the current date year.
If you want to return values for 2022, please change to this, I've also added an end date since you had wanted to return a value between two data sets:
set datefirst 6 /* Saturday */
declare #StartDate as date = '10/1/2022'
declare #EndDate as date = '12/31/2022'
DROP TABLE IF EXISTS #WriterSurveys
DROP TABLE IF EXISTS #WriterResumes
Select * into #WriterSurveys
From
(
select
#StartDate as StartDate,
case month(#StartDate)
when 1 then 1
when 2 then 1
when 3 then 1
when 4 then 2
when 5 then 2
when 6 then 2
when 7 then 3
when 8 then 3
when 9 then 3
when 10 then 4
when 11 then 4
when 12 then 4
else 0
end as Qtr,
cd.WriterUserID,
u.lastname + ', ' + u.firstname as WriterName,
cfsa.SurveyAnswer,
case cfsa.SurveyAnswer
when 1 then 'Excellent'
when 2 then 'Very Good'
when 3 then 'Good'
when 4 then 'Fair'
when 5 then 'Poor'
else 'N/A'
end as SurveyAnswerText,
cf.ClientID,
c.lastname + ', ' + c.firstname as ClientName,
cf.SendDate as SurveySendDate,
cf.ReceivedDate as SurveyReceivedDate,
cd.ClientDocumentTypeID,
cd.IsResumeASAP,
cd.IsResumeOnly,
cd.IsRush
from ClientForm cf
Left outer join ClientDocument cd on cd.ClientID = cf.ClientID
left outer join ClientFormSurveyAnswer cfsa on cfsa.SurveyQuestionID = 202 and cfsa.ClientFormID = cf.ClientFormID
left outer join [User] u on u.UserID = cd.WriterUserID
left outer join Client c on c.ClientID = cf.ClientID
where cf.FormTypeID = 7
and cf.SendDate >= #StartDate
and cf.SendDate <= #EndDate
/*and cf.ReceivedDate is not null*/
) as WS
Select * into #WriterResumes
From
(
select
count(ClientID) as NumResumes,
max(WriterUserID) as WriterUserID
from
(
select
ClientID,
cd.ClientDocumentID,
cds.ClientDocumentWorkflowStepID,
convert(date,cds.MovedIntoStepOn) as Step3Date,
cd.WriterUserID
from ClientDocument cd
inner join ClientDocumentStep cds on cds.ClientDocumentID = cd.ClientDocumentID
left outer join [user] u on u.UserID = cd.CreatedBy
where cd.ClientDocumentTypeID = 5 and
cds.ClientDocumentWorkflowStepID = 4 and
convert(date,cds.MovedIntoStepOn) >= #StartDate
) as p1
group by WriterUserID
) as WR
/*
select
*
from #WriterSurveys
order by Qtr,
WriterName
*/
select
Qtr,
year(#StartDate) as 'Year',
WriterName,
/*sum(isnull(NumResults0,0)) as 'N/A',*/
sum(isnull(NumResults1,0)) as 'Excellent(1)',
sum(isnull(NumResults2,0)) as 'Very Good(2)',
sum(isnull(NumResults3,0)) as 'Good(3)',
sum(isnull(NumResults4,0)) as 'Fair(4)',
sum(isnull(NumResults5,0)) as 'Poor(5)',
isnull(
convert(decimal(10,2),convert(decimal(10,2),(
sum(isnull(NumResults1,0))*1 +
sum(isnull(NumResults2,0))*2 +
sum(isnull(NumResults3,0))*3 +
sum(isnull(NumResults4,0))*4 +
sum(isnull(NumResults5,0))*5)) /
convert(decimal(10,2),(sum(TotalResp))))
,0) as AverageRate,
isnull(
convert(decimal(10,2),convert(decimal(10,2),(
sum(isnull(NumResults1,0)) +
sum(isnull(NumResults2,0)) +
sum(isnull(NumResults3,0)) )) /
convert(decimal(10,2),(sum(TotalResp)))) * 100
,0) as '% E/VG/G',
sum(isnull(TotalResp,0)) as TotalResp,
sum(TotalSent) as TotalSent,
isnull(max(wr.NumResumes),0) as NumResumes
from
(
select
Qtr,
WriterUserID,
WriterName,
SurveyAnswer,
case when SurveyAnswer = 0 then count(clientid) end as NumResults0,
case when SurveyAnswer = 1 then count(clientid) end as NumResults1,
case when SurveyAnswer = 2 then count(clientid) end as NumResults2,
case when SurveyAnswer = 3 then count(clientid) end as NumResults3,
case when SurveyAnswer = 4 then count(clientid) end as NumResults4,
case when SurveyAnswer = 5 then count(clientid) end as NumResults5,
case when SurveyAnswer > 0 then count(clientid) end as TotalResp,
count(clientid) as TotalSent
from #WriterSurveys
group by Qtr,
WriterName,
WriterUserID,
SurveyAnswer
) as p1
left outer join #WriterResumes wr on wr.WriterUserID = p1.WriterUserID
/*where TotalResp > 0*/
group by Qtr,
WriterName
order by Qtr,
WriterName
Hope this helps :)

Changing the old SQL lanuage to the new [duplicate]

This question already has answers here:
How to find LEFT OUTER JOIN or RIGHT OUTER JOIN with ORACLE JOIN (+)
(2 answers)
Closed 7 years ago.
I am currently a Trainee trying to develop my SQL. Could someone help me with changing the old language to the new(orale 11g or sql 2014) Below is an example that I am current changing;
select apa.invoicE_num ap_invoice_num
, apa.creation_date
, rat.TRX_NUMBER ar_trx_number
, rat.creation_date loaded_into_ar
,asw.inserted_date
, decode(pvs.org_id,86,'SP',87,'SV')
, pv.vendor_nam
,pv.segment1,
pvs.vendor_site_code
, asw.attribute6
, asw.invoice_amount
, asw.INVOICE_NUM
from asw.asw_ap_invoices_interface_aud asw
, ra_customer_trx_all rat
, po_vendor_sites_all pvs
, po_vendors pv
, ap_invoices_all apa
where asw.invoice_num = rat.trx_number (+)
and asw.VENDOR_SITE_ID = pvs.vendor_site_id
and pvs.vendor_id = pv.vendor_id
and asw.invoice_num = apa.invoicE_num (+)
and asw.invoice_amount = apa.invoicE_amount (+)
and asw.vendor_site_id = apa.vendor_site_id (+)
and asw.attribute_category = 'Retek Import'
and asw.attribute6 in ('MB','DA','RM','SB')
and trunc(inserted_date) > '29-OCT-2015'
Thank you for reading this and I hope you can help!
SELECT *
FROM a, b
WHERE a.id=b.id(+)
the same as
SELECT *
FROM a LEFT OUTER JOIN b ON a.id=b.id
So in your case:
select apa.invoicE_num ap_invoice_num
, apa.creation_date
, rat.TRX_NUMBER ar_trx_number
, rat.creation_date loaded_into_ar
,asw.inserted_date
, decode(pvs.org_id,86,'SP',87,'SV')
, pv.vendor_nam
,pv.segment1,
pvs.vendor_site_code
, asw.attribute6
, asw.invoice_amount
, asw.INVOICE_NUM
from asw.asw_ap_invoices_interface_aud asw
left outer join ra_customer_trx_all rat on asw.invoice_num = rat.trx_number
inner join po_vendor_sites_all on pvs asw.VENDOR_SITE_ID = pvs.vendor_site_id
inner join po_vendors pv on pvs.vendor_id = pv.vendor_id
left outer join ap_invoices_all apa on asw.invoice_amount = apa.invoicE_amount and asw.vendor_site_id = apa.vendor_site_id
where asw.attribute_category = 'Retek Import'
and asw.attribute6 in ('MB','DA','RM','SB')
and trunc(inserted_date) > '29-OCT-2015'

I just cant switch some columns SQL server (simple) [duplicate]

This question already has answers here:
Convert Rows to columns using 'Pivot' in SQL Server
(9 answers)
Closed 7 years ago.
My query:
select (sum( c20 ))as total , año
FROM(
select distinct datepart(year,DATEADD(DAY,CONVERT(FLOAT(10),KDUD.C16),A1.C7))as año, (a1.c20) as c20
from
KDUE A1
LEFT JOIN KDMM ON KDMM.C1='U' AND A1.C3=KDMM.C2 AND A1.C4=KDMM.C3 AND A1.C5=KDMM.C4
LEFT JOIN KDMS ON A1.C1=KDMS.C1
LEFT JOIN KDUD ON A1.C2=KDUD.C2
LEFT JOIN KDUV ON A1.C18=KDUV.C2
where a1.c20 != 0) as aas
group by año
this gives this table as a result:
total año
--------------------------------------- -----------
409782.45 2013
2442993.24 2014
10810460.01 2015
but i wish i could get the years as headers and the totals as a single row... help :c
with x as
(select (sum( c20 ))as total , año
FROM(
select distinct datepart(year,DATEADD(DAY,CONVERT(FLOAT(10),KDUD.C16),A1.C7))as año, (a1.c20) as c20
from
KDUE A1
LEFT JOIN KDMM ON KDMM.C1='U' AND A1.C3=KDMM.C2 AND A1.C4=KDMM.C3 AND A1.C5=KDMM.C4
LEFT JOIN KDMS ON A1.C1=KDMS.C1
LEFT JOIN KDUD ON A1.C2=KDUD.C2
LEFT JOIN KDUV ON A1.C18=KDUV.C2
where a1.c20 != 0) as aas
group by año)
select case when ano = '2013' then total end as '2013',
case when ano = '2014' then total end as '2014',
case when ano = '2015' then total end as '2015'
from x
This is one way to do it but will be more manual as you have more than a handful of values in ano column.

Calculate days difference between two dates from two queries conslidated by union

I have two queries with a union all between them. I am trying to calculate the differences between two dates, one of them in the 1st query, and the other is in the 2nd query.
The 1st date in the 1st query is t0.duedate, the 2nd date in the 2nd query is t0.docdate.
I'll use this query in crystal report where i'll put a condition to order the resualt based on the field t0.[u_a_id] which available in both queries
the resualt right now is like this
DocENTRY slpname DocNum SeriesName Series DocType DocDate DocdueDate CardCode CardName DocTotal u_a_id type
1 - 1 Primary 12 C 2006-01-31 00:00:00.000 2006-01-31 00:00:00.000 C20000 Norm Thompson 14023.800000 NULL A/R INCOMING PAYMENT
323 Sales Manager 323 Primary 1 I 2012-06-09 00:00:00.000 2012-07-09 00:00:00.000 C20000 Maxi-Teq 4253.250000 NULL A/R INVOICE
i'll need to have one more column need Days Difference which will calculate the diffrences between days of the two dates "2012-07-09 - 2006-01-31" like this:
Days Difference
1921 Day
Here is the query :
SELECT DISTINCT
t0.[DocENTRY] ,
oslp.slpname ,
t0.[DocNum] ,
nnm1.SeriesName ,
t0.[Series] ,
t0.[DocType] ,
t0.[DocDate] ,
t0.[DocdueDate] ,
t0.[CardCode] ,
t0.[CardName] ,
t0.[DocTotal] ,
t0.[u_a_id] ,
'A/R INVOICE' AS type
FROM OINV t0
INNER JOIN NNM1 ON nnm1.series = t0.Series
INNER JOIN OSLP ON oslp.slpcode = t0.slpcode
WHERE t0.DOCSTATUS = 'C'
UNION ALL
SELECT DISTINCT
t0.[DocENTRY] ,
'-' AS slpname ,
t0.[DocNum] ,
nnm1.SeriesName ,
t0.[Series] ,
t0.[DocType] ,
t0.[DocDate] ,
t0.[DocdueDate] ,
t0.[CardCode] ,
t0.[CardName] ,
t0.[DocTotal] ,
t0.[u_a_id] ,
'A/R INCOMING PAYMENT' AS type
FROM orct t0
INNER JOIN NNM1 ON nnm1.series = t0.Series
ORDER BY t0.[CardCode]
By using a nested query to bring the date from the 2nd query to the 1st query , then in CR made a formula to calculate the different between the dates ,
query now after update it :
select distinct T0.[DocENTRY],
T0.[DocNum],nnm1.SeriesName, T0.[Series],oslp.SlpName,
T0.[DocDate] as 'Invoice Date',T0.[DocDueDate], T0.[CardCode],
T0.[CardName], T0.[DocTotal], T0.[Comments],
'A/R invoice' as type, t0.U_A_ID,
(select docdate from ORCT where orct.U_A_ID = t0.U_A_ID)
as PaymentDt
from oinv t0
inner join NNM1 on nnm1.series =t0.Series
inner join OSLP on oslp.SlpCode =t0.SlpCode
where T0.DocStatus ='c'
union all
select distinct T0.[DocENTRY],
T0.[DocNum],nnm1.SeriesName, T0.[Series],
'_' AS SlpName, T0.[DocDate],
T0.[DocDueDate] AS 'Payment Due Date',
T0.[CardCode], T0.[CardName], T0.[DocTotal],
T0.[Comments],
'A/R Incoming Payment' as type, t0.U_A_ID , getdate() as paymentdt
from ORCT t0
inner join NNM1 on nnm1.series =t0.Series
order by t0.CardCode,t0.u_A_id
and the formula used in CR is :
DATEDIFF("d",{Command.PaymentDt},{Command.DocDueDate})

Error while creating view in SQL Server 2000

I am trying to create a view as following in SQL Server 2000
CREATE VIEW [dbo].[AC_VW0901]
AS
SELECT
CASE T2.ItmsGrpCod
WHEN 102 THEN T2.ItmsGrpCod
WHEN 103 THEN T2.ItmsGrpCod
WHEN 107 THEN T2.ItmsGrpCod
WHEN 108 THEN T2.ItmsGrpCod
ELSE 100
END AS ItmsGrpCod,
CASE
WHEN Month(T0.TaxDate) >=4 AND Month(T0.TaxDate) <= 12 THEN Month(T0.TaxDate) - 3
WHEN Month(T0.TaxDate) >=1 AND Month(T0.TaxDate) <= 3 THEN Month(T0.TaxDate) + 9
END As MonthNum,
CASE
WHEN Month(T0.TaxDate) >=4 AND Month(T0.TaxDate) <= 12 THEN Year(T0.TaxDate)
WHEN Month(T0.TaxDate) >=1 AND Month(T0.TaxDate) <= 3 THEN Year(T0.TaxDate) - 1
END As YearNum,
T1.Quantity, T1.LineTotal, T4.U_Username, Month(T0.TaxDate) As [Month], Year(T0.TaxDate) As [Year], T0.TaxDate
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OCRD T3 ON T0.CardCode = T3.CardCode
INNER JOIN OSLP T4 ON T3.SlpCode = T4.SlpCode
This throws an error as following
Msg 208, Level 16, State 1, Server
N4IDEL130007, Procedure AC_VW0901,
Line 26 Invalid object name 'OSLP'.
The table OSLP is very much there. I can use it another view, perform select query on it.
What could be the reasons?
Regards,
Rahul Jain
Sorry, the problem was resolved by using full name like Abc.dbo.OSLP
Thanks.