I've got the following problem to solve with an excel table:
Column A contains country names and column B, values, as the example below shows.
There are some names in column A that came with aditional words added to it, so i need them to be treated like an unique country other columns.
Follows an example:
A | B
------------------------------------
Country Name | Number
------------------------------------
ITALY (MOBILE) | 100
PORTUGAL (MOBILE) | 180
UNITED KINGDOM (MOBILE) | 160
ARGENTINA BUA | 120
FRANCE MOBILE ORANGE | 100
CHINA (MOBILE) | 100
ITALY | 93
SPAIN (MOBILE) | 90
PORTUGAL | 85
GERMANY (MOBILE) | 75
UNITED KINGDOM | 10
GERMANY | 70
ECUADOR (MOBILE) | 55
The exit could be a table like the following, in the same worksheet, to columns D and E, for example.
It would sum countries values and show them as the right unique country name (The right name would be the first one that shows up, before the "(", and without the values between "()" ).
A | B
------------------------------------
ITALY | 193
PORTUGAL | 265
UNITED KINGDOM | 170
GERMANY | 145
ARGENTINA | 120
FRANCE MOBILE ORANGE | 100
CHINA | 100
SPAIN | 90
ECUADOR | 55
Is it easier using VBA?
Thanks, guys!
Here's what I would do:
Label column C Country (or whatever you need) and write this formula in column C:
=SUBSTITUTE(A2," (MOBILE)","")
Create a pivot table on columns A through C, with column C as your row labels and the sum of column B as your values.
#pnuts makes a good point below. I'd use this formula instead in part 1:
=IFERROR(TRIM(REPLACE(A2,FIND("(",A2),FIND(")",A2)-FIND("(",A2)+1,"")),A2)
Related
Say I have this very simple table with duplicate entries. Is the relationship between the A and B columns one-to-one or many-to-many?
A
B
C
1
2
x
1
2
y
Undoubtedly a simple question, but I can't find confirmation for this corner case... Thanks in advance!
EDIT: Changed the content of the table to avoid stick to the math definition.
As I said in the comment this is a one-to-many relation. for clarifying, let's take a look at this example(I normalized your table into these bellow tables):
Suppose You have a table entitled with continent like below:
id title
---|--------
1 | Asia
2 | Europe
3 | America
Now we have another table with this name country like below:
id title continent_Id
---|----------|--------------
1 | Norway | 2
2 | Germany | 2
3 | Canada | 3
4 | Japan | 1
also, we have a state table with this structure:
id stateTitle country_Id
---|----------|--------------
1 | Munich | 2
2 | Berlin | 2
3 | Torento | 3
4 | Tokio | 4
5 | Osaka | 4
I'm working on an assignment for class. I need to write an SQL statement that returns every single country in the database where no single ethnic group makes up more than 25% of the population. This is the closest I can get:
SELECT * FROM ethnicgroup WHERE percentage < 25;
But, that only returns records where ethnic groups are over 25% population. It doesn't remove other rows in the table where a different ethnic group for the same country is under 25%.
The table looks like this (I limited the snippet below to 10 entires. It's much, MUCH bigger.):
country | name | percentage
---------+------------+------------
AL | Albanian | 97
AL | Greek | 0.9
AL | Macedonian | 0.9
AL | Roma | 0.3
AL | Aromanian | 0.3
GR | Greek | 91
GR | Albanian | 4.4
GR | Roma | 2
GR | Macedonian | 1.5
GR | Turkish | 1
I think you are looking for this
SELECT country FROM ethnicgroup
GROUP BY country
HAVING MAX(percentage)<25
I have to do this in SQL.
I have a table called 'locations'. It contains a list of locations ranging from houses, to streets, to cities all the way up to continents.
locationId | name | desiredValue
1 | Wimbledon |
2 | Peckham |
3 | London |
4 | UK |
5 | France | 123
6 | Europe | 456
7 | Australia |
8 | Paris |
I have a second table called 'links' which contains the link of locations, and their relation
Location1 | Location2 | Linktype
3 | 1 | 5
3 | 2 | 5
4 | 3 | 5
6 | 4 | 5
5 | 8 | 5
linktype 5 indicates that location2 is situated 'in' location1. In the example above, locationId 1 (wimbledon) is located 'in' locationId 3 (london). LocationId 3 (london) is located 'in' locationId 4 (Europe) and so on.
The linktype just describes this 'in' relationship - the link table contains other relations as well which are not pertinent to this question, I just mention it in case it needs to be in a where clause.
For a given location, I want to get the first instance in its location hierarchy that has a 'desiredValue'
For example:
if I was interested in Peckham, I'd like to see that Peckham has no value, that London has no value, that UK has no value but that Europe does (456).
If I was interested in London, I'd see that it has no value, nor does the UK, but that Europe does (456)
If I was interested in Europe, I'd see that it has a value (456)
If I was interested in Paris, I'd see that it has no value, but France does (123)
I know I should probably be using recursive CTEs for this, but I'm stumped. Any help would be greatfuly received!
I want to perform some kind of combinatorics from a database.
my database table:
start | end | costs | date
____________________________________
berlin | Moscow | 100 | 2014-12-10
berlin | paris | 200 | 2014-12-13
Moscow | berlin | 150 | 2014-12-20
Moscow | berlin | 100 | 2014-12-11
Possible pairs are all start-end combinations that have an equal combination ofend-start.
In my table, this applies to berlin-moscow and moscow-berlin.
I want to compute "roundtrips" going from one city to another, and returning later to the same startcity.
The resulting table I want to achieve would be:
start | end | costs | away | wayback
berlin | moscow | 250 | 2014-12-10 | 2014-12-20
berlin | moscow | 200 | 2014-12-10 | 2014-12-11
(this implies that when starting in berlin and going to moscow, the wayback will be moscow-berlin).
Is that possible with database queries?
First of all: how could I selfjoin a tables and get all distinct start-end pairs?
As a comment, i would advise you to not use reserved words as End for user defined objects as tables or columns.
SELECT F.Start,
F."end",
F.costs + R.costs AS costs,
F.date AS away,
R.date AS wayback
FROM Table1 F
JOIN Table1 R
ON F."end" = R.Start
AND R.date>F.date
AND F.start = R."end"
SQL Fiddle Demo
Is there a more efficient way of querying a table (or collection of table) for all possible combinations of a few columns, I'm currently running group by and then max, but this doesn't seem to be the most efficient way.
SQL Fiddle for the below example: http://sqlfiddle.com/#!2/25f8b/3
Example Table
ID | Name | Age | City | Color
--------------------------------
1 | Dave | 10 | London | Red
2 | Dave | 11 | London | Purple
3 | Dave | 10 | Paris | Orange
4 | Jim | 10 | London | Red
5 | Jim | 10 | London | Green
6 | Jim | 11 | London | Lazer
etc... (around 500,000 rows)
Currently doing:
SELECT max(ID), Name, Age, City, Color
from People
group by Name, Age, City
To produce:
MAX(ID) NAME AGE CITY COLOR
1 Dave 10 London Red
3 Dave 10 Paris Orange
2 Dave 11 London Purple
5 Jim 10 London Red
6 Jim 11 London Lazer
Note 4 is missing as it's a exact duplicate of 5
3 Is included as it has a different city to 1, even with same age/name
However currently on this massive database it takes around a ten minutes to return the results (note it's actually a join of a few tables)
Is there a more efficient way to return the same results? I was imagining a mass collection of SELECT * WHERE name = %, age = % and city = % LIMIT 1 or something similar
To get the different combinations there is as reserved word DISTINCT :
SELECT DISTINCT Name, Age, City
FROM People
This gives the same result as :
SELECT Name, Age, City
FROM People
GROUP BY Name, Age, City
However it is limited :
If you add a column (like Color), it is included in the combinations analysis
You can't use aggregate functions, like MAX
I don't know if it's any better performance wise