How can I model a non-numerical variable using a mixture of numerical and non-numerical predictors? - regression

I have a dataset that looks like this:
age
gender
school
group
13
male
high school
A
16
female
professional
B
18
male
technical
B
17
male
professional
A
15
female
high school
A
and so on for about 500 rows.
I would like to use R to know if being assigned to group "A" rather than "B" (or vice-versa) may depend on "age", "gender" or "school" (or a combination of these):
which kind of analysis should I conduct? Thank you very much!

Related

How to separate one column's data into multiple columns?

Here's my situation : I have a table that has large amounts of records, I need to pull out a number of these records for each name in the database, note that TOP will not work for my use case. My end user wants the report formatted in such a way that each user shows up only once, and up to 3 different dates are shown for the user.
Table format
AutoID
Enum
TNum
Date
Comments
1
25
18
2/2/22
2
25
18
1/2/21
Blah
3
18
18
1/2/21
4
18
18
1/2/20
5
25
17
1/2/22
6
25
17
1/2/20
Now the Enum and TNum fields are fk with other tables, I have created a join that pulls the correct information from the other tables. In the end my query provides this output
RecordID
Training
CompletedDate
FirstName
LastName
Location
2821
MaP
1/1/21
David
Simpson
123 Sesame St.
2822
1/2/22
Fuller
MaP
Dough
GHI
David
123 Sesame St.
2825
1/1/20
Simpson
The two "Blank fields" represent information that is pulled and may or may not be needed in some future report.
So to my question : How do I manage to get a report, with this query's pull to look like this:
Place
LastName
FirstName
Training
FirstCuttoff
Secondcutoff
ThirdCutoff
Comments
123 Sesame St.
David
Simpson
MaP
1/1/20
1/1/21
123 Sesame St.
John
Dough
MaP
1/1/22
I was originally planning on joining my query to itself using where clauses. But when I tried that it just added two extra columns of the same date. In addition it is possible that each record is not identical; locations may be different but since the report needs the most recent location and the name of the trainee. In addition, to add more complexity, there are a number of people in the company with effectively the same name as far as the database is concerned, so rejoining on the name is out. I did pull the Enum in my query, I can join on that if needed.
Is there an easier way to do this, or do I need to sort out a multiple self-joining query?
I have a project I am working on where I am going to have to do this. Some of the suggestions I received were to use a Pivot query. It wouldn't work in my case but it might for yours. Here is a good example
Pivot Columns

MS Access - Count of multiple values

I have a this Table in MS Access:
Table1
ID EMP ROLE ASSESS
1 JOE Weld 4
2 TOM Weld 4
3 JIM Ship 4
4 PAT Ship 3
5 JAY Weld 4
6 TIM Ship 4
"ROLE" is short text and "ASSESS" is a number field. "ASSESS" is assessing employees' roles on a scale of 1-4. I want to collect and total assessments that are "4" for each role.
Returning something like:
ROLE TOTAL
Weld 3
Ship 2
I however have around 100 different roles that I am needing to do this with. Is there a way with SQL or a combination of query and macro to make this work? I am at a loss.
Thank you.
You could use a where clause to filter just the the assesses that are 4 and a group by clause to aggregate them:
SELECT role, COUNT(*)
FROM table
WHERE assess = 4
GROUP BY role

SSRS parent-child list when child is sometimes empty

I'm trying to build an SSRS report where the dataset is "presented" to SSRS through the application (Microsoft Dynamics NAV2009). The concept of the report is simple - it's a list of pieces of equipment - computers, cellphones, etc. The table has a "related to equipment" field. So Equipment ID "CELL01" may have a "Related to equipment ID" = "Computer 123". And Equipment ID "CAR456" may also have a "Related to equipment ID" = "Computer 123".
I want to create a report that lists each piece of equipment, and below it, any "related equipment". Many of the pieces have no related pieces, so the listing needs to look like this:
COMPUTER001 Dell Latitude 4mb RAM
COMPUTER002 Dell Latitude 16mb RAM
COMPUTER123 Dell Latitude 8mb RAM
Related Equipment
CELL01 Nokia Cellphone
CAR456 2011 Ford Taurus
COMPUTER135 Sony Laptop 12gb RAM
CELL01 Nokia Cellphone
CAR456 2011 Ford Taurus
Note that the "related items" is only unidirectional - the related items need only to be reported under the "Related to" equipment, and not vice-versa.
I have done some due diligence and investigated recursive hierarchies. It appears that this method depends on every "child" having a "parent". In my case, most "children" are "NULL", i.e. most equipment does not have a "related to equipment" id. So if I try to create the list from the bottom up and use recursive hierarchies, in the example below, only the COMPUTER123 and its children would be listed. None of the the other "Parent" records (e.g. COMPUTER001) have any children.
Has anyone done an SSRS report similar to this? In "Pure" SQL, this would be akin to having a query using a left outer join, and listing the fields in the primary record even if the JOIN'ed results were NULL, e.g. a list of all salesmen with their orders booked for the week:
Select s.Name, i.Invoice_No, i.Invoice_Amt
from Salesperson s
LEFT OUTER JOIN Invoice i on
s.SalespersonID = i.SalespersonID
If Salesperson "Joe" didn't make any sales this week, but Salesperson "Sam" made 3 sales and Salesperson "Bruno" made 2 sales, You might expect a result something like this:
Name Invoice_No Invoice_Amt
Bruno 1287 200.00
Bruno 1289 400.00
Joe NULL NULL
Sam 1281 65.00
Sam 1283 450.00
Sam 1286 175.00
So how would you construct a Report where the output would be:
Bruno
Invoices:
1287 200.00
1289 400.00
Joe
Sam
Invoices:
1281 65.00
1283 450.00
1286 175.00
Any suggestions would be appreciated.
Ron

Subquery in Access

I have 2 tables in Access with these fields
Student:
ID(PK) Name Family Tel
Lesson:
ID StudentRef(FK(Student)) Name Score
Imagine we have these records
Student :
1 Tom Allen 09370045230
2 Jim leman 09378031380
Lesson:
1 1 Math 18
2 1 Geography 20
3 2 Economic 15
4 2 Math 12
How can I write a query that result will be this (2 fields)?
Tom Math : 18 , Geography 20
Jim Economic :15 , Math :12
SELECT s.Name, l.Name, l.Score
INNER JOIN tbl_lessons as l ON s.student_id = l.student_id
FROM tbl_students as s
That won't give you your formatting, but it'll get you the data.
The most tricky part of your problem is how to aggregate strings in your sub-query. MS Access does not have any aggregation function that is applicable to strings (except for Count()) and there is no way to define your own function. This means you can't just get the desired "subject:score , subject:score" concanetation. As long as you can go without you can easily take the solution provided in the answer by Corith Malin.

Extract values of a sorted request with SQL

I have this sql table with people's name and ages.
Bob 28
Bryan 30
Jim 25
John 42
Bill 22
Sam 28
Tom 26
I would like to make a sql command to order all people by age desc, find a name in it, a return the preceding one, the founded and the next one with their position.
For example, admit that I would like to find Tom, my request should return :
Name Age Rank
Jim 25 2
Tom 26 3
Bob 28 4
Jim has the number 2 because Bill is the youngest
Is it possible to do something like this ?
Thanks in advance for any help
SQL isn't suited for row-based operations. There's no easy way to do "find a row where some condition(s) = true, then return the previous row" in a single query. You can do it in a couple steps, though:
a) Run one query to retrieve 'Tom' and his age (26).
b) Run another query to get the next older person
SELECT name, age FROM ... WHERE age > 26 ORDER BY age ASC LIMIT 1
c) Repeat but for next younger:
SELECT name, age FROM ... WHERE age < 26 ORDER BY age DESC LIMIT 1
This'll fetch people who are at least 1 year old/younger... You don't specify what happens if there's multiple people of the same age (e.g. There's Fred who's also 26, or Doug and Elmer who are both 25), so I'm ignoring those conditions.