473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to do the following sql query

How to do the following using sql statements?
I would like to be able to have the following table
Everytime I insert a new record the AutoIncrementKey field will increase by one
but I want the PriKey to be in the order of the datetime as shown below.
INSERT TABLE DateTime = '1/1/2001 12:00:00'
INSERT TABLE DateTime = '31/1/2001 8:00:00'
INSERT TABLE DateTime = '18/1/2001 4:00:00'
INSERT TABLE DateTime = '21/1/2001 3:00:00'
............
...........
DateTime AutoIncrementKey PriKey(according to date)
1/1/2001 12:00:00 1 1
31/1/2001 8:00:00 2 4
18/1/2001 4:00:00 3 2
21/1/2001 3:00:00 4 3

How to do this?
Jul 20 '05 #1
7 4310

"Steve" <ng*****@my-deja.com> wrote in message
news:97**************************@posting.google.c om...
How to do the following using sql statements?
I would like to be able to have the following table
Everytime I insert a new record the AutoIncrementKey field will increase by one but I want the PriKey to be in the order of the datetime as shown below.
INSERT TABLE DateTime = '1/1/2001 12:00:00'
INSERT TABLE DateTime = '31/1/2001 8:00:00'
INSERT TABLE DateTime = '18/1/2001 4:00:00'
INSERT TABLE DateTime = '21/1/2001 3:00:00'
...........
..........
DateTime AutoIncrementKey PriKey(according to date)
1/1/2001 12:00:00 1 1
31/1/2001 8:00:00 2 4
18/1/2001 4:00:00 3 2
21/1/2001 3:00:00 4 3

How to do this?
Only with extreme difficulty because inserting a row may change the PriKey
of every other row.

EG
after
INSERT TABLE DateTime = '1/1/2001 12:00:00'
INSERT TABLE DateTime = '31/1/2001 8:00:00'

you have

DateTime AutoIncrementKey PriKey(according to date)
1/1/2001 12:00:00 1 1
31/1/2001 8:00:00 2 2
then, after

after INSERT TABLE DateTime = '18/1/2001 4:00:00'


you have

DateTime AutoIncrementKey PriKey(according to date)
1/1/2001 12:00:00 1 1
31/1/2001 8:00:00 2 3
18/1/2001 4:00:00 3 2

Very, very ugly stuff.

Do do this you would have to run something like

update my_table t set prikey = (select 1+count(*) from my_table where
my_date < t.my_date)

in an update, insert and delete trigger.

Very ugly stuff, and very slow and 100% guaranteed to scale poorly.

David


Jul 20 '05 #2
> Everytime I insert a new record the AutoIncrementKey field will increase
by one
but I want the PriKey to be in the order of the datetime as shown below.


Why do you need to STORE this data? I would write a stored procedure or
view that calculated it at select time. As David points out, you would need
triggers to do this and it would certainly kill the performance of your app.

(Also, not sure why this was posted to comp.databases.paradox?)

A
Jul 20 '05 #3
Aaron Bertrand - MVP wrote:
(Also, not sure why this was posted to comp.databases.paradox?)

Maybe because it's a paradox database and the queston should not have been
crossposted to the sql server groups ...
Jul 20 '05 #4
First of all, the request for additional columns this table is redundant.
There is no information about the nature of the datetime column; is it
unique? If so, declare it as your primary key, there is no need for another
column.

CREATE TABLE tbl (
dt DATETIME NOT NULL PRIMARY KEY ) ;

If you need a numeric identifier, the prikey column will suffice & the
'autoincrementkey' makes little sense. What is the rule for serialization in
case of multi-row inserts? How do you even know the order in which row is
inserted ? A popular workaround used in t-SQL is to use an IDENTITY column
like:

CREATE TABLE tbl (
dt DATETIME NOT NULL PRIMARY KEY,
autoincr INT NOT NULL IDENTITY);

And you can have a view like:

CREATE VIEW (dt, col, incr)
AS
SELECT dt, ( SELECT COUNT(*)
FROM tbl t1
WHERE t1.dt <= tbl.dt) AS "intcol",
autoincr
FROM tbl ;

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #5
> > (Also, not sure why this was posted to comp.databases.paradox?)
Maybe because it's a paradox database and the queston should not have been
crossposted to the sql server groups ...


Well, I figured majority rules. :-)
Jul 20 '05 #6
DateTime Mode
1/1/2001 12:00:00 1
31/1/2001 8:00:00 7
18/1/2001 4:00:00 3
21/1/2001 3:00:00 3
21/1/2001 5:00:00 3
22/1/2001 3:00:00 7
22/1/2001 8:00:00 7
23/1/2001 3:00:00 3
23/1/2001 9:00:00 5

What I actually want to do is just this, the insertion might not be in
any order or time,
for the above table,
get the total time for each mode Example
Mode 1 Duration = 31/1/2001 8:00:00 - 1/1/2001 12:00:00
Mode 7 Duration= 18/1/2001 4:00:00 - 31/1/2001 8:00:00 + 23/1/2001
3:00:00 - 22/1/2001 3:00:00
Mode 3 Duration= 22/1/2001 3:00:00 - 18/1/2001 4:00:00 + 23/1/2001
9:00:00 - 23/1/2001 3:00:00
Mode 5 Duration= CurrentTime Now - 23/1/2001 9:00:00
How should the sql statement be???
I actually wanted to get the starttime using the statement below but
how to get the endtimes???
Select DateTime, AutoIncrementKey Where Mode = 1
Select DateTime, AutoIncrementKey Where Mode = 7
Select DateTime, AutoIncrementKey Where Mode = 3
Select DateTime, AutoIncrementKey Where Mode = 5
Jul 20 '05 #7
Steve,

I think you are not following the implications. The calculation you have
shown as :

Mode 1 Duration =
31/1/2001 8:00:00 - 1/1/2001 12:00:00
Mode 7 Duration =
18/1/2001 4:00:00 - 31/1/2001 8:00:00 +
23/1/2001 3:00:00 - 22/1/2001 3:00:00
Mode 3 Duration =
22/1/2001 3:00:00 - 18/1/2001 4:00:00 +
23/1/2001 9:00:00 - 23/1/2001 3:00:00
Mode 5 Duration =
CurrentTime Now - 23/1/2001 9:00:00 <<

depends on the how the rows are being represented positionally in the table.
In other words, for you to decide which datetime value to be subtracted from
which other one, you have to rely on the position of a row in relative to
another. This cannot be, since the rows in a table are not ordered, it may
return different ordering of rows under various circumstances. You have 3
rows with mode 7 and 4 rows with mode 3. How do you decide which one should
be considered for Mode 7 & Mode 3 calculations in which order?

To do this reliably, you need to have a logical value ( loosely put, another
column which can represent the required sequence of datetime values on which
you do your calculations ) for each datetime value.

--
- Anith
( Please reply to newsgroups only )
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: hendershot | last post by:
can't figure out why the following locking scenario works the way it does: spid 1: start transaction select a row from table T1 with updlock result: see an Update lock for the row and index...
1
by: ravi | last post by:
I have created the following interest to calculate the interest for the following currency pairs. I have tried to combine them in macros using conditions but the next query that is run in the macro...
6
by: David | last post by:
I am using ADO to run some SQL statements in Access. First, I run query to create a table using SELECT . . . INTO Table A. Then, I run a query to SELECT . . . FROM Table A to get some data and...
2
by: Eckhart | last post by:
Dear All, Plz help me in optimising the following query, Reduce repeatable reads from the table via select ,ythe table sare not having referntial integrity constarints ,relations CREATE proc...
3
by: cmay | last post by:
The question is: Which one of the following is NOT a valid state management tool? And the possible answers are: 1. Hidden Form Fields 2. Cookies
1
by: shiniskumar | last post by:
i need a query to get a collection ... Following is the situation. ive got a collection in which item of each element is different. but each element has an item_id tat can be same.all these...
1
by: Don Li | last post by:
Hi, Env: MS SQL Server 2000 DB Info (sorry no DDL nor sample data): tblA has 147249 rows -- clustered index on pk (one key of datatype(int)) and has two clumns, both are being used in joins;...
6
by: dba | last post by:
using the following code with a problem.... echo "<input type='hidden' name='member_id' value=\"{$row}\">{$row}"; echo "<input type='radio' name='member_name' value=\"{$row}\">{$row}<br />"; ...
1
by: rajeev14th | last post by:
How can i modify the following query in DB2 (CAST(SUBS_REFILL_COUPONNO_CHANNELID AS INTEGER) (FORMAT '9(16)') (CHAR(16)) ) where SUBS_REFILL_COUPONNO_CHANNELID is the column name of my table...
0
by: vikassawant | last post by:
hi, I am explain now Database structure. Database Name:- FLB Tables :- Offence offence table contains following fields ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.