Create a new column in a table that compares the value of one column with its previous value

The DDL:
DECLARE
@T TABLE
IDNO
int,
name
varchar(40),
[Date]
datetime2,
Price1
float,
Price2
float
DECLARE
@K TABLE
IDNO
int,
name
varchar(40),
[Date]
datetime2,
Price1
float,
Price2
float
INSERT
INTO @T
VALUES(22,'C_V_Harris','2014-01-02 10:23:49.0000000',
23.335,      
23.347)
INSERT
INTO @T
VALUES(21,'C_V_Harris','2014-01-02 10:05:13.0000000',
23.357,      
23.369)
INSERT
INTO @T
VALUES(20,'C_V_Harris','2014-01-02 09:56:15.0000000',
23.364,      
23.377)
INSERT
INTO @T
VALUES(19,'C_V_Harris','2014-01-02 09:45:26.0000000',
23.351,      
23.367)
INSERT
INTO @T
VALUES(18,'C_V_Harris','2014-01-02 09:43:20.0000000',
23.380,      
23.396)
INSERT
INTO @T
VALUES(17,'C_V_Harris','2014-01-02 09:34:28.0000000',
23.455,      
23.468)
INSERT
INTO @T
VALUES(16,'C_V_Harris','2014-01-02 09:30:37.0000000',
23.474,      
23.486)
INSERT
INTO @T
VALUES(15,'C_V_Harris','2014-01-02 09:18:12.0000000',
23.419,      
23.431)
INSERT
INTO @T
VALUES(14,'C_V_Harris','2014-01-02 09:16:06.0000000',
23.360,      
23.374)
INSERT
INTO @K
SELECT
ROW_NUMBER()
OVER (ORDER
by IDNO)
AS RN,*
FROM
@T
SELECT
* FROM
@K
--not working:
SELECT
a.RN,a.Price2
FROM
@K a
INNER
JOIN @K
b
ON
a.RN=b.RN-1
WHERE
a.Price2>b.Price2
I need to create  a view with a column (say 'Comp' below) that compares the value of each row in Price2 with the previous Price2 row, and it is greater then +1, the
same 0, and less -1.
The processed table should be:
IDNO
name
Date
Price1
Price2
Comp
22
C_V_Harris
1/2/2014 10:23:49
23.335
23.347
0
21
C_V_Harris
1/2/2014 10:05:13
23.357
23.369
1
20
C_V_Harris
1/2/2014 9:56:15
23.364
23.377
1
19
C_V_Harris
1/2/2014 9:45:26
23.351
23.367
-1
18
C_V_Harris
1/2/2014 9:43:20
23.38
23.396
1
17
C_V_Harris
1/2/2014 9:34:28
23.455
23.468
1
16
C_V_Harris
1/2/2014 9:30:37
23.474
23.486
1
15
C_V_Harris
1/2/2014 9:18:12
23.419
23.431
-1
14
C_V_Harris
1/2/2014 9:16:06
23.36
23.374
-1
 How can I structure the statement to get (the most recent - order by date ) result for Comp?

Satheesh Variath, I just had to make some corrections from your script to get the correct answer:
CREATE
VIEW vw_Comp
AS
SELECT
TOP 1 t.IDNO,t.name,t.[Date],t.Price1,t.Price2,
CASE
WHEN t.Price2
> LAG(Price2,1)
OVER (PARTITION
BY name
ORDER BY IDNO) 
THEN 1
WHEN t.Price2
< LAG(Price2,1)
OVER (PARTITION
BY name
ORDER BY IDNo) 
THEN -1
ELSE 0
END
AS Comp
FROM 
@T t
ORDER
BY DATE
DESC
The adjustments: the selection of the most recent comparison (Top 1) and the use of the function LAG (instead of LEAD) to get the previous value of the column.

Similar Messages

  • If I create a new apple ID will I lose all the apps I have purchased with my original ID?

    If I create a new apple ID will I lose all the apps I have purchased with my original ID?

    I set up FaceTime and Messages on four different devices with only one Apple ID. I used the exact method that is described in the video that AnaMusic has provided to you in the link in her response above.  That method worked fine for my family, but you can create new Apple ID's just to use for FaceTime and Messages if you like. Many users prefer doing that as opposed to adding new email addresses.
    IMO - using unique email addresses (like in the video) makes sense, because we (my family) all have different email addresses anyway, and it was very simple to add each family members email address to both apps and remove my Apple ID email address as the contact address on each device.
    BTW .... if you are using your Apple ID on both devices - your phone number should be the number that is showing in the settings - not you daughter's. Did you set up the Apps with your Apple ID?

  • Can I create a view based on two tables that have the same column name?

    I have two tables A and B. Each table has 50+ columns.
    I want to create a view that includes all the columns in A and all the columns in B. I created a view with a select statement that says
    Select A.*, B.*
    From A, B
    where A.id = B.id
    It returns an error because in each table I have a column that keeps track if a record has been changed called Modified_By. That's where it chokes up on I figure. I would like to write the view without explicitly writing each column name from A and B as part of the select statement. The actual select statement works fine and only bombs when trying to turn the select statement into a view.

    You will have to type the full column list at least once. You can save a few keystrokes (i.e. alias. on every column) by providing the column names to the CREATE part instead of in the SELECT part. Something like:
    SQL> desc t
    Name                                      Null?    Type
    ID                                                 NUMBER
    NAME                                               VARCHAR2(10)
    SQL> desc t1
    Name                                      Null?    Type
    T_ID                                               NUMBER
    LOC_ID                                             NUMBER
    NAME                                               VARCHAR2(15)
    SQL> CREATE VIEW t_v (id, t_name, t_id, loc_id, t1_name) AS
      2  SELECT t.*, t1.*
      3  FROM t, t1
      4  WHERE t.id = t1.t_id;
    View created.HTH
    John

  • I want to create a new itunes account for myself and leave the current one for my wife. Is that possible?

    Ok. My wife uses itunes for her work and she has asked me to open my own account without erasing the one we currently have so that our music can be seperate. I have my own pc and sync to my own itunes program but our purchases still download to the same account and the money comes from the same account. Can I open a new account and not lose everything? Thanks!

    I hope this isn't too wordy but I want to be as clear and explicit as I can for you.
    You can't transfer purchases from one account to another account. Apple doesn't allow it.
    You can continue to use your daughter's Apple ID in order to continue to use the apps that you purchased with her ID and you will need to use her ID and password any time that you want to update those apps. What you do want to avoid is having to download any of those purchased apps again on any other devices - if you create a new Apple ID.
    If you create a new ID and then you sign into her ID in order to download a previous purchase-  you will lock yourself out of the new ID for 90 days and there is nothing that you can do about it. So as long as you do NOT download any past purchases with her ID - you will not lock yourself out of the new ID. So you can keep using the apps that were bought with her ID, you can also update those apps using her ID (you have to sign into her account in Settings>Store) but you can't download past purchases without messing things up with your new ID.
    When you download a past purchase with a different ID - you associate that device with the different ID for 90 days. This is one way that Apple hopes to cut down on pirating of apps - friends using other friends IDs in order to download apps without patting for them.
    This explains it in a little more detail.
    http://support.apple.com/kb/ht4627
    The bottom line is that you can create a new ID, still use her ID - judiciously - but you really want to avoid downloading past purchases with her ID. Transfer all of those purchases into iTunes so that if something happens and you need the purchased content again, you can just sync it again rather than downloading it.
    This explains transferring purchases.
    http://support.apple.com/kb/HT1848

  • If i selected to "Create a new backup" and then cancelled that option but want to go back and select "Inherit backup history"; how do i go back and do that since now when i plug in my HD, that option doesn't appear anymore?? Any Help?

    If i selected to "Create a new backup" and then canceled that option but want to go back and select "Inherit backup history"; how do I go back and do that since now when I plug in my external hard drive, that option doesn't appear anymore?? Any Help? I bought a MacBook Air, but decided to return it for the newest model, and I wanted to back it up so that I can then back it up on my new MacBook Air. I didn't understand the option to Inherit or start a new back up, and therefore selected the option to create a new backup. I believe I should have selected to Inherit backup history so that when i back it up on the new MackBook Air, it appears the same as the one I'm returning.... Is there anyway that i can re-select the option to Inherit backup history for time machine?

    Thanks for sharing this link.
    I've posted a question here https://discussions.apple.com/message/22049103#22049103 describing my situation. Are you able to suggest me the right option?

  • Just installed Firefox 3.6. Firefox will not start and Firefox safemode does not start either. Using Task manager I do not see if started either. If I create a new profile and start from that window, Firefox starts up. I see no error windows/messages. I d

    Just installed Firefox 3.6. Firefox will not start and Firefox safemode does not start either. Using Task manager I do not see if started either. If I create a new profile and start from that window, Firefox starts up. I see no error windows/messages. I do have it listed in my Firewall as an exception program with Permit All. What is happening?
    == This happened ==
    Every time Firefox opened
    == After removing Firefox and Reinstalling it. ==
    == User Agent ==
    Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB6.4; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; MS-RTC LM 8)

    If that new profile works then you can transfer some files from the old profile to that new profile (be careful not to copy corrupted files)
    See http://kb.mozillazine.org/Transferring_data_to_a_new_profile_-_Firefox
    See also [[Basic Troubleshooting|#Make_a_new_profile|Basic Troubleshooting: Make a new profile]]

  • I'm trying to create a new ichat Acc because for some reason the old one is not working, so I go to Create an Apple ID and start creating a new apple ID, when I submit the Acc it tells me that my Email address is already verified for another Apple ID and

    I'm trying to create a new ichat Acc because for some reason the old one is not working, so I go to Create an Apple ID and start creating a new apple ID, when I submit the Acc it tells me that my Email address is already verified for another Apple ID and thats the only email address I have... What can I do?

    There is no way to consolidate iTunes accounts.
    Setting up an iPhone has never required creation of a new Apple ID.
    You've apparently purchased content with both Apple ID's.  Sign into the device with the correct Apple ID and sync the content.

  • FaceTime won't accept the password which I wrote down some time ago and doesn't provide a "forgot your password" option. And when I try to create a new account it tells me that my email is already registered. What can I do to revise my password?

    FaceTime won't accept the password which I wrote down some time ago and doesn't provide a "forgot your password" option. And when I try to create a new account it tells me that my email is already registered. What can I do to revise my password?

    Hello Sheila,
    Thank you for the details of the issue you are experiencing with FaceTime.  It sounds like your Apple ID password is not being recognized. 
    You can reset your password using the steps in the following article:
    Apple ID: If you forget your password
    http://support.apple.com/kb/HT5787
    You can contact the Account Security team if you are unable to reset your password using those steps:
    Apple ID: Contacting Apple for help with Apple ID account security
    http://support.apple.com/kb/HT5699
    Thank you for using Apple Support Communities.
    Best,
    Sheila M.

  • HT5312 How can I reset my security question and I cannot create a new account because I have £18 on the account that I can't remember the security questions to

    How can I reset my security question and I cannot create a new account because I have £18 on the account that I can't remember the security questions to

    If you have a rescue email address (which is not the same thing as an alternate email address) set up on your account then steps 1 to 5 on the page that you posted from should let you reset them : go to https://appleid.apple.com/ and click 'Manage your Apple ID' on the right-hand side of that page and log into your account, then click on 'Password and Security' on the left-hand side of that page and on the right-hand side you should see an option to send security question reset info to your rescue email address.
    If you don't have a rescue email address (you won't be able to add one until you can answer 2 of your questions) then you won't get the reset option - you will need to contact iTunes Support or Apple to get the questions reset.
    e.g. you can try contacting iTunes Support : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page, then Account Management , and then 'Forgotten Apple ID security questions'
    or try ringing Apple in your country and ask to talk to the Accounts Security Team : http://support.apple.com/kb/HE57
    When they've been reset (and if you don't already have a rescue email address) you can then use the steps half-way down the page that you posted from to add a rescue email address for potential future use, or if it's available in your country you could change to 2-step verification : http://support.apple.com/kb/HT5570

  • From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?

    From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?

    Hi,
    Use NVL or COALESCE:
    NVL (col_a, col_b)
    Returns col_a if col_a is not NULL; otherwise, it returns col_b.
    Col_a and col_b must have similar (if not identical) datatypes; for example, if col_a is a DATE, then col_b can be another DATE or it can be a TIMESTAMP, but it can't be a VARCHAR2.
    For more about NVL and COALESCE, see the SQL Language manual: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions119.htm#sthref1310
    I hope this answers your question.
    If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
    Explain, using specific examples, how you get those results from that data.
    Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
    See the forum FAQ: https://forums.oracle.com/message/9362002

  • HT1848 MY wife has an Iphone 4  and I have a 5. To date we haev used one apple is/account to manage music purchases. Recently we bought a Mac and created a new apple account for her. How do I synched her phone with new apple account?

    MY wife has an Iphone 4  and I have a 5. To date we have used one apple account to manage music purchases/library. I updated ny phone to the new OS version and all her texting is showing up on my phone.  I understand if i create an apple account for her I will not see her texts. Recently we bought a Mac and created a new apple account for her. How do I synched her phone with new apple account and get access to the music library under my account on the new MAC laptop  before I update her phone to the new OS?
    Going forward if I purchase music under my apple id and want her to have it on her device, how can I acheive that?

    You can continue to share an Apple ID for purchases.
    Sign her in to her new account here:
    Settings > iCloud > Delete Account (Keep on Phone if you want to transfer contacts, calendars, etc...) then sign in with her new ID
    Settings > Messages > Send & Receive > Apple ID > Sign out then in with new
    Settings > FaceTime > Apple ID > Sign out then in with new
    Use the same Apple ID here:
    Settings > iTunes & App Stores > Apple ID

  • I have to create a new iTunes account as I cannot remember the security question answers to my current account, how do I move over my purchases from my current account to my new account?

    I have to create a new iTunes account as I cannot remember the security question answers to my current account, how do I move over my purchases from my current account to my new account?

    fayry wrote:
    ... how do I move over my purchases from my current account to my new account?
    This is not possible...
    Anything Downloaded with a Particular Apple ID is tied to that Apple ID and Cannot be Merged or Transferred to a Different Apple ID.
    Apple ID FAQs  >  http://support.apple.com/kb/HE37

  • When creating a new window in safari, using command T, the marker have not activated the search field. It means you have to click there every time. Changed after upgrading to Mavericks. Anyone knows how to change this?

    When creating a new window in safari, using command T, the marker have not activated the search field. It means you have to click there every time. Changed after upgrading to Mavericks. Anyone knows how to change this?

    Yes, you can do something like that. What you would do is create a button for each image and then hide them. When I do this type of thing I place all of the buttons on a template page and then hide the template.
    You can then use JavaScript to copy the icon from any of the hidden buttons to the main button that you've set up to display the image. For example, suppose you set up 10 buttons named b1, b2, b3, ...b10. The code to copy the icon from one of them to the button used to display the image (b0) would be something like this:
    // Get the icon from the b3 button
    var oIcon = getField("b3").buttonGetIcon();
    // Get  reference to the b0 button
    var f = getField("b0");
    // Set the icon of the b0 button to the icon retrieved from the hidden button
    f.buttonSetIcon(oIcon);
    // Show the b0 button
    f.display = display.visible;
    This code would go in the Mouse Up event of the smaller button.

  • How to create a new wage type in IT0014 and assign the GL?

    Dear Gurus
    Please guide me about Creating of new wage Type in 14.
    My client wants to add a new wage type says:-1HRR ( Honorarium)  &  assign  the same to  Training Expenses GL account and order number. How to do it. Please guide.
    Regards
    Vikas K Sharma

    To copy a standard WT and assign a new name with the same attributes and characterstics:
    IMG > Personnel Management> Personnel Administration> Payroll data> Recurring Payments and Deductions. Copy any standard Wage Type.
    Check assignment of WT to a Symbolic Account :
    (The WT will inherit the Symbolic account from the copeid standard WT)
    Go through SM30 to Table V_T52EL_NOFM. Make neccessary changes to the Symbolic account, if needed.
    To create a new Symbolic Account and assign it to the WT:
    Payroll --> Payroll (country) --> Posting to Financial Accounting --> Activities in the HR-System --> Employee grouping and symbolic accounts --> Define symbolic accounts
    To assign the WT to the new symbolic account (same as table V_T52EL_NOFM):
    Payroll --> Payroll (country) --> Posting to Financial Accounting --> Activities in the HR-System --> Wage Type Maintenance --> Define WT posting attributes
    To assign a symbolic account to a G/L or Check the assignment of Sym Acc to G/L:
    Payroll --> Payroll (country) --> Posting to Financial Accounting --> Activities in the AC-System -->Account Assignment --> Assign expense accounts.
    Hope this helps.
    Soujanya.

  • I created a new administrator USER account. I deleted the original one but the process never ends. It's been 3 days now.

    I created a new administrator USER account. I deleted the original one but the process never ends. It's been 3 days now. And I can't close preferences window or turn off the computer.
    It's stays in the "deleting" but I can't cancel it or close the window.
    What can I do?

    Here are two screen shots to show you what I am seeing.  The first screen shot shows it allowing me to select (highlighted in blue) my admin user (which is what I am locced in as).  The second screen shot shows it allowing me to select the "Guest" user (highlighted in blue).  However when I click on the user "Orion" nothing happens.  It will not change to highlight that user.

Maybe you are looking for