473,394 Members | 2,002 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,394 software developers and data experts.

Difference between rows

Hi,
I have a table which is something like this :

Date Hour Minute Name Cost
12/12/01 2 4 ABC 15
12/12/01 2 15 ABC 7
12/12/01 2 55 ABC 8
12/12/01 2 13 XYZ 88
12/12/01 4 17 ABC 20
12/12/01 4 49 ABC 66
12/12/01 4 8 DEF 99
12/12/02 1 9 XYZ 11

I would like to write a SQL query that would generate this result :

Date Hour Name TotalCost
12/12/01 2 ABC (15-4)*15)+ (55-15)*7 + (60-55)*8
12/12/01 2 XYZ (60-13)*88
12/12/01 4 ABC (49-17)*20 + (60-49)*66
12/12/01 4 DEF (60-8)*99
12/12/02 1 XYZ (60-9)*11

For each unique [date hour name], compute the total cost that would be
individual costs multiplied by the difference in minutes.

Any suggestions would be genuinely appreciated.

Thanks,
Sam
Nov 12 '05 #1
6 2046
Sameer wrote:
Hi,
I have a table which is something like this :

Date Hour Minute Name Cost
12/12/01 2 4 ABC 15
12/12/01 2 15 ABC 7
12/12/01 2 55 ABC 8
12/12/01 2 13 XYZ 88
12/12/01 4 17 ABC 20
12/12/01 4 49 ABC 66
12/12/01 4 8 DEF 99
12/12/02 1 9 XYZ 11

I would like to write a SQL query that would generate this result :

Date Hour Name TotalCost
12/12/01 2 ABC (15-4)*15)+ (55-15)*7 + (60-55)*8
12/12/01 2 XYZ (60-13)*88
12/12/01 4 ABC (49-17)*20 + (60-49)*66
12/12/01 4 DEF (60-8)*99
12/12/02 1 XYZ (60-9)*11

For each unique [date hour name], compute the total cost that would be
individual costs multiplied by the difference in minutes.

Any suggestions would be genuinely appreciated.


Yeah. Don't use queries for the process. Use recordsets.

Otherwise, call a function within the query. You could create a function
that computes the total cost based on the date, hour, and name that is
passed to it. You could create a column called TotalCost :
ComputeTotalCost({DateField],[HourField],[NameField])
and in a module create a function to compute the costs.

Nov 12 '05 #2

"Sameer" wrote:
Hi,
I have a table which is something like this :

Date Hour Minute Name Cost
12/12/01 2 4 ABC 15
12/12/01 2 15 ABC 7
12/12/01 2 55 ABC 8
12/12/01 2 13 XYZ 88
12/12/01 4 17 ABC 20
12/12/01 4 49 ABC 66
12/12/01 4 8 DEF 99
12/12/02 1 9 XYZ 11

I would like to write a SQL query that would generate this result :

Date Hour Name TotalCost
12/12/01 2 ABC (15-4)*15)+ (55-15)*7 + (60-55)*8
12/12/01 2 XYZ (60-13)*88
12/12/01 4 ABC (49-17)*20 + (60-49)*66
12/12/01 4 DEF (60-8)*99
12/12/02 1 XYZ (60-9)*11

For each unique [date hour name], compute the total cost that would be
individual costs multiplied by the difference in minutes.

Any suggestions would be genuinely appreciated.

Thanks,
Sam


A two steps solution, due some sql restrictions (btw, you shoud'nt use
reserved words like Date, Hour, Minute as field names):

Query1:
SELECT Date, Hour, Name,
Nz( ( SELECT Min(Minute) FROM Table as T
WHERE T.Date = Table.Date And T.Hour = Table.Hour
And t.Name = Table.Name
and T.Minute > Table.Minute), 60 ) -
Minute ) * Cost AS PartialCost
FROM Table;

Query2:
SELECT Date, Hour, Name, Sum(PartialCost) AS TotalCost
FROM Query1
GROUP BY Date, Hour, Name;

hth
Roberto
Nov 12 '05 #3
Thanks Roberto,
That was very helpful.

"Roberto Spier" <fo*****@invalid.com> wrote in message news:<bs************@ID-66191.news.uni-berlin.de>...
A two steps solution, due some sql restrictions (btw, you shoud'nt use
reserved words like Date, Hour, Minute as field names):

Query1:
SELECT Date, Hour, Name,
Nz( ( SELECT Min(Minute) FROM Table as T
WHERE T.Date = Table.Date And T.Hour = Table.Hour
And t.Name = Table.Name
and T.Minute > Table.Minute), 60 ) -
Minute ) * Cost AS PartialCost
FROM Table;

Query2:
SELECT Date, Hour, Name, Sum(PartialCost) AS TotalCost
FROM Query1
GROUP BY Date, Hour, Name;

hth
Roberto

Nov 12 '05 #4
Hi Roberto,
This query works very well in MsAccess. However, if i write this in
VB, it gives me an error indicating Nz as an invalid function. I tried
using Query Defs as well, where you execute the query in MsAccess.
That too gives the same error. I then tried using ADODB commands and
that gives me an error on the SELECT command. I am using the latest
version of ADODB, just wondering does ADODB and DAO(3.6) support the
Nz function call?.
Any comments would be truly appreciated.

Sameer
"Roberto Spier" <fo*****@invalid.com> wrote in message news:<bs************@ID-66191.news.uni-berlin.de>...
Query1:
SELECT Date, Hour, Name,
Nz( ( SELECT Min(Minute) FROM Table as T
WHERE T.Date = Table.Date And T.Hour = Table.Hour
And t.Name = Table.Name
and T.Minute > Table.Minute), 60 ) -
Minute ) * Cost AS PartialCost
FROM Table;

Query2:
SELECT Date, Hour, Name, Sum(PartialCost) AS TotalCost
FROM Query1
GROUP BY Date, Hour, Name;

hth
Roberto

Nov 12 '05 #5
This isn't the best place to get answers for accessing Jet databases (yes,
they call them "Access databases", but Access isn't involved) from VB. We
tend to assume that questions here are about Access database applications,
and NZ is a builtin function in Access so is available to Access queries. It
is not a builtin function of Jet SQL, as far as I know.

Larry Linson
Microsoft Access MVP
"Sameer" <sw***@yahoo.com> wrote in message
news:7d**************************@posting.google.c om...
Hi Roberto,
This query works very well in MsAccess. However, if i write this in
VB, it gives me an error indicating Nz as an invalid function. I tried
using Query Defs as well, where you execute the query in MsAccess.
That too gives the same error. I then tried using ADODB commands and
that gives me an error on the SELECT command. I am using the latest
version of ADODB, just wondering does ADODB and DAO(3.6) support the
Nz function call?.
Any comments would be truly appreciated.

Sameer
"Roberto Spier" <fo*****@invalid.com> wrote in message

news:<bs************@ID-66191.news.uni-berlin.de>...
Query1:
SELECT Date, Hour, Name,
Nz( ( SELECT Min(Minute) FROM Table as T
WHERE T.Date = Table.Date And T.Hour = Table.Hour And t.Name = Table.Name
and T.Minute > Table.Minute), 60 ) -
Minute ) * Cost AS PartialCost
FROM Table;

Query2:
SELECT Date, Hour, Name, Sum(PartialCost) AS TotalCost
FROM Query1
GROUP BY Date, Hour, Name;

hth
Roberto

Nov 12 '05 #6
You're right, Larry: Nz isn't a builtin function of Jet SQL. However, I
think IIf is, so that you can use IIf(IsNull(A), A, 0) instead of Nz(A, 0)

However, I don't believe Jet's going to allow the SQL statement that Sameer
has inside the Nz function, even running inside of Access.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(No private e-mails, please)

"Larry Linson" <bo*****@localhost.not> wrote in message
news:Hi****************@nwrddc01.gnilink.net...
This isn't the best place to get answers for accessing Jet databases (yes,
they call them "Access databases", but Access isn't involved) from VB. We
tend to assume that questions here are about Access database applications,
and NZ is a builtin function in Access so is available to Access queries. It is not a builtin function of Jet SQL, as far as I know.

Larry Linson
Microsoft Access MVP
"Sameer" <sw***@yahoo.com> wrote in message
news:7d**************************@posting.google.c om...
Hi Roberto,
This query works very well in MsAccess. However, if i write this in
VB, it gives me an error indicating Nz as an invalid function. I tried
using Query Defs as well, where you execute the query in MsAccess.
That too gives the same error. I then tried using ADODB commands and
that gives me an error on the SELECT command. I am using the latest
version of ADODB, just wondering does ADODB and DAO(3.6) support the
Nz function call?.
Any comments would be truly appreciated.

Sameer
"Roberto Spier" <fo*****@invalid.com> wrote in message

news:<bs************@ID-66191.news.uni-berlin.de>...
Query1:
SELECT Date, Hour, Name,
Nz( ( SELECT Min(Minute) FROM Table as T
WHERE T.Date = Table.Date And T.Hour = Table.Hour And t.Name = Table.Name
and T.Minute > Table.Minute), 60 ) -
Minute ) * Cost AS PartialCost
FROM Table;

Query2:
SELECT Date, Hour, Name, Sum(PartialCost) AS TotalCost
FROM Query1
GROUP BY Date, Hour, Name;

hth
Roberto


Nov 12 '05 #7

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

Similar topics

3
by: lkrubner | last post by:
Suppose I make a call to MySql and zero rows come back. How do I tell the difference between zero rows and failure?
1
by: Shane Niebergall | last post by:
Can someone explain why there is such a big performance difference in these two queries? Note: I have a multicolumn index on ltid and inuse. mysql> UPDATE leads SET inuse='0' WHERE inuse!='0' and...
8
by: Paulo Jan | last post by:
Hi all: I have here a table with the following schema: Table "todocinetv" Column | Type | Modifiers...
5
by: BBFrost | last post by:
Win2000 ..Net 1.1 SP1 c# using Visual Studio Ok, I'm currently in a "knock down - drag out" tussle with the .Net 1.1 datagrid. I've come to realize that a 'block' of rows highlighted within...
10
by: Tina | last post by:
In vb we say.... Dim theRow As dsLogin.CustLoginRow theRow = DsLogin1.CustLogin.Rows(0) If I try to say the following in C# it says I can't do an implicit conversion... ...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
4
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
7
by: alexbf | last post by:
Hello, I have a stored procedure in SQL Server 2000. When I execute it through my .NET application (OleDbCommand), I can see (in SQL Profiler) that it takes 423 ms to complete. If I run the...
3
by: Sandman | last post by:
Hi, So I read the manual where it says to use mysql_affected_rows() for everything except SELECT and SHOW, and use mysql_num_rows() for those two, which actually return a result. However, I...
3
by: lavadan | last post by:
Hello All, Please help me with the SQL Query. The table structure is: EffectiveDate Amount 08/31/2008 400 09/30/2008 350 10/31/2008 200 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.