I am trying to create a temporary table with information from the orders table in. I then want to join stock.name, and most of the supplier information using the supplier ID which is the foreign key in the orders table. This is my code below, but I seem to get an error in access.
This query will be run in VB to create a temporary table to pass over data to excel. The table should only store one record but should be deleted soon after excel takes the data from it. Thanks in advance to anyone that can help me.
SELECT orders.purchase_order_number, orders.part_number, stock.name, orders.quantity, orders.order_date, suppliers.last_name, suppliers.first_name, suppliers.address_1, suppliers.address_2, suppliers.city, suppliers.postcode
INTO order_temp FROM orders
INNER JOIN suppliers ON orders.supplier_ID = suppliers.supplier_ID
INNER JOIN stock ON orders.part_number = stock.part_number
WHERE orders.purchase_order_number = 'PO10367' and suppliers.supplier_ID = 20
If you are querying from VB (such as VB.net), then when you query to your LOCAL VB app from the connection, you do not need to select into table... The error you are probably getting is that the table already exists and can't recreate it, it must be dropped... But if you just query to VB app, and no table created, you should be good to go.
Related
I have an object in the database which has millions of accounts. Say there is another object which has the accounts that i need for building my report. I want to write a query that selects all from first object but only for the accounts that are in the second object.
If i write
select * from first f inner join second s on f.ID=s.ID
I'm getting the columns in second object which i don't want because there are similarly named fields in both and Tableau throws an error when i connect to it. Can someone help me with this?
Thakns
I want to write a query that selects all from first object but only for the accounts that are in the second object.
You've got these...
-- Object with mIlLiOns of accounts
millions
-- Object with accounts for your report
accounts_for_report
I'd start with something like...
SELECT mil.*
FROM millions mil INNER JOIN accounts_for_report afr
ON mil.ID = afr.ID;
If you wanted to accomplish this without a join (as the subject alludes), you can do this...
SELECT *
FROM millions mil
WHERE EXISTS (
SELECT * accounts_for_report WHERE ID = mil.ID
);
I have a database with two tables, table 'product' and table 'factory'.
The table product has a column called factory_id and the table factory has both factory_id and 'factory_name'.
I have a bootstrap table which outputs all the fields the product table has, but how do I get the factory_name in there? I know I have to join tables but I don't really know how.
SELECT product.*, factory.factory_name
FROM product
LEFT JOIN factory ON product.factory_id = factory.factory_id;
This should work, but next time try including some code you tried yourself when you ask a question.
After looking at example after example of both inner joins and subqueries as fields, I'm apparently not getting some aspect, and I would appreciate help please. I am trying to write one query that must, alas, run in MS Access 2007 to talk to an Oracle database. I have to get values from several different places for various bits of data. One of those bits of data is GROUP_CODE (e.g., faculty, staff, student, alum, etc.). Getting that is non-trivial. I am trying to use two inner joins to get the specific value. The value of borrower category must be the value for my main row in the outer query. Here is what this looks like:
Patron table Patron_Barcode table Patron_Group table
Patron_id Barcode Patron_Group_iD
Barcode Patron_Group_id PATRON_Group_Code
I want to get the PATRON_GROUP.PATRON_GROUP_CODE. This is only one of 35 fields I need to get in my query. (Yes, that's terrible, but wearing my librarian hat, i can't write the Java program I'd like to write to do this in a snap.)
So as a test, I wrote this query:
select PATRON.PATRON_ID As thePatron,
(SELECT PATRON_GROUP.PATRON_GROUP_CODE As borrowwerCategory
FROM (PATRON_GROUP
INNER JOIN PATRON_BARCODE ON PATRON_GROUP.PATRON_GROUP_ID = PATRON_BARCODE.PATRON_GROUP_ID
) INNER JOIN PATRON ON PATRON_BARCODE.PATRON_ID = thePatron.PATRON_ID
));
I don't know what I'm doing wrong, but this doesn't work. I've written a fair amount of SQL in my time, but never anything quite like this. What am I doing wrong?
PATRON.BARCODE is the foreign key for the BARCODE table.
PATRON_BARCODE.PATRON_GROUP_ID is the foreign key for the PATRON_GROUP table. PATRON_GROUP_CODE in PATRON_GROUP is he column value that I need.
PATRON.BARCODE -> BARCODE.PATRON_GROUP_ID -> PATRON_GROUP.PATRON_GROUP_CODR>
The main table, PATRON, will have lots of other things, like inner and outer join to PATRON_ADDRESS, etc., and I can't just do an inner join directly to what I want in my main query. This has to happen in a subquery as a field. Thanks.
Ken
Let me start by saying that I am not using the right tools for the job but they are the only tools I have access to.
I am using Excel as a database that contains multiple tables with associated data. Normally in a database, this data could be associated with a foreign key I believe however that is not the case with Excel.
I have 2 tables:
TABLE items
batch_id customer_id
1 1
2 1
3 2
and
TABLE customers
id customer
1 cust1
2 cust2
I have a userform which only allows the user to select a customer by name.
What I would like to be able to do is update the customer_id in the items table based on a specific batch_id and a customer name.
This is what I have so far that isn't working.
UPDATE [items$]
SET [items$].customer_id=[customers$].id
INNER JOIN [customers$]
ON [items$].customer_id=[customers$].id
WHERE [items$].batch_id='value1'
AND [customers$].customer='value2'
[UPDATE]
The following seems to be a little closer to answer but is giving me a 'Operation must use an updateable query.' error.
UPDATE items
SET items.customer_id=(
SELECT FIRST(customers.id)
FROM customers
WHERE customers.customer=value2)
WHERE items.batch_id=value1;
I keep getting a 'Operation must use an updateable query error' with this but otherwise I see no reason why it shouldn't work
I'm not sure how to put in excel VBA. But the update syntax should be like (if u want to update all the rows)
Update items
set items.customer_id = (select customers.id
from customers inner join items
on customers.id = items.customer_id
where customers.id='Value2'
and items.batch_id='Value1');
Hope this might help u
Try with the following once may be you will get your expected result.
But this is the case of SQL.
UPDATE items i
JOIN customers c ON i.customer_id = c.id
SET i.customer_id = c.customer
I am trying to understand the JOIN command from w3schools tutorial and there is an example.
Can you please "translate" it to me what exactly it does? I already know what do the dots do, but INNER JOIN, ON and so on messed me up?
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
And does it creates a new table in my SQL database or it just creates (lets call it..) "virtual" table which I can use it in the moment
The dot notation here signifies the column of a table.
Table.Column
So the SELECT is retrieving the columns specified from each table.
JOIN matches the tables based on the condition that appears after ON
this queries joins customers to orders based the condition of having the same CustomerID.
For more on joins check out http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Perhaps the first thing to understand is the "dot notation." When you see a value like Orders.CustomerID SQL will read that to mean the CustomerID column within the Orders table.
The INNER JOIN is looking for an exact match for the records you specify with the ON clause...
So, in this example, SQL will first find all the records in the Orders table which have a CustomerID that matches the CustomerID found in the Customers table.
Then, it will look to the SELECT clause and show you the OrderID (from the Orders table), the CustomerName (from the Customers table) and the OrderDate from the Orders table.
If you're using SQL Server Management Studio (SSMS) or some other similar tool, it should display your results - kindof like a virtual table... but it doesn't actually create a table.
Hope that helps,
Russell
There are INNER AND OUTER JOINS. I think this post sums up those differences well.
What is the difference between "INNER JOIN" and "OUTER JOIN"?
As far as the concept itself you are 'joining' the tables by finding things in common between them based on a shared value in each. If you have ever done a vlookup with Microsoft Excel then you are familiar with this concept. In the query you've posted you are selecting values from both the Orders and Customers table. You can tell which table you are selecting values from by the prefix Orders. or Customers. You can then tell which column by the value following the dot i.e. OrderID, CustomerName, OrderDate.
The way the query knows to pull a conjoined field is if the CustomerID field matches.
So step by step
You are selecting
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
From the table Orders
FROM Orders
You wish to pull corresponding values from Customers. There are numerous join methods and logic that goes with each but you have chosen INNER JOIN explained in the link above.
INNER JOIN Customers
The criteria for joining is CustomerID
ON Orders.CustomerID=Customers.CustomerID;
To use an approximate Excel Vlookup analogy
The Lookup_value is CustomerID
The Table array is ORDERS
The range lookup is the type of join you are performing
SELECT statement returns data, it doesn't create any table not even virtual table whatsoever (if you see any table after executing SELECT statement, thats just a way the returned data presented). Create and modify table done using DDL (Data Definition Language) statements when SELECT is one of DML (Data Manipulation Language) [reference].
Join statement explained very well here, from basic use to more advance with illustration.