Which Excel formula should I use - excel-2007

I have two Excel sheets. I want to copy name and address into D and E columns of sheet 1 from sheet 2 if both sex and age match.
sheet 1 has three columns; case_no, age and sex
sheet 2 has; no, age, sex, Address and name
How can I do this using Excel 2007.
Sheet 1
case_no age sex
101 23 F
102 18 F
103 45 M
208 64 F
209 19 M
Sheet 2
no name age sex address
101 Abe 56 M dilla
209 zedo 19 M bonga
206 rute 18 F saris
70 hana 67 M paissa
102 Feve 45 F masr
103 sara 23 F AA
107 Sam 64 F wolo

Assuming case_no and no are in the A1 cells, a simple way would be to insert a new ColumnA into Sheet2 and populate it with:
=D2&E2
Then in Sheet1 D2:
=IFERROR(VLOOKUP($B2&$C2,Sheet2!$A:$F,3,0),"")
and E2 copy the above across and change the 3 to a 6 before copying the pair down to suit.
However if there is more data and it includes people of the same age and sex then only the first to be found will be returned.

Related

How to join different table some column?

Student Table 1
ID Name Surname School Number Class Number ClassBranch
-----------------------------------------------------------------------------
113 Jane Smith 19 4 A
121 John Konl 42 5 B
331 Albert Smith 61 4 A
742 Jack Ronal 52 5 B
759 Jan Ronal 84 6 C
Student Table 2
ID Name Surname School Number Class Number Class Branch
-----------------------------------------------------------------------------
113 Jane Smith 11 4 D
151 John Konl 18 4 D
804 Albert Smith 26 5 F
605 Jack Ronal 32 5 F
785 Jan Ronal 87 8 L
Created Student Table
ID Name Surname School Number Class Number Class Branch
--------------------------------------------------------------------
113 Jane Smith NULL NULL NULL
151 John Konl NULL NULL NULL
804 Albert Smith NULL NULL NULL
605 Jack Ronal NULL NULL NULL
NULL NULL NULL 11 4 D
NULL NULL NULL 18 4 D
NULL NULL NULL 26 5 F
NULL NULL NULL 32 5 F
I want this table
ID Name Surname School Number Class Number Class Branch
113 Jane Smith 11 4 D
151 John Konl 18 4 D
804 Albert Smith 26 5 F
605 Jack Ronal 32 5 F
I want to Student Table 1 ---> ID,Name,Surname and Student Table 2 --> School Number,Class Number and ClassBranch joın.But joın is not successful.Class Branch A and B removed and D,F is adding.
First table ID,Name,Surname (3 columns) and Second table School Number,Class Number,Class Branch join.
Where conditions:
Removed column --> 4 - A AND 5 - B
Adding column --> 4- D AND 5- F
How can I write query?
Now that you've completely changed the original tables, this should be a simple JOIN.
SELECT t2.id, t1.name, t1.surname, t2.SchoolNumber, t2.ClassNumber, t2.ClassBranch
FROM Student1 AS t1
JOIN Student2 AS t2 ON t1.name = t2.name AND t1.surname = t2.surname
DEMO
To get those results?
1) The complicated way. By joining them.
SELECT
s2.ID,
s1.Name, s1.Surname,
s2."School Number", s2."Class Number", s2.ClassBranch
FROM Student1 AS s1
JOIN Student2 AS s2 ON (s2.Name = s1.Name AND s2.Surname = s1.Surname)
WHERE s1."Class Number" IN (4, 5);
2) The simple way, select only from the 2nd table
SELECT *
FROM Student2
WHERE "Class Number" IN (4, 5);
Test on SQL Fiddle here

How do I change my SQL SELECT GROUP BY query to show me which records are missing a value?

I have a list of codes by area and type. I need to get the unique codes for each type, which I can do with a simple SELECT query with a GROUP BY. I now need to know which area does not have one of the codes. So how do I run a query to group by unique values and tell me how records do not have one of the values?
ID Area Type Code
1 10 A 123
2 10 A 456
3 10 B 789
4 10 B 987
5 10 C 654
6 10 C 321
7 20 A 123
8 20 B 789
9 20 B 987
10 20 C 654
11 20 C 321
12 30 A 137
13 30 A 456
14 30 B 579
15 30 B 789
16 30 B 987
17 30 C 654
18 30 C 321
I can run this query to group them by type and get get the unique codes:
SELECT tblExample.Type, tblExample.Code
FROM tblExample
GROUP BY tblExample.Type, tblExample.Code
This gives me this:
Type Code
A 123
A 137
A 456
B 579
B 789
B 987
C 321
C 654
Now I need to know which areas do not have a given code. For example, Code 123 does not appear for Area 10 and code 137 does not appear for codes 10 and 20. How do I write a query to give me that areas are missing a code? The format of the output doesn't matter, I just need to get the results. I'm thinking the results could be in one column or spread out in multiple columns:
Type Code Missing Areas or Missing1 Missing2
A 123 30 30
A 137 10, 20 10 20
A 456 20 20
B 579 10, 20 10 20
B 789
B 987
C 321
C 654
You can get a list of the missing code/areas by first generating all combinations and then filtering out the ones that exist:
select t.type, c.code
from (select distinct type from tblExample) t cross join
(select distinct code from tblExample) c left join
tblExample e
on t.type = e.type and c.code = e.code
where e.type is null;

How to add a column keeping incremental unique -letter(s)- for each record to a table in sql?

I have a table in sql server 2008 like
Id Name Volume
1 Jack 20
2 John 20
3 White 40
... (23 more)
27 Jennifer 12
28 Brown 33
29 Brown 55
... (23 more)
53 Alex 67
53 Smith 15
53 Mary 73
What I want is to add a column to this table, which keeps unique letter(s) for each different id. Example output:
Id Name Volume LetterKey
1 Jack 20 A
2 John 20 B
3 White 40 C
... (23 more) (...Z)
27 Jennifer 12 AA
28 Brown 33 AB
29 Brown 55 AC
... (23 more) (...AZ)
53 Alex 67 BA
53 Smith 15 BB
53 Mary 73 BC
... (...BZ)
We do not need 3-digit letters, ZZ would be the last possible one. I could not find a proper way to add such a column to the table. I appreciate if someone helps me with it. Thanks.
With only a two character code, you can use row_number() and some character arithmetic:
with t as (
select t.*, row_number() over (order by id) as seqnum
from t
)
select ((case when seqnum > 26
then char(ascii('A') + seqnum / 26 - 1)
else ''
end) +
char(ascii('A') + seqnum % 26)
)
from t

how to get the unique records with min and max for each user

I have the following table:
id gender age highest weight lowest weight abc
a f 30 90 70 1.3
a f 30 90 65 null
a f 30 null null 1.3
b m 40 100 86 2.5
b m 40 null 80 2.5
c f 50 105 95 6.4
I need this result in sql server. What I need is the minimum of the weight and maximum of the weight and one record per user.
id gender age highest weight lowest weight abc
a f 30 90 65 1.3
b m 40 100 80 2.5
c f 50 105 95 6.4
Just do a grouping:
select id,
max(gender),
max(age),
max([highest weight]),
min([lowest weight]),
max(abc)
from SomeTable
group by id
You can do this using grouping:
select id, gender, max(highest_weight), min(lowwest_weight) from student
group by id, gender
But you need do define the rule for the other fields with variable value, like abc
Can you post more information?

How to select value based on between Range in SQL?

I need to get the Result value based on given Input from the following table.
Gender |MinAge |MaxAge | Result
-------------------------------
C 0 25 0.11
M 0 25 1.12
M 26 30 2.25
M 31 35 1.20
M 36 40 3.58
F 0 25 1.25
F 26 30 2.25
F 31 35 1.20
F 36 40 2.02
Input values 1
***************
Gender - M
Age - 28
Expected Output
---------------
2.25
Input Values 2
***************
Gender - F
Age - 32
Expected Output
----------------
1.20
I try with Between condition but unable to find solution. I'm new to SQL. Please tell me how to write query for above condition.
Thanks
select
*
from
your_table
where Gender = 'M'
and 28 between MinAge and MaxAge