how can I link rows of a SQL table for some columns but not others? - sql

I have a table of values in excel that I want to put into sql as a lookup table. the table looks like this:
the sql table looks like this:
having this in SQL, I now want to never use the excel file ever again.
I also need the ability to change the parameters, but some of them in the excel file were linked by merging the cells and thereby shared the same value, if it changed for one it changed for all.
for example: when I change Parameter B for Product 1, I need it to change it for Products 2, 3, 4, and 5 because they share the same cell in the excel table. And if I change parameter A for Product 2, It only changes for product 2 and 3. I am looking for a SQL Query solution. I have the ability to change the table structure as well.
Here goes my example query:
Update [Table] Set [Parameter_A] = '{new_parameter_tag}'
Where [product] = '{selected_product_tag}'
except I want to have the Where include all the rows that share the same cell from the excel table.
I want to be able to update the SQL table for multiple products at a time based on if they share the same cell for that parameter in the excel file.
here is my initial guess at an answer:
Select [{Parameter}],[Product],[Extra_column]
From [Table]
Where [Product] = '{selected_product}'
this returns one row and [Extra_column] that contains a grouping number shared by others in the same cell grouping. this then gets stored as {Extra_column}. then:
Update [Table] Set [{Parameter}] = '{new_parameter_value}'
Where [product] = '{selected_product}' Or [Extra_column] = '{Extra_Column}'
this requires two queries and also means that i need twice as many columns as i had before. I am looking for something a little more elegant.
This is SQL Server 2012 and the {} indicate a value that I am passing in form a script.

I ended up doing something similar to what I had above, the user enters the group they want to edit (it's pretty easy to pick out which one you want when viewing the table) as:
Update [table]
Set [{Parameter}]={NewValue}
Where [Extra_Column] = '{Extra_Column}'
I had to add three columns for the grouping indexes but over 43 parameters that doesn't add much to my table size. I did not take into account the fact that if I change a single parameter for a single product that would remove it from the "group" essentially for just that parameter, and later I would overwrite the changed value if I do a group change for that parameter. I could add in a check to only change values that match within that group but either way the user will have to be smart about what they do. luckily, they can see the table before they change it.

Related

sql query expression

The table retrieves data from the table "Group" with 2 columns First key "GroupNumber" and GroupName"I am trying to create a sql query in Microsoft Access from a table with two columns that will works as below:
Based on the user's selection from the first column the query has to return the value of the second column, and based on the user's selection from the second column the query has to return the value of the first.
Any idea how is possible to express the user's selection in sql? Ty
Some more data would be useful. AT this moment, it is hard to understand what it is exactly you are trying to do.
However, you mention two different selecting actions that need to be done by the user, so two SELECT statements would work.
To return the value of the second column based on user selection of the first: SELECT second_column_value FROM table WHERE first_column_value = value_selected_by_user
To return the value of the first column based on user selection of the second:SELECT first_column_value FROM table WHERE second_column_value = value_selected_by_user
or you can use drop-down lists, if the situation allows
Try providing more data to get more useful answers.

How to use UPDATE IIF

I am using SQL in MS-Access.
I created some queries that analyze if a code is present in two different excel spreadsheets and generate a new table, informing "yes" or "no" for each comparison.
However, sometimes, a certain code appears in just one table, but after a while (when someone update the excel spreadsheets), that code appears.
I already have a query that makes this comparison, but it doesn't work for those codes that are introduced in the table AFTER the comparison has already been made.
So, I would like to create a query that uses an UPDATE, checking if the given code is now in both tables, and if it is, it would update the comparison column of my table.
This is the query I created for this, but it is not working:
UPDATE
comparationTable
SET
col_comp = IIf(spreadSheet1.code = spreadSheet2.code),"Yes","******No******")
WHERE
code1 = code2;
Note: code1 and code2 are columns that only show the code coming from excel spreadsheet1 and excel spreadsheet2, respectively.
Edit: Here's the images of the two spreadsheets (sp1, sp2) and the comparation table:
Too many parenthesis - must always be in pairs. Field names are not correct. The WHERE clause is not appropriate - records without value in Code2 will not update with any value.
UPDATE comparationTable SET col_comp2 = IIf(code1 = code2, "Yes", "******No******")

How can I use record content from one table to update another, without a join option?

I need to update values in a column in a specific table that exists in all our databases, but do not know the name of the column as it is user-generated.
I have two tables: one of them with user-generated columns tab_Case. In this table there is a column attachment that I need to update if the following condition applies: WHERE attachment = '0' (if true then the value needs to be changed to NULL).
In its simplest form the update query would look something like this:
UPDATE tab_Case
SET attachment = 'NULL'
WHERE attachment = '0'
This table is used in all our databases, so I need to write a query general enough to be usable across all of them.
The problem is that as the table uses user-generated columns, I have no way of knowing what the exact name is of concerned column-type, and exactly how many of those columns exist in the table.
I can, however, find out the type of the column by looking it up in another table tbl_itemPart inner joined with tbl_ValueType, like this:
SELECT ip.DbReference, ip.DbTableName, vt.ValueDescription
FROM tbl_itemPart ip
INNER JOIN tbl_ValueTypes vt ON ip.ValueTypeId = vt.ValuetypeId
WHERE vt.ValueDescription = 'file'
AND ip.DbTableName = 'tab_Case'
The columns I need are always of type 'file' and as the tab_Case table is referenced in tbl_ItemPart it is easy to find out 1) if any columns of type 'file' exist in this table, and 2) when true, what their respective names are.
So great, now I know the names of the columns that I need to potentially update. But, this is where I get lost: how do I use that information in my update query?
How do I write a script that first checks the tbl_itemPart for existence of any columns in tab_Case of type ' file', then retrieves the actual values (= names of those columns) from the DbReference column in tbl_itemPart and then finally uses those values in the update query for tab_Case?
Remember that this scripts needs to automatically do this for each of our databases, so I do not want to look up column names manually per database and then adjust my script accordingly for each of the databases.
I am very new to programming, and may be missing something very obvious, but so far I haven't been able to find a solution, or any relevant information to help me on my way.

One row per column with column name

background
I am trying to create an SSRS report which will run select * on a table passed in as a parameter and display all the data from that table. As I understand it, I can't use a table for this. I want to use a pivot table to achieve this.
select * from #table will return something like this (from the adventureworks DB)
I want to display the date in this format:
Question
How do I achieve this? I looked at using PIVOT/UNPIVOT, but all the examples I've seen use static column names and aggregates.
I won't know the column names at design time (or run time), I'm assuming I will need column headers like 'table name', 'value1', 'value2' etc?
Limitations
I won't have access to create stored procedures. Ideally, the report should be able to be run entirely from SSRS without having to create new tables etc.
Performance is not a concern.
Edit
Editing to add some clarity. The column names in the example above are only an example. The #table parameter could be any table, column names won't be know at design time. The column names could be col1, col2, or name, address... etc.
You can do this with a normal tablix, with a column group on BusinessEntityID
Create a normal tablix.
Remove the row group (group only)
Insert a new parent column group, grouped by BusinessEntityID
Delete the top row (row only)
Add each of the row titles in, as per your suggested output
Add each of the values in the second column

Update existing database values from spreadsheet

I have an existing MSSQL database where the values in some columns need updating according to a spreadsheet which contains the mappings of old data and new data.
The spreadsheet is like this:
| OLD DATA | NEW DATA |
RECORD | A | B | C | D | A | B | C | D |
1 |OLD|OLD|OLD|OLD|NEW|NEW|NEW|NEW|
2 |OLD|OLD|OLD|OLD|NEW|NEW|NEW|NEW|
Where ABCD are the column names, which relate to the database, and OLD / NEW relates to the data.
Thus for each line (approx 2500 rows)
The database values that match OLD in each column, need to be changed to NEW
My current thoughts are to do it in a similar way to this:
SQL Statement that Updates an Oracle Database Table from an Excel Spreadsheet
Essentially getting Excel to formulate a list of replace statements, though this feels like a horribly convoluted way to deal with the problem!
Is there a way to have SQL cycle though each row of the spreadsheet, check all records for a=old, b=old2, c=old3, d=old4 and then replace those values with the appropriate a=new, b=new2, c=new3, d=new4?
You shouldn't need to loop through each row in the spreadsheet. You can use the OPENROWSET command, like in the answer you linked to, to load the spreadsheet data into a sort of temporary table. You can then run a regular UPDATE statement against that table.
It would look something like this
UPDATE YourTable
SET YourTable.A = ExcelTable.NewDataA,
YourTable.B = ExcelTable.NewDataB,
YourTable.C = ExcelTable.NewDataC,
YourTable.D = ExcelTable.NewDataD
FROM YourTable
INNER JOIN OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:\foldername\spreadsheetname.xls;',
'SELECT column1name, column2name, column3name, column4name
FROM [worksheetname$]') AS ExcelTable
ON YourTable.ID = ExcelTable.ID
WHERE (YourTable.A = ExcelTable.OldDataA
AND YourTable.B = ExcelTable.OldDataB
AND YourTable.C = ExcelTable.OldDataC
AND YourTable.D = ExcelTable.OldDataD)
Looks like Jeff got you the answer you needed, but for anyone looking to update a database from a Google Sheet, here's an alternative, using the SeekWell desktop app. For a version of this answer with screen shots, see this article.
Get the right rows and columns into the spreadsheet (looks like #sbozzie already had this)
Write a SQL statement that SELECT 's all the columns you want to be able update in your sheet. You can add filters as normal in the WHERE clause.
Select 'Sheets' the top of the app and open a Sheet. Then click the destination icon in the code cell and select "Sync with DB"
Add your table and primary key
In the destination inputs, add your table name and the primary key for the table.
Running the code cell will add a new Sheet with the selected data and your table name as the Sheet name. Please note that you must start your table in cell A1. It's a good idea to include an ORDER BY.
Add an action column
Add a "seekwell_action" column to your Sheet with the action you'd like performed for each row. Possible actions are:
Update - updates all columns in the row (unique primary key required)
Insert - adds the row to your database (you need to include all columns required for your database)
Sync - An Update action will be taken every time the query runs, on a schedule (see "5. Set Schedule" below)
Complete - status after the schedule has run (see below) and the actions have been taken. The new data should now be in your database. Note that 'Sync' actions will never show complete, as they run every time the schedule runs. To stop a 'Sync' action, change it manually.
Set Schedule
To execute the actions, select the clock icon at the top of the application, indicate the frequency and exact time, and click 'Save.' You can manage your schedule from the inserted 'RunSheet' (do not delete this sheet or you will need to reset your schedule) or from seekwell.io/profile. If you need to run the actions immediately, you can do so from /profile.
Gotchas
You need to start your table in cell A1.
Snowflake column names are case sensitive. Be sure to respect this when specifying the primary key, etc.
If your server is behind a firewall, you will need to whitelist SeekWell's static IP address to use scheduling. See more about whitelisting here.