Need a short simple aggregated function SQL query - sql

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.

Related

How to combine If statement with Substring command in SQL?

I have two types of entries in one column. One type starts with V2, the other starts with a digit. How do I write a query in SQL where I can extract different part of the string based on how it starts?
TextStringColumn
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE
I wrote
SELECT TextStringColumn, If(TextStringColumn like 'V2%',SUBSTRING(TextStringColumn ,10,7),SUBSTRING(TextStringColumn ,1,7)) As NumberCol
FROM TestTable
But I keep getting syntax errors.
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'If'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
The desired result will be
TextStringColumn NumberCol
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ 1000000
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE 2000308
You may use a CASE expression:
SELECT
CASE WHEN TextStringColumn LIKE 'V2%'
THEN SUBSTRING(TextStringColumn, 10, 7)
ELSE SUBSTRING(TextStringColumn, 1, 7) END AS NumberCol
FROM TestTable;
The above logic assumes the only two varieties of strings are those which start with V2, and those which do not start with V2.
If the strings are variable length ... consider a little JSON
Example
Declare #YourTable Table ([TextStringColumn] varchar(100)) Insert Into #YourTable Values
('V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ')
,('2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE')
Select A.*
,Val = case when [TextStringColumn] like 'V2%'
then JSON_VALUE(S,'$[2]')
else JSON_VALUE(S,'$[0]') end
From #YourTable A
Cross Apply ( values ( '["'+replace([TextStringColumn],'|','","')+'"]' ) ) B(S)
Returns
TextStringColumn Val
V2|T:GSK|1000000|S1:TES|S2:N/A|Q:24|S:0.5gx3|PD:2020-10-22|C:QQ 1000000
2000308|S1:BES|T:SKY|Q:16446G|BSI:BPKGVAXQOHZFWGE 2000308

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, ',') )

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

PIVOT not working Incorrect syntax near ')'

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;