Change Value in Access VBA - sql

I have a table full of code IDs and their descriptions in access. And in another table is a field that has code IDs that correlate to the IDs in the Codes table. I am trying to design a macro that when executed will replace the code ID in the second table with the correct description but I am unsure a way to do this. I was thinking of using a SQL Insert query to do so but am unsure of what the statement would look like.
JOIN statement:
SELECT ShouldImportMetricsIDsTable.FORMULARYID, ReasonCodes.Description
FROM ShouldImportMetricsIDsTable,ReasonCodes
INNER JOIN ReasonCodes
ON ShouldImportMetricsIDsTable.ReasonCode=ReasonCodes.CodeID

Mention ReasonCodes only once in your query's FROM clause.
Change this ...
FROM ShouldImportMetricsIDsTable,ReasonCodes
INNER JOIN ReasonCodes
To this ...
FROM ShouldImportMetricsIDsTable
INNER JOIN ReasonCodes
As general advice, I suggest you begin your queries in the Access query designer. At least choose the data sources (tables or saved queries) and set up joins there.
With your original example, the designer would have applied an alias, ReasonCodes_1, for one of those duplicate ReasonCodes names. And that could be an early warning that the data sources aren't correct.

Related

sql - perform join excluding repeated fields

I have the following problem, I use sql server, I need to join two tables by a field, but when performing the join I am duplicating the key field, my query is as follows:
select A.*, B.*
from Database.dbo.Module1 A
LEFT JOIN RRHH.dbo.Module2 B on A.key1 = B.key1
is it possible to exclude from the select the key1 field from the module2 table?
In the tables, I have another few duplicate fields, I could write every field I need from the tables in the select, but , it would be easier to exclude the fields I don't need. Consider that each table has hundreds of fields that are needed.
It is impossible. Specify fields you need.
There is no "all columns except <these>" syntax in T-SQL, sorry.
Of course it's very easy to generate the list of columns from any table by simply dragging the Columns node onto a query window. This works in both SSMS and Azure Data Studio, as I describe in this Bad Habits post:
Then just prefix the ones you need, and delete the ones you don't.

How to see pull up all relevant info from a table given a list of values SQL

Currently I have a list of manufacturing numbers in a table called "Mfr_Numbers_Table".
I also have a master table called Billings
I would like to generate a query where I see all line items that matches with what is on Mfr_Numbers_Tables from the Billings Table.
How do I do this? I can just write 50 queries with a where clause being the Mfr_numbers, but that would take too long.
You need put the tables in a relation in your query.
This is done by a join on the tables on a field which is in both of them.
For example if this field is named MfrNumber:
Select Billings.*, Mfr_Numbers_Table.*
From Mfr_Numbers_Table Inner Join Billings
On Mfr_Numbers_Table.MfrNumber = Billings.MfrNumber
Usually you don't select all fields from all joined tables, but select what you really need.
Also you can still filter (Where) and/or order (Order By) the result.
The name of the joined field isn't relevant, but its datatype and for sure the content.
For more please read this article: Join tables and queries

SQL Join table does not display results in SQL FIddle

I am trying to complete a project for school and the last step has me do a complex join between all of my tables. I have been going over this so many times, but I can't understand why it won't display the table when I run the SQL. Any help would be awesome.
you can use left join to test if system is able to find any record.
add left join instead of join so you can see where you are missing data it might possible that system was not able to find records from sub table that's why its empty.

SAS SQL join criteria

I am working on a project where I have inherited an SQL Join that uses join
criteria in a format I have not seen before. The basic format of the join
is this:
Proc Sql;
create table mytest as
select t1.var1,
t1.var2,
t1.var3
from mysource1 t1
left join mysource2 t2 on
(t1.var1 = t2.var1), myparam t3;
quit;
The bit I am confused about is why myparam is included as a join
condition within the ON statement of the LEFT JOIN. The contents of
'myparam' is derived from the SAS Parameter File we have defined on our
system and contains just one row, with two columns. One contains month
start date, the other month end date.
None of the columns in this parameter file are in the other two source
tables and none of the columns in the parameter file appear in the final
output (they aren't referenced in the SELECT statement so they won't do).
I'm guessing that including the 'myparam' dataset in this context is
somehow using the date values within in it to cut the data in mysource1 and
mysource2, but could someone please provide confirmation that this is the
case and the exact mechanism at work please?
Thanks
This is an unusual construction for a join in SAS, but it's basically a Cartesian product. The myparam table isn't part of the LEFT JOIN condition but a new table, starting a new join. Any table included using a comma and no join condition causes it to be joined with all rows from one table matching to all rows in the other. This can be dangerous when two large tables are used (as the amount of rows is multiplied) but in your case the myparam table has one row, so it's only 1 x n.
However, saying all that, the query you have come across doesn't use any values from myparam (or mysource2 for that matter), so I don't see why these tables are being joined on at all. I'm fairly certain the following query would be equivalent:
proc sql;
select var1,var2,var3
from mysource1;
quit;
I'm aware this answer might come across as incomplete, so please feel free to comment...

When is it required to give a table name an alias in SQL?

I noticed when doing a query with multiple JOINs that my query didn't work unless I gave one of the table names an alias.
Here's a simple example to explain the point:
This doesn't work:
SELECT subject
from items
join purchases on items.folder_id=purchases.item_id
join purchases on items.date=purchases.purchase_date
group by folder_id
This does:
SELECT subject
from items
join purchases on items.folder_id=purchases.item_id
join purchases as p on items.date=p.purchase_date
group by folder_id
Can someone explain this?
You are using the same table Purchases twice in the query. You need to differentiate them by giving a different name.
You need to give an alias:
When the same table name is referenced multiple times
Imagine two people having the exact same John Doe. If you call John, both will respond to your call. You can't give the same name to two people and assume that they will know who you are calling. Similarly, when you give the same resultset named exactly the same, SQL cannot identify which one to take values from. You need to give different names to distinguish the result sets so SQL engine doesn't get confused.
Script 1: t1 and t2 are the alias names here
SELECT t1.col2
FROM table1 t1
INNER JOIN table1 t2
ON t1.col1 = t2.col1
When there is a derived table/sub query output
If a person doesn't have a name, you call them and since you can't call that person, they won't respond to you. Similarly, when you generate a derived table output or sub query output, it is something unknown to the SQL engine and it won't what to call. So, you need to give a name to the derived output so that SQL engine can appropriately deal with that derived output.
Script 2: t1 is the alias name here.
SELECT col1
FROM
(
SELECT col1
FROM table1
) t1
The only time it is REQUIRED to provide an alias is when you reference the table multiple times and when you have derived outputs (sub-queries acting as tables) (thanks for catching that out Siva). This is so that you can get rid of ambiguities between which table reference to use in the rest of your query.
To elaborate further, in your example:
SELECT subject
from items
join purchases on items.folder_id=purchases.item_id
join purchases on items.date=purchases.purchase_date
group by folder_id
My assumption is that you feel that each join and its corresponding on will use the correlating table, however you can use whichever table reference you want. So, what happens is that when you say on items.date=purchases.purchase_date, the SQL engine gets confused as to whether you mean the first purchases table, or the second one.
By adding the alias, you now get rid of the ambiguities by being more explicit. The SQL engine can now say with 100% certainty which version of purchases that you want to use. If it has to guess between two equal choices, then it will always throw an error asking for you to be more explicit.
It is required to give them a name when the same table is used twice in a query. In your case, the query wouldn't know what table to choose purchases.purchase_date from.
In this case it's simply that you've specified purchases twice and the SQL engine needs to be able to refer to each dataset in the join in a unique way, hence the alias is needed.
As a side point, do you really need to join into purchases twice? Would this not work:
SELECT
subject
from
items
join purchases
on items.folder_id=purchases.item_id
and items.date=purchases.purchase_date
group by folder_id
The alias are necessary to disambiguate the table from which to get a column.
So, if the column's name is unique in the list of all possible columns available in the tables in the from list, then you can use the coulmn name directly.
If the column's name is repeated in several of the tables available in the from list, then the DB server has no way to guess which is the right table to get the column.
In your sample query all the columns names are duplicated because you're getting "two instances" of the same table (purchases), so the server needs to know from which of the instance to take the column. SO you must specify it.
In fact, I'd recommend you to always use an alias, unless there's a single table. This way you'll avoid lots of problems, and make the query much more clear to understand.
You can't use the same table name in the same query UNLESS it is aliased as something else to prevent an ambiguous join condition. That's why its not allowed. I should note, it's also better to use always qualify table.field or alias.field so other developers behind you don't have to guess which columns are coming from which tables.
When writing a query, YOU know what you are working with, but how about the person behind you in development. If someone is not used to what columns come from what table, it can be ambiguous to follow, especially out here at S/O. By always qualifying by using the table reference and field, or alias reference and field, its much easier to follow.
select
SomeField,
AnotherField
from
OneOfMyTables
Join SecondTable
on SomeID = SecondID
compare that to
select
T1.SomeField,
T2.AnotherField
from
OneOfMyTables T1
JOIN SecondTable T2
on T1.SomeID = T2.SecondID
In these two scenarios, which would you prefer reading... Notice, I've simplified the query using shorter aliases "T1" and "T2", but they could be anything, even an acronym or abbreviated alias of the table names... "oomt" (one of my tables) and "st" (second table). Or, as something super long as has been in other posts...
Select * from ContractPurchaseOffice_AgencyLookupTable
vs
Select * from ContractPurchaseOffice_AgencyLookupTable AgencyLkup
If you had to keep qualifying joins, or field columns, which would you prefer looking at.
Hope this clarifies your question.