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.
Related
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
I'm running the following query on our SQL Server DB.
The query takes at least 20 minutes to complete. I don't have permissions on the db to run execution plan
Thoughts or suggestions on what to try would be appreciated
SELECT
UsageDate,
--convert(date,dateadd(day, 22 - 1,dateadd(month,cast(datepart("MM", dateadd("d",-21, getdate()-2)) as int)- 1,dateadd(year,cast(datepart("yyyy", dateadd("d",23,getdate()-2)) as int) - 2000,'2000-01-01'))) ) AS BILLING_CYCLE_START_DT,
(sum(MVNO301TotUsg)-sum(ROAMING_3G_LTE)) MB,
sum(ROAMING_3G_LTE)ROAMING_3G_LTE,
case when convert(date,UsageDate) = convert(date,dateadd(day, 9 - 1,dateadd(month,cast(datepart("MM", dateadd("d",-8, getdate()-2)) as int)- 1,dateadd(year,cast(datepart("yyyy", dateadd("d",10,getdate()-2)) as int) - 2000,'2000-01-01'))) )
then 100000000 else 0 end as BeginBaseAvgMB
FROM
(
select convert(date,EVENT_DT) UsageDate,
sum((USAGE_KB_3G+LTE_USAGE_KB+EHRPD_USAGE_KB)/1024)AS MVNO301TotUsg,
0 ROAMING_3G_LTE
from [spmp].[dbo].SubscriberCombinedUsageallmvnos USG with (NOLOCK)
JOIN aMVNO.dbo.Subscribers S with (NOLOCK) ON USG.subscriberkey =S.subscriberkey
left Join aMVNO.dbo.exMVNOs_Realm_RISE RLM with (NOLOCK) ON S.SubscriberNumber = RLM.SBSCR_NBR
where (RESELLER_ID = 'a9')
and convert(date,EVENT_DT) between dateadd(d,-33,GETDATE()) and dateadd(d,-2,GETDATE())
group by convert(date,EVENT_DT)
UNION ALL
SELECT
convert(date,EVENT_DT) UsageDate,
0 AS MVNO301TotUsg,
sum(DOMESTIC_ROAM_3G + INTERNATIONAL_ROAM_3G +GSM_3G +ROAMING_LTE_KB)/1024 ROAMING_3G_LTE
from [spmp].[dbo].SubscriberCombinedUsageallmvnos USG with (NOLOCK)
JOIN aMVNO.dbo.Subscribers S with (NOLOCK) ON USG.subscriberkey =S.subscriberkey
left Join aMVNO.dbo.exMVNOs_Realm_RISE RLM with (NOLOCK) ON S.SubscriberNumber = RLM.SBSCR_NBR
JOIN [aMVNO].[dbo].[BusinessDate] BD WITH (NOLOCK) ON convert(date,USG.EVENT_DT) =convert(date,BD.DT)
where
(RESELLER_ID = 'a9')
and convert(date,EVENT_DT) between dateadd(d,-33,GETDATE()) and dateadd(d,-2,GETDATE())
GROUP BY
convert(date,EVENT_DT)
)Z
group by UsageDate
ORDER BY USAGEDATE
I tried to have a running total row at the bottom of my table by running this following SQL code.
SELECT
T1.[ItemCode],T4.[ItmsGrpNam],
CASE
WHEN T3.[OnHand] - SUM(T1.[OpenQty]) < SUM(T1.[OpenQty])
THEN T3.[OnHand] - SUM(T1.[OpenQty])
ELSE
SUM(T1.[OpenQty])
END As 'Missing Quantity'
FROM ORDR T0
INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
INNER JOIN OITB T4 ON T2.ItmsGrpCod=T4.ItmsGrpCod
WHERE
T3.[WhsCode] = T1.[WhsCode]
and T0.[DocDueDate] <= '[%D1]'
and (T4.[ItmsGrpNam] = 'Commercial' Or T4.[ItmsGrpNam] = 'Residentiel')
GROUP BY T1.[ItemCode],T3.[OnHand],T4.[ItmsGrpNam]
HAVING T3.[OnHand] - SUM(T1.[OpenQty]) < 0 Or SUM(T1.[OpenQty]) < 0
UNION ALL
SELECT 'See', 'Total Missing Quantity' , CASE
WHEN T3.[OnHand] - SUM(T1.[OpenQty]) < SUM(T1.[OpenQty])
THEN T3.[OnHand] - SUM(T1.[OpenQty])
ELSE
SUM(T1.[OpenQty])
END
FROM ORDR T0
INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
INNER JOIN OITB T4 ON T2.ItmsGrpCod=T4.ItmsGrpCod
WHERE
T3.[WhsCode] = T1.[WhsCode]
and T0.[DocDueDate] <= '[%D1]'
and (T4.[ItmsGrpNam] = 'Commercial' Or T4.[ItmsGrpNam] = 'Residentiel')
I have this error message '
1). [Microsoft] [SQL Server Native Client 11.0] [SQL Server] The 'OITW.OnHand' column is not valid in the selection list because it is not contained in an aggregate function or in the clause BY GROUP. 2). [Microsoft] [SQL Server Native Client 11.0] [SQL Server] Can not prepare statements. 'User Defined Values' (CSHS)'
When I try to
GROUP BY T1.[ItemCode],T3.[OnHand],T4.[ItmsGrpNam]
HAVING T3.[OnHand] - SUM(T1.[OpenQty]) < 0 Or SUM(T1.[OpenQty]) < 0
In the second part of the query, I get multiple rows when I only want one that sum all.
I spent the day to figure this out and there's multiple answers on the NET but I don't know what i'm doing wrong.
Does anyone can help me with this issue? Thanks in advance.
I have the following query. Please have a look on that first
select
X.ITEMCODE, X.[ITEM_DESCRIPTION], X.[OPENING_BALANCE],
X.[SALES], X.[TOTAL_REJECTS], x.[REJECTION_DISEASES]
(X.[SALES] - X.[TOTAL_REJECTS]) as [PRODUCTION_TOTAL],
X. [DOCDATE],
(X.[OPENING_BALANCE] + (X.[SALES]-X.[TOTAL_REJECTS])) AS [CLOSING_BALANCE]
from
(SELECT DISTINCT
T0.ItemCode AS ITEMCODE, T0.ItemName AS [ITEM_DESCRIPTION],
ISNULL((SELECT SUM(T1.OnHand)
FROM OITW T1
WHERE T1.WhsCode = 'FGS'
AND T1.ItemCode = T0.ItemCode), 0) AS [OPENING_BALANCE],
ISNULL((SELECT SUM(T1.[Quantity])
FROM INV1 T1
WHERE T0.[ItemCode] = T1.[ItemCode]), 0) AS [SALES] ,
(SELECT T1.[DocDate] FROM INV1 T1
WHERE T0.[ItemCode] = T1.[ItemCode]) AS [DOCDATE],
ISNULL((SELECT SUM(cast(T1.[Quantity] as int))
FROM IGE1 T1
WHERE T0.[ItemCode] = T1.[ItemCode]), 0) AS [TOTAL_REJECTS],
ISNULL((SELECT T1.[U_RejectionOthers]
FROM IGE1 T1
WHERE T0.[ItemCode] = T1.[ItemCode]), 0) AS [REJECTION_DISEASES]
from
OITM T0
where
T0.[ItmsGrpCod] in ('104', '121', '122', '123', '124', '125', '126', '127', '129', '130', '134', '135', '136', '139', '140', '141', '142', '143', '144', '145')) X
WHERE
X.[OPENING_BALANCE] > 0 OR X.[TOTAL_REJECTS] > 0 OR X.[SALES] > 0
In this code when I tried to add X.[DOCDATE] and x.[REJECTION_DISEASES] in the main select statement then I get the following error
Subquery returned more than 1 value. This is not permitted when the
subquery follows =, !=, <, <= , >, >= or when the subquery is used as
an expression.
Please experts help me out.
I suspect below sub-query returns more than one row for given condition in where clause
Isnull((SELECT T1.[u_rejectionothers]
FROM ige1 T1
WHERE T0.[itemcode] = T1.[itemcode]), 0) AS[REJECTION_DISEASES]
Either you need to fix the where clause to return one row or TOP plus Order by to return one row
Fixing where clause. Put the all the conditions in where clause through which you can identify a unique line in ige1 table
Isnull((SELECT T1.[u_rejectionothers]
FROM ige1 T1
WHERE T0.[itemcode] = T1.[itemcode]
AND T1.somecol = 'XXX'), 0) AS[ REJECTION_DISEASES]
Adding TOP plus Order by in sub-query
Isnull((SELECT TOP 1 T1.[u_rejectionothers]
FROM ige1 T1
WHERE T0.[itemcode] = T1.[itemcode]
Order by somecol), 0) AS [REJECTION_DISEASES]
I'm trying to create a view where I union 4 tables. One of the fields is a document number. The only way I can pull the related document number from one of the tables is by running a LEFT on the 6 left-most characters. But when I do this and then CAST to integer I still cannot query the field. It's like it's looking past my where clause and trying to convert ALL of the characters in the field.
This is what I'm doing:
SELECT dbo.OSLP.U_RepStatus AS RepStatus, dbo.OSLP.SlpCode, dbo.OPCH.CardCode, dbo.OPCH.CardName, 'IN' AS DocTypeDesc, dbo.OPCH.DocNum,
dbo.OPCH.DocEntry, dbo.OPCH.DocDate, dbo.OPCH.NumAtCard, dbo.OPCH.DocStatus, dbo.OPCH.DocTotal, dbo.OPCH.U_CommInvNo AS ARInvNo
FROM dbo.OPCH INNER JOIN
dbo.OCRD ON dbo.OPCH.CardCode = dbo.OCRD.CardCode INNER JOIN
dbo.OSLP ON dbo.OCRD.U_SalesRepNumber = dbo.OSLP.SlpCode
WHERE (dbo.OSLP.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.OPCH.U_CommInvNo IS NOT NULL)
UNION
SELECT OSLP_1.U_RepStatus AS RepStatus, OSLP_1.SlpCode, dbo.ORPC.CardCode, dbo.ORPC.CardName, 'CM' AS DocTypeDesc, dbo.ORPC.DocNum, dbo.ORPC.DocEntry,
dbo.ORPC.DocDate, dbo.ORPC.NumAtCard, dbo.ORPC.DocStatus, - 1 * dbo.ORPC.DocTotal AS DocTotal, dbo.ORPC.U_CommInvNo AS ARInvNo
FROM dbo.ORPC INNER JOIN
dbo.OCRD AS OCRD_1 ON dbo.ORPC.CardCode = OCRD_1.CardCode INNER JOIN
dbo.OSLP AS OSLP_1 ON OCRD_1.U_SalesRepNumber = OSLP_1.SlpCode
WHERE (OSLP_1.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (dbo.ORPC.U_CommInvNo IS NOT NULL)
UNION
SELECT T3.U_RepStatus AS RepStatus, T3.SlpCode, T0.ContraAct AS CardCode, T2.CardName, 'JE' AS DocTypeDesc, T1.TransId AS DocNum, T1.TransId AS DocEntry,
T1.RefDate AS DocDate, T0.LineMemo AS NumAtCard, T1.BtfStatus AS DocStatus, T0.Debit AS DocTotal, CAST(LEFT(T0.LineMemo, 6) AS INT) AS ARInvNo
FROM dbo.JDT1 AS T0 INNER JOIN
dbo.OJDT AS T1 ON T0.TransId = T1.TransId INNER JOIN
dbo.OCRD AS T2 ON T0.ContraAct = T2.CardCode INNER JOIN
dbo.OSLP AS T3 ON T2.U_SalesRepNumber = T3.SlpCode
WHERE (T0.ShortName = 'CompanyName') AND (T1.TransType = 30) AND (T0.Account = '_SYS00000000624') AND (T3.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T0.Debit > 0)
UNION
SELECT T7.U_RepStatus AS RepStatus, T7.SlpCode, T4.ContraAct AS CardCode, T6.CardName, 'JE' AS DocTypeDesc, T5.TransId AS DocNum, T5.TransId AS DocEntry,
T5.RefDate AS DocDate, T4.LineMemo AS NumAtCard, T5.BtfStatus AS DocStatus, - (1 * T4.Credit) AS DocTotal, CAST(LEFT(T4.LineMemo, 6) AS INT) AS ARInvNo
FROM dbo.JDT1 AS T4 INNER JOIN
dbo.OJDT AS T5 ON T4.TransId = T5.TransId INNER JOIN
dbo.OCRD AS T6 ON T4.ContraAct = T6.CardCode INNER JOIN
dbo.OSLP AS T7 ON T6.U_SalesRepNumber = T7.SlpCode
WHERE (T4.ShortName = 'CompanyName') AND (T5.TransType = 30) AND (T4.Account = '_SYS00000000624') AND (T7.U_RepStatus IN ('Dir', 'Ind', 'Sal')) AND (T4.Credit > 0)
If I query this view with WHERE ARInvNo = 295696 I get this error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'A/P In' to data type int.
If I take out the bottom 2 unions from my view I can query with WHERE ARInvNo = 295696 without an issue. If I run the second 2 parts of the query, nothing in my results includes 'A/P In', although that does exist in rows in JDT1 outside of the WHERE criteria I'm using. Any idea why SQL is looking past my WHERE statement and trying to LEFT and CAST every field before it returns any data?
To clarify, this returns no results:
SELECT T0.LineMemo
FROM JDT1 T0 INNER JOIN OJDT T1 ON T0.TransId = T1.TransId INNER JOIN OCRD T2 ON T0.ContraAct = T2.CardCode INNER JOIN OSLP T3 ON T2.U_SalesRepNumber = T3.SlpCode
WHERE T0.ShortName = 'CompanyName' AND T1.TransType = 30 AND T0.Account = '_SYS00000000624' AND T3.U_RepStatus IN ('Dir', 'Ind', 'Sal') AND left(T0.LineMemo,3)='A/P In'
I see it now.
It's seeing 'A/P in' in your values. It can't CAST the '/' character to INT
MSSQL cast( [varcharColumn] to int) in SELECT gets executed before WHERE clause filters out bad values
In short you will want to do this:
select (case when isnumeric(TO.LineMemo) = 1 then cast(TO.LineMemo as int) end)