After importing data from an excel document I ended up with a table that looks like this (quite similar to a pivot table):
EMPLOYEEID | SKILL1 | SKILL2 | SKILL 3
---------------------------------------
emp1 | 1 | | 3
emp2 | 2 | 3 |
emp3 | | | 1
emp4 | | 2 | 3
And in my database I have another table which stores each level of knowledge of each skill for
every employee:
EMPLOYEEID | SKILLID | LEVEL_OF_KNOWLEDGE
------------------------------------------
emp1 | SKILL1 | 1
emp1 | SKILL3 | 3
emp2 | SKILL1 | 2
emp2 | SKILL2 | 3
emp3 | SKILL3 | 1
emp4 | SKILL2 | 2
emp4 | SKILL3 | 3
My question is, how can I retrieve the data from the first table and store it in the second one? Is it possible using only Access queries or have I to deal with vba?
I have found plenty of examples doing the opposite (pivoting the second table to get the first one) but I haven't managed to find the way to do solve this case.
Sure
SELECT EmployeeID, "SKILL1" AS SkillID, SKILL1 AS Level_OF_Knowledge WHERE SKILL1 IS NOT NULL
UNION ALL SELECT EmployeeID, "SKILL2" AS SkillID, SKILL2 AS Level_OF_Knowledge WHERE SKILL2 IS NOT NULL
UNION ALL SELECT EmployeeID, "SKILL3" AS SkillID, SKILL3 AS Level_OF_Knowledge WHERE SKILL3 IS NOT NULL
*repeat last line for each additional column in your first table
Related
I'm trying to display data with a pivot table with a distinct of rows
Data
Id | Date | Count | Date 2 | ...
1 | 21/02/2020 | 1 | | ...
1 | 21/02/2020 | 1 | 21/02/2020 | ...
2 | 15/05/2021 | 0 | 15/05/2021 | ...
3 | 17/09/2020 | 2 | 17/09/2020 | ...
3 | 17/09/2020 | 2 | 19/10/2021 | ...
3 | 17/09/2020 | 2 | 25/11/2021 | ...
Expected result in the Pivot Table
Id | Date | Count
1 | 21/02/2020 | 1
2 | 15/05/2021 | 0
3 | 17/09/2020 | 2
Can you please explain me quickly how to setup the Pivot Table to get this result ?
Indeed, I'm getting 2 for id 1 and 6 for id 3 ... SUM is performed (instead of 1 and 2).
Moreover, I'm not able to put the value of the Date as a column ...
Maybe that Pivot Table is not the correct way to do it.
I hope it is clear ...
Many thanks in advance
Just adding the answer per the comment..
To achieve your Expected result, you can use a pivot table where you put those 3 fields in your "rows" value. Then change your design to Tabular mode, do not show Grand or Sub totals.
You could also simply copy those 3 columns, and paste elsewhere, then with all 3 columns highlighted do to Data and remove duplicates.
I have Sale Invoices for bread, jam, etc. like this table.
+-------+--------+-------+
| Item | Date | Price |
+-------+--------+-------+
| Bread | 1-Dec | 5 |
+-------+--------+-------+
| Jam | 1-Dec | 5 |
+-------+--------+-------+
| Bread | 8-Dec | 6 |
+-------+--------+-------+
| Jam | 8-Dec | 4 |
+-------+--------+-------+
| Bread | 15-Dec | 4 |
+-------+--------+-------+
| Jam | 15-Dec | 7 |
+-------+--------+-------+
I want the highest price date for each item like
+-------+--------+---------------+
| Item | Date | Highest Price |
+-------+--------+---------------+
| Bread | 8-Dec | 6 |
+-------+--------+---------------+
| Jam | 15-Dec | 7 |
+-------+--------+---------------+
It is like finding Max Values depending on Lookup Values. It is very much like Group By and Max in SQL. How do I do it in excel? I've tried index match and also googling. Nothing helps. Please help me.
This is typically done through a pivot table:
Select your data.
Insert a pivot table.
In your case use "Item" & "Date" as rows.
In your case use "Price" as value.
Then click "Price" and under it's field settings choose "Max".
Then in the pivot table itself right any date, click "Filter" > "Top Ten" and make that top 1 based on the max price.
There are many ways to do this through formulae, but if one has Excel O365 it can be done through one single formula, for example:
Formula in E2:
=TRANSPOSE(CHOOSE({1,2,3},TRANSPOSE(UNIQUE(A2:A7)),TRANSPOSE(MINIFS(B2:B7,A2:A7,UNIQUE(A2:A7),C2:C7,MAXIFS(C2:C7,A2:A7,UNIQUE(A2:A7)))),TRANSPOSE(MAXIFS(C2:C7,A2:A7,UNIQUE(A2:A7)))))
Or:
=FILTER(A2:C7,ISNUMBER(MATCH(A2:A7,UNIQUE(A2:A7),0))*LET(X,MAXIFS(C2:C7,A2:A7,UNIQUE(A2:A7)),ISNUMBER(MATCH(C2:C7,MAXIFS(C2:C7,A2:A7,UNIQUE(A2:A7))))*ISNUMBER(MATCH(B2:B7,MINIFS(B2:B7,A2:A7,UNIQUE(A2:A7),C2:C7,X),0))))
Let's say I have 2 excel tabs (A) & (B):
TAB (A)
+----------+
|City |
+----------+
| Seattle |
| New York |
| Boston |
| Miami |
+----------+
TAB (B)
+------------+---------+
|City | Name |
+------------+---------+
| Seattle | Klay |
| Seattle | Walis |
| New York | Walis |
| Boston | Klay |
| Miami | John |
| New York | Klay |
+------------+---------+
I am trying to group them in order to obtain a new tab (result) where I have the list of city where people NEVER went group by name:
TAB (RESULT)
+------------+---------+
|Name | City |
+------------+---------+
| Klay | Miami |
|----------------------|
| Walis | Boston |
| | Miami |
|----------------------|
| John |Seattle |
| |New York |
| |Boston |
+------------+---------+
The only solution I came with was using a pivot table but I am looking for opposite result! I have also use Index & Match but it's not working.
Since you mentioned you are trying to do this in Excel, here's an Excel solution. Let's pretend you have your data setup all in one tab, like so:
In cell G2 and copied over and down is this formula:
=IF(COLUMN(A2)>ROWS($A$2:$A$5)-COUNTIF($D$2:$D$7,$F2),"",INDEX($A$2:$A$5,MATCH(1,INDEX((COUNTIFS($D$2:$D$7,$F2,$C$2:$C$7,$A$2:$A$5)=0)*(COUNTIF($F2:F2,$A$2:$A$5)=0),),0)))
You can cut and paste each section to a different tab if desired.
in sql server it would be something like this
--tsql
with tableC AS
(
SELECT
a.City
,b.name
FROM tableA a
cross join (select distinct name from tableB) b
)
SELECT
c.*
FROM tableC c
LEFT JOIN tableB b
ON c.City = b.City
AND c.name = b.name
WHERE b.city IS NULL
If this is indeed a MySQL problem, you need to get every combination of name and city, and then eliminate combinations that have visits.
SELECT bNames.Name, tableA.City
FROM (SELECT DISTINCT Name FROM tableB) AS bNames
CROSS JOIN tableA
WHERE (bNames.Name, tableA.City) NOT IN (SELECT Name, City FROM tableB)
ORDER BY bNames.Name, tableA.City
;
The result will not omit a repeated user name on successive entries, but that is something almost always better handled by post processing the results anyway.
One possible solution
Select b.name, a.city city_to_visit
From a join b on 1 = 1
Minus — some db use except
Select b.name, b.city city_visited
From b
Is this your desired ?
SELECT NAME,
CASE WHEN (SELECT
CITY
FROM TAB1) NOT IN
CITY
Then City
END CASE From Tab1 LEFT JOIN
TAB2 ON TAB1.CITY=Tab2.CITY
GROUP BY NAME;
I have two tables in Excel. One has Key and Date - this can be table A. The other has Key, Begin Date, End Date, and Value - let's call this table B.
I am trying to pull into Table A the Value from Table B for the Key , where the Date from Table A is between the Begin Date and End Date from Table B. The value should be 0.4 using the example tables below.
NOTE: There will never be overlapping dates and shouldn't have multiple rows for the same date range.
Table A -
| Key | Date |
|-----|------------|
| 2 | 10/29/2018 |
Table B -
| Key | Begin Date | End Date | Value |
|-----|------------|------------|-------|
| 1 | 07/01/2018 | 12/31/2999 | 0.1 |
| 1 | 01/01/1995 | 06/30/2018 | 1 |
| 1 | 01/01/1900 | 12/31/1994 | 0.5 |
| 2 | 10/31/2018 | 12/31/2999 | 3.6 |
| 2 | 01/01/1995 | 10/30/2018 | 0.4 |
| 2 | 01/01/1900 | 12/31/1994 | 10 |
| 3 | 01/01/1900 | 12/31/2999 | 100 |
Thanks!
Assuming there'll only be one match, use SUMIFS.
=SUMIFS($I$1:$I$8,$F$1:$F$8,A2,$G$1:$G$8,"<="&B2,$H$1:$H$8,">="&B2)
Note - changed two instances of 12/31/1995 in Table B to 12/31/1994, assuming that it's a typo and date ranges shouldn't overlap between rows.
EDIT:
You can use INDEX and AGGREGATE if you need to return text.
=INDEX(I2:I8,AGGREGATE(15,6,ROW($A$1:$A$7)/(($F$2:$F$8=A2)*($G$2:$G$8<=B2)*($H$2:$H$8>=B2)),1))
--------------------
|bookname |author |
--------------------
|book1 |author1 |
|book1 |author2 |
|book2 |author3 |
|book2 |author4 |
|book3 |author5 |
|book3 |author6 |
|book4 |author7 |
|book4 |author8 |
---------------------
but I want the booknames as columns and authors as its rows
ex
----------------------------------
|book1 |book2 |book3 |book4 |
----------------------------------
|author1|author3 |author5|author7|
|author2|author4 |author6|author8|
----------------------------------
is it possible in postgres? How can I do this?
I tried crosstab but I failed to do this.
You can get the result using an aggregate function with a CASE expression but I would first use row_number() so you have a value that can be used to group the data.
If you use row_number() then the query could be:
select
max(case when bookname = 'book1' then author end) book1,
max(case when bookname = 'book2' then author end) book2,
max(case when bookname = 'book3' then author end) book3,
max(case when bookname = 'book4' then author end) book4
from
(
select bookname, author,
row_number() over(partition by bookname
order by author) seq
from yourtable
) d
group by seq;
See SQL Fiddle with Demo. I added the row_number() so you will return each distinct value for the books. If you exclude the row_number(), then using an aggregate with a CASE will return only one value for each book.
This query gives the result:
| BOOK1 | BOOK2 | BOOK3 | BOOK4 |
-----------------------------------------
| author1 | author3 | author5 | author7 |
| author2 | author4 | author6 | author8 |