I have a table (in an MS Access DB) with every employee at a company. The table also has a column to indicate each employee's manager. There are columns (with no data) to indicate that employee's full management chain to the CEO. I need to determine/obtain/find and fill this data using SQL (and/or VBA).
I know what needs to be done but I'm drawing a complete blank on how to do it efficiently.
I know I could go row by row but that seems so inefficient. There has to be a better way.
For example, take this table below:
+------------+-----------+----------+----------+----------+
| employeeID | managerID | manager1 | manager2 | manager3 |
+------------+-----------+----------+----------+----------+
| a | | | | |
| b | a | | | |
| c | a | | | |
| d | a | | | |
| e | b | | | |
| f | b | | | |
| g | c | | | |
| h | c | | | |
| i | d | | | |
| j | e | | | |
| k | f | | | |
| l | g | | | |
+------------+-----------+----------+----------+----------+
a has no manager (CEO)
b's manager is a
l's manager is g whose manager is c whose manager is a
etc...
So this would result in the table:
+------------+-----------+----------+----------+----------+
| employeeID | managerID | manager1 | manager2 | manager3 |
+------------+-----------+----------+----------+----------+
| a | | | | |
| b | a | a | | |
| c | a | a | | |
| d | a | a | | |
| e | b | a | b | |
| f | b | a | b | |
| g | c | a | c | |
| h | c | a | c | |
| i | d | a | d | |
| j | e | a | b | e |
| k | f | a | b | f |
| l | g | a | c | g |
+------------+-----------+----------+----------+----------+
Related
I have 3 records in pdatatest1:
| ID1 | Name1 | Class1 |
|------|-----------|--------|
| 1 | Methushal | 11 |
| 2 | Bush | 11 |
| 3 | Paul | 11 |
Datelist table is ptable:
| iDate | INAM |
|------------|------|
| 01-06-2022 | AM |
| 02-06-2022 | AM |
| 03-06-2022 | AM |
| 04-06-2022 | AM |
| 05-06-2022 | AM |
| 06-06-2022 | AM |
I inserted values are record1:
| ID | Name | Class | Indate | Intime | INAM1 |
|------|-----------|-------|------------|----------|-------|
| 1 | Methushal | 11 | 01-06-2022 | 08:00:00 | P |
| 1 | Methushal | 11 | 02-06-2022 | 08:00:00 | A |
| 1 | Methushal | 11 | 03-06-2022 | 08:00:00 | A |
| 1 | Methushal | 11 | 04-06-2022 | 08:00:00 | P |
| 1 | Methushal | 11 | 05-06-2022 | 08:00:00 | A |
My SQL Code giving result as below:
| dates12 | Name1 | INAM | INAM1 |
|------------|-----------|------|-------|
| 01-06-2022 | Methushal | AM | P |
| 01-06-2022 | Bush | AM | P |
| 01-06-2022 | Paul | AM | P |
| 02-06-2022 | Methushal | AM | A |
| 02-06-2022 | Bush | AM | A |
| 02-06-2022 | Paul | AM | A |
| 03-06-2022 | Methushal | AM | A |
| 03-06-2022 | Bush | AM | A |
| 03-06-2022 | Paul | AM | A |
| 04-06-2022 | Methushal | AM | P |
| 04-06-2022 | Bush | AM | P |
| 04-06-2022 | Paul | AM | P |
| 05-06-2022 | Methushal | AM | A |
| 05-06-2022 | Bush | AM | A |
| 05-06-2022 | Paul | AM | A |
| 06-06-2022 | Methushal | AM | NULL |
| 06-06-2022 | Bush | AM | NULL |
| 06-06-2022 | Paul | AM | NULL |
Looking for the result like below:
| dates12 | Name1 | INAM | INAM1 |
|------------|-----------|------|-------|
| 01-06-2022 | Methushal | AM | P |
| 01-06-2022 | Bush | AM | NULL |
| 01-06-2022 | Paul | AM | NULL |
| 02-06-2022 | Methushal | AM | A |
| 02-06-2022 | Bush | AM | NULL |
| 02-06-2022 | Paul | AM | NULL |
| 03-06-2022 | Methushal | AM | A |
| 03-06-2022 | Bush | AM | NULL |
| 03-06-2022 | Paul | AM | NULL |
| 04-06-2022 | Methushal | AM | P |
| 04-06-2022 | Bush | AM | NULL |
| 04-06-2022 | Paul | AM | NULL |
| 05-06-2022 | Methushal | AM | A |
| 05-06-2022 | Bush | AM | NULL |
| 05-06-2022 | Paul | AM | NULL |
| 06-06-2022 | Methushal | AM | NULL |
| 06-06-2022 | Bush | AM | NULL |
| 06-06-2022 | Paul | AM | NULL |
My sql code is:
Select Case when ptable.iDate Is null
then record1.Indate else ptable.idate end as dates12,
pdatatest1.Name1, ptable.INAM, record1.INAM1
into project1
From ptable full Join record1
on ptable.iDate=record1.Indate,pdatatest1
Select * From project1
And I have another code getting error in sql query I didn't get any result.
SELECT iDate dates12,
Name1,
INAM,
(SELECT INAM1
FROM record1
WHERE record1.ID = pdatatest1.ID1
) INAM1
FROM pdatatest1
JOIN ptable
ORDER BY iDate
I'm working this in vb.net table SQL Query. Please someone have a look once to correct my code to get correct result which I'm looking for.
You can use a cross join and a left join. For example:
select a.idate, b.name, a.inam, c.inam1
from ptable a
cross join pdatatest1 b
left join record1 c on c.id = b.id and c.indate = a.idate
I am trying to find out all the member ids who have more than 2 records on a different dates in the same class.
+----------+------------+-------+-----------+
| MemberId | Date | Class | |
+----------+------------+-------+-----------+
| 118111 | 2/18/2020 | A | Valid |
| 118111 | 10/15/2020 | A | Valid |
| 118216 | 1/31/2020 | B | Valid |
| 118216 | 5/16/1981 | B | Valid |
| 118291 | 6/9/2020 | A | Valid |
| 118291 | 12/5/2020 | A | Valid |
| 118533 | 4/9/2020 | A | Not valid |
| 118533 | 11/11/2020 | B | Not valid |
| 118533 | 7/22/2020 | C | Valid |
| 118533 | 10/25/2020 | C | Valid |
| 118293 | 3/30/2020 | A | Not valid |
| 118293 | 3/30/2020 | A | Not valid |
| 118499 | 4/16/2020 | B | Valid |
| 118499 | 7/26/2020 | B | Valid |
| 118499 | 3/25/2020 | A | Not valid |
+----------+------------+-------+-----------+
I have made a query which checks only 2 records but unable to find a solution for checking more than 2 records.
select mc.*
FROM table1 AS mc
JOIN table1 AS ma ON ma.memberid = mc.memberid
AND ma.date != mc.date
AND ma.class = mc.class
Assuming you just want the memberid you can us a HAVING:
SELECT memberid
FROM dbo.YourTable
GROUP BY memberid
HAVING COUNT(DISTINCT [Date]) > 2;
I'd like to create a pivot table with the counts of values in a list, filtered by another column but am not sure how to use pandas pivot table (or function) with a list.
Here's an example what I'd like to do:
| Col1 | Col2 |
| --- | ----------- |
| A | ["e", "f"] |
| B | ["g", "f"] |
| C | ["g", "h"] |
| A | ["e", "g"] |
| B | ["g", "f"] |
| C | ["g", "e"] |
Ideal Pivot Table
| 1 | 2 |count|
| A | e | 2 |
| | f | 1 |
| | g | 1 |
| B | g | 2 |
| | f | 2 |
| C | g | 2 |
| | h | 1 |
| | e | 1 |
I cannot use a list to make a pivot table and am struggling to figure out how to modify the data or find a different method. Any help would be much appreciated!
Try this:
cols = ['Col1','Col2']
df.explode('Col2').groupby(cols).size()
I have a set of data of cars as follow:
| class | car |
| S | Hilux |
| M | Hilux |
| M | Toyota|
| M | Hilux |
| S | toyota|
| S | toyota|
| L | toyota|
And I want to show as per below:
| class | Hilux | Toyota |
| S | 1 | 2 |
| M | 2 | 1 |
| L | 0 | 1 |
How can it be done using Ms Access?
This might work:
TRANSFORM COUNT(car)
SELECT class
FROM Table_name
GROUP BY class
PIVOT car;
I am trying to find the SQL command to do something but I don't know how to explain it so I'll use an example. I have a table like so:
| one | two | three | four |
|-----|-----|-------|------|
| a | h | i | j |
| b | k | l | |
| c | m | n | o |
| d | p | | |
| e | q | | |
| f | r | s | |
| g | t | | |
I need to create new columns that take the first non-null column from the right and kind of reverse it going up and joining/concatenating the fields.
| one | 1-up | 2-up | 3-up |
|-----|------|------|---------|
| a | j | j, i | j, i, h |
| b | l | l, k | |
| c | o | o, n | o, n, m |
| d | p | | |
| e | q | | |
| f | s | s, r | |
| g | t | | |
For b, since column four doesn't have data it uses three as the first value. Same for the other rows.
I hope this makes sense. I'm not sure how else to explain this.
You can use COALESCE like this :
select one, COALESCE(four,three,two,'') as '1-up',
COALESCE(four+','+three,three+','+two,'') as '2-up',
COALESCE(four+','+three+','+two,'') as '3-up'
from Table1
SQL Fiddle link Here