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)
Related
I am trying to get available balance on last(max) date. I am trying to write below query but it is showing error.
select ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE,MAX(LAST_ACTIVITY_DATE)
from ACCOUNT
group by CUST_ID;
Column 'ACCOUNT.ACCOUNT_ID' is invalid in the select list because it
is not contained in either an aggregate function or the GROUP BY
clause.
I am new to sql. Can anyone let me know where I am wrong in this query?
Any column not having a calculation/function on it must be in the GROUP BY clause.
select ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE,MAX(LAST_ACTIVITY_DATE)
from ACCOUNT
group by ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE;
If you're wanting the most recent row for each customer, think ROW_NUMBER(), not GROUP BY:
;With Numbered as (
select *,ROW_NUMBER() OVER (
PARTITION BY CUST_ID
ORDER BY LAST_ACTIVITY_DATE desc) rn
from Account
)
select ACCOUNT_ID,AVAIL_BALANCE,OPEN_DATE,LAST_ACTIVITY_DATE
from Numbered
where rn=1
I think you want to select one records having max(LAST_ACTIVITY_DATE) for each CUST_ID.
For this you can use TOP 1 WITH TIES like following.
SELECT TOP 1 WITH TIES account_id,
avail_balance,
open_date,
last_activity_date
FROM account
ORDER BY Row_number()
OVER (
partition BY cust_id
ORDER BY last_activity_date DESC)
Issue with your query is, you can't select non aggregated column in select if you don't specify those columns in group by
If you want to get the max activity date for a customer then your query should be as below
select CUST_ID, MAX(LAST_ACTIVITY_DATE)
from ACCOUNT
group by CUST_ID;
You can't select any other column which is not in the group by clause. The error message also giving the same message.
with query(CUST_ID, LAST_ACTIVITY_DATE) as
(
select
CUST_ID,
MAX(LAST_ACTIVITY_DATE) as LAST_ACTIVITY_DATE
from ACCOUNT
group by CUST_ID
)
select
a.ACCOUNT_ID,
a.AVAIL_BALANCE,
a.OPEN_DATE,
a.LAST_ACTIVITY_DATE
from ACCOUNT as a
inner join query as q
on a.CUST_ID = q.CUST_ID
and a.LAST_ACTIVITY_DATE = q.LAST_ACTIVITY_DATE
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'
In my table I have three columns amount,date,memberID. Now I want to get Latest amount inserted in to the table and the sum of whole amount inserted so far.
My Query was like this
SELECT amount , SUM(amount) as TotalAmount FROM [Transactions]
WHERE memberid = 1629 Order By Date DESC
But this throws an error like this
Msg 8120, Level 16, State 1, Line 1
Column 'Transactions.amount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone point out what I am doing wrong here?
SELECT amount AS LatestAmount,
(SELECT SUM(amount) FROM [Transactions]) AS TotalAmount
FROM [Transactions]
WHERE date = (SELECT MAX(date) FROM [Transactions])
Note that in the event of a tie for more than one latest amount, the above query would produce one record for each tying transaction. If you want only one result, and you are using SQL Server 2008 or later, you can use TOP(1) to limit to a single result:
SELECT TOP(1) amount AS LatestAmount,
(SELECT SUM(amount) FROM [Transactions]) AS TotalAmount
FROM [Transactions]
WHERE date = (SELECT MAX(date) FROM [Transactions])
Just Add Group by clause
SELECT amount , SUM(amount) as TotalAmount FROM [Transactions]
GROUP BY amount WHERE memberid = 1629 Order By Date DESC
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
This is my Query:
select top 60 * from ABC_Sessions (nolock)
where EntryDate > '06-22-2012 23:59:59'
GROUP BY TargetedID
then it gives me this error:
Msg 8120, Level 16, State 1, Line 1 Column 'BI_Sessions.SessionID' is
invalid in the select list because it is not contained in either an
aggregate function or the GROUP BY clause.
The * in your query expands to all the columns in your table.
select top 60 TargetedID, SessionID, ...
from ABC_Sessions (nolock)
where EntryDate > '06-22-2012 23:59:59'
GROUP BY TargetedID
As the error message states, it's not valid to select SessionID without an aggregate function (e.g. MAX).
This would work:
select top 60 TargetedID, MAX(SessionID) AS MaxSessionID
from ABC_Sessions (nolock)
where EntryDate > '06-22-2012 23:59:59'
GROUP BY TargetedID
You used star * but not group by columns from abc_sessions table.
Using GROUP BY without any aggregate function looks strange for me.
What would you like to see?
It's a matter of theory: as the error says, you're not allowed to SELECT something that you have not at your disposal.
So, when you use a simple SELECT / FROM / WHERE query, everything you have in your FROM tables is OK for you to select.
But if you use a GROUP BY, you're narrowing your view: anything you don't use in your GROUP BY is discarded (think it twice, it makes no sense to select data from several lines when you explicitly ask to group those lines).
Maybe you're confusing GROUP BY and ORDER BY?
The reason why you are getting this error is because you are not using either sum/average/count/max/min or any similar functions in the select statement of yours. Once you use any of them, you won't get this error. ex :
select top 60 TargetedID, MAX(EntryDate)
from ABC_Sessions (nolock)
where EntryDate > '06-22-2012 23:59:59'
GROUP BY TargetedID