Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL query optimization

Author  Topic 

zion99
Posting Yak Master

141 Posts

Posted - 2008-06-18 : 08:15:56
Hi All,

table with initial data:
Primary key (COL1 + COL2)

COL1 COL2 NEW LATEST
124 1 1 1
125 0 1 1

by default, NEW and LATEST columns will have values 1, 1.

Now, one row is inserted with values (124,2)
Table data should be:

COL1, COL2, NEW, LATEST
124 1 1 0
125 0 1 1
124 2 0 1

LATEST column value changes for Row 1 since there is a repetition of value 124, meaning this row is no longer the latest.

NEW COLUMN value changes for ROW 2 since there it is no longer new; we already have an occurrence of 124 in the first row.

I m not sure if i can solve this query using any option other than cursor. it will be like taking first row --> comparing it with all the other rows and then moving further.

Plz. suggest me if there is a better approach for doing this

pootle_flump

1064 Posts

Posted - 2008-06-18 : 08:17:33
quote:
Originally posted by zion99

Plz. suggest me if there is a better approach for doing this

Get rid of the New and Latest columns and have a date_of_injsert column. Vwalah!!!
Go to Top of Page

zion99
Posting Yak Master

141 Posts

Posted - 2008-06-18 : 08:20:12
actually... that won't be possible :) this data is req. for some datamart purpose
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2008-06-18 : 08:24:15
What data? You mean New and Latest? You realise that my point is that with date of insert you can work out this data instead of recording it?
Go to Top of Page

zion99
Posting Yak Master

141 Posts

Posted - 2008-06-18 : 08:28:46
i understand that with date of insert, its possible & will be quite easy.

but unfortunately, the 2 columns need to be there for some data measures. By means of a better approach, i was asking if a simple SQL query can solve this issue or there is no way without cursors....
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2008-06-18 : 08:31:45
quote:
Originally posted by zion99

but unfortunately, the 2 columns need to be there for some data measures. By means of a better approach, i was asking if a simple SQL query can solve this issue or there is no way without cursors....

One of us is not understanding the other.
This stores the data you want to to store, uses a date column and returns what you requested.
USE tempdb
GO

IF EXISTS (SELECT NULL FROM sys.tables WHERE object_id = OBJECT_ID('dbo.my_t')) BEGIN
DROP TABLE dbo.my_t
END

CREATE TABLE dbo.my_t
(
col_1 INT NOT NULL
, col_2 INT NOT NULL
, date_of_insert DATETIME NOT NULL CONSTRAINT df_my_t_date_of_insert DEFAULT GETDATE()
, CONSTRAINT pk_my_t PRIMARY KEY CLUSTERED (col_1, col_2) WITH (FILLFACTOR = 100)
, CONSTRAINT ix_my_t_col_2_u_nc UNIQUE NONCLUSTERED (col_1, date_of_insert) WITH (FILLFACTOR = 100)
)
GO

INSERT my_t (col_1, col_2)
SELECT 124, 1
UNION ALL
SELECT 125, 0

WAITFOR DELAY '00:00:01'

INSERT my_t (col_1, col_2)
SELECT 124, 2

SELECT col_1
, col_2
, new = CASE
WHEN new_rc = 1 THEN
1
ELSE
0
END
, latest= CASE
WHEN latest_rc = 1 THEN
1
ELSE
0
END
FROM
(
SELECT col_1
, col_2
, date_of_insert
, new_rc = ROW_NUMBER() OVER (PARTITION BY col_1
ORDER BY date_of_insert ASC)
, latest_rc = ROW_NUMBER() OVER (PARTITION BY col_1
ORDER BY date_of_insert DESC)
FROM dbo.my_t
) AS rcs

edit - Forum b0rked formatification
Go to Top of Page

zion99
Posting Yak Master

141 Posts

Posted - 2008-06-18 : 08:46:31
Hi pootle_flump,
As per ur logic, the columns NEW, LATEST are created on the fly. its necessary to have these 2 columns present physically in the database because the table which i use gets a FEED from some other table on a daily basis. this means, whatever updates happen on source table are pushed to my table.
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2008-06-18 : 09:01:19
Still doesn't explain why you need to store them....You would need to explain what you mean by FEED really.

But if you are adamant you want to store this data then just tailor the above into an update query in an insert trigger.
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2008-06-18 : 11:36:30
More efficient, portable and probably easier to understand\ adapt.
SELECT	  my_t.col_1
, my_t.col_2
, new = CASE
WHEN last_first.first_date = date_of_insert THEN
1
ELSE
0
END
, latest= CASE
WHEN last_first.last_date = date_of_insert THEN
1
ELSE
0
END
FROM dbo.my_t
LEFT OUTER JOIN
(
SELECT col_1
, last_date = MAX(date_of_insert)
, first_date = MIN(date_of_insert)
FROM dbo.my_t
GROUP BY col_1
) AS last_first
ON last_first.col_1 = my_t.col_1
Go to Top of Page

zion99
Posting Yak Master

141 Posts

Posted - 2008-06-19 : 03:03:08
quote:
Originally posted by pootle_flump

Still doesn't explain why you need to store them....You would need to explain what you mean by FEED really.

But if you are adamant you want to store this data then just tailor the above into an update query in an insert trigger.



i use SSIS to get the data from source to destination. I actually tried this out. All the rows which were inserted into dest. had the same date_of_insert; thats' the reason i wanted to avoid this column.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-19 : 03:08:36
quote:
Originally posted by zion99

quote:
Originally posted by pootle_flump

Still doesn't explain why you need to store them....You would need to explain what you mean by FEED really.

But if you are adamant you want to store this data then just tailor the above into an update query in an insert trigger.



i use SSIS to get the data from source to destination. I actually tried this out. All the rows which were inserted into dest. had the same date_of_insert; thats' the reason i wanted to avoid this column.


Have you heard of SCD task in SSIS? If you use it and set the type of data change as Type 2 it will automatically create the required tasks for doing this. It will create the paths required to check if its new data (insert) or changed one (update) and accordingly it will provide you with two output paths one for old row update (124,1)and other for inserting new row(124,2).You just need to include the code in UPDATE Statement to set the LATEST to 0 in update path and 1 in insert path.
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2008-06-19 : 03:14:50
The date of insert needs to be in the source table, not the destination table.
Go to Top of Page
   

- Advertisement -