Microsoft Sql Server Management Studio invalid in the select list - sql-server

I was trying to search from my table and use group by but I was receiving error while executing the query. below is my query and the error I am getting:
select * from loans where ac_no='100001' group by ac_no, branch
and the error:
Msg 8120, Level 16, State 1, Line 1
Column 'loans.gl_no' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Please, what am I doing wrong

If you are trying to aggregate something...
select ac_no, branch, count(*)
from loans
where ac_no = '100001'
group by
ac_no
, branch
If you are just trying to select rows, you do not need group by
select *
from loans
where ac_no = '100001'
If you are trying to select distinct rows...
select distinct *
from loans
where ac_no = '100001'

Related

SQL Server 2016 virtual tables - Incorrect syntax 'AS'

Assuming a table level with columns level_id and level_name
I have a virtual table in my SQL Server 2016 query:
This is not a real query - I've simplified it as much as possible to illustrate the error
SELECT
LEVEL .level_id,
LEVEL .level_name
FROM
LEVEL,
((SELECT
LEVEL_ID AS lev_sum_level_id
FROM
LEVEL
GROUP BY
level_id) AS lev_sum
JOIN
(SELECT
LEVEL_ID AS lev_det_level_id
FROM
LEVEL
GROUP BY
level_id) AS lev_det ON (lev_sum_level_id = lev_det_level_id)
) AS totals
The syntax error is on the line AS totals.
Msg 156, Level 15, State 1, Line 35
Incorrect syntax near the keyword 'AS'
Why is SQL Server not allowing this syntax? Seems fine if I simplify the virtual table query. Postgres allows it as-is
I think you can try to add a select so that you can complete the syntax of the statement
Please try
SELECT
T1.MiscID,
T2.lev_sum_level_id
FROM
Miscs AS T1,
(
SELECT * FROM
(
SELECT
MiscID AS lev_sum_level_id
FROM
Miscs
GROUP BY
MiscID
) AS lev_sum
JOIN
(
SELECT
MiscID AS lev_det_level_id
FROM
Miscs
GROUP BY
MiscID
) AS lev_det
ON( lev_sum_level_id = lev_det_level_id )
) AS T2

COUNT(1) in DB2

I have query where there is COUNT(1) in select statement.
I want to know what does it return. COUNT(*) will return the number of rows but COUNT(1) I have no idea. I tried to execute one statement in DB2 but got error saying COLUMN OR EXPRESSION IN THE SELECT LIST IS NOT VALID.
Post your SQL statement.
I suspect you have something like
select customer, count(1)
from salesHistory
In which case, DB2 isn't complaining about count(1) which is perfectly valid; but it's complaining because you've got a aggregate function in the select list along with a non-aggregate column. In order to do that, you have to include a GROUP BY clause.
select customer, count(1)
from salesHistory
group by customer

Why i get error when want insert to table in sql server?

I'm beginner in write query,i write this query for insert any data to table:
insert into ScoreTable (UserID,Score,PhoneNumber,date_)
select id,sum(Duration) / 60 ,phoneNumber,Date_ from CDR p
where p.phoneNumber=phoneNumber
but when i execute the that query,i get this error:
Msg 8120, Level 16, State 1, Line 27
Column 'CDR.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can i solve that problem?
Your issue is with the select statement. Run that independently and you will get the same issue. Because you're aggregating (SUM) you require a group by clause. Give this a go;
SELECT p.id
,sum(p.Duration) / 60
,p.phoneNumber
,p.Date_
FROM CDR p
WHERE p.phoneNumber = phoneNumber
GROUP BY p.id, p.phoneNumber, p.Date
Also remember to use your table aliases in the queries to avoid ambiguity.

GROUP BY date not datetime in SQL Server

I am trying to write a query to count every date in my database as you can see here :
SELECT
[SubmitDateTime],
COUNT(*)
FROM
[ParkingDB].[dbo].[Traffic]
GROUP BY
submitdatetime
The result is :
I think SQL Server is grouping my date based on date+time and it's my problem, but in fact I need to group them based on date. I use this type of query :
SELECT
[SubmitDateTime],
COUNT(*)
FROM
[ParkingDB].[dbo].[Traffic]
GROUP BY
CAST(myDateTime AS DATE)
But it doesn't work. I'm getting this error:
Msg 8120, Level 16, State 1, Line 3 Column
'ParkingDB.dbo.Traffic.SubmitDateTime' is invalid in the select list
because it is not contained in either an aggregate function or the
GROUP BY clause.
You also need to modify the columns in your SELECT statement:
SELECT
CAST([SubmitDateTime] AS DATE),
COUNT(*)
FROM [ParkingDB].[dbo].[Traffic]
GROUP BY
CAST([SubmitDateTime] AS DATE)
When using GROUP BY clause, all non-aggregated columns in the SELECT statement must appear in the GROUP BY clause.
Can you try the query below?
SELECT
CAST(myDateTime AS DATE) [SubmitDateTime],
COUNT(*)
FROM [ParkingDB].[dbo].[Traffic]
GROUP BY
CAST(myDateTime AS DATE)

How to Write Trigger(on after insert) to Delete all Rows in Table Except Last Two Rows?

I want a Sql Script Trigger on after insert, Which can Delete all Rows in table except Last Two Rows.
And I Want Similar Code in Entity linq too Please...
I know my question is cheap for you experts but i really do not know how to code it...
So help me please?
I Use This Code But Has error in syntax :
select * from TblGold where id not in (
select id from TblGold
order by id desc
limit 2
)
The Error Is :
Msg 102, Level 15, State 1, Line 4 Incorrect syntax near 'limit'.
By the way, I know how to write a trigger but do not know how to Delete All Records Except 2 Last record.
And nither Linq Codes.
WITH cte
AS
(
SELECT ROW_NUMBER() OVER(ORDER BY Id DESC) AS RowNumber, *
FROM TblGold
)
DELETE TblGold
FROM
TblGold T
INNER JOIN cte ON cte.Id = T.Id
WHERE RowNumber > 2

Resources