PIVOT not working Incorrect syntax near ')' - sql

T-SQL code:
SELECT iCarrierInvoiceDetailsID, [1],[2],[3]
FROM [GroundEDI].[dbo].[tblCarrierInvoiceDetails]
PIVOT(MAX(dTotalCharge) FOR iCarrierInvoiceHeaderID IN ([1],[2],[3]))AS P
Error:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ')'.
Any idea why I am getting this error?

It looks like you are trying to directly select the pivot columns from the table itself and not the pivot. You will need to do something like this:
SELECT p.[1],p.[2],p.[3]
FROM
(SELECT iCarrierInvoiceHeaderID
,dTotalCharge
FROM [GroundEDI].[dbo].[tblCarrierInvoiceDetails]) t
PIVOT(MAX(dTotalCharge) FOR iCarrierInvoiceHeaderID IN ([1],[2],[3])
)AS P;

Related

SQL Cross Apply Select with Table-Valued Function Error

I have the following query and receive the error when I check syntax. I don't see what's wrong. The parameter and field are the same data type.
select
p.ID
from dbo.PART as p
cross apply dbo.FN_SEC_BOM_Listing(p.ID) as bl
Error:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '.'.

Error messages when I execute SQL Query in the Where Statement

I have created a SQL query using SQL Server Management Studio,
But, I faced errors in the Where statement.
Here WHERE clause of the SQL code:
where
(case when 'All' in (select Items from CDB.dbo.Split (#a,','))
then innt.code **IS** NOT NULL
else innt.code in (select Items from CDB.dbo.Split (#a,',')) end) and
I get these error messages:
Msg 156, Level 15, State 1, Line 90
Incorrect syntax near the keyword 'IS'.
Msg 156, Level 15, State 1, Line 92
Incorrect syntax near the keyword 'and'.
Can you guys please help me solve this?
Booleans are not a type in SQL Server. So a case cannot return a boolean expression.
So, just use regular comparisons
where ('All' in (select Items from CDB.dbo.Split(#a, ',') and innt.code IS NOT NULL) or
(innt.code in (select Items from CDB.dbo.Split(#a, ',') )

Need a short simple aggregated function SQL query

I have a database base with a table named "extract1" with a column named "contactsphonenumber". I am trying to find what specific phone number appears the most.
Attempt 1:
SELECT MAX(COUNT(contactscellphone))
FROM extract1
GROUP BY contactscellphone;
Attempt 2:
SELECT MAX(contactscellphone)
FROM extract1
GROUP BY contactscellphone
(
SELECT COUNT(contactscellphone)
FROM extract1
GROUP BY contactscellphone
);
Attempt 1 Error:
Msg 130, Level 15, State 1, Line 1 Cannot perform an aggregate
function on an expression containing an aggregate or a subquery.
Attemp 2 Error:
Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword
'select'. Msg 102, Level 15, State 1, Line 8 Incorrect syntax near
')'.
Use TOP 1 and ORDER BY:
select top (1) contactscellphone
from extract1g
group by contactscellphone
order by count(*) desc;
If you want all top values when there are ties, use top (1) with ties.

i try to combine rows in one column but there was an error in my syntax

select
Elm_EmployeeId as 'Badge',
Left(T_EmployeeLeave , Len(T_EmployeeLeave) - 1) As 'a'
from
(select
E2.Elm_EmployeeId as 'Badge2',
(select Elm_EmployeeId
from T_EmployeeLeave E1)
from
T_EmployeeLeave E2)
Error is:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ')'.
1.you select Elm_EmployeeId and T_EmployeeLeave from a subquery but in the subquery u don't have these two columns what you have is badge2 and a non_named column
the select Elm_EmployeeId from T_EmployeeLeave E1 is meaningless
the query is miserable i can't even tell what excatly you want

Incorrect syntax near ',' SQL Server Error

I am having the following error on my SQL Server Query I don't know how to overcome it Because I tried my best Please help me getting out of it:
CREATE TABLE d3 as SELECT sessionnumber, sessioncount, LEFT(timespent, 1) , COUNT
as cnt
FROM clusters
GROUP BY 1, 2, 3
The following error is generated:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Please help me! Thanks in advance
This is not SQL Server syntax. You want select into:
SELECT sessionnumber, sessioncount, LEFT(timespent, 1) as TimeSpentCode, COUNT(*) as cnt
into d3
FROM clusters
GROUP BY sessionnumber, sessioncount, LEFT(timespent, 1);
All the columns need to have names. So I added one for the third column.
And, group by does not accept positional indicators in SQL Server, so I replaced them with the appropriate expressions.
You have the word count instead of count(something).