How can I show zeros count? - google-data-studio

I am preparing a report with some data of places and the number of visits to these places by a list of staff. That is, each person has to visit a number of places in the week. I need to show the list of people, the place and count the total visits in the week.
I have two databases, the first one contains the person in charge and the place. And in the other one I have the history of visits where is the person, the week and the place.
However, I want to show the places that are still to be visited in a specific week. But as there is no data int the history database, Google Data Studio does not show anything, I need it to show me the place, week and zeros.
I tried to merge those two databases but I couldn't find solution. The week is numeric and the other are text.
Table 1: visits_history
Personal
Station
Week
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
MOVIL 1
30
Person1
MOVIL 1
30
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
Portal Américas
30
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Eldorado
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
MOVIL 3
31
Person1
MOVIL 3
31
Person1
Banderas P. Central
31
Person1
Banderas P. Central
31
Person1
Banderas P. Central
31
Person1
Banderas P. Central
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Person1
Portal Américas
31
Table 2: visits_to_do_per_week
Supervisor
Punto
Person1
Banderas P. Central
Person1
Centro Comercial Gran Plaza Soacha
Person1
Centro Comercial Hayuelos
Person1
Centro Comercial Milenio Plaza
Person1
Movil 1
Person1
Movil 2
Person1
Movil 3
Person1
Portal Américas
Person1
Portal Sur
Person1
Super Cade Bosa
Person1
Zona Franca
Expected Result:
person
station_asigment
visits in the week
Person1
Banderas P. Central
1
Person1
Centro Comercial Gran Plaza Soacha
0
Person1
Centro Comercial Hayuelos
0
Person1
Centro Comercial Milenio Plaza
0
Person1
Movil 1
1
Person1
Movil 2
1
Person1
Movil 3
2
Person1
Portal Américas
1
Person1
Portal Sur
2
Person1
Super Cade Bosa
3
Editable Data Set (Google Sheets)

Assuming that visted table be as:
week
person
visited
35
Alice
Paris
35
Bob
Paris
35
Bob
New York
36
Alice
Paris
and the task table be:
person
place
Alice
Paris
Alice
Berlin
Alice
Amsterdam
Bob
Paris
Bob
New York
Bob
Chicago
Claire
Los Angeles
Case 1
I assume that each week the same places have to be visited.
In Data studio create in the visited table a slider parameter week_select to select a week. Then create a field with
week - week_select
Create a filter on the table with that field equal zero.
Blend the datasets:
Add a table chart with person and place and click twice on the blue plus button under Metric:
Then add a metric field visited with the formula
case when Record Count is null then 0 else 1 end
You end up with that
Case 2
In the task table there is also a week column.
So for each week other places have to be visited. Then there is no need to add the parameter and the week formula and the filter of it.

Related

Select query on a linking table to get all related results per primary key in one field as comma separated values

I have a database with 3 tables: Employees, Clients, and a linking table. The Employees and Clients tables have a many-to-many relationship. One or more employee(s) can be working with one or more client(s). Some employees (generally new employees) may not be working with any clients.
The Employees table has the following columns:
Employoee_ID, Employee_FirstName, Employee_LastName
The Clients table has these columns:
Client_ID, Client_Name, Client_Address
I need to write a SQL query on Microsoft SQL Server that selects Employee_ID, Employee_FirstName, Employee_LastName, then has a Clients field which lists all of the clients ID's that each employee is working with in one single field.
Tables:
Employees:
Employoee_ID
Employee_FirstName
Employee_LastName
1
George
Washington
2
John
Adams
3
Tom
Jefferson
Clients:
Client_ID
Client_Name
Client_Address
11
ClientAl
1 Al Road
22
ClientBill
2 Bill Road
33
ClientChris
3 Chris Road
44
ClientCharlie
30 Charlie Street
Linking table
EmployeeID
ClientID
1
11
2
22
2
33
3
44
I need a Microsoft SQL Server query that will return the following output (each employee is listed ONLY once and all of his/ her related clients' IDs are shown in the same row, in one field, as comma-separated / semicolon-separated values):
Employoee_ID
Employee_FirstName
Employee_LastName
Clients
1
George
Washington
11
2
John
Adams
22;33
3
Tom
Jefferson
44
Please advise if you know of a way to do write a query to get the results above. Currently, I am getting each employee multiple times with each client ID in separate rows.
I tried using SELECT distinct & other functions but couldn't generate comma separated client ID's in one field for each employee.
Currently my query is listing Employee "John Adams" twice, once with each of its clients.
Employoee_ID
Employee_FirstName
Employee_LastName
Clients
1
George
Washington
11
2
John
Adams
22
2
John
Adams
33
3
Tom
Jefferson
44
Tried select (distinct) EmployeeID to get each employee only listed once but didn't work in listing all related clientID's in one field.
You could use string_agg - STRING_AGG (Transact-SQL)
select e.employee_id,
e.employee_firstname,
e.employee_lastname,
string_agg(cli.clientid, ',') AS clients
from Employees e
left join [linking table] cli
on e.employee_id= cli.employeeid;
Since SQL Server 2017 you can achieve the desired output via group by and STRING_AGG function:
select
e.Employoee_ID,
MAX(e.Employee_FirstName) as Employee_FirstName,
MAX(e.Employee_LastName) as Employee_LastName,
STRING_AGG(CONVERT(NVARCHAR(max),ClientID), ';') as Clients
from employoee e
join links l on e.Employoee_ID = l.EmployeeID
join client c on c.Client_ID = l.ClientID
group by e.Employoee_ID
Output:
Employoee_ID
Employee_FirstName
Employee_LastName
Clients
1
George
Washington
11
2
John
Adams
22;33
3
Tom
Jefferson
44
Check out the demo fiddle.

SSRS: repeat tablix row group value on each row

I already consulted SSRS: repeat tablix left-most row group value on each row but didn't get quite the answer I need (I'm open to get corrected).
I've got the issue that I need the group value repeated in an Excel export but also need a total row after the group value changes. So my table is built up like this:
[values1] [values2] [TOTAL by group2]
[group1] [group2] [group3] ###.## ####.##
[TOTAL] ###.## ####.## ####.##
And my expected result should be:
Hours Jan Hours Feb TOTAL by Person
Categrory1 Person1 Task1 5.5 4.5
Person1 Task2 3.0 7.0
Person1 TOTAL 8.5 11.5 20.0
Person2 Task3 1.0 0.0
Person2 Task4 2.0 0.0
Person2 TOTAL 3.0 0.0 3.0
Catergory2 Person3 Task1 .....
But what I get now is:
Hours Jan Hours Feb TOTAL by Person
Categrory1 Person1 Task1 5.5 4.5
Task2 3.0 7.0
TOTAL 8.5 11.5 20.0
Person2 Task3 1.0 0.0
Task4 2.0 0.0
TOTAL 3.0 0.0 3.0
Catergory2 Person3 Task1 .....
What leads to the problem, that users aren't able to filter by group2 (person) in Excel.
Any advice is appreciated!
You should be able to remove the Person column without removing the group. Then, right-click the header of the Task column to insert a column on the left ("inside the group") and let the person's name display in that column.

Get unique count of instance SQL Server

This is something I am not getting for some or other reason.
I have a list of records
BusinessUnitId Position Date Time
-----------------------------------------------------------
76 Staff 3/30/2009 11:00:00 AM
76 Staff 3/30/2009 04:00:00 PM
76 Management 3/30/2009 05:00:00 PM
78 Exco 3/30/2009 09:00:00 AM
78 Staff 3/30/2009 09:30:00 AM
If I do a group by
select
count(answer.BusinessUnitId)
from
AnswerSelected answer
group by
answer.BusinessUnitId
I retrieve a value of 3 and 2
What I am trying to achieve is get a count of 2
So in total 2 business units submitted answers, 76 and 78(hope this makes sense)
How can I do this?
Thanks
Use COUNT(DISTINCT ...)
SELECT
COUNT(DISTINCT BusinessUnitId)
FROM AnswerSelected
You don't need to use GROUP BY in this particular case to get the distinct count of business units across your entire table.
Demo here:
Rextester

Group by value of record's in a column of table

I have a table and I need to group all the country details in the table as count.
customer id city country
----------------------------------
1 Hyderabad India
2 Tamilnadu India
3 New York USA
4 Los Angeles USA
5 Sydney Australia
I need the output as below - I need to count all the fields and display output.
India 2
USA 2
Australia 1
India is twice in the table,USA is 2 times in the table and Australia is once in the table.
select country, count(*)
from your_table
group by country

how to use stuff function in oracle and SQL Server

I run a simple query below and outputs the following:
select subject_name, charge_section
from Arrest_table a, Charge_table b
where a. charge_id = b.charge_code
Subject_name Charge_section
Person1 333a.2322
Person2 222a.a6b4
Person2 8usa.1883
Person3 1111.b222
Person3 3233.a332
Person3 8usa.1111
How do i write in SQL SERVER or ORACLE platform that can show as follow
Subject_name Charge_section1 Charge_section2 Charge_sector3
Person1 333a.2322
Person2 222a.a6b4 8usa.1883
Person3 1111.b222 3233.a332 8usa.1111
Thanks and Happy Holidays everyone!!!
Use PIVOT. You may use row_number to get your 1-3 records.

Resources