BigQuery Syntax Error : SELECT list expression references column UnitPrice which is neither grouped nor aggregated at [5:1] - group-by

SELECT
EXTRACT(YEAR FROM Date) as Year,
EXTRACT(MONTH FROM Date) as Month,
ProductId,
UnitPrice,
Quantity,
(UnitPrice * Quantity) AS Total_price,
FROM sales.sales_info
GROUP BY
Year,
Month,
ProductId
ORDER BY
Year,
Month,
ProductId;

Which aggregated function are you using? Do not see any aggregated function in select.
Also in group by UnitPrice and Quantity column are missing.

Related

Query to group by for last n number of days

i have a table of employees with empnum,name, age , date of join.
I need to find out count of records for the last 90 days , group by age.
How can a write a query in TSQL.
SELECT age , count(*)
from table1
where date >= DATEADD(dd,-90,GETUTCDATE())
Group by age
You can apply filter, followed by group by.
SELECT age, COUNT(*) as CountOfEmployees
FROM employees
WHERE dateOfJoin < CAST(DATEADD(dd,-90,GETDATE()) AS DATE)
group by age
It would be incorrect to save AGE, usually, it is something like DateOfBirth and you calculate the age from there.
SELECT DATEDIFF(year, DateOfBirth, getdate()), COUNT(*) as CountOfEmployees
FROM employees
WHERE CAST(DATEADD(dd,-90,GETDATE()) AS DATE) < dateOfJoin
group by DATEDIFF(year, DateOfBirth, getdate())

Output all values of group even if null for each day of the year postgresql

my problem is the following:
I have a movement table that documents whenever a car is moved from one station to another or a station gets supplied with more cars. The tables entries are: id, productId, quantity, updatedAt, date, locationId, createdAt.
I want so display the sum of the cars available for each day of the year, grouped by productId and locationId.
with you as(
SELECT "productId"
, "locationId"
, "date" as start_date
, SUM(SUM("Movement"."quantity")) OVER (PARTITION BY "productId", "locationId" ORDER BY "date") as schau
, "quantity"
, LEAD ("date") OVER (PARTITION BY "productId", "locationId" ORDER BY "date") as end_date
FROM "Movement"
GROUP BY 1,2,3,5
Order BY "date"),
calendar as (
select date '2022-01-01' + (n || ' days')::interval calendar_date
from generate_series(0, 365) n
)
SELECT * FROM calendar
left join you on calendar.calendar_date between start_date and end_date
The SumSum delivers the rolling sum of the products available. Check. The LEAD function allows to define periods per movement which are necessary in order to match the calendar I get by using the generate_series function. Like this I get a date for every day of the year. Check.
The only problem I have, is that my output won´t show me products that don´t have an entry by the time of the day. Is there any way to ALWAYS show all members of a group even if there is no entry for them? In my case I want to show all possible product and locationId s even if for this date there is no entry in my table? (I want to output 0 in that case)

How to include three or more aggregators in a sql query?

I have a table called retail which stores items and their price along with date of purchase. I want to find out total monthly count of unique items sold.
This is the sql query I tried
select date_trunc('month', date) as month, sum(count(distinct(items))) as net_result from retail group by month order by date;
But I get the following error
ERROR: aggregate function calls cannot be nested
Now I searched for similar stackoverflow posts one of which is postgres aggregate function calls may not be nested and but I am unable to replicate it to create the correct sql query.
What am I doing wrong?
From your description, it doesn't seem like you need to nest the aggregate functions, the count(distinct item) construction will give you a count of distinct items sold, like so:
select date_trunc('month', date) as month
, count(distinct items) as unique_items_sold
, count(items) as total_items_sold
from retail
group by "month"
order by "month" ;
If you had a column called item_count (say if there was row in the table for each item sold, but a sale might include, say, three widgets)
select date_trunc('month', date) as month
, count(distinct items) as unique_items_sold
, sum(item_count) as total_items_sold
from retail
group by "month"
order by "month" ;
Use subqueries:
Select month, sum(citems) as net_result
from
(select
date_trunc('month', date) as month,
count(distinct(items)) as citems
from
retail
group by month
order by date
)
I am suspect your group by statement will throw an Error because your month column are condition column and you cannot put in the same level in your query so put your full expression instead.
select
month,
sum(disct_item) as net_results
from
(select
date_trunc('month', date) as month,
count(distinct items) as disct_item
from
retail
group by
date_trunc('month', date)
order by
date) as tbl
group by
month;
You cannot make nested aggregate so you wrap first count to subquery and after that in outer you make sum to do the operation.

Google BigQuery: Select and group by "yyyy-mm" from date field ("yyyy-mm-dd") or timestamp

I would like to group by "yyyy-mm" from a date field ("yyyy-mm-dd") or timestamp field so that I can pull and group transactional data over multiple years without having to pull separate queries grouping by month for each year.
SELECT
CONCAT(STRING(YEAR(timestamp)),'-',RIGHT(STRING(100 + MONTH(timestamp)), 2)) AS yyyymm,
<any aggregations here>
FROM YourTable
GROUP BY 1
another option:
SELECT
STRFTIME_UTC_USEC(timestamp, "%Y-%m") AS yyyymm,
<any aggregations here>
FROM YourTable
GROUP BY 1
both versions should work with timestamp or date
You can use the following for Standard SQL:
SELECT concat(cast(format_date("%E4Y", cast(current_date() as date)) as string),'-',cast(format_date("%m", cast(current_date() as date)) as string)) as yyyymm, <other aggregations>
FROM <YourTable>
GROUP BY 1;
Just replace the current_date() with your column name containing the timestamp.

SSRS 2008: How to count employees on each year

I have a problem where I need to count the number of employees and group on selected years. The years comes from Parameters.
E.g. if i via parameters select 2010-2013, I want to count the number of employees on Dec. 31 on those selected years. The problem is that I am limited to using Fecth from CRM 2013.
On the employees there is employment date and end date but I can’t group on this because it doesn’t tell me how many employees there are on a certain selected year. I also need to make different charts on number of employees and grouping on year.
It's not difficult to use an IIF and calculate the number of employees and put into a specific cell, but I need it to be dynamically.
EDIT: Some more thoughts...
If I use a multivalued parameter and set the available values to the years 2010-12-31…2013-12-31 and on each year I have a selection where I calculate the number of employees. Then I only need to be able to group on this multivalued parameter. Is this possible?
I guest that for your task if you have employee which has end date for example 2011-06-04 than he should be included in report to 2011 year. If I'm right that you need SQL expression like this one:
With Employee AS
(
SELECT 'employee1' as Name, '2010-02-01' as StartDate, '2012-04-01' as EndDate UNION ALL
SELECT 'employee2' as Name, '2010-02-01' as StartDate, '2011-04-01' as EndDate UNION ALL
SELECT 'employee3' as Name, '2010-02-01' as StartDate, '2011-04-01' as EndDate UNION ALL
SELECT 'employee4' as Name, '2011-02-01' as StartDate, '2012-04-01' as EndDate UNION ALL
SELECT 'employee5' as Name, '2011-02-01' as StartDate, '2012-04-01' as EndDate UNION ALL
SELECT 'employee6' as Name, '2011-02-01' as StartDate, '2012-04-01' as EndDate UNION ALL
SELECT 'employee7' as Name, '2011-02-01' as StartDate, '2012-04-01' as EndDate UNION ALL
SELECT 'employee8' as Name, '2012-02-01' as StartDate, '2012-04-01' as EndDate UNION ALL
SELECT 'employee9' as Name, '2012-02-01' as StartDate, '2012-04-01' as EndDate
)
SELECT Employee2.Name AS EmployeeName, YEAR(Employee.StartDate) AS EmployeeWorkedYear
FROM Employee
JOIN Employee as Employee2 ON YEAR(Employee2.EndDate) >= YEAR(Employee.StartDate) AND YEAR(Employee2.StartDate) <= YEAR(Employee.StartDate)
GROUP BY YEAR(Employee.StartDate), Employee2.Name
It will show employee name and year in which he worked. Then you should create dataset with such expression (stored procedure) and group by second column (year) and create grouping table, charts etc.