pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliveiros d'Azevedo Cristina <oliveiros.cristina@marktest.pt>
To: LALIT KUMAR <lalit.jss@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: [NOVICE] Compare rows
Date: Thu, 14 Jul 2011 12:30:40 +0100
Message-ID: <58E9388858734E8E8988EBD9E49E678F@marktestcr.marktest.pt> (raw)
References: <CAMojEG+D5uNfiE5ChJp37pbJVQsB007S9WYo-GonmbNR04tFrA@mail.gmail.com>
	<CAA-aLv7Pa+j-c0sK_2KUVwHXvDP80-xH+3saKZZ6EJDLuow4jA@mail.gmail.com>
	<275BDBE63A0449DABCAF052D8EC34AC5@marktestcr.marktest.pt>
	<CAMojEG+VY61A6udkbaM7JrbFiz-B6-pJyMU_=XrM9jU91LTbBA@mail.gmail.com>


  ----- Original Message ----- 
  From: LALIT KUMAR 
  To: Oliveiros d'Azevedo Cristina 



  Hi,


  I do have order of rows. The problem originally is: I need to set all flag with value 0 to -1 whose next row has flag as 1. This  is to be done for each city separetly, i.e. flag of last row of city A need not be changed due to first row of city B.


  City          Date                  Flag                       
  A         31-01-1991               0            
  A         03-03-1991               1     
  A         04-04-1991               0
  A         06-08-1991               0
  A        13-01-1992                1
  A        12-03-1992                1
  B        12-01-1988                0
  B        19-03-1999               1
  B        01-04-1999                0
  B        09-06-2001                 1


  The dates for a villages are in sorted order.Expected output:




                City                            Date                  Flag                       
                  A                        03-03-1991               1      
                  A                        31-01-1991               -1      
                  A                        04-04-1991               0
                  A                         06-08-1991               -1 
                  A                         13-01-1992                1
                  A                          12-03-1992                1
                  B                          12-01-1988                -1
                  B                          19-03-1999               1
                  B                           01-04-1999                -1
                  B                            09-06-2001                 1
                  
  The (city,Date) is unique for each tuple.Hope I am able to state the problem correctly.
  Thanks Lalit


  * Hi, Lalit, 

  First, please always CC to mailing list. You may get help faster from someone with more "savoir faire" than me.

  Second,

  Try this out and tell me if it produced the output you intended


  UPDATE yourTable
  SET "Flag" = -1
  FROM
  (
  SELECT  x."Date" as "Date",x."City" as "City",x."Flag" as "Flag" ,MAX(y."Date")  as anterior
  FROM yourTable x
  JOIN yourTable y
  ON y."City" = x."City"
  AND y."Date" < x."Date" 
  GROUP BY x."Date",x."City",x."Flag"
  ) umq
  WHERE yourTable."City" = umq."City" and anterior = yourTable."Date"
  AND yourTable."Flag" = 0
  AND umq."Flag" = 1


  HTH,

  Best,
  Oliver

Message-ID: <58E9388858734E8E8988EBD9E49E678F@marktestcr.marktest.pt>
Permalink:  ../58E9388858734E8E8988EBD9E49E678F@marktestcr.marktest.pt/
Also on:    postgresql.org/message-id/58E9388858734E8E8988EBD9E49E678F@marktestcr.marktest.pt

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: oliveiros.cristina@marktest.pt, lalit.jss@gmail.com
  Subject: Re: [NOVICE] Compare rows
  In-Reply-To: <58E9388858734E8E8988EBD9E49E678F@marktestcr.marktest.pt>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox