|
I need to query and do a comparison on a versioned data table with a separate table dedicated to the version specific information.
The versioned data is related to pricing and the comparison is based on the date of service for the product sold. I need to find the correct price based on the version "effective date". However, there may be multiple entries for the selected product that are within the Effective date range. In this case, I need to use the record with the highest (or most recent) Version Date.
My product billing information is housed in one table. My referential pricing data is housed in another table and the version ids and dates are located in another table.
Any ideas?
|
|
|
|
|
We had the same situation with effective pay rates for employees.
I believe the structure of the table was something like this:
EmpID, Rate, EffFromDate, EffToDate
The query would be something like:
Select rate
from ratetable
where EmpId=ID and targetDate => EffFromDate and targetdate <= effTodate
Example, new employee:
123,$10.00,1/1/2000,12/31/2999
Example, pay increase on May 1, 2009
123,$10.00,1/1/2000,4/30/2008
123,$12.25,5/1/2008,12/31/2999
Notice that the most recent record should always have the artificially high "EffTodate" of 12/31/2999.
You could also make the oldest record equal to the employee's hire date instead of some artificial low date of 1/1/2000.
Best of luck.
David
|
|
|
|
|
Need Query for this requirement. The data in my table is as follows
Zip Blac White green
123 1.76 2.04 1.00
234 3.49 2.39 3.24
Need Columname as output if the column value is > 2 for given zipcode.
For example.if i give zip as "123" the query has to find out the value among the columns which is > 2. i.e White
If i give zip as "234" then the query has to find out the value among the columns which is > 2.
But here all values are > 2. So you have to pick the max value. i.e 3.49 = Blac
G. Satish
|
|
|
|
|
What have you managed to come up with so far?
I'd imagine there are quite a few different solutions to this problem so if you show what you have so far I'll try and help from there.
|
|
|
|
|
Hi,I hope i understood you good enought. Here is TSQL solution which I hope will works for you.
select * from mytable<br />
<br />
declare @zip as int<br />
set @zip = 123<br />
declare @white as varchar(100)<br />
set @white =(select white from mytable where zip=@zip)<br />
<br />
declare @black as varchar(100)<br />
set @black =(select black from mytable where zip=@zip)<br />
<br />
declare @green as varchar(100)<br />
set @green =(select green from mytable where zip=@zip)<br />
<br />
create table #temptable (valstr varchar(100),colname varchar(100))<br />
<br />
insert into #temptable values (@white,'white')<br />
insert into #temptable values (@black,'black')<br />
insert into #temptable values (@green,'green')<br />
<br />
declare @colname as varchar(100) <br />
set @colname=( select top 1 colname from #temptable order by valstr desc )<br />
drop table #temptable<br />
<br />
declare @selectquery as nvarchar(max)<br />
set @selectquery ='select '+@colname+' from mytable where zip='+cast(@zip as varchar(10))<br />
exec(@selectquery)
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Hi,
I have a table with 2,000,000 records and there is no way for me to filter it by range.
I was able to get the first 1,000,000, my problem is to get the next 1,000,000 without any duplicates from the first 1M records. Any Idea.
SELECT TOP 1000000 * from Table1
how about the next 1M??
Pls..
Dabsukol
|
|
|
|
|
Select top 1000000 colname from
(
Select top 2000000 colname from urtable order by colname desc
) T
order by colname asc
G. Satish
|
|
|
|
|
In short
For First 1000000
Select top 1000000 colname from urtable order by colname ASC
For Next 1000000
Select top 1000000 colname from urtable order by colname DESC
ZAK
|
|
|
|
|
Oops just added another record - this is a Microsoft answer, technically correct practically useless.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Select top 1m records order by ID
Get the last ID in the first set (you do have an ID field)
Select the top 1m records where ID > last ID
You can also look into Row_Number in SQL Server, this will eliminate the detect requirement but is more complex in the select. I would assume you are processing the 1m records and therefore detecting the last ID would be trivial.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I am using postgreSQL 8.0 (pgAdmin III). Whenever I create a trigger, an error "There is no in-built function 'funtion name'".
What the actual problem is with the trigger or postgresql?
Please send me the solution..
Thank u.
|
|
|
|
|
Hey,
I want to access letters in strings selected from DB.
I would like to do something like that:
SELECT word FROM dictionary
WHERE
-- and now the pseudo-code part:
word[0] is in {'a','e','o','u','y'}
AND word[1] is in {'b','c','d'}
where word[0] means first letter in selected word, word[1] second letter etc.
Is it possible to do in MS SQL?
|
|
|
|
|
Look into CHARINDEX and SUBSTRING
pseudo code ...
IF (CHARINDEX(SUBSTRING(WORD,1,1),'aeiou',1) > 1) then
found it
End if
|
|
|
|
|
big thanks!!! I haven't test it yet but it seems to be the solution
|
|
|
|
|
You should use the Like keyword:
select word from dictionary
where word like '[aeouy][bcd]%'
Wout Louwers
|
|
|
|
|
Wow ! That is a neat way of using the "like" comparison. I like it!
|
|
|
|
|
You got my 5.
Briliant.
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Hi Experts
iam having two sqlservers .where i want to move entire table from the server1 database with the data to anothers server(server2) datbase using query .plz guide me......
regards
venu
modified on Friday, July 24, 2009 5:55 AM
|
|
|
|
|
You could use SSIS or linked servers.
|
|
|
|
|
i know using linked servers but using query i shuld get it becoz this query is used in c#..... to execute so many tables ...
|
|
|
|
|
Hey Venu,
Please elaborate a bit more on what exactly are you looking for inside your C# code.
Are you trying to transfer data between different databases having identical schema from inside your C# code or you want to query different database at the same time???
Robin
|
|
|
|
|
hi robin.
actual i want to transfer the entire table structure along with datafrom local sqlserver to hosting sqlserver using c#
|
|
|
|
|
This is usually achieved by backing up the local database and restoring it on the host.
Additionaly you can script the database and uses SSIS to move the data
Or use SQL Compare from http://www.red-gate.com/index.htm?gclid=CLDSwcDM6JsCFcItpAod-kAU5Q to move the code and Data Compare to move the data. This is what I use.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
hi venu,
server1 database structure is static or dynamic??
icanmakeiteasy
|
|
|
|
|