Updating tables pt2 [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Entering data in a table using a text box in a form
This question is similar to my last but in this case I want to be able to remove certain amounts from a table using a text box in a form.
Same scenario, the user inputs the data they want to remove from the table.
For example;
The user has taken out 15 MEC-0004 from stock and wants to update the table. They open the (Stock In/Out) form, select the part (using a combo box) and then alter the amount in the table by typing in 15 into the text box and pressing save or enter.
The SQL code for the last question is something like this:
UPDATE table_to_update
SET column_to_update=value_to_enter
WHERE criteria_column=criteria_value
I could also try VBA if someone pointed me in the right direction
Thanks

You can reference the value itself in the set clause. So a query to change the amount of stock by a relative value would look like:
UPDATE stock_table
SET amount = amount - #Amount
WHERE stock_id = #StockId

Related

Once there is a Closed Date in a column, I need all corresponding account numbers to be transferred or copied to a new table

I am trying to create a query in Microsoft Access that generates a new table that displays all closed requests. There is a column for account numbers, and a column for closed accounts—however, there are multiple rows with the same account number. I used a query criterion on the “closed” column: Is Not Null. This successfully brings over all accounts that are closed because there is a date in the column, but I also need it to bring all the duplicate account numbers that are in different rows but don’t have a closed date.
Table example:
I think you want:
select t.*
from t
where exists (select 1
from t as t2
where t2.acctnumber = t.acctnumber and
t2.closeddate is not null
);
This returns all rows for accounts where at least one row has a non-NULL closeddate.
I would suggest another approach which is 2 step. Looking ahead to a structured set of queries; I would make a Closed Accounts query, that is just Acct Number and Closed Date. One would think this is needed repeatedly in reports and such.
Then I would make a Closed Accounts Detail query. Here you join the Closed Accounts query to the table on the Account number. This will return all rows of each account.
You then have 2 query objects in the navigation pane that are reusable individually as needed in reports, etc.

SQL data sorting by column [duplicate]

This question already has answers here:
How does sql server sort your data?
(4 answers)
Closed 5 years ago.
I am facing one issue I cant handle yet.
Here's the deal: I am working on a program which should monitor employees working hours. So far, I created a SQL Server table called TablicaSQL with 4 columns:
Id, Ime (Name), Datum (date), BrojSati (WorkingHours)
It saves data according to the time of saving.
Example: if I enter Kristijan (name) worked on 2017-11-03 4 hours, but tomorrow if I save that Kristijan worked on 2017-11-01 4 hours, it will show which data has been saved first, which in this case is 2017-11-03.
So my question is: How can I sort my data according the column Datum (date), NOT by the date of saving the data.
Also, I am not looking for query which says something like this:
SELECT *
FROM..
ORDER BY...ASC/DESC
I need some kind of "permanetly asc/desc query".
Here is the screenshot of my table
There isn't a permanent order on database table. They are unorder data set. The data isn't order by the data of creation. Is just returned in the order is storage. But that can change if db engine optimizer find a better way to read the data. Multiple Partition, Clusters, etc.
If you want the data return in a specific order YOU MUST include ORDER BY

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

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.

Create table of differences

I have a sheet of data to which I have run several macros to identify differences, I am now looking to create a matrix table which shows me the differences per column per department.
This is how my sheet looks after it has identified differences:
[DifferencesSheet] http://imgur.com/na6nvNH
And this is what I want to get to:
[FinalSheet] http://imgur.com/i6W60m7
I currently have image 1 which is a table of highlighted differences and I need to create matrix department on the y-axis vs. column headers along the x-axis and the amount of differences per column
Not sure if I can use a pivot table as the data is always changing.
Any advice will help thanks.
Chart will be more suitable as you got 2 manupulations
Use MS Query:
SELECT outTab.Department, SUM(outTab.DepartmentCode), SUM(outTab.Sales)
FROM
(SELECT
S1.Department,
Iif(S1.DepartmentCode=S2.DepartmentCode,0,1) as DepartmentCode,
Iif(S1.Sales=S2.Sales,0,1) as Sales
FROM [Sales$] as S1
INNER JOIN [Compare$] as S2 ON S1.ID = S2.ID) AS outTab
GROUP BY outTab.Department
You need to add the remaining columns above - I added the first two DepartmentCode and Sales. To refresh you will need to only right-click and hit Refresh.
How to create an MS Query in Excel?
Two ways:
Go to Data->From Other Sources->From Microsoft Query
Download my SQL AddIn (free and open sources) [here][4] and just input the output range of the query (F1) and input the SQL and hit Ok

How to edit MULTIPLE rows/data entries in SQL Server Management Studio [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to update/edit multiple rows in a database using SQL Server Management Studio. I know you can edit one entry using solutions provided here, here or here using Edit Top(200) function. But I want to edit >100 entries so this is not an adequate solution. I am using SSMS 2012.
EDIT:
As an simplified example we can look at the following situation:
I have a database of peoples names and their height. But many of the heights are wrong or not updated. So I want to update their heights. This update includes a lot of people and it is tedious to update one by one using the solution I linked too. I have to updated values stored in an Excel file.
I am not sure of how to attack this problem, but is seeking a way that I can copy paste the updated values directly into the tables or do this indirectly using some sql statement.
If you select Edit Top 200 Rows first, you can then modify your query to return 250 rows.
In order to do this you need to click on Show SQL Pane button, in the top left corner, just below the New Query button.
In your query window change 200 to 250 and hit Ctrl + R to refresh.
However, if you really need to update this number of rows, you should probably use raw SQL and write proper UPDATE statements.
EDIT:
In the situation you described, I normally create individual UPDATE statements within Excel and then copy the whole lot to execute in SQL:
You can actually paste content to and from excel directly into the ssms editable datagrid. And you can easily change the number of rows available for edit in the ssms "tools > options > Sql Server object explorer" section to return the 250 rows you would need to edit.
As jerry said though it might be easier to write t-sql statements, there is a quick primer on sql update statements over at w3schools
EDIT: based on the discussion in the comments, here is how to update a table based on the values from another table. I've left the original answer above
Provided you have access to a table #myTable with the correct values, and this table has two columns (Name, height), you can do an update FROM
UPDATE t
SET Height = temp.Height
FROM TableIWantToUpdate t
JOIN #myTable temp ON temp.Name = t.Name
EDIT2: as requested, here is a breakdown of the code
UPDATE t
means update the table aliased as "t" in the FROM + JOIN clauses
SET Height = temp.Height
Here we are setting the column Height of the tabled aliased "t" to the value of the column Height in the table aliased temp. The "t" alias is implicit because its been specified in the UPDATE statement
JOIN #myTable temp on temp.Name = t.Name
Basically we are creating a list of records which we want to update, saying that each record in table "t" that matches a record in table "temp" will be updated accordingly.
As to the values in #myTable aliased "temp" in the sample, you could probably get it with some SQL like this
DECLARE #myTable TABLE (name varchar, height properNumericType)
INSERT INTO #myTable (name, height)
SELECT 'John', 5.6
UNION
SELECT 'Mary', 5.4
UNION
...