Update SQL Column With Comparision Between Last And Current Records
Jun 13, 2006
My question is concerned with the three columns below (customerID, RepairDate, CompletedRepair (Yes or No). The column name "CompletedRepair " is blank initially. I need to update the CompletedRepair column with this logic below:
- A customer comes to our store to fix their car, if we fix their problem on the first time and they don’t return later for this same issue, then the
•CompletedRepair column = Y
- If a customer needs to come back to our store to re-fix the same issue within 7 days windows based on the RepairDate on the previous transaction then
•On the last return transaction: CompletedRepair = Y (example: RepairDate =6/12/2006)
•On all previous transactions: CompletedRepair = N (example: RepairDate =6/8/2006, 6/9/2006, 6/10/2006)
- If a customer needs to come back to our store to re-fix the same issue but out of the 7 days windows based on the RepairDate then
•On the last return transaction: CompletedRepair = Y (RepairDate =6/12/2006)
•On the previous transaction: CompletedRepair = Y (RepairDate =6/1/2006)
Every time customer comes to for car repair shop for a new issue or an old issue, we create a new repair transaction in our SQL db. The update on the "CompletedRepair " column will be run every day. Today's records will be run against with last 7 days records (based on Repair Date) to check when customer has been really fixed: the last fix counted Y, the previous fix counted as N but comparison in only 7 days. In other words, a repair today is considered as a completed repair when comparing with last 7 day repairs but it might become not a completed repair if this same customer would come back within next 7 days for the same issue.
The CompletedRepair column is dynamic column and is updated daily by using the logic above.
Below is the expected outcome after we update the Completed Repair column:
CustomerID Repair DateCompleted Repair
ab1 06/12/06 Y
ab1 05/28/06 Y
ab1 05/18/06 Y
ab105/15/06 N
ab1 05/12/06 N
Initially 5/12/06 had Y, when 5/15/06 transaction came, it took the Y and made the 5/12/06 become N. The 5/18/06 transaction did the same to 5/15/06 transaction, made itself Y and converted 5/15/06 into N. The 5/28/06 is Y because comparing with 5/18/06, it is out of 7 days window. The 6/12/2006 is Y because comparing with 5/28/06, it is out of 7 days window.
ab2 06/02/06 Y
ab2 05/28/06 N
ab2 04/19/06 Y
ab2 04/14/06 N
The 4/14/06 transaction initially was Y, it became N when new transaction on 4/19/06 came. Same thing with transactions on 5/28/06 and 6/2/06
ab3 05/11/06 Y
ab3 03/29/06 Y
ab3 03/23/06 N
ab3 03/12/06 Y
The 3/23/06 was Y, when new transaction on 3/29/06 came, it became N and the new transaction is Y. The 5/11/06 is Y because comparing back to 3/29/06, they are out of 7 days window.
ab4 05/11/06 Y
This ab4 customer came to fix her car only one time and don't come back. We supposed the fix was sucessfully and so we mark the CompletedRepair as Y.
I think that I would need to use SQL cursor or case statement for this but I really don't know how to start. Please advice and help me out. Any ideas and suggestion are really appreciated! If you need more information, please let me know!
Thank you!
Tracy
View 4 Replies
ADVERTISEMENT
Oct 9, 2013
I have a table named galleryindex which contains rows of photo gallery names along with an associated id (gsid).
example:
gsid (assigned ID), gnumpics (# of photos), gname (gallery name)
=================
43, 133, our summer visit
493, 53, camping photos
When someone uploads more photos I do the following to recalculate the NEW number of photos for the gallery.
SELECT gnumpics FROM galleryindex where gsid= 43
(select current # of photos from location 43 - our summer visit)
in ASP classic I then do this:
sb = objrs("gnumpics")
sb = sb + totupl
SQL = "UPDATE galleryindex SET gnumpics =" & sb & " where gsid= '" & fnum & "'"
to update the gallery with the new amount of photos which is obtained by pulling the old value into SB, and adding "totupl" (total pics uploaded) to it and then updating the values.In SQL speak it would be like this if 20 photos were just uploaded:
SELECT gnumpics FROM galleryindex where gsid= 43
(results in 133 photos)
UPDATE galleryindex SET gnumpics =153 where gsid= 43
(write back 133 + 20)
Basically I'm trying to update the current column gnumpics by a numeric value and I know there's an easier way then pulling the current, adding to it and then writing it back to SQL.
View 2 Replies
View Related
May 6, 2007
Hi,
I have a set of records in database table and I want to update one column to be the same for all of them.
Can you suggest code solution?
View 1 Replies
View Related
Nov 17, 2015
I have a Table Having Date,Opening,Addition,Sale values where opening value comes in the very first row other times it is zero.
In ssrs how can i have a report showing closing value = Opening+Addition-Sale  in current row (it is simple for 1st row ). this closing be the opening value in next row and same formula to be continued...
Date Opening AdditionTotalTank saleClosing Stock
01-11-15 14435 0 14435 8243 20627
02-11-15 0 15000 15000 9433
03-11-15 0 9000 9000 9436
04-11-15 0 12000 12000 8392
05-11-15 0 6000 6000 8157
06-11-15 0 12000 12000 8456
07-11-15 0 15000 15000 10903
08-11-15 0 6000 6000 8485
09-11-15 0 6000 6000 9413
10-11-15 0 21000 21000 6413
View 7 Replies
View Related
Apr 23, 2008
I have a table in SQLCE with ProductCode, Age, Rate, Gender, EffectiveDate. I need to get the list of records with the latest effectivedate not later than the current date for the combination of the other fields.
Because it is SQL CE I can't use subqueries.
Thanks.
View 6 Replies
View Related
Apr 3, 2014
I have a problem with a date in my sql view, I need the records from the 3 last months but without the current month, if I execute my view right now I have the records from January to april but I just need from January to march, I must have always the 3 previous month but I don't know how I can do it
(dbo.frhkrg.fakdat >= DATEADD(MM, - 3, GETDATE()))
View 2 Replies
View Related
May 14, 2007
Hi all
I've been having a really difficult time finding out how to run a query which selects records from an SQL database for the current logged on user. I've tried a whole manner of different approaches but here is where I am at now:
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Page.IsPostBack Then
ds_meobservingcolleagues.SelectParameters(0).DefaultValue = User.Identity.Name
End If
End Sub
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ds_meobservingcolleagues">
</asp:GridView>
<asp:SqlDataSource ID="ds_meobservingcolleagues" runat="server" ConnectionString="<%$ ConnectionStrings:StPaulsDatabase %>"
SelectCommand="SELECT [observeTeacher], [observeBy], [observeReason] FROM [Observations] WHERE ([observeBy] = @observeBy)">
<SelectParameters>
<asp:Parameter Name="observeBy" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
This is not resulting in any error. But it is not returning the records that it should be either.
Please can anyone help? I'm sure it shouldn't be this difficult to do
View 3 Replies
View Related
Jun 29, 2015
I am using the following query in a view to retrieve the latest 24 hourly records for a site.This returns 24 hourly records for the last day of measurements at a Site.This works great. However, I now need to retrieve the latest hourly records from the current hour. For example, hours will run from 00:00 to 23:00 and if the query is executed at 15:00, I will return only hourly records for 00:00 to 15:00 etc. I believe I need to filter the result set or modify the query to exclude records greater than the current hour.
View 6 Replies
View Related
Sep 7, 2006
I have a table like this:
Bottom Top
0 2
4 6.5
9 14
15 17
In Transact SQL I need to calculate the difference between the current bottom and the previous top. If there is no previous top, then the result would be the current bottom.
So, the result would be 0, 2, 2.5, 1
I must return the value in a table. Thank you all for your help!
Karen
View 9 Replies
View Related
Aug 3, 2015
I have some my below requirment to loading some last year and currnet year records for some ID's in my table,
We have to load the ID's that are active at the end of the year for the prior year and ID's that are active as of today for the current year.Here is the scenario when the ID is currently terminated but active at the end of the prior year and the record is not in the table.so, we didn’t load the count for the prior year
Here prior year is 2015-2015 and Current year is 2015-2016
CREATE TABLE remp_year
(ID INT,
STATUS NVARCHAR(100) NULL,
START_DATE DATE NULL,
END_DATE DATE NULL,
date_year nvarchar(10) NULL)INSERT INTO remp_year VALUES (10,'Active','2015-05-26','2015-12-31','2015-2016');
[Code] ...
Here ID 20 and 50 for terminated records is the prior year records so it should count for the last year and those are active in this year those will count for this year.
View 3 Replies
View Related
Jun 16, 2015
I am looking to pull all records for current & previous calendar year in one query. I know how to pull the current calendar year, but how would I pull current & previous?
select id, list_date
from tableA
where list_date > DATEADD(year,-1,GETDATE())
View 5 Replies
View Related
Mar 6, 2005
When I return from executing a sql storedprocedure where I insert a record, where can I find the keyID?
View 2 Replies
View Related
Jul 23, 2005
I need a little help here..I want to transfer ONLY new records AND update any modified recordsfrom Oracle into SQL Server using DTS. How should I go about it?a) how do I use global variable to get max date.Where and what DTS task should I use to complete the job? Data DrivenQuery? Transform data task? How ? can u give me samples. Perhaps youcan email me the Demo Package as well.b) so far, what I did was,- I have datemodified field in my Oracle table so that I can comparewith datelastrun of my DTS package to get new records- records in Oracle having datemodified >Max(datelastrun), and transferto SQL Server table.Now, I am stuck as to where should I proceed - how can I transfer theserecords?Hope u can give me some lights. Thank you in advance.
View 2 Replies
View Related
May 27, 2015
My data has 2 fields: Customer Telephone Number, Date of Visit.
Basically I want to add a field ([# of Visits]), which tells me what number of visit the current record is within 6 months.
Customer TN | Date of Visit | # of Visits (Within 6 month - 180 days)
1111 | 01-Jan-2015 | 1
1111 | 06-Jan-2015 | 2
1111 | 30-Jan-2015 | 3
1111 | 05-Apr-2015 | 4
1111 | 07-Jul-2015 | 3
As you can see, the last visit would counts as 3rd because 180 days from 07-Jul-2015 would be Jan-8-2015.
View 3 Replies
View Related
May 6, 2006
Well, I really messed up. Instead of changing the name of a current company record in a table I changed ALL the company names in the table. Me.CustomerDataSource.SelectCommand = "UPDATE tbl_customers SET company = '" & companyTextBox.Text & "'"
So, I need to insert a WHERE clause to fix this. My problem is that I've been searching everywhere for this simple command structure and cannot find anything that specifically addresses a simple way to reference the current record.
I tried...Me.CustomerDataSource.SelectCommand = "UPDATE tbl_customers SET company = '" & companyTextBox.Text & "' WHERE recno = @recno"
But I get the error:
Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@recno".
Can anyone provide this simple query clause?
View 2 Replies
View Related
Mar 16, 2015
I am new to SQL and want to understand how to update current date and time into DB2 in SQL.
View 1 Replies
View Related
Feb 12, 2008
I am making a web site in which i need to update a database with certain values which include the current user who invoked the insert command so how to retrieve the current userID
View 4 Replies
View Related
Nov 2, 2006
How can I set a column in a table to auto update the date and time everytime something in that row is updated or when the row is first added? Thanks ahead for the help,Jason
View 4 Replies
View Related
Oct 10, 2013
I have successfully created a linked server between SQL Server 2008 R2 and a Postgres db, and all is working fine, except when I try to run a stored procedure that returns a TEXT column.The top lines of the stored procedure (function in postgres) that is called are:
CREATE OR REPLACE FUNCTION get_defects()
RETURNS TABLE(defectid bigint, featurevalues text) AS ...
The function obviously executes correctly in postgres, however when I try to execute the function in SQL Server via the linked server:
SELECT *
FROM OPENQUERY(POSTGRES, 'SELECT * FROM get_defects()')
I get the error:
OLE DB provider "MSDASQL" for linked server "POSTGRES" returned message "Requested conversion is not supported.".
Msg 7341, Level 16, State 2, Line 1
Cannot get the current row value of column "[MSDASQL].featurevalues" from OLE DB provider "MSDASQL" for linked server "POSTGRES".The problem seems to be when trying to return the TEXT column featurevalues, as the following query executes as expected:
SELECT defectid
FROM OPENQUERY(POSTGRES, 'SELECT * FROM get_defects()')
View 3 Replies
View Related
Apr 11, 2012
I am currently working on a website that deals with sales of products. For one of my pages for the website I need it to be able to change the current sales information for a specific product.
The top part of the following code selects the specific product however I cannot get the update query to work.
Code:
$describeQuery = "SELECT p.ID, p.NAME, dt.[Year], dt.[Month], dt.SalesVolume FROM Products p
join
(select ProductCode, sum(SalesVolume) as SalesVolume, [Year], [Month] from MonthlySales
group by ProductCode, [Year], [Month])dt
on dt.ProductCode = p.ID WHERE [NAME] = '$desiredProduct' AND [Year] = '$desiredYear' AND [Month] = '$desiredMonth'";
$editQuery = "UPDATE MonthlySales SET SalesVolume = '$NewSales' WHERE ID = '$desiredProduct' AND Year = '$desiredYear' AND Month = '$desiredMonth'";
$query = sqlsrv_query($link, $describeQuery);
View 3 Replies
View Related
Oct 18, 2007
Hey All,
I am new to programming in SQL developer and I was hoping one of you kind salmon of knowledge could help me
I am running an SQL script every 15 minutes to pull data. I would like to insert the results into a column in a table. I have two issues:
1.How can I give the result column the current time stamp as an alias?
2.How can I add this column to the results table (I know its Alter table but do I put this into the insert sql?)
I would appreciate any help possible
Thanks
View 10 Replies
View Related
Aug 15, 2000
I need to compare two tables and output everything that doesn't match.
The tables are joined by the "domainname" column, and I need to output everything in both tables where the "domainname" doesn't match.
Any ideas?
View 3 Replies
View Related
May 3, 2008
I am using SQL Server 2005 Express Edition. One table contains field docdate having format dd/MM/yyyy hh:mm:yyyy. Here hours & mins are also required.
I am working on VB6.0. User will input date on form in format 'dd/MM/yyyy'. I just want to check this with SQL Server date considering only dd/MM/yyyy.
How to do this?
View 2 Replies
View Related
Dec 22, 2006
Language : VB.NET
Database : MSSQLserver2000
i have developed a tool to compare two databases, but it is taking long time to compare. First it will take the whole table into main memory(order by primary key). Same procedure for table2 of second database. Then it will start comparison by taking row by row. i tested with one third party tool, which is taking only 2 min to compare 800000 records of a table. My tool is taking 1 hour,40 mins to do that. Is there any other optimized method?
Regards,
Loka
View 4 Replies
View Related
Nov 12, 2012
I am trying to exclude records from a table where the ID column is the same but the Mail code Column is multi-valued.For Example: (the table looks like....)
ID Mail_code
111111 XNT
111111 N11
111111 XNC
222222 XNC
222222 XNL
333333 XNC
So, if there is any ID that has a value of XNC, I want to exclude the ID all together from my output regardless of the other values.
View 3 Replies
View Related
Jan 19, 2000
How could I tell the performance difference between two queries:
One is:
select * from table
where Lower(colomnname) = 'value'
The other is:
select * from table
where colomnname = 'value'
Basically the difference is in lower() function, how much this function will affect the query performance.
Is there a formal way to test it out, or by any logic.
Thanks, Mike
View 2 Replies
View Related
Jul 10, 2002
Hi,Does any of u folks used any tools for database structures comparision??
I have used DBDIFF(from DKGAS.com) ChangeManager from Embarcadaro and SQL COMPARE from Redgate.
I did have have some problem in each of these tools AND am looking for some good tools which are economical also.
Thx in advance
Ravi
View 2 Replies
View Related
Jun 12, 2006
Hi Gurus,
I am looking for a script which lists me the differences between two table schemas.
Thanks in advance
Srini
View 3 Replies
View Related
May 19, 2006
whats the difference betwwen mysql and sql server2000 in terms of usingstoredprocedures,stored functions and views.
View 3 Replies
View Related
Feb 6, 2004
does any one have a script.. to compare objects(tables, views, sps, func etc) between two different databases.
thanks
View 5 Replies
View Related
Apr 2, 2006
Do you know some performance differences between reading data from a stored procedure using:
1. OLE DB Connection and OLE DB Source
2. ADO.NET Connection and DatareaderSource
Przemo
View 1 Replies
View Related
Jan 31, 2007
I am trying to compare developer edition and Express edition. I almost purchased the developer edition and wanted to compare it with other editions. no info to be found. Would greatly appreciate it, if someone could direct me to a resourse that shows this comparision.
thanks
ps.The product comparision page doesnt show developer edition.
View 1 Replies
View Related
May 2, 2008
Dear all,
I have building an authitication mecanism through a database standard login table as :
UserName varchar
PassWord varchar
UserId Identity
From my client application I need to validate the username and the password.
For that I have a store procedure with the following statement :
SELECT @loginID = Id from [NomosConfig].[dbo].Users
WHERE [UserName]=@UserName
AND [PassWord] = @pw
From that point this works well except that for the passowrd, whre of course there will be no difference between:
PassWor = myPassw or MYPASSW
How can I check in password field in order that it is case sensitive....?
Later on, the password will be encrypted of course, will it behaves similar ?
thnaks for help
regards
serge
View 6 Replies
View Related