combining data from multiple tables to excel - sql

I am trying to get data from multiple tables into one dataset in excel. I am a beginner sql.
Each table has Patient Number as a unique identifier.
Table 1 may have 1 columns that result in 4 rows of data for any given patient.
Table 2 may have 7 columns that result in 26 rows of data for any given patient.
and so on. I pull data from 24 tables and it ends up being 61 columns and about 8000 rows. Problem is, if I try to join all of these tables with the Patient Number, I end up with 10's of thousands of rows returned.
Right now I am just running 24 separate queries, putting the results of each one in a separate tab on my spreadsheet and then running a macro to combine all the tabs into one spreadsheet with 61 columns.
Seems like sql should be able to do this. But I am a beginner so I am having trouble figuring it out.

Question was not exatcly clear, But what I understood is that you need a easy way to save thease data in "sql" to a spreadsheet. I recommend you to use CSV(Comma separated Vlaues) formatting .Because You can easily define the Column headers on your own and it easy to add data to each column and you can foget about the "Tab" thing which is a complex approach.
Use ArrayList to save all pataients Objects.Each and every object in that List will carry all the information for a particular patient ,which fetched from your SQL Database.
Refer this for more info in CSV
Hope this Helps!

Ok I got some help with this.
I ended up using a Union All. The problem with that before was that all my different tables had different numbers of columns and different numbers of rows for my key identifier. So I can use a Union All if I use Null in my select statements so that I have the same number of columns in all my select statements. Then the query just stacks all of the data together:
Many thanks to Kris at http://www.essentialsql.com/
I have 19 tables and 62 column, so I have a way to go before I finish the query.
(SELECT PatientID, 1 as TableOrder, A1, A2, NULL as B1, NULL as B2, NULL as C1, NULL as C2
FROM TableA
UNION ALL
SELECT PatientID, 2, NULL, NULL, B1, B2, NULL, NULL
FROM TableB
UNION ALL
SELECT PatientID, 3, NULL, NULL, NULL, NULL, C1, C2)
ORDER BY PatientID, TableOrder

Related

SQL question: How to get a list of cities?

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.

Joining Different Database Tables

I have two tables in Access pulling from databases. There is no primary key linking the two tables, because the databases pull from different programs.
Using SQL, I need all of the information from both tables to pull into a query, and this is where I have problems. The two tables are pulling the same data, but they column titles might not necessarily be the same. For now, I'm assuming they are. How can I get it so that the data from both tables pull into the correct column together?
Here's an example of code (I can't post the real code for certain reasons):
SELECT system1_vehiclecolor, system1_vehicleweight, system1_licenseplate, system2_vehiclecolor, system2_vehicleweight, system2_licenseplate
FROM system1, system2
To further explain this, I want the table to have a column for vehiclecolor, vehicleweight, and licenseplate that combines all of the information. Currently, the way I have it, it is making a column for each of the names in each table, which isn't what I want.
You can use 2 queries to get this done
Select col1as c1 ,col2 col as c2 into resulttable from table1
Insert into resulttable (c1,c2) select colX as c1, colY as c2 from table2
Hope this will help you

Inserting lots of rows that only have a single value change into table with lots of column

I have a table I need to insert about 260 rows into, the data will be exactly the same EXCEPT for the value of a single column "project". If this was a small table I would just write it all out using a UNIN ALL but the problem is there are 66 total columns in the table and that is a LOT of repetitive typing. Is there a method of inserting nearly identical info without having to repeat it all like this? If it makes any difference it is on an MS SQL 2008 R2 server.
Assuming I'm understanding your requirements correctly, something like this could perhaps work with a subquery building the project values:
insert into yourtable
select 1, 'Another Value', ..., t.project
from (select 1 as project union all select 2 ... select 260) t
Depending on your table structure, you may need to supply the column names.

Creating a join list between two tables VBA

Good afternoon,
I am still quite a novice with VBA but am trying to create a loop that will be able to sift through a long list of data within a given column (in my case, both tables have one common identifier, the system ID) and if a system ID is matched in one column with a column from the other table, then a new sheet is created that combines all of the rows associated with both sets of data into one row.
For example, if my data looked like this:
Table 1
Column A, Column B, Column C |
ID, Name, Birthday
Table 2
Column A, Column B, Column C|
Purchase, Amount, ID
And I had the same ID in both Tables 1 and 2, for each match, I would like to have all rows associated with the match joined together.
This would really enable me to speed things up with organizing information, so I was not sure if it would be possible... Any Ideas are welcome!
since excel is not a database program like access, you can not use sql-like joins natively. you would have to program your own join function:
(Since i do not have MS Office installed, i can only give you pseudo-code)
for each-loop going through IDs of Table1
for each-loop going through IDs of Table2
if(Table1.ID = Table2.ID) then
copy data of Table1 into a new sheet
copy data of Table2 into the same sheet, next to Table1 data
PS: i assume you use excel because of the vocabulary (column, worksheet,..)

Compare two tables look for gaps, append them in new table

This is related to MS Access 2007. I'm in bit of a pickle and anyone who can solve this for me I will be greatly indebted to.
I have two tables: Actual and Schedule. My job is to compare these two tables, look for gaps, and fill them in a new table (append). Important point: tbnum corresponds to timeband. It's something I came up with to identify gaps easily. I have created these sample tables:
http://i39.tinypic.com/2mhesxs.jpg
There are 2 scenarios:
JFKATL: there is a match in Actual table, so bring the original record for JFKATL from Schedule as well as from Actual for the missing JFKATL record into NewTable
ORDSLC: there is no match in Actual table, but there is an apparent "gap" in timeband. So extend the timeband for the unmatched record and paste it in NewTable
Edit: Sorry, there was a small issue i noticed in the original image. Posted new one.
I may not understand your problem completely or correctly, but it appears that an UNION operator will work for you. UNION will combine the unique records between two queries and merge them into one recordset. UNION ALL will include duplicates.
SELECT c1, c2
FROM table1
UNION
SELECT c1, c2
FROM table2;