Pretend there is a database with every city in the world and a unique ID to go with each one. I have a list of 50 IDs in one column of an excel document.
How do I return the names of the 50 cities most efficiently? Do I really need to do a WHERE clause with ID# OR ID# ....etc?
You just do a in
select cityname from tableofeverycity a where id in (select id from tbl50 ids)
In SQL terms you would be looking for something like
SELECT names FROM tablofcities
This code assumes that you want them in the same order that they are listed in.
Because you mention that these are listed in a excel document rather than a database, it makes it a little different.
I'd recommend checking out the following link for more details on your question.
How to run a SQL query on an Excel table?
In Excel, create a formula that is essentially (assuming the empty cell is A1):
empty
1 =B1&","&A2
2 =B2&","&A3
3 =B3&","&A4
You can write the formula once and copy it down.
The results will look like:
1 ,1
2 ,1,2
3 ,1,2,3
Go to the 50th row and copy the formula.
Next, paste them into a query in your favorite GUI:
select c.*
from cities c
where cityid in (<paste list here>);
Remove the first comma.
Run the query.
Related
I have a database with a couple tables that tracks personnel errors that require rework by another person. Basically, a person on the job could rework up to 10 different work packages by other people throughout their shift. To make it easy, I just have columns in the table for rework_1/original_worker_1/rework_comment_1 (repeated up to 10) and the person who had to rework it. All of my worker's names are in a separate table so I can add people and my forms update dynamically with their names. What I want to do is this:
Pull a person from my worker's name table.
Search for all occurrences of their name in another table in in column original_worker_X (where X is 1 - 10).
Output the values: Workers Name / How Many Times I found it in the original_worker_X columns.
From here I would need to make a bar graph so that each person's name had a bar with how many times someone had to rework something they did originally.
If I could do this with PHP and MySQL I would be in the money because I could brute force something with some PHP variables, queries, and loops but I am an access novice at best! I appreciate any help you wizards can provide.
Table 1:
Table 2:
Expected Output Numbers:
so i will suggest you do the following
Create a new table,lets say table 3 with three fields
A. ID, pkey, auto number
B. original_worker, text field
C. Person_doing_rework, text field
You will need ten insert statements that will insert each of the original worker 1-10, as well as person doing re-work , this is to a normalise table
Currently, the design of your table is a bit crude, and having a select statement with group by columns numbering 10 is not achievable
Below are samples of the insert statements
INSERT INTO Table3 (original_worker,Person_doing_rework)
SELECT original_worker1,Person_doing_rework
FROM table2 where isnotNull(original_worker1)
INSERT INTO Table3 (original_worker,Person_doing_rework)
SELECT original_worker2,Person_doing_rework
FROM table2 where isnotNull(original_worker2)
replicate this for original_worker3 to original_worker10
Third step
You need a delete statement that will delete all from table 3, this is to ensure that the records from table 3 is not duplicated, since we don't have a pkey/fkey relationship between table 2 and 3
Fourth step
Place all the queries into a macro in the following order
A. Delete query to run first
B. Insert queries to run next
Fifth step
Add a msgbox in the macro, that will run last, this is to inform you that all the other macro steps, i.e A and B above has successfully run.
Sixth step
You can now have a select statement from table 3 that can count the number of times an original workers' work is re worked upon, because you now have two main fields in table 3, one for original_work, and two for Person_reworked.
So any time you want to find out how many times some ones work has been re worked upon, you have to just click the macro button, this will run all the queries and put values you need in the table 3, after which you can view the details via the query in step 6.
SELECT original_worker, Count(Person_doing_rework), FROM table3 GROUP BY original_worker;
I have a table of data in Excel. Column A contains Names, Column B contains their interest. Each interest has a separate row. I want to take the data from this table and have a single row with the name of the customer and a column for each of their interests. IE RAW Data:
I am looking to take the 4000 row table and grouping by the name. I am unsure how many times each name appears in the list (Once or Fifty times) but I want the interests placed on a single row with each interest in a separate column EG Desired Data:
I have tried the standard transpose....html table....and pivot tables but it will put the interests all in a row along the top regardless if the customer is interested or not and using a record count T/F that means the data sheet in harder to understand then if I leave it as one block and sort by name
Sure I am not alone with this but all searches for the past 2 hrs keep returning pivot/transpose or duplicate items. Any is appreciated
If you don't want to use VBA, you could first add a column, for instance in column C, with the title "InterestNum."
In C2, just put 1.
In C3, put =COUNTIF($A$2:$A2, $A3) + 1. This will find the number interest it is for the person.
Make a lookup column, for instance in column D. In D2, put =A2&C2
Then, make a list of all the people. I assume that you put this list starting in cell A2 of a new sheet. Then put headers starting in B1 so that B1 contains the title "1" and C1 contains the title "2" standing for the interest number and as many columns as you wish.
Then in Cell B2, put the formula =IF(ISNA(MATCH($A2&B$1,data!$D$2:$D$5,0)),"",INDEX(data!$B$2:$B$5,MATCH($A2&B$1,data!$D$2:$D$5,0)))
This assumes that your original data is in the data tab. I only tested with 4 rows, so you would need to change $D$2:$D$5 to have as many rows as you do. This works by looking up a combination of the name and interest number. It first checks to see if that combination exists in the data. If not, it leaves that interest blank. If so, it finds the actual interest by going to the same row of the lookup.
First remove duplicates using standard excel functionality to prevent having the same interest twice for a person.
Now, you could of course use VBA and perform exactly what you need.
However, I suggest that you use the pivot table.
If your data looks something like this...
... just use "Insert | Pivot table" and insert a pivot table to a new worksheet.
Then, configure the columns as follows:
Et voilĂ , there you have all your interests listed only once and if a person shares an interest, there is a "1".
If you would rather use VBA, just comment and I will edit my answer.
Okay, so let's say I have two tables in Excel. I want to create a third table that looks up data from the other two and combines them. I'll explain what I mean
Here is my first table:
It has a number, which is like a primary key, and a corresponding name for each entry.
I also have a second table:
This one just contains some random info, and there's numerous entries. The Client Name is not listed here, but it does have the key for the client that each entry corresponds to.
I want to create a third table that shows me all the Client Info data, but replaces the key with the actual client name. So basically, wherever it sees a key of "1", it'll look that up from the first table and find "Comet" instead.
The desired table would look like this:
What kind of formula would I need to pull data from one table based on a value? Note that all my tables are in different worksheets.
Assuming that the three tables are in Sheet1, Sheet2, Sheet3, this is the formula you need in Sheet3, cell A2
=vlookup(Sheet2!A2,Sheet1!$A2$b$6,2,false)
Copy down as many rows as there are rows in Sheet2
If you need the client info from Sheet2, use this in Sheet3, cell B2 and copy down
=sheet2!B2
As you put an "SQL" tag, I assume that you are also looking for an SQL based answer. So in addition to #teylyn suggestion, you can use the following SQL query if you are working with SQL databases:
SELECT table1.Client_Name, table2.Client_Info
FROM table1
RIGHT JOIN table2 ON table1.ID_Number = table2.ID_Number
Here is what this query does:
The RIGHT JOIN will return all rows from the right table (here table2), with the matching rows in the left table (here table1) according to the condition specified by the ON clause (here, if the ID Number is the same). Then, the SELECT clause will return a table with ONLY the columns specified after the keyword SELECT.
A B C
1. c First Last
2. d First Last
3. e First Last
4. c First Last
5. c First Last
I'm trying copy the information from columns B & C over to another worksheet (tab in the same document) based on the letter in column A. I only want first and last names to be transferred if the letter in Column A is a "c". Is there a formula or macro I can use to do this?
I think the easiest answer is to create a pivot table based on on the data on your first worksheet. You can format the pivot table so that it looks similar to your original spreadsheet and then filter your column A to select only the letter "C".
One thing to be careful of, is that when you select your data range for the pivot table, be sure to select the entire columns. that way you can add more names and the pivot table will pick it up.
A couple of notes of concern in the pivot table options:
turn off the totals and subtotals to make the pivot table look more
like your original spreadsheet
set it to update automatically, so that you don't have to be constantly refreshing
Hope this helps!
I am using SQL and PL/SQL.
I have a table with a single column "name". The data in this column is a semicolon separated string. I want to count the number of elements in each row.
For example, if there are two rows in the table, one with the string 'smith;black;tiger'and one with the string 'x;y', I want the result of the query to be 3 for the first row and 2 for the second row.
How can I write a SQL query that will count the number of elements in a separated list of values?
Your question is very hard to understand, but I think you are saying that you have a table with a column "name", and inside that column, each cell contains multiple values separated by semicolons. You want to count the number of semicolon-separated values in each row.
The PLSQL string functions could help you here, but really you are talking about writing program code to do this task (this isn't normally the job for a database query language). For example, you could use this trick to count the number of semicolons, and then add one:
LENGTH(name) - LENGTH(TRANSLATE(name,'x;','x')) + 1
But the point #Johan is making is that this is a bad way to structure your data. For example, it makes it impossible to lookup by name properly. You can't say where name == "smith", because the name doesn't equal "smith", it equals "smith;black;tiger". You'll have to do a substring search, saying where name like "smith", and that will be inefficient and wrong. What if I ask to look up the name "smi"? You'll say where name like "smi" which will incorrectly find that result -- there is no way to say that "smith" is in the table but "smi" is not, because both are substrings of "smith;black;tiger".
A much better solution, if you want an entry to have multiple names, is to give the entry a unique ID; say 123. (You can ask SQL to automatically generate unique IDs for table rows.) Then, have a separate table for names which maps back onto that row. So say you gave the "smith;black;tiger" row the ID 123 and the "x;y" row the ID 124. Now you would have another table NameMap with two columns:
name | entry
------------------
smith | 123
black | 123
tiger | 123
x | 124
y | 124
Now you can look up names in that table and map them onto the name entries table with a join.
And you'll be able to answer your question: "how many names correspond to each row of the entry table" with a GROUP BY statement like this:
SELECT name, count(*) as value_count FROM NameMap GROUP BY name
You can hack this using this code
SQL code horror
SELECT name
,(LENGTH(COALESCE(vals,''))
- LENGTH(TRANSLATE(COALESCE(vals,''), ';',''))) + 1 AS value_count
FROM table1
ORDER BY name
Remarks
CSV in a database is a very bad anti-pattern.
VALUES is a reserved word, it's a bad idea to name a column after a reserved word.