Access Issue and best function to accomplish task - sql

New user here and I've read many threads, but can't seem to figure out the best way to accomplish my task.
Current Issue:I'm using a switch function in Access to accomplish my goal. Here is what I have, but i'm getting a syntax error?
UPDATE all_rugs_prod
SET construction_facet =
Switch(
construction = Machine Woven, Machine Made,
construction = Machine Made, Machine Made,
construction = Printed, Printed,
construction = Hand Hooked, Hand Hooked
)
all_rugs_prod is Database,
construction_facet is the field I want to value to be returned in,
construction is the field it is going to search in.
I'm very new to all this so, i need as much help as I can get.....
Backdrop:I'm taking say database 1, then mapping/matching the fields to database 2. database 2 has many other fields that require data to be popluated in that were added in database 2 that were added.
I created an Append database from database 1 into databas 2 and matched those fields that were appended from database 1 that match database 2.
My biggest issue is the fact that I need to normalize/map data in database 2. Example: in database 2 there is a field from database 1 that has many different text values. I need to search that field and bring back a predetermined text value based on a predetermined list it would fit into. So say in database 2.field7 the text is "aqua blue", I need to normalize/map it to return it to database 2.field8 "blue" and so on and so forth. what is the best way to accomplish this. The list in some cases of say various colors is very long. Thanks!

The syntax error arises because you need to enclose literal strings in double quotes, e.g.
"Machine Woven"`
Otherwise each word separated by whitespace will be interpreted as a field (as opposed to a literal string), which, if not found in the source dataset, will result the fields being interpreted as parameters requiring a value to be supplied by the user; but more critically, this will result in too many arguments supplied to the switch function.
However, since you are only updating the value of records which contain the value "Machine Woven" in the construction field, your query could be simplified to:
update all_rugs_prod
set construction_facet = "Machine Made"
where construction = "Machine Woven"
For a situation in which many possible values in place of "Machine Woven" are being mapped to "Machine Made", I would suggest creating a separate mapping table, e.g.:
Mapping_Table
+---------------------+--------------+
| map_from | map_to |
+---------------------+--------------+
| Machine Woven | Machine Made |
| Machine Built | Machine Made |
| Machine Constructed | Machine Made |
+---------------------+--------------+
And then use a simple update query with inner joins to the above mapping table to perform an implicit selection and update the new value, e.g.:
update
all_rugs_prod inner join mapping_table on
all_rugs_prod.construction = mapping_table.map_from
set
all_rugs_prod.construction_facet = mapping_table.map_to

Related

Is it possible to use SQL to show the average of some values in one column and then in subsequent columns display the individual values?

I have a bunch of data and I want the output to display an average of all the data points but also the individual data points in subsequent columns. Ideally it would look something like this:
Compound | Subject | Avg datapoint | Datapoint Experiment 1 | Datapoint Exp 2 | ...
..........XYZ......|.....ABC....|............40...............|...............20..............................|...............60...............|......
..........TUV......|.....ABC....|............30...............|...............20..............................|...............40...............|......
..........TUV......|.....DEF....|............20...............|...............10..............................|...............30...............|......
One problem I'm running in to is that I get repetitive lines of information. Another is that I have some rows pulling in info that doesn't apply, such that some of the individual datapoints in, say, row 2 would have info from subject DEF when I only want it to have info from subject ABC.
I hope this makes sense! I'm currently using inner join with a ton of where qualifiers. I'm close but not quite there. Any help is appreciate and let me know if I can provide additional info to help you help me.
The SQL language has a very strict rule requiring you to know the exact number of columns for your result set in advance, before looking at any data in your tables.
Therefore, if this average is based off a known fixed number of columns, or if the number of potential columns is reasonably small, where you can manually setup placeholders, then this will be possible. The key search terms to learn how to do this is "conditional aggregation", where you may also need to join the table to itself for each field.
Otherwise, you will need to pivot and aggregate your data in your client code or reporting tool.

Filter data with parameter tableau

I'm trying to filter data with parameter based on user_id with tableau. There's 3 columns: user_id, content_team, seconds. Some of the sample datas are:
user_id | content_team | seconds
1 | AR | 2030
2 | VR | 1020
1 | AR | 1500
2 | VR | 3000
3 | HOLO | 2230
Right now I have the sum(seconds) of the content_team for all the users combine
How can I create a parameter in which it takes a user_id and display the sum(seconds) of the content_team for that particular user only? Thanks
The use case for using parameters in filters is usually that you have more than one data source that are not joined but do contain a common field. There is little benefit to using parameters when there is only one data source as simple filters work better.
But when you want common filters across multiple sources, can't join them into a common source, but don't want to lose some of the functionality in each sheet, then parameters work.
Say you have multiple data sources at different levels of detail but containing the same filterable field say user_id then you can create a parameter built on that field (and recent versions of Tableau can automatically update the content of the parameter on loading the workbook). The way to filter across the sources is to create a parameter called, say, user_id_parameter which is updated when the workbook is opened and then to filter each individual sheet using a calculation eg [user_id]=user_id_parameter where the user_id in each case is from the relevant data source. then, when the parameter is changed, each separate data source will be filtered to show only the data with that user_id even across multiple otherwise unlinked data sources.
This functionality (now that parameters can update on load) vastly simplifies the process of creating "universal" filters that work across multiple data sources in a workbook.
If you only have one data source, the technique still works but has little to no advantage over just filtering on the relevant field (and, don't forget, you can set a filter to apply to all sheets or a range of specified sheets).

SQL "LEFT JOIN" from multiple databases with a variable database name

I'm trying to query an existing SQL server which has a very different structure than I'm used to. This server manages Bill of Materials (BOMs)for a company. For some reason, this is set up so that each project has it's own database. There is a separate database ("project") to hold the basic information for each project.
My goal is to return a list of all projects which meet a condition in the "project" database AND contain a certain component in their BOM.
I can run a simple SELECT command on the "project" database to come up with all projects which meet the first condition. For example, lets say I'm returning (3x) projects which have an int value (87,89,93).
My challenge is that I now need to check the BOM of these (3x) projects to see if they meet my second condition. I essentially need to add a "WHERE" condition to the end of my search that looks like the following.
WHERE
(SELECT COUNT(item)
FROM [PROJECT_87].[dbo].[BOM]
WHERE item like '%ComparePN%') > 0
I'm stumped by figuring out how to change the name of the database "PROJECT_87" for each of the returned projects (87,89,93). I know I can use the SET + REPLACE commands to do the actual replacing of the '87' with the other project numbers. I don't know how to go through all 3 results and perform the SET + REPLACE command for each row to evaluate if the row should stick around.
Thoughts?

Unable to load distinct records via lookup in informatica

This is a very strange thing happening i dont know why. I have created a mapping that transforms the data via expression and loads the data into the target(file) based on lookup on the same target.
Source table
#CompanyName
Acne Lmtd
Acne Ltd
N/A
None
Abc Ltd
Abc Ltd
X
Mapping
Source
->Exp(trim..)
->Lookup(source.company_name
= tgt.company_name)
ReturnPort is CompId
-> filter(ISNULL(CompId))
-> Target
Compid (via sequence
gen)
CompName
The above mapping logic inserts duplicate companynames as well like in source 2 Abc Ltd records same is repeated in target as well. I dont know why. I have tried to debug as well the condition evaluates to true in filter that companyid is null even if the record is already inserted in target.
Also, i thought it might be the case of lookup cache i do enabled dynamic as well but same result. It should have worked like an sql query
select company_id
From lkptarget where
company_name
In (select company_name
from
Source)
Therefore, for Abc Ltd the filter condition should have result in false
Isnull(company_id) false
But, this is getting true. How do I get unique records via lookup and without using distinct?
Note: lookup used is dynamic lookup already
That was in fact a dynamic cache issue the newLookupRow gets assigned a value of 0 on duplicates so I have added the condition in filter as ISNULL(COMPANYID) AND NEWLOOKUPROW=1
and finally that did work.
The Lookup transformation has not way to know what happens in further transformations in the mapping. It can't see results in the target itself, because the Lookup cache is loaded once at the beginning of the mapping using a separate connection to the database. Even if you disable caching (that would mean one query for each Lookup input row), data is not immediately committed (so not visible to other connections) when writing to the target.
That's the reason to use dynamic Lookup cache, which works by adding new lines to the Lookup cache. However in your case there is a catch : the company_id is created after the Lookup (it's the right place to do so), so it can't be added to the Lookup cache.
I think you could configure the Lookup so that :
You activate the options Dynamic Lookup Cache, Update Else Insert and Insert Else Update
You use the company_name to make comparison between source data and Lookup data
You create a fake field company_id with value 0 before the Lookup and associate it to the corresponding Lookup field
You check the checkbox Disable in comparison for the company_id field
You can then use the predefined field NewLookupRow (it appears when you check the Dynamic Lookup Cache option) which should have a value of 1 for new rows or 2 for existing rows with updates (0 for identical rows)
The Lookup should now output NewLookupRow = 1 for the first Abc Ltd and then NewLookupRow = 0 for the second. The filter just after the Lookup should have a condition like NewLookupRow = 1.
For more details you can have a look at the Informatica documentation :
https://docs.informatica.com/data-integration/data-services/10-2/developer-transformation-guide/dynamic-lookup-cache.html

SQL Server: Remove substrings from field data by iterating through a table of city names

I have two databases, Database A and Database B.
Database A contains some data which needs to be placed in a table in Database B. However, before that can happen, some of that data must be “cleaned up” in the following way:
The table in Database A which contains the data to be placed in Database B has a field called “Desc.” Every now and then the users of the system put city names in with the data they enter into the “Desc” field. For example: a user may type in “Move furniture to new cubicle. New York. Add electric.”
Before that data can be imported into Database B the word “New York” needs to be removed from that data so that it only reads “Move furniture to new cubicle. Add electric.” However—and this is important—the original data in Database A must remain untouched. In other words, Database A’s data will still read “Move furniture to new cubicle. New York. Add electric,” while the data in Database B will read “Move furniture to new cubicle. Add electric.”
Database B contains a table which has a list of the city names which need to be removed from the “Desc” field data from Database A before being placed in Database B.
How do I construct a stored procedure or function which will grab the data from Database A, then iterate through the Cities table in Database B and if it finds a city name in the “Desc” field will remove it while keeping the rest of the information in that field thus creating a recordset which I can then use to populate the appropriate table in Database B?
I have tried several things but still haven’t cracked it. Yet I’m sure this is probably fairly easy. Any help is greatly appreciated!
Thanks.
EDIT:
The latest thing I have tried to solve this problem is this:
DECLARE #cityName VarChar(50)
While (Select COUNT(*) From ABCScanSQL.dbo.tblDiscardCitiesList) > 0
Begin
Select #cityName = ABCScanSQL.dbo.tblDiscardCitiesList.CityName FROM ABCScanSQL.dbo.tblDiscardCitiesList
SELECT JOB_NO, LTRIM(RTRIM(SUBSTRING(JOB_NO, (LEN(job_no) -2), 5))) AS LOCATION
,JOB_DESC, [Date_End] , REPLACE(Job_Desc,#cityName,' ') AS NoCity
FROM fmcs_tables.dbo.Jobt WHERE Job_No like '%loc%'
End
"Job_Desc" is the field which needs to have the city names removed.
This is a data quality issue. You can always make a copy of the [description] in Database A and call it [cleaned_desc].
One simple solution is to write a function that does the following.
1 - Read data from [tbl_remove_these_words]. These are the phrases you want removed.
2 - Compare the input - #var_description, to the rows in the table.
3 - Upon a match, replace with a empty string.
This solution depends upon a cleansing table that you maintain and update.
Run a update query that uses the input from [description] with a call to [fn_remove_these_words] and sets [cleaned_desc] to the output.
Another solution is to look at products like Melisa Data (DQ) product for SSIS or data quality services in the SQL server stack to give you a application frame work to solve the problem.