Compare multiple values based on cell Value - vba

I have a 3 datasets.
Master dataset have
A B C D
11 T Jim India
12 U Mary UK
13 V Bob US
14 P Peter India
India dataset
A B H K
10 11 T Jim
10 13 0 Krestel
10 14 P Peter
10 15 L Robert
If the D coulmn had India then the details of columns A, B and C should match that in India dataset with coulmn B, H and K respectively. (The combination of the column A, B and C should present in Dataset- India, If not hoghlighted or add comment in last column of master dataset)
I have been doing this manually by adding several helper columns in all the datasets using concatenation and then using vlookup.
Is it possible to automate this process using vba?
Any help will be appreciated.

Actually, I think that you can achieve this through spreadsheet functions alone, without the need of VBA. Check the usage of the function VLOOKUP.
The idea would be to deploy a formula in, say, column "E" of the Master dataset that would check for an entry in the relevant country dataset matching the values of A, B and C. You will need to build the reference to the range VLOOKUP uses taking into account the country name.
Hope this serves you as a good guide.

Related

PostgreSQL data transformation - Turn rows into columns

I have a table whose structure looks like the following:
k | i | p | v
Notice that the key (k) is not unique, there are no keys, nothing. Each key can have multiple attributes (i = 0, 1, 2, ...) which can be of different types (p) and have different values (v). One attribute type may also appear multiple times (p(i-1) = p(i)).
What I want to do is pick certain attribute types and their corresponding values and place them in the same row. For example I want to have:
k | attr_name1 | attr_name2
I have managed to make a query that does this and works for all keys (k) for which attr_name1 and attr_name2 appear in the column p of the initial table:
SELECT DISTINCT ON (key) fn.k AS key, fn.v AS attr_name1, a.v AS attr_name2
FROM Table fn
LEFT JOIN Table a ON fn.k = a.k
AND a.p = 'attr_name2'
WHERE fn.p = 'attr_name1'
I would like, however, to take into account the case where a certain key has no attribute named attr_name1 and insert a NULL value into the corresponding column of the new table. I am not sure how to achieve that. I have no issue using multiple queries or intermediate tables etc, but there are quite a lot of rows in the table and I need something that scales to millions of rows.
Any help would be appreciated.
Example:
k i p v
1 0 a 10
1 1 b 12
1 2 c 34
1 3 d 44
1 4 e 09
2 0 a 11
2 1 b 13
2 2 d 22
2 3 f 34
Would turn into (assuming I am only interested in columns a, b, c):
k a b c
1 10 12 34
2 11 13 NULL
I would use conditional aggregation. That is, an aggregate function around a CASE expression.
SELECT
k,
MAX(CASE WHEN p='a' THEN v END) AS a,
MAX(CASE WHEN p='b' THEN v END) AS b,
MAX(CASE WHEN p='c' THEN v END) AS c
FROM
your_table
GROUP BY
k
This presumes that (k, p) is unique. If there are duplicate keys, this will clearly find the one v with the highest value (for each (k,p))
As a general rule this kind of pivoting makes the data harder to process in SQL. This is often done for display purposes because humans find this easier to read. However, from a software engineering perspective, such formatting should not be done in the data layer; be careful that by doing this you don't actually make your future life harder.

Vlookup to Make a list?

This site has been super helpful, thank you to everyone who has answered my questions. Here is the next one I am working on. Not sure if I should use vlookup, hlookup, a combination of both or something else.
So I have a list of teams with lineups
Team
Player
A
Sam
A
Chris
A
Tom
A
Scott
B
Mark
B
Dan
B
Greg
B
Ben
C
Sara
C
Beth
C
Luara
C
Britt
On a separate page I am trying to fill in a line up "IF" a team is selected.
For reference this is the current formula I have been trying:
=IFERROR(INDEX('Team LineUps'!$B:$B,Match(0,COUNTIF($C$16,IF('Team LineUps'!$A:$A=$C$16,'Team LineUps'!$B:$B,$C$16)),0)),"")
This will get me The first player on the list for a team. If I change the 0 to a 1 it will get me the last player on the team. How can I/ Can I? get the entire list 1-4? Or is it only a "true" OR "False"
Answer:
Use a QUERY.
Formula:
=QUERY('Team LineUps'!A2:B13, "SELECT B WHERE A='"&B4&"'")
Example Usage:

SQL find all rows with assigned values

MSSQL: i have this example data:
NAME AValue BValue
A 1 11
B 1 11
C 2 11
D 2 21
E 3 21
F 3 21
G 4 31
H 4 31
I 5 41
J 5 NULL
...
I am looking for algorhitm which looks for all the Names closed by values by different seed (AValue and Bvalue, in this case seed is given by 2 for AValue and by 3 for Bvalue, but this can be skipped and given later and so on, not only looking for smallest multiple). In this case output should be 1,2,3,4,11,21,31 as a first group/result. Then all the Names with these values can be updated etc.
I need to find out all the Names in "closed circle" of values by different seed.
EDIT:
(try of simplier example)
Imagine that you have list of names. Each name is given two numbers. In most cases these numbers are given by some seed (in this example AValue is given twice, BValue three times) but some numbers can be skipped, so you cannot just count smallest multiple of these different seeds(in this case it would be 2x3, ever 6 names you have closed group where no Name contains AValue or BValue from next/different group). For example Name A have 1 and 11. 1 is given for A and B, 11 for A, B, C. These Names have 1,2,11,21. So you check for 2 and 21 and then you get E and F in addition and then the loop of checking should continue, but as long as no more Names are contained there should be output 1,2,3,11,21. "Closed circle"

Compare the above cell value and return alphabets

I am having two columns Last Name and First Name. Column Last Name contains real names and it may contain same names multiple times (if a name is repeating then it will be in the subsequent rows only and not elsewhere).
Requirement:
Now In the column First Name (its empty only), I need to capture alphabets based on the 'Last Name'. i.e. i am expecting the first name column to be filled with A || B || C || D|| if a last name contains 4 times. e.g.:
Lastname Firstname
SMITH A
SMITH B
Conte A
Conte B
Watts A
Watts B
Speirs A
Speirs B
CONNOLLY A
Austin A
Austin B
Austin C
Austin D
Austin E
Austin F
Austin G
=CHAR(COUNTIF(A$1:A1,A2)+65)
to be entered in B2 and pulled down.
Instead of multiple nested IF's, given a sorted list, you could try:
=IF(A2<>A1,"A",CHAR(CODE(B1)+1))

Excel: one column has duplicates of each value, I need to take averages of the corresponding two values from the other columns

Example:
column A column B
A 1
A 2
B 2
B 2
C 1
C 1
I would somehow like to get the following result:
column A column B
A 1.5
B 2
C 1
(which are averages of 1 and 2, 2 and 2 and 1 and 1)
How do I achieve that?
Thanks
If you're using Excel 2007 or above, you can also use the shorter AVERAGEIF function:
=AVERAGEIF($A$1:$A:$6,D1,$B$1:$B$6)
Less typing, easier to read..
In D1:D3, type A, B, C. Then in E1, put this formula
=SUMIF($A$1:$A$6,D1,$B$1:$B$6)/COUNTIF($A$1:$A$6,D1)
and fill down to E3. If you want to replace the existing data, copy E1:E3 and paste-special-values over itself. Then delete A:C.
Alternatively, you can add headers to your data, say "Letter" and "Number". Then create a Pivot Table from your data. Put Letter in the rows section and Number in the Data section. Change your Data section from SUM to AVERAGE and you'll get the same result.