The multi part identifier could not be bound - sql-server

I am getting this error while running following query
SELECT
T2.[AcctCode],
T2.[AcctName],
T5.[Name],
SUM(T0.[Debit]) AS Actual,
SUM(T3.[DebLTotal]) AS Budget
FROM [dbo].[JDT1] T0
INNER JOIN [dbo].[OJDT] T1
ON T0.[TransId] = T1.[TransId]
INNER JOIN [dbo].[OACT] T2
ON T0.[Account] = T2.[AcctCode],
[dbo].[OBGT] T#
INNER JOIN [dbo].[BGT1] T4
ON T3.[AbsId] = T4.[BudgId]
INNER JOIN OBGS T5
ON T3.[Instance] = T5.[AbsId]
WHERE T2.[AcctName] LIKE '%Travel%'
AND T5.[Name] LIKE 'Main Budget 2015'
GROUP BY T2.[AcctCode], T2.[AcctName], T5.[Name]

Related

SQL Query: customer name including activity count

There's probably a very simple solution to this that I'm not seeing right now.
I need a script for an SQL report. I can get all customer names in a simple SELECT:
SELECT
t5.[Name] AS CustomerName
FROM
[T5] t5
WHERE
t5.Type IN (1, 2)
ORDER BY
t5.[Name]
This gives me an ordered list for the dropdown menu, to select the customer name. The result set looks like this:
CustomerName
------------
Customer 1 Name
Customer 2 Name
Customer 3 Name
...
But I don't just need the name, I need the number of related activities to that customer name inside the name string as well. For example, with 38 activities for that customer it should look like : "(38) Customer Name here"
The statement giving me the correct count for one particular customer (in this example, "Customer 5 Name") looks something like this, it needs a couple of joins to get there:
SELECT
COUNT(*)
FROM
[T1] t1
INNER JOIN
[T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN
[T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN
[T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN
[T5] t5 ON t5.[ID] = t4.[X]
WHERE
t5.[Name] = 'Customer 5 Name'
Now obviously I need to combine these somehow, to get the following result set:
CustomerName
-------------------
(a) Customer 1 Name
(b) Customer 2 Name
(c) Customer 3 Name
(d) Customer 4 Name
(e) Customer 5 Name
(f) Customer 6 Name
...
(a-f being the respective activity count for that customer)
Thanks for help!
WITH pre AS
(
SELECT t5.[Name] AS CustomerName, COUNT(*) AS ActivityCount
FROM [T1] t1
INNER JOIN [T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN [T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN [T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN [T5] t5 ON t5.[ID] = t4.[X]
WHERE t5.Type IN (1, 2) AND t5.[Name] = 'Customer 5 Name'
GROUP BY t5.Name
)
SELECT '(' + ActivityCount + ') ' + CustomerName
FROM pre
You should use group by
SELECT
t5.[Name], COUNT(*)
FROM
[T1] t1
INNER JOIN
[T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN
[T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN
[T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN
[T5] t5 ON t5.[ID] = t4.[X]
group by t5.[Name]
I Think this will give you the number with the customer name as per your requirement.
SELECT
'(' + CAST(COUNT(*) AS nvarchar(4) ) + ') ' + t5.[Name] as [CustomerName]
FROM
[T1] t1
INNER JOIN
[T2] t2 ON t2.[ID] = t1.[Activity]
INNER JOIN
[T3] t3 ON t3.[ID] = t2.[Username]
INNER JOIN
[T4] t4 ON t4.[ID] = t3.[ID]
INNER JOIN
[T5] t5 ON t5.[ID] = t4.[X]
group by t5.[Name]

SQL Server Select Not Returning All Rows

I have run into a very strange problem where a query is returning most rows but not all of them. If I add an addition condition to my where clause, the missing row appears.
Below is my query:
SELECT T1.Table1ID,
SUM(ISNULL([T4].Qty,0)) TotalQty
FROM dbo.Table1 (NOLOCK) T1
INNER JOIN [dbo].Table2 (NOLOCK) T2 ON T1.Table2ID = T2.Table2ID
INNER JOIN [dbo].Table3 (NOLOCK) T3 ON T2.DocTypeID = T3.DocTypeID
INNER JOIN dbo.Table4 (NOLOCK) T4 ON T4.Table1ID = T1.Table1ID
WHERE T3.is1 = 0
GROUP BY T1.Table1ID
ORDER BY T1.Table1ID
This returns all rows except for the one with T1.Table1ID = 185671. If I add it as a filter like such:
SELECT T1.Table1ID,
SUM(ISNULL([T4].Qty,0)) TotalQty
FROM dbo.Table1 (NOLOCK) T1
INNER JOIN [dbo].Table2 (NOLOCK) T2 ON T1.Table2ID = T2.Table2ID
INNER JOIN [dbo].Table3 (NOLOCK) T3 ON T2.DocTypeID = T3.DocTypeID
INNER JOIN dbo.Table4 (NOLOCK) T4 ON T4.Table1ID = T1.Table1ID
WHERE T3.is1 = 0
AND T1.Table1ID = 185671
GROUP BY T1.Table1ID
ORDER BY T1.Table1ID
Now the row shows. Any ideas?
EDIT: New query after removing NOLOCK hint.
SELECT T1.Table1ID,
SUM(ISNULL([T4].Qty,0)) TotalQty
FROM dbo.Table1 T1
INNER JOIN [dbo].Table2 T2 ON T1.Table2ID = T2.Table2ID
INNER JOIN [dbo].Table3 T3 ON T2.DocTypeID = T3.DocTypeID
INNER JOIN dbo.Table4 T4 ON T4.Table1ID = T1.Table1ID
WHERE T3.is1 = 0
AND T1.Table1ID = 185671
GROUP BY T1.Table1ID
ORDER BY T1.Table1ID

How to break sales query results by day

I have a sales query by date range, where the date range is defined by user input. I would like to divide the results by day. i.e.: say the user input the date range from 01/01/16 - 01/15/16, I would like the break the results for each day.
I'm using DATENAME(DD,T1.[DocDate]) to break it, and it is kind of working, but the results are no accurate. I figure I have to use the same break in the Returns subquery. Please see the full query below:
Thank you
SELECT
'2016' as 'Year',
t4.remarks as 'Department',
DATENAME(DD,T1.[DocDate]) as 'Day',
sum(t0.[quantity])-(ISNULL(p.quantity,0)) as 'Quantity',
sum(t0.linetotal - t0.linetotal*t1.discprcnt/100)-(ISNULL(p.total,0)) as 'Total',
sum(T0.[GrssProfit])-(ISNULL(p.profit,0)) as 'Profit $',
(sum(T0.[GrssProfit])-(ISNULL(p.profit,0)))/(sum(t0.linetotal - t0.linetotal*t1.discprcnt/100)-(ISNULL(p.total,0)))*100 as 'Profit%'
FROM INV1 T0 with (nolock)
INNER JOIN OINV T1 with (nolock) on t0.docentry = t1.docnum
INNER JOIN OSLP T2 with (nolock) on t0.SlpCode = t2.SlpCode
LEFT JOIN OHEM T3 with (nolock) on t0.slpcode = t3.SalesPrson
LEFT JOIN OUDP T4 with (nolock) on t3.dept = t4.Code
--BEGINS QUERY FOR THE RETURNS--
left join (select t9.name as 'dept',sum(t5.quantity) as 'quantity',sum(t5.linetotal - t5.linetotal*t6.discprcnt/100) as 'total',sum(t5.grssprofit) as 'profit'
from [dbo].[rin1] t5 with (nolock)
inner join orin t6 with (nolock) on t5.docentry = t6.docentry
INNER JOIN OSLP T7 with (nolock) on t5.SlpCode = t7.SlpCode
LEFT JOIN OHEM T8 with (nolock) on t5.slpcode = t8.SalesPrson
LEFT JOIN OUDP T9 with (nolock) on t8.dept = t9.Code
INNER JOIN OITM T10 with (nolock) on t5.itemcode = t10.itemcode
where t5.docdate between '[%1]' and '[%2]' and t10.invntitem = 'Y'
and (t5.linetotal - (t5.linetotal*t6.discprcnt/100)) <> '0'
group by t9.name) p on p.dept = t4.name
--ENDS QUERY FOR THE RETURNS--
WHERE t1.docdate between '[%1]' and '[%2]'
and t4.remarks is not null
and t4.remarks = 'perfume provider'
and (t0.linetotal - (t0.linetotal*t1.discprcnt/100)) <> '0'
group by DATENAME(DD,T1.[DocDate]),t4.remarks,p.quantity,p.total,p.profit
Instead of a sub-query for your returns (with the same kind of group by as your Invoices query), you should use a UNION instead. Your Group-By is fine, but like you mentioned, your subquery is going to result in bad data.
Any time you have distinct "starting points" for your data, a Union is the way to go.

What type of SQL join is it in which the inner-join is not directly followed by an ON clause?

What is the name of this type of join? I've looked all over! The query is inner-joining back-to-back and then specifying two ON clauses.
Bonus points: What is the benefit of joining this way?
SELECT
<some columns>
FROM
ProductTypes AS t0
INNER JOIN Table1 AS t1
INNER JOIN Table2 AS t2
ON t2.CodeId = t1.CodeId
AND t2.[Enabled] = 1
ON t1.ClassId = t0.ClassId
Still it will be INNER JOIN.
It will interpreted as
SELECT <some columns>
FROM producttypes AS t0
INNER JOIN table2 AS t2
ON t1.classid = t0.classid
INNER JOIN table1 AS t1
ON t2.codeid = t1.codeid
AND t2.[enabled] = 1
Compiler is smart enough to rearrange the JOIN order
Here is demo of what's happening internally. I have used my own table's with similar JOIN order
Your query JOIN order
SELECT
*
FROM
users AS t0
INNER JOIN products AS t1
INNER JOIN orders AS t2
ON t2.productid = t1.productid
AND t2.productid = 1
ON t2.uid = t0.uid
Execution Plan
The rearranged JOIN order
SELECT
*
FROM
users AS t0
INNER JOIN orders AS t2
ON t2.uid = t0.uid
INNER JOIN products AS t1
ON t2.productid = t1.productid
AND t2.productid = 1
Execution Plan
As you can see both has identical execution plan. So there wont be any difference
This is a form of doing nested joins. There's no purpose with all inner unless you want to do it for readability. It can be useful if you use left outer. Consider:
SELECT
<some columns>
FROM
ProductTypes AS t0
LEFT OUTER JOIN Table1 AS t1
INNER JOIN Table2 AS t2
ON t2.CodeId = t1.CodeId
AND t2.[Enabled] = 1
ON t1.ClassId = t0.ClassId
Now this does something. If you did it without putting the t1/t0 at the end the t1/t2 inner would basically negate the fact that t1/t0 is a left outer. So doing it this way lets you have t0 records with no t1 records (just like a normal left outer join), but will only show the t1 records that ALSO have a t2. The inner join is enforced at this lower level.
See also:
http://sqlity.net/en/1435/a-join-a-day-nested-joins/
SQL join format - nested inner joins

SubQueries in MSSQL

I am trying to compile a subquery in MSSQL 2012 to extract container volumes in import shipments using the following
SELECT TOP (100) PERCENT JobShipment_1.JS_UniqueConsignRef, JobContainer_1.JC_ContainerNum,
(SELECT dbo.JobPackLines.JL_ActualVolume
FROM dbo.JobPackLines INNER JOIN
dbo.JobShipment ON dbo.JobPackLines.JL_JS = dbo.JobShipment.JS_PK INNER JOIN
dbo.JobContainerPackPivot ON dbo.JobPackLines.JL_PK = dbo.JobContainerPackPivot.J6_JL INNER JOIN
dbo.JobContainer ON dbo.JobContainerPackPivot.J6_JC = dbo.JobContainer.JC_PK) AS Expr1
FROM dbo.JobConsol INNER JOIN
dbo.JobConShipLink ON dbo.JobConsol.JK_PK = dbo.JobConShipLink.JN_JK INNER JOIN
dbo.JobShipment AS JobShipment_1 ON dbo.JobConShipLink.JN_JS = JobShipment_1.JS_PK INNER JOIN
dbo.cvw_JobShipmentOrgs ON JobShipment_1.JS_PK = dbo.cvw_JobShipmentOrgs.JS_PK INNER JOIN
dbo.JobContainer AS JobContainer_1 ON dbo.JobConsol.JK_PK = JobContainer_1.JC_JK INNER JOIN
dbo.JobDeclaration ON JobShipment_1.JS_PK = dbo.JobDeclaration.JE_JS
But get the following error
"Error Message: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression."
I have looked ad nauseum on the web, but cannot find an answer, can anyone help?
Many thanks
Try this query
SELECT TOP (100) PERCENT JobShipment_1.JS_UniqueConsignRef, JobContainer_1.JC_ContainerNum,
(SELECT a.JL_ActualVolume
FROM dbo.JobPackLines a
INNER JOIN dbo.JobShipment b ONa.JL_JS = b.JS_PK
INNER JOIN dbo.JobContainerPackPivot c ON a.JL_PK = c.J6_JL
INNER JOIN dbo.JobContainer d ON c.J6_JC = d.JC_PK
where b.JS_PK=JobShipment_1.JS_PDK and d.JC_PK=JobContainer_1.JC_PK
) AS Expr1
FROM dbo.JobConsol
INNER JOIN dbo.JobConShipLink ON dbo.JobConsol.JK_PK = dbo.JobConShipLink.JN_JK
INNER JOIN dbo.JobShipment AS JobShipment_1 ON dbo.JobConShipLink.JN_JS = JobShipment_1.JS_PK
INNER JOIN dbo.cvw_JobShipmentOrgs ON JobShipment_1.JS_PK = dbo.cvw_JobShipmentOrgs.JS_PK
INNER JOIN dbo.JobContainer AS JobContainer_1 ON dbo.JobConsol.JK_PK = JobContainer_1.JC_JK
INNER JOIN dbo.JobDeclaration ON JobShipment_1.JS_PK = dbo.JobDeclaration.JE_JS
Please try below:
SELECT TOP (100) PERCENT JobShipment_1.JS_UniqueConsignRef, JobContainer_1.JC_ContainerNum,
(SELECT JPL.JL_ActualVolume
FROM dbo.JobPackLines AS JPL
INNER JOIN dbo.JobShipment AS JS ON JS.JS_PK = JPL.JL_JS
INNER JOIN dbo.JobContainerPackPivot AS JCP ON JCP.J6_JL = JPL.JL_PK
INNER JOIN dbo.JobContainer AS JC ON JC.JC_PK = JPL.J6_JC) AS Expr1
FROM dbo.JobConsol
INNER JOIN dbo.JobConShipLink ON dbo.JobConsol.JK_PK = dbo.JobConShipLink.JN_JK
INNER JOIN dbo.JobShipment AS JobShipment_1 ON dbo.JobConShipLink.JN_JS = JobShipment_1.JS_PK
INNER JOIN dbo.cvw_JobShipmentOrgs ON JobShipment_1.JS_PK = dbo.cvw_JobShipmentOrgs.JS_PK
INNER JOIN dbo.JobContainer AS JobContainer_1 ON dbo.JobConsol.JK_PK = JobContainer_1.JC_JK
INNER JOIN dbo.JobDeclaration ON JobShipment_1.JS_PK = dbo.JobDeclaration.JE_JS

Resources