I need to link rows of one table to pull data based on columns of another table. I am attaching the picture that shows the two tables and the desired result. I would really appreciate any help with this. If it can't be done then any workaround will be appreciated.
Use UNION query to rearrange data in Table2 into normalized structure it should be in the first place. There is no query designer or wizard for UNION, must type or copy/paste in SQLView of query builder.
SELECT Site, "Infant" AS Category, [Capacity Infant] AS Capacity FROM Table2
UNION SELECT Site, "Toddler", [Capacity Toddler] FROM Table2
UNION SELECT Site, "Preschool", [Capacity Preschool] FROM Table2;
Another approach:
SELECT Site, Category, DLookUp("[" & "Capacity " & [Category] & "]","Table2","Site='" & [Site] & "'") AS Capacity
FROM Table1;
Either can perform slowly with very large dataset, not sure which would be slower.
Related
I have a database of stock tickers. Each ticker has a "structure" that signifies the type of deal we participated in in order to receive the shares. Examples of structures are Primary Offerings ("PO"), Secondary Offerings ("SEC"), "CMPO", "PIPE", etc. It is possible that the database includes a ticker multiple times with different structure. For example, the ticker "AAPL" could exist 2 times - one time with a structure of "PO" and another time with a structure of "SEC". I am trying to write a query that gathers all of the instances of a ticket if at least one of the structures is "PO". I want to show all of the structures that we have participated in in that ticker.
select * from #mytable where structure = 'PO' will only show me the deals with structure PO. If a ticker has a structure equal to 'PO' then I want all deals with that ticker regardless of the structure.
I have also tried:
select * from (select distinct ticker from #mytable where structure = 'PO') PO
join #mytable m on PO.ticker = m.ticker
This works, but joining a table to the same table seems redundant and it seems like there should be a better way to do this.
I am writing this query in SQL -- could someone please help me write a query for this situation? Thank you!!
I don't think there is a way around selecting from the table twice. however if you don't like the self join you could do a correlated subquery.
select * from mytable where ticker in
(select ticker from mytable where structure = 'PO')
I have two tables
table A contain many ids, which one is related to the table B.
I have to make a query on the table A to select only some ids and then take the data from the table B using the selected ids from the table A
How can i do that? (MYSQL)
Based on your description, here is a simple structure of how the query might go, although I recommend you share some sample results & the desired output.
SELECT * FROM tableA
JOIN tableB
ON tableA.id_song == tableB.id_song
WHERE "your condition HERE"
I have two tables that have different data that I need to merge. They do have similarities such as: Order number, Name, type or product. But they have separate data as well like: Order date, and Engravings.
Would I do two separate Append queries in Access into a merged table? Or one Append queries? Or just keep the data separate?
I am new to Access and trying to find the best way to approach this.
Merging the two tables into one completely defeats the purpose of using a database and you're better off using excel at that point. You want to split the data as much as possible along logical lines so that you can find, say... all the orders that Mr X has ever made for a specific product. And in that case you're going to want to have separate tables for customers, orders, engravings and the like.
The best practice from a design standpoint is to place fields that each table has in common into a third "master" table, then create relationships from that table to the existing tables and delete the data that has been transferred to the main table (except for the primary keys, which have to be common with your master table).
To create the master table, use a Make Table query to generate the master table based on one of your tables, then an append query to add any products in the master table that might not be common to both, based on the other table. Finally, delete queries for each table would rid you of redundant data in both original tables.
However, I strongly suggest you use Microsoft's tutorials and download the NorthWind sample database so you can get an idea of what a properly structured database looks like. The beginner's learning curve for access is very steep and having well built example databases is almost a requisite.
Make a backup of your database(s) and play with it until it turns out right. Do not make the mistake of playing with live data until you know what you're doing.
As you have similar fields on either table, take the Order number field from both tables using a union query. Something like:
SELECT tbl_Delivery_Details.OrderNo
FROM tbl_Delivery_Details
GROUP BY tbl_Delivery_Details.OrderNo
UNION
SELECT tbl_Delivery_Header.[Order number]
FROM tbl_Delivery_Header
GROUP BY tbl_Delivery_Header.[Order number];
This would take the order numbers from the delivery details table and from the delivery header table and merge them into one list with only one instance of each order number. Save the query.
You could then use this query in a new query. Bring in your 2 tables to this query and insert the fields from either table that you require.
As users add records to the tables they will be added to the union selet query when it is next run.
PB
It depends on what you want to do. Let's assume you have tables A (with 50 records) and B (with 75) records, and both tables have a similar column called OrderID.
Appending Rows:
If you want to create a table with 125 total records by combining records (rows) from A and records (rows) from B, run the following two queries:
Query 1:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1 as DATA INTO C
FROM A;
Query 2:
INSERT INTO C ( ORDER_NUMBER, DATA )
SELECT B.ORDER_NUMBER, B.TEXT_FIELD2
FROM B;
Appending Columns: If you want to create a table with 75 total records where you are appending columns from A to the columns in B, then run the following query:
SELECT B.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A RIGHT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
... in a similar way, you can append columns in B to columns in A in a new table C with a total of 50 records by running the following query:
SELECT A.ORDER_NUMBER, A.TEXT_FIELD1, B.TEXT_FIELD2 INTO C
FROM A LEFT JOIN B ON A.ORDER_NUMBER = B.ORDER_NUMBER;
This site has been super helpful with this database i am creating for work, i have a lot of VB knowledge but little SQL.
i am creating a database of cisco IP phones which records the MACs, serials and to what site id they are assigned to.
My managers have now decided to change the goal posts of my application once again and are now asking for me to create a form that displays data in a way i have no idea how to do.
what they now want and what i am hoping to achieve somehow is to create a sql statement that gets the count of the number handsets by site id and then to compare that number against the data from another table.
Here is the fields that we need to work with:
TABLE 1
Handset_Type
Handset_Site_Id
TABLE 2
Handset_Type
Handset_Site_Id
This is how i would like to display the data
SITE ID | Handset_Type1 | Handset_Type1
Handset_Site_id1 |COUNT TYPE1 FROM T1|COUNT TYPE1 FROM T2
Handset_Site_id2 |COUNT TYPE1 FROM T1|COUNT TYPE1 FROM T2
If we can use the data in the Handset_type field to create the column headings then that would be great as there are only a few models of phones but if they are static columns then that will be ok also.
I hope i have explained this ok as i can see this may be complex to understand.
if you have any suggestions please let me know
Thank you
Steve
If I understand the question correctly, you would like to present the count of records from both tables, with the handset type and source table as column headings, for each site ID.
The first step is to merge your tables into a single table, creating a column which expresses the handset type and source table:
SELECT Handset_Type & " in T1" as TypeFromT, Handset_Site_Id FROM [Table 1]
UNION ALL
SELECT Handset_Type & " in T2" as TypeFromT, Handset_Site_Id FROM [Table 2]
This will form an inner query for the crosstab. The syntax for crosstabs can be a bit tricky so I usually build mine in design view. We define the TypeFromT as our new column heading and we use Handset_Site_Id both as our row groupings and as our counting field:
TRANSFORM Count(SubQry.Handset_Site_Id) AS SiteIDCount
SELECT SubQry.Handset_Site_Id
FROM (
SELECT Handset_Type & " in T1" as TypeFromT, Handset_Site_Id FROM [Table 1]
UNION ALL
SELECT Handset_Type & " in T2" as TypeFromT, Handset_Site_Id FROM [Table 2]
) AS SubQry
GROUP BY SubQry.Handset_Site_Id
PIVOT SubQry.TypeFromT;
The only catch is that if there are zero entries for the particular Handset Type, Source Table, and Site ID, the resulting value will be null instead of a 0. If that is undesirable, replace Count(SubQry.Handset_Site_Id) with Nz(Count(SubQry.Handset_Site_Id),0)
I wanted to know if it was possible to make a select on a table that contains multiple field and join them in 1 result :
Example :
Table :
id
dayOne_City
dayTwo_City
dayThree_City
Result : one column that contains the rows of all the cities (Distinct).
2) Am i better to do a view if i have a lot of query to that specific list ?
3) should i do 3 select with union ?
Thank you
You should be fine with:
select dayOne_City from YourTable
UNION
select dayTwo_City from YourTable
UNION
select dayThree_City from YourTable
However, you should review your design to allow multiple cities per whatever-is-that-your-table stores. That is, create an actual many-to-many relationship by creating an intermediate table between YourTable and Cities.
select concat_ws(',', id, dayOne_city, dayTwo_city, dayThree_city, etc...) as allInOne
Details on the function used here. However, I should ask why you're doing this. By joining the fields together, you're destroy any chance of reliably extracting/separating the data again later. Only do this kind of "bulking" if you never plan on using separate portions of the data elsewhere based on the results of this query.