Showing posts with label queryi. Show all posts
Showing posts with label queryi. Show all posts

Tuesday, March 27, 2012

check previous row in the table

Hi...

Is there any way to check previous row in SQL Query?

I have a table with these column :
Name1
Name2
Audit_Time (datetime)
Changes

I want to delete record from database in which the Audit_time is <'01/05/2004'.
However before deletion, I want to check, if the Changes value is 'OLD' And the previous value is 'NEW', I will check the Audit_time of the NEW instead of OLD.


Table :
Row Name1 Name2 Audit_Time(mm/dd/yyyy) Changes
1 ABCD EFGH '01/01/2004' ADD
2 ABCD EFGHIJ '01/04/2004' NEW
3 ABCD EFGH '01/04/2004' OLD
4 Klarinda Rahmat '02/08/2004' NEW
5 Klarinda Rahmat '01/04/2004' OLD

In this case, I want to delete row 1,2,3 Where the audit_time are < '01/05/2004'.
Row 5 the audit_time also < '01/05/2004', however the changes='OLD' and the previous value changes='NEW', so I will check the Audit_Time of row 4 which is not < '01/05/2004'.
So I can't delete row5.

Is there any way to check previous row or the row before a specific row in SQL.
Any suggestion is welcomed.
Thank you in advanced.You can use a Cursor in a Stored Procedure. It would be very complex though...|||Check out the EXISTS keyword (assuming SQL server)...sql

Tuesday, March 20, 2012

Check for no value in a local variable

How can I check the value of a local variable to see whether it reurned
a record after setting it to a SELECT Query?
I also need to be able use the value as an integer if it finds a match
Declare @.Found as Int
SET @.Found= ( SELECT TOP 1 ID FROM tbl WHERE col1=@.col1 AND col2=@.col2
)
IF @.Found ' -- how to test for no match found
Thanks.Read this article
http://vyaskn.tripod.com/difference..._and_select.htm
IF @.var IS NULL ?
"hals_left" <cc900630@.ntu.ac.uk> wrote in message
news:1143041257.585219.145960@.u72g2000cwu.googlegroups.com...
> How can I check the value of a local variable to see whether it reurned
> a record after setting it to a SELECT Query?
> I also need to be able use the value as an integer if it finds a match
> Declare @.Found as Int
> SET @.Found= ( SELECT TOP 1 ID FROM tbl WHERE col1=@.col1 AND col2=@.col2
> )
> IF @.Found ' -- how to test for no match found
> Thanks.
>|||Thanks!

Saturday, February 25, 2012

Chart with Data Fields from Multiple Queries

Is there any way to create a chart using data fields from more that one query?

I tried to create two different datasets, but the chart has to be bound to only one dataset. So when I drop the data field from the second dataset onto the chart I get a SQL error.

I've also tried UNION ALL. Each of these queries is correct by itself, but UNION ALL combines GLBUDAMOUNT and GLTRXAMOUNT into one field. I need them to be two different fields so that I can do GLBUDAMOUNT VS GLTRXAMOUNT in the chart.


Solved using a simple case statement.