SSRS 2008: How to count employees on each year - ssrs-2008

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.

Related

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

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.

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)

Count distinct loop in sql

I am trying to pull unique active users before a date.
So specifically, I have a date range (let's say August - November) where I want to know the cumulative unique active users on or before a day within a month.
So, the pseudocode would look something like this:
SELECT COUNT(DISTINCT USERS) FROM USER_DB
WHERE
Month = [loop through months 8-11]
AND
DAY <= [day in loop of 1:31]
The output I desire is something Like this
step-by-step demo: db<>fiddle
SELECT
mydate,
SUM( -- 3
COUNT(DISTINCT username) -- 1, 2
) OVER (ORDER BY mydate) -- 3
FROM t
GROUP BY mydate -- 2
GROUP BY your date and count the users
Because you don't want to count ALL user accesses, but only one access per user and day, you need to add the DISTINCT
This is a window function. This one aggregates all counts which where previously done cumulatively.
If you want to get unique user over ALL days (count a user only on its first access) you can filter the users with a DISTINCT ON clause first:
demo: db<>fiddle
SELECT DISTINCT ON (username)
*
FROM t
ORDER BY username, mydate
This yields:
SELECT
mydate,
SUM(
COUNT(*)
) OVER (ORDER BY mydate)
FROM (
SELECT DISTINCT ON (username)
*
FROM t
ORDER BY username, mydate
) s
GROUP BY mydate

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.

TSQL selecting distinct based on the highest date

Our database has a bunch of records that have the same invoice number, but have different dates and different notes.
so you might have something like
invoice date notes
3622 1/3/2010 some notes
3622 9/12/2010 some different notes
3622 9/29/1010 Some more notes
4212 9/1/2009 notes
4212 10/10/2010 different notes
I need to select the distinct invoice numbers, dates and notes. for the record with the most recent date.
so my result should contain just
3622 9/29/1010 Some more notes
4212 10/10/2010 different notes
how is it possible to do this?
Thanks!
Use analytical functions :
WITH TT AS (
SELECT invoice, date, notes, RANK() OVER(PARTITION BY invoice ORDER BY date DESC) AS R
FROM table
)
SELECT invoice, date, notes
FROM TT
WHERE R = 1;
select invoice, date, notes
from table
inner join (select invoice, max(date) as date from table group by invoice) as max_date_table
on table.invoice = max_date_table.invoice and table.date = max_date_table.date
Try:
SELECT I.*
FROM MyInvoice AS I
INNER JOIN
(SELECT Invoice, MAX([Date]) AS MaxDate
FROM MyInvoice
GROUP BY Invoice
) AS M ON I.Date = M.MaxDate
AND I.Invoice = M.Invoice