DataStudio ::: Count_Distinct Calculation based on two values - sql

I have a data set with two values. The first being User ID, the second being Type.
I previously had a count_distinct on the user ID. However, if a user changes their Type, the metrics we have to show 'Type' of user shows two instances of that once unique user.
I would like some SQL which basically states if the User ID and Type Match, then count that as a unique user. If either one of those change, count that also as a unique user. So that..
1) Jane Doe - Support User
2) Jane Doe - Employee User
3) John Smith - Support User
This would show as 3 unique users, rather than 2 based on a count_unique on just the User ID.

One way to accomplish this is with a calculated field that looks something like the following:
COUNT_DISTINCT(CONCAT(USER_ID, ' - ', TYPE))
You might need to replace USER_ID and TYPE with your field names.

Related

Ordering based on one value of many

I have three SQL tables. Users, Registration Field Values, and Registration Fields.
Name
zip code
favorite food
Sue
55555
sushi
Gary
12345
eggs
Where zip code and favorite food are different registration fields.
The relationship is a user has many registration field values, and those values belong to the registration field.
I'm wondering how I can order my table based on a certain registration field. For example, selecting "favorite food", I would want "eggs" before "sushi".
This is confusing to me because I've only seen ORDER BY for an individual column or series of columns. I can't just ORDER BY registration_field_value.value because it needs to be based on only one of those registration fields.
This is like "ORDER BY field value where the associated field id is 'favorite food'", although I don't want to filter anything out.
I'm using Postgres if that makes a difference.
EDIT, adding a
:
You can use case to order based on specific value.
For eg:
ORDER BY
CASE "favorite food"
WHEN 'eggs' THEN 1
ELSE 2
END
The above query will move row with eggs to start and all other value will be moved to bottom.

Using LIKE clause when formats are different

I was given a patient list with names and I am trying to match with a list already in our database and am having troubles given the format of the name field in the patient list. This list is taken from a web form so people can input names however they want so it does not match up well.
WEBFORM_NAME
PATIENT_NAME
JOHN SMITH
SMITH,JOHN L
SHANNON BROWN
BROWN,SHANNON MARIE
Is there a way to use a LIKE clause in an instance like this? All I really need is the LIKE clause to find the first name because I have joined on phone number and email address already. My issue is when households have the same phone number and email address (spouses for example) I just want to return the right person in the household.
Not sure if all you need is to get first name, here is the WIldCard expression to get first name
SELECT LEFT(WEBFORM_NAME,CHARINDEX(' ',WEBFORM_NAME)-1) AS FirstName1,
SUBSTRING(PATIENT_NAME,CHARINDEX(',',PATIENT_NAME)+1,(CHARINDEX(' ',PATIENT_NAME)-CHARINDEX(',',PATIENT_NAME))) AS FirstName2
FROM yourTable
The assumption here seems to be that the webform (where user would manually) type in the name would be of the format <First Name> [<optional middle Name(s)>] <Last Name>, where as the data stored in the table are of the form <Last Name>,<First Name> [<optional middle Name(s)>]. Its not an exact science, but since other criteria (like email, phone etc) have been matched best case
select *
from webform w, patient p
where
-- extract just the last name and match that
regexp_like(p.name,
'^' ||
regexp_extract(w.name,
'([^[:space:],][[:space:],])*([^[:space:],]+)', 1, 2))
and -- extract the first name and match it
regexp_like(p.name,
',[[:space:]]*' ||
regexp_extract(w.name, '(^[^[:space:],]+)'))
Since webform is free form user input, its hard to handle abbreviated middle name(s) and other variations so using the above will do first name and last name based matching which in addition to the matching you are already doing should help.

SQL - selecting multiple dates from a table for unique ID's - part 2

Gordon got me pretty straight on my former question and now I've learned a lot more :). my query now looks like this:
select ident, min(input_date) as 'date requested',
min(case when op='u' and user IN (lists tech's names)) as 'date assigned'
max(case when status='closed' then date end) as 'date completed'
max(case when op='u' then user end) as 'tech'
from table
so this give me all that i want except the column that's labelled as 'tech'. for the majority of the results, i get the correct results but for a handful it gives me either the wrong tech or the 'user'
so I've added a field so you can better understand - it's field called 'role'. when i run this query, i never want the person listed as 'user' in the role column to be listed as 'tech'. in query above, it lists the user instead of tech and it's always in cases where the user updated their request. so if you look at the table, in ident 1, tom is user and harry is tech. harry assigned it to himself on 2/2; however, tom updated request (to give more info) after harry assigned it. running the above query, will result in tom being listed as the tech in the tech field. my guess is b/c of max and tom is after harry alphabetically.
so i have 5 tech's to track (only 2 listed in table - bertha & harry). if i add an IN clause so that it reads like this:
max(case when op='u' and user IN ('bertha','harry',etc) then user end) as 'tech'
the results is it always gives me the last tech alphabetically rather than the one who assigned it/closed it.
my goal is to have the tech who assigns & closes it to be listed. if the request has not been assigned then the 'tech' field should read null.
the date fields all have time stamps so i could go about it that way and i guess i could target their role as well.
i know that max is probably the problem as the tech could assign it then the user could update but if i put IN statement in there I don't get why it lists a tech who's not associated w/that ident.
finally, the other scenario, in ident 2, bertha actually takes the assignment away from harry so i need to say bertha; however, harry will always be the one reported in field - probably due to max()
thanks

Excel 2013 VLOOKUP() based on multiple criteria, retrieving multiple rows

I have an issue that can (I believe) be solved by just excel, and may won't require VBA (I could be wrong). I believe it can be solved by nested functions but the formula I've tried has not worked.
Here's my data:
Name Report # Name
Mark Doe ReportXXX Mark Doe
Connie Doe ReportYYY Connie Doe
Debbie Doe REPORTYYY Debbie Doe
Valerie Doe FSMVALTR1 Valerie Doe
Jeff Doe FSMVALTR1 Jeff Doe
Andy Doe RAZXYBCA1 Andy Doe
Ryan Doe RAZXYBCA1 Ryan Doe
Andy Doe RAZ111111 Jill Doe
Ryan Doe RAZ222112 Amanda Doe
This list goes on for about ~4000 rows in the first NAME and REPORT # columns. In the second NAME column I have ~160 rows.
The second name column identifies all the users who actually use the report, with no duplicates. The two name and report # columns have many duplicates, since users have access to multiple reports, and many of them are the same report used for different purposes. Since the second NAME column has so few rows, the names don't match up all the way through, which can be observed near the bottom of both the NAME columns.
What I need to do is have a VLOOKUP that identifies the name in both of the columns and then returns the report number that each individual has access to across rows (horizontally), not down the columns. It also needs to I.D. numerous reports since individuals have access to anywhere from 1-15 reports, starting at the second and so on after the previous has been extracted.
Ideally it would look something like this:
Name Report # Name ex column ex column ex column
Mark Doe ReportXXX Mark Doe ReportXXX ReportAAA ReportB
I didn't list the other reports "Mark Doe" has access to and these would be somewhere down the long list of ~4000, along with his name repeated multiple times in the first NAME column, but the second "unique" name column would be where it is returning the reports to, across rows.
I made a method that uses additional three columns and it worked for me.
I used the data you provided above.
Add three columns to the left.
in A2 enter =COUNTIFS($D$2:$D2,D2,$E$2:$E2,E2).
in B2 enter =SUMIFS($A$2:$A2,$A$2:$A2,1,$D$2:D2,D2).
in C2 enter =D2&B2.
Now copy those cells to all your 4000 rows.
Now next to the second name column add column headers numbered from 1 to as many reports you think can be the max one user can have (you said 15).
Now enter this vlookup in F2 =IFERROR(VLOOKUP($F2&G$1, your table array starting from column C as absolute/fixed ,3,FALSE),"") in my case it was =IFERROR(VLOOKUP($F2&G$1,$C$2:$E$11,3,FALSE),"").
Copy the formula accross all columns with number headers and down all rows that have names in second name column (160).
your sheet shuld look something like this image
All the best let me know if it worked for you.
Have you explored the option of using a Pivot Table?
The output would look like this:
With the Pivot Table setup like this:
This method would make for a cleaner view, as your current method looks to repeat ~4,000 times (once for each report & name combo). Hopefully this works for what you need.

Crystal Reports: Display Subdetail Section Without Repeat Detailt Section

I'm doing a report using Crystal Report in VB.NET. I'm using 4 tables, and I want to display user information with a list of actions/permissions for every user, but my report
is displaying the user data with one record from his action, the same user data and another record of his actios, like this:
Last Name First Name Occupation
Doe John Student
Record some data 16/12/2013
Doe John Student
Record some data 17/12/2013
Doe John Student
Erase data 18/12/2013
Doe John Student
Print data 19/12/2013
Xavier Professor Master
Erase data 21/12/2013
Xavier Professor Master
Record some data 21/12/2013
But what I want its this:
Last Name First Name Occupation
Doe John Student
Record some data 16/12/2013
Record some data 17/12/2013
Erase data 18/12/2013
Print data 19/12/2013
Xavier Professor Master
Erase data 21/12/2013
Record some data 21/12/2013
If I put user data with action data at the same DetailSection, my user data is repeated with every action from the user, but I want just one time the user data, and then all his actions
Also, If i put the user data in the PageHeaderSection with the titles, my user data is displayed at the header, and the all his actions, but when an user data is finished, it does not print the next user data and his actions. It prints the actions of the next user, and the user data is displayed until the next page...
How you are going to distinguish students with the same name ?
Put the user data in the details section but add grouping by a unique field (or a formula , which combines few fields).
Group by user and put actions in detail section.