I have this sql query where I want to retrieve the sum of the amount of sales in August 2005. Whenever I run this, I receive the error "Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause". How can I fix this so it displays all columns and display the sum in my repsales.amount column?
SELECT *, SUM(repsales.amount) AS amount
FROM repsales
WHERE repsales.saledate BETWEEN '2005-08-01' AND '2005-08-31'
You can use window functions:
SELECT rs.*, SUM(rs.amount) OVER () AS total_amount
FROM repsales rs
WHERE rs.saledate BETWEEN '2005-08-01' AND '2005-08-31';
My answer is using SUM as an analytic/window function. This means that each row will be assigned a value, as contrasted to using SUM with GROUP BY, where an aggregation of rows takes place.
Using SUM(amount) OVER () defines the window to be the entire table. That is, the sum will be the total sum of the amount.
Another alternative solution is to state the column names one by one in the select and groupby statements. I.E. the example below groups by the data based department and category:
select rs.department,rs.category,sum(rs.amount) as SumAmount
from repsales rs
where rs.saledate BETWEEN '2005-08-01' AND '2005-08-31';
group by rs.department, rs.category
If you need all columns, you can save the resultset of the query above in a temp table (select ... into #temp from ...) and use it like:
select rs.*,t.SumSmount
from repsales rs
inner join #temp t on t.department=rs.department and t.category=rs.category
insert the sum of amount into a temp table with the unique value columns dept,category etc ...
Select rs.salespersonID, rs.OtherUniqueColumes, Sum(rs.amount) AS Amout
INTO #tempSales FROM repsales
WHERE repsales.saledate BETWEEN '2005-08-01' AND '2005-08-31'
Group By rs.salespersonID, rs.OtherUniqueColumes
join the temp table with the original table to get all other columns
SELECT rs.*, t.Amount
FROM repSales rs INNER JOIN #tempSales t on rs.salespersonID = t.salespersonID AND rs.OtherUniqueColumes = t.OtherUniqueColumes
DROP TABLE #tempSales
Related
I want to do a count grouping by the first column but omitting the others columns in the group by. Let me explain:
I have a table with those columns
So, what I want to get is a new column with the work orders total by Instrument, something like this:
How can I do that? Because if I do a count like this:
SELECT INSTRUMENT, WORKORDER, DATE, COUNT(*)
FROM TABLE1
GROUP BY INSTRUMENT, WORKORDER, DATE;
I get this:
Just use a window function:
select t.*,
count(*) over (partition by instrument) as instrument_count
from table1 t;
Although answer given by Gordon is perfect but there is also another option by using group by and subquery. You can add date column to this query as well
SELECT * FROM
(
SELECT A.INSTRUMENT, B.TOTAL_COUNT_BY_INSTRUMENT
FROM work_order A,
(SELECT COUNT(1) AS TOTAL_COUNT_BY_INSTRUMENT,
INSTRUMENT
FROM WORK_ORDER
GROUP BY INSTRUMENT
) B
WHERE A.INSTRUMENT = B.instrument);
I want to get numbers of rows in a table according to certain criteria.
Please see the below table:-
Herein I want to get numbers of rows according to Column StationTo.
I want to get numbers of rows of each StationTo entries.
You could group by the StationTo and use the aggregate count(*) function:
SELECT StationTo, COUNT(*)
FROM mytable
GROUP BY StationTo
EDIT:
If you just want the number of rows for a single StationTo, you could use a where clause:
SELECT COUNT(*)
FROM mytable
WHERE StationTo = 'P11004400000'
Hi have you master table for stationTo records?
select s.stationto, count(data.*) from stationtomaster
left join data on data.stationto=stationtomaster.stationto
group by s.stationto
Select StationTo,Date, count(*) from table group by StationTo, Datemeaning all the stationTo having rows display their count.
or select count(distinct StationTo) from table or Select count(*) from table where stationTo='yourvalue'
I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1
I need to group by more than one columns but in special case:
I have a table of (Payment_Type,Year,TotalMoney)
I need to get sum of total grouping by payment_type(cash or credit) only and I need to select year in my query how can I do it? my query is:
select Payment_Type,Year,SUM(TotalMoney)
from table
where 1=1
group by Payment_Type,Year
I get an error message as:
Year is not contained in either an aggregate function or the GROUP BY clause
select Payment_Type,Year(YourDateColumn),SUM(TotalMoney)
from table
group by Payment_Type,Year(YourDateColumn)
if your column is named year then
select Payment_Type,[Year],SUM(TotalMoney)
from table
group by Payment_Type,[Year]
When I try to use the following SELECT statement:
SELECT [lots of columns]
FROM Client, Customer, Document, Group
WHERE [some conditions]
GROUP BY Group.id
SQL Server complains that the columns I selected are not part of the GROUP BY statement nor an aggregate function. Am I using GROUP BY wrong? What should I be using instead?
To return all single occurences of a group by field, together with associated field values, write a query like:
select group_field,
max(other_field1),
max(other_field2),
...
from mytable1
join mytable2 on ...
group by group_field
having count(*) = 1;
Yes, you are using GROUP BY incorrectly. The point of using GROUP BY is to use aggregate functions. If you have no aggregrate functions you probably want SELECT DISTINCT instead.
SELECT DISTINCT
col1,
col2,
-- etc
coln
FROM Client
JOIN Customer ON ...
JOIN Document ON ...
JOIN [Group] ON ...
WHERE ...
My first guess would be that the problem is that you have table called Group, which I believe is a reserved word in SQL. Try wrapping the Group name with ' '
You want to group by all columns you are selecting that is not in an aggregate funcion.
SELECT ProductName, ProductCategory, SUM(ProductAmount)
FROM Products
GROUP BY ProductName, ProductCategory
This will give you a disticnt result of Product names and categories with the sum total of product amount in all aggregate child records for that group.