question

Tess9083 avatar image
Tess9083 asked

Sql query to select a new attribute based on certain criterias

Please help me generate SQL to select a new attribute column3 based on the following conditions The table data is in order based on certain condition. The table has two columns,Column1 and Column2. Conditions for generating attribute column3: Whenever the value of Column2 is 'N' ,value of Column3 must be 1 added to value of Column3 in previous row. Whenever the value of Column2 is 'Y',value of Column3 must be same as that of the value of Column3 in previous row Whenever the value of Column1 changes,the value of Column3 must reset to 1 Data sample: Column1 Column2 Column3 1 Y 1 1 Y 1 1 N 2 1 N 3 1 Y 3 1 Y 3 1 Y 3 1 Y 3 2 Y 1 2 Y 1 2 N 2 Thanks Teresa
sqlnetezza
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Gazz avatar image
Gazz answered
you want to read up on CASE: [ https://msdn.microsoft.com/en-us/library/ms181765.aspx][1] it will be something like this: SELECT CASE Column3 WHEN 'Y'THEN 1 ELSE 0 END FROM TABLE [1]: https://msdn.microsoft.com/en-us/library/ms181765.aspx
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.