The multi-part identifier "T0.LocCode" could not be bound - sql-server-2008

Alter proc [dbo].[#SMS_RPT_AllUnitsDebitNotes]
(
#FromDate Date,
#ToDate Date,
#Location int
)
as
begin
select
T0.ItemCode [ItemCode], Sum(T0.Quantity) [Quantity], sum(T0.TotalSumSy) TotalSumSy,T0.LocCode,T0.VatPrcnt,
T0.TaxCode,T0.WtLiable,T1.CardCode,T1.CardName,T1.NumAtCard,T1.DocNum,T1.DocDate,
T2.Name 'Tax Name',T3.Code,T3.Name 'Description',T3.Rate,T3.SalesTax,T4.Name
from RPC1 T0,
ORPC T1,
OSCT T2,
OSTA T3,
OSTT T4,
OLCT l1
inner join OLCT l1
on l1.Code = T0.LocCode
where
T0.DocEntry = T1.DocEntry
and T0.LocCode =#Location
and TaxDate Between #FromDate and #ToDate
group By
T0.ItemCode,T0.Dscription,T0.Quantity,T0.TotalSumSy,T0.LocCode,T0.VatPrcnt,
T0.WtLiable,T0.TaxCode,T1.CardCode,T1.CardName,T1.NumAtCard,T1.DocNum,T1.DocDate,
T2.Name ,T3.Code,T3.Name ,T3.Rate,T3.SalesTax,T4.Name
end

Try like this, Mention your inner join followed by cross join
ALTER PROC [dbo].[#SMS_RPT_AllUnitsDebitNotes] (
#FromDate DATE
,#ToDate DATE
,#Location INT
)
AS
BEGIN
SELECT T0.ItemCode [ItemCode]
,Sum(T0.Quantity) [Quantity]
,sum(T0.TotalSumSy) TotalSumSy
,T0.LocCode
,T0.VatPrcnt
,T0.TaxCode
,T0.WtLiable
,T1.CardCode
,T1.CardName
,T1.NumAtCard
,T1.DocNum
,T1.DocDate
,T2.NAME 'Tax Name'
,T3.Code
,T3.NAME 'Description'
,T3.Rate
,T3.SalesTax
,T4.NAME
FROM RPC1 T0
INNER JOIN ORPC T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OLCT l1 ON l1.Code = T0.LocCode
CROSS JOIN OSCT T2
CROSS JOIN OSTA T3
CROSS JOIN OSTT T4
CROSS JOIN OLCT l1
WHERE T0.LocCode = #Location
AND TaxDate BETWEEN #FromDate
AND #ToDate
GROUP BY T0.ItemCode
,T0.Dscription
,T0.Quantity
,T0.TotalSumSy
,T0.LocCode
,T0.VatPrcnt
,T0.WtLiable
,T0.TaxCode
,T1.CardCode
,T1.CardName
,T1.NumAtCard
,T1.DocNum
,T1.DocDate
,T2.NAME
,T3.Code
,T3.NAME
,T3.Rate
,T3.SalesTax
,T4.NAME
END

Related

Get invoice that has sales order and invoice without Sales Order

I want to achieve the following query.
What i want here is to compare the Invoice Amount from the Sales so the fields that i'll be needing are these:
OSLP.SalesPerson|OINV.CardName|OINV.DocDate|OINV.DocNum|OINV.DocTotal|ORDR.DocDate|ORDR.DocTotal|OINV.GrosProfit
What i have done so far is this,
Declare #mindate date = '10.01.16', #maxdate date = '10.31.16'
SELECT DISTINCT
T6.SlpName [Sales Person],
T5.CardName [Customer Name],
T5.DocDate [TRA Date],
T5.DocNum [TRA No],
T5.DocTotal [TRA Total],
(SELECT SUM(T8.DocTotal) FROM ORIN T8 INNER JOIN RIN1 T9 ON T8.DocEntry=T9.DocEntry WHERE T8.DocDate BETWEEN #mindate AND #maxdate) [Credit Amnt],
T1.DocDate [SO Date],
T1.DocTotal [SO Total],
T5.GrosProfit [Gross Profit]
FROM RDR1 T0
INNER JOIN ORDR T1 ON T0.DocEntry = T1.DocEntry
left outer JOIN DLN1 T2 on T2.BaseEntry = T0.DocEntry
left outer JOIN ODLN T3 on T2.DocEntry = T3.DocEntry
left Outer JOIN INV1 T4 on T4.BaseEntry = T3.DocEntry AND T4.BaseLine = T2.Linenum AND T4.BaseType = 15
OR (T4.Basetype=17 and T4.BaseEntry=T0.DocEntry AND T4.BaseLine=T0.LineNum)
left outer JOIN OINV T5 on T5.DocEntry = T4.DocEntry
left outer JOIN OSLP T6 on T6.SlpCode = T1.SlpCode
WHERE T5.DocDate BETWEEN #mindate AND #maxdate
Group by
T6.SlpName, T5.CardName, T1.CardCode,
T5.DocDate, T5.DocNum, T5.DocTotal,
T1.DocDate, T1.DocTotal, T5.GrosProfit
ORDER BY
T5.CardName, T5.DocDate
As you can i see, i included the ORIN(Credit Memo) table to deduct the SUM of OINV.DocTotal with ORIN.DocTotal.
So, this query works as it's suppose to be BUT, I am not able to get those invoices that has no related documents like Sales order or Delivery. Solely, it's just invoice.

Use where clause in temporary table

I am trying to use where clause in temporary table. Actually i used it but results aren't true. trying to find products between 2 dates but when i use where clause it gets no data.
CREATE TABLE #TEMPCOZGU
(
ROW INT,
TEZGAH VARCHAR(50),
COZGU VARCHAR(50),
PERSONEL VARCHAR(50),
DATE DATETIME,
FIRE INT,
FIRE_METRE NUMERIC(25,6)
)
INSERT INTO #TEMPCOZGU
select row_number() over(order by TEZGAH) AS ROW, TEZGAH, COZGU, PERSONEL, TARIH, FIRE, FIRE_METRE
from cozgu_fırelerı
where tezgah = 'M01' and cozgu = 'SAĞ ALT BAĞLANTI'
SELECT T1.TEZGAH, T1.COZGU, T1.PERSONEL,T1.FIRE, T1.FIRE_METRE, t1.DATE, T2.DATE AS PREVIOUSDATE, ISNULL(DATEDIFF(DAY, T2.DATE, T1.DATE), 0) AS GUNFARKI , SUM(MKARE) AS TOPLAMMKARE
FROM #TEMPCOZGU as t1
left outer join #TEMPCOZGU as t2 on t1.row-1 = t2.row
left outer join HALI_TEZGAH AS TE ON T1.TEZGAH = TE.TEZGAH_NO
LEFT OUTER JOIN HALI_GUNLUK_URETIM AS U ON U.TEZGAH = TE.ID
--WHERE U.TARIH BETWEEN T1.TARIH AND T2.TARIH
GROUP BY T1.TEZGAH, T1.COZGU, T1.PERSONEL, T1.DATE,T1.FIRE, T1.FIRE_METRE, T2.DATE
drop table #TEMPCOZGU`
If the datetime values always have the time set to "00:00:00", try
SELECT T1.TEZGAH, T1.COZGU, T1.PERSONEL,T1.FIRE, T1.FIRE_METRE, t1.DATE, T2.DATE AS PREVIOUSDATE, ISNULL(DATEDIFF(DAY, T2.DATE, T1.DATE), 0) AS GUNFARKI , SUM(MKARE) AS TOPLAMMKARE
FROM #TEMPCOZGU as t1
left outer join #TEMPCOZGU as t2 on t1.row-1 = t2.row
left outer join HALI_TEZGAH AS TE ON T1.TEZGAH = TE.TEZGAH_NO
LEFT OUTER JOIN HALI_GUNLUK_URETIM AS U ON U.TEZGAH = TE.ID
WHERE U.TARIH >= T1.TARIH AND U.TARIH < DateADD (T2.TARIH, INTERVAL 1 DAY)
GROUP BY T1.TEZGAH, T1.COZGU, T1.PERSONEL, T1.DATE,T1.FIRE, T1.FIRE_METRE, T2.DATE
If that's not the case, then your between should have worked in the first place.

Can't create stored procedure with parameters

I'm trying to create a stored procedure with the in parameters first date, last date and country
This is the normal query witch works just fine.
SELECT CAR_TYPE.NAME, CAR_TYPE.TYPE, CAR.DAY_PRICE, CAR.REGISTRATION_NUMBER, AGENCY.NAME, BRANCH.NAME
FROM CAR
JOIN CAR_TYPE ON CAR_TYPE.ID = CAR.CAR_TYPE_ID
JOIN BRANCH ON BRANCH.ID = CAR.BRANCH_ID
JOIN BRANCH_ADDRESS ON BRANCH_ADDRESS.BRANCH_ID = BRANCH.ID
JOIN AGENCY ON AGENCY.ID = BRANCH.ID`
WHERE CAR.ID NOT IN
(SELECT BOOKING.CAR_ID FROM BOOKING WHERE BOOKING.PICKUP_DATE < '2013-01-01' AND BOOKING.RETURN_DATE > '2013-12-12')
AND BRANCH_ADDRESS.CITY_NAME = 'Stockholm'
end
I can't make out what you are doing from your question, as it is not formatted well.
I have reformatted it so that it looks like a proper query, and made some changes to improve readability.
SELECT t.NAME, t.TYPE, c.DAY_PRICE,
c.REGISTRATION_NUMBER, a.NAME, b.NAME`
FROM CAR c
JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
JOIN BRANCH b ON b.ID = c.BRANCH_ID
JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
JOIN AGENCY a ON a.ID = b.ID
WHERE ba.CITY_NAME = 'Stockholm'
And c.ID NOT IN
(SELECT CAR_ID
FROM BOOKING
WHERE PICKUP_DATE < '2013-01-01'
AND RETURN_DATE > '2013-12-12')
if this is SQL server, then the statement to cerate a basic (without error handling) stored proc would be
Create Procedure dbo.FetchCarData -- or whatever you want to name it
#firstDate DateTime,
#lastDate DateTime,
#country varChar(50)
As
SELECT t.NAME, t.TYPE, c.DAY_PRICE,
c.REGISTRATION_NUMBER, a.NAME, b.NAME`
FROM CAR c
JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
JOIN BRANCH b ON b.ID = c.BRANCH_ID
JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
JOIN AGENCY a ON a.ID = b.ID`
WHERE ba.CITY_NAME = #country
And c.ID NOT IN
(SELECT CAR_ID
FROM BOOKING
WHERE PICKUP_DATE < #firstDate
AND RETURN_DATE > #lastDate )
in MySQL I think it would look like this...
Create Procedure FetchCarData( -- or whatever you want to name it
#firstDate DateTime,
#lastDate DateTime,
#country varChar(50))
Begin
SELECT t.NAME, t.TYPE, c.DAY_PRICE,
c.REGISTRATION_NUMBER, a.NAME, b.NAME`
FROM CAR c
JOIN CAR_TYPE t ON t.ID = c.CAR_TYPE_ID
JOIN BRANCH b ON b.ID = c.BRANCH_ID
JOIN BRANCH_ADDRESS ba ON ba.BRANCH_ID = b.ID
JOIN AGENCY a ON a.ID = b.ID`
WHERE ba.CITY_NAME = #country
And c.ID NOT IN
(SELECT CAR_ID
FROM BOOKING
WHERE PICKUP_DATE < #firstDate
AND RETURN_DATE > #lastDate )
End

More than one Case in same query are generating more than one row

My problem is actually I have multiple tables and I'm using two case statements to generate one column for ARV1 and one for ICA1, but I need the results are generated in the same row. When I usecase, generate the two columns but the values are displayed in two rows. What am I missing?
the thing is, i have an invoice the table OINV and have the table INV5 that is the table with the Holding Taxes, i need to put on the same row the invoice with all the Holding Taxes in different columns that are applying on it, thanks
this is the example tables
CREATE TABLE Invoice
(
Id INT, InvoiceNumber VARCHAR(10), Total INT
)
INSERT INTO Invoice
VALUES
(1,'200000',100),
(2,'200001',200),
(3,'200002',500),
(4,'200003',700),
(5,'200004',200),
(6,'200005',100),
(7,'200006',300)
CREATE TABLE HoldingTaxes
(
Id INT, HoldingTaxCode VARCHAR(10),HoldedAmount INT)
)
INSERT INTO HoldingTaxes
VALUES
(1,'ARV1',20),
(1,'ARV2',30),
(1,'ARV3',35),
(2,'ICA1',20),
(2,'ARV1',10),
(1,'ICA3',50)
I want a query that returns something like this:
InvoiceNumber Total ARV1 ARV2 ARV3 ICA1 ICA2 ICA3
200000 100 20 30 35 null null 50
This is what i am trying to do with my real tables
SELECT T0.DocNum [No. Factura],
CASE
WHEN t5.WTCode ='ARV1' and (t5.U_Ret_ML <>0 AND t5.U_Ret_ML is not null)
THEN 'Perro1'
else NULL
end AS ARV1
,
CASE
WHEN t5.WTCode ='ICA1' and (t5.U_Ret_ML <>0 AND t5.U_Ret_ML is not null)
THEN 'Perro2'
else NULL
end AS ICA1
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T4 ON T0.SlpCode = T4.SlpCode
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
INNER JOIN INV5 T5 ON T5.AbsEntry = T0.DocEntry
WHERE T1.WhsCode = T3.WhsCode`enter code here`
GROUP BY T0.DocNum,T0.DocDate,T0.DocTotal, T0.GrosProfit, T4.SlpName,T5.WTCODE,t5.U_Ret_ML
Alternative way :
SELECT inv.InvoiceNumber,inv.Total,[ARV1],[ARV2],[ARV3],[ICA1],[ICA2],[ICA3]
FROM INVOICE inv
JOIN(
SELECT id,[ARV1],[ARV2],[ARV3],[ICA1],[ICA2],[ICA3]
FROM
(SELECT * FROM HoldingTaxes ) t1
PIVOT(SUM(HoldedAmount) for HoldingTaxCode in
([ARV1],[ARV2],[ARV3],[ICA1],[ICA2],[ICA3])) t2
) ht
ON inv.id =ht.id
sql fiddle :http://sqlfiddle.com/#!3/ea3a4/10
I would use PIVOT to solve this - as demonstrated in the SQL Fiddle:
SELECT EmpName
,CASE WHEN ARV1 > 0 THEN 'PERRO1' ELSE NULL END
,CASE WHEN ARV2 > 0 THEN 'PERRO2' ELSE NULL END
,CASE WHEN ICA1 > 0 THEN 'PERRO3' ELSE NULL END
FROM (SELECT t0.EmpName, t1.Name, t1.Value
FROM Table0 t0 INNER JOIN Table1 t1 ON t0.Id = t1.Id ) as src
PIVOT (
MAX(src.Value)
FOR src.Name IN ([ARV1],[ARV2],[ICA1])) as p
http://sqlfiddle.com/#!3/a6ff0/2
If your having issues and willing to share your structure, I can put this into a a closer match to what you are using on SQL Fiddle.
Edit:
Based on the update you gave, here is the fiddle I created with the pivot.
http://sqlfiddle.com/#!3/47511/4
SELECT * FROM
(SELECT InvoiceNumber = Invoice.InvoiceNumber
,Total = Invoice.Total
,HoldingTaxCode = HoldingTaxes.HoldingTaxCode
,HoldedAmount = HoldingTaxes.HoldedAmount
FROM Invoice
LEFT JOIN HoldingTaxes
ON Invoice.Id = HoldingTaxes.Id) PivotSource
PIVOT
(
SUM(HoldedAmount) FOR HoldingTaxCode IN(ARV1, ARV2, ARV3, ICA1, ICA2, ICA3)
) PivotData
ORDER BY InvoiceNumber
The two CASE expressions employ the same logic. Only the value in t5.WTCode is different. Since that column is unlikely to have the value ARV1 and ICA1 at the same time you'll always get a NULL in at least one of the computed columns in any row.
You are putting different case statements for 2 different values. Therefore for each row one of them would be valid and other one null, which is what you are getting.
This is how i resolved my issue, thanks to everyone for the help
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
SET #cols = STUFF((SELECT distinct ',' + QUOTENAME(t5.WTCode)
FROM INV5 T5
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT Factura, Fecha, SN, TotalFacturaSinImpuesto, Total$Descuento, Total$Impuesto, Total$Retenciones, Total$Factura, CostoTotal$Factura, Margen$Factura, Costo$PromedioInventario, Margen$Inventario, NombreVendedor, ' + #cols + ' from
(
select T0.DocNum [Factura], T0.DocDate [Fecha], T0.CardName [SN],SUM(T1.LineTotal) [TotalFacturaSinImpuesto], T0.DiscSum [Total$Descuento], T0.VatSum [Total$Impuesto],(T0.WTSum*-1) [Total$Retenciones],t5.WTCode [CodigoRetencion],t5.U_Ret_ML [CantidadRetenida],T0.DocTotal [Total$Factura],SUM(T1.GrossBuyPr*T1.Quantity) [CostoTotal$Factura], T0.GrosProfit [Margen$Factura],SUM(T1.Quantity*T3.AvgPrice) [Costo$PromedioInventario],(SUM(T1.LineTotal*T1.Quantity))-(SUM(T1.Quantity*T3.AvgPrice)) [Margen$Inventario], T4.SlpName [NombreVendedor]
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T4 ON T0.SlpCode = T4.SlpCode
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode
INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
INNER JOIN INV5 T5 ON T5.AbsEntry = T0.DocEntry
WHERE T1.WhsCode = T3.WhsCode
GROUP BY T0.DocNum,T0.DocDate, T0.CardName,T0.BaseAmnt, T0.DiscSum, T0.VatSum, T0.WTSum,T0.DocTotal, T0.GrosProfit, T4.SlpName,T5.WTCODE,T5.U_RET_ML
) x
pivot
(
sum(CantidadRetenida)
for CodigoRetencion in (' + #cols + ')
) p '
execute(#query)

SQL Server get Max record

I have the following
(SELECT
ts.PID,
tt.ID,
tt.ItemOrder,
tt.DisplayVal as Task,
tt.Responsible as ResParty,
tt.DaysDue,
DATEADD(day, tt.DaysDue, PgStartDate) as DueDate,
ts.spate As ActualDate ,
ps.PgStartDate as ProgramDate
FROM tblTimeline tt
CROSS JOIN tblSers ts
JOIN PG ps ON ps.PID = ts.PId
WHERE tt.ID = 2
AND ps.cancelled = 0 )
t1
INNER JOIN(
SELECT
ts.PID,
max(DATEADD(day, tt.DaysDue, PgStartDate)) as DueDate
FROM tblTimeline tt
CROSS JOIN tblSers ts
JOIN PG ps on ps.PID = ts.PId
WHERE tt.ID = 2
AND ps.cancelled = 0)
t2
ON t1.PID = t2.PID
AND t1.DueDate = t2.DueDate
In t1, what I have is duplicates for PID, DueDate for a given row.
I like to get the full rows for each PID, I like to show the row that has the Max DueDate.
I get incorrect syntax near t1, t2
It seems you are missing a GROUP BY as well as the start of your initial SELECT statement:
SELECT * --missing this
FROM --missing this
(
select ts.PID,
tt.ID,
tt.ItemOrder,
tt.DisplayVal as Task,
tt.Responsible as ResParty,
tt.DaysDue,
DATEADD(day, tt.DaysDue, PgStartDate) as DueDate,
ts.spate As ActualDate ,
ps.PgStartDate as ProgramDate
from tblTimeline tt
cross join tblSers ts
join PG ps on ps.PID = ts.PId
where tt.ID = 2 and ps.cancelled = 0
) t1
inner join
(
select ts.PID,
max(DATEADD(day, tt.DaysDue, PgStartDate)) as DueDate
from tblTimeline tt cross join tblSers ts
join PG ps
on ps.PID = ts.PId
where tt.ID = 2 and ps.cancelled = 0
GROUP BY ts.PID --missing this
) t2
on t1.PID = t2.PID
and t1.DueDate = t2.DueDate
You're missing Group By.
select ts.PID, max(DATEADD(day, tt.DaysDue, PgStartDate)) as DueDate
from tblTimeline tt cross join tblSers ts
join PG ps on ps.PID = ts.PId
where tt.ID = 2 and ps.cancelled = 0
group by ts.PID