474,064 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query help needed (JOIN)

I have two tables populated during the use of an application to log
user events and application states. They are named "EventTable " and
"StateTable " and the structures follow:

EventTable:

ID EventTimeStep EventID
-- ------------- ---------
1 5 E1
2 22 E2
3 56 E3

StateTable

ID StateTimeStep StateID
-- ------------- -------
1 1 S1
2 39 S2

I want to perform a query that reports the StateID of the application
at the time that each event was logged to the EventTable. The desired
output is:

ID TimeStep EventID StateID
-- -------- ------- -------
1 5 E1 S1
2 22 E2 S1
3 56 E3 S2

I have tried to create a query with an INNER JOIN where the value for
the StateID output field comes from the last row in the StateTable
WHERE StateTable.Time Step <= EventTable.Time Step and where I use a
GROUP_BY EventTable.ID to merge the following rows from the join:

3 56 E3 S1
3 56 E3 S2

However, the closest I can get is a query that gives me the wrong
state when applying the GROUP BY clause

3 56 E3 S1

I also think that the queries I have written is slow and inefficient.
Is there a better way to perform this query or is my database design
fatally flawed?

Thanks,

Adam Nemitoff
Jul 20 '05 #1
2 1310

"Adam Nemitoff" <ad***@TeamNemi toff.com> wrote in message
news:15******** *************** ***@posting.goo gle.com...
I have two tables populated during the use of an application to log
user events and application states. They are named "EventTable " and
"StateTable " and the structures follow:

EventTable:

ID EventTimeStep EventID
-- ------------- ---------
1 5 E1
2 22 E2
3 56 E3

StateTable

ID StateTimeStep StateID
-- ------------- -------
1 1 S1
2 39 S2

I want to perform a query that reports the StateID of the application
at the time that each event was logged to the EventTable. The desired
output is:

ID TimeStep EventID StateID
-- -------- ------- -------
1 5 E1 S1
2 22 E2 S1
3 56 E3 S2

I have tried to create a query with an INNER JOIN where the value for
the StateID output field comes from the last row in the StateTable
WHERE StateTable.Time Step <= EventTable.Time Step and where I use a
GROUP_BY EventTable.ID to merge the following rows from the join:

3 56 E3 S1
3 56 E3 S2

However, the closest I can get is a query that gives me the wrong
state when applying the GROUP BY clause

3 56 E3 S1

I also think that the queries I have written is slow and inefficient.
Is there a better way to perform this query or is my database design
fatally flawed?

Thanks,

Adam Nemitoff


I think this should work (not tested):

select et.EventTimeSte p, et.EventID, max(st.StateID)
from EventTable et
join StateTable st
on st.StateTimeSte p <= et.EventTimeSte p
group by et.EventTimeSte p, et.EventID

If this doesn't work as expected, please provide CREATE TABLE statements,
with INSERTs for the data, to avoid any confusion.

As a side remark, it's not clear what the "ID" columns are - EventID and
StateID look like the natural primary keys. Also, my query doesn't return
any "ID" in the output - if you need to number the rows, it's usually best
to do that in the client application:

http://www.aspfaq.com/show.asp?id=2427

Simon
Jul 20 '05 #2
On 25 Aug 2004 06:16:46 -0700, Adam Nemitoff wrote:

(snip)

Hi Adam,

You posted this same question to comp.databases. theory as well. I believe
I saw an answer there - no need to go for doubles.

In the future, please don't multi-post!

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3

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

Similar topics

13
5603
by: aaron | last post by:
I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - judy - 3 3 - joe - 1 the favorite icecream table is this:
4
2379
by: Wanny | last post by:
Hi There, I can't seem to see what's wrong with the query below DELETE FROM Users_Details UD1 WHERE UD1.UserID = ( SELECT TOP 1 UD2.UserID FROM Users_Details UD2 WHERE UD1.useremail = UD2.useremail
9
3155
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use SUBSTRING(ProductName, 1, CHARINDEX('(', ProductName)-2). I can get this result, but I had to use several views (totally inefficient). I think this can be done in one efficient/fast query, but I can't think of one. In the case that one query is not...
4
2087
by: Christoph Bisping | last post by:
Hello! I'm seeking advice on a rather complex type of query I need to build in an Access ADP (SQL-Server 7). There are four tables: tblPeople ID(PK) PRENAME --------------- 1 Thomas 2 Frank
15
2576
by: GTi | last post by:
I have a query like: SELECT "ProjectMembers"."RoleText", "ProjectMembers"."Description", "ContactTable"."Name1", "ContactTable"."Name2", "ContactTable1"."Name1", "ContactTable1"."Name2" FROM "ContactTable","ProjectMembers","ContactTable" "ContactTable1" WHERE ("ProjectMembers"."ProjectNDX"=4) AND ("ProjectMembers"."ContactNDX" = "ContactTable"."NDX") AND ("ContactTable"."RefContact" = "ContactTable1"."NDX") This works as expected.
5
1516
by: Norma | last post by:
I am trying to make a query pull data from between the dates I enter in the parameter but also look back 'in time' to see where 2 other fields have null values, and only pull data into the query if those 2 fields are null prior to the beginning date of my parameter. The reason for this (to help make this a little clearer)is to pull production into a query if it is a 'new' for the month. That is, never run before the dates entered. I have 2...
2
1675
by: AJ | last post by:
Hi all, I have this monster query (at least i think it is). SELECT c.ID, c.Company_Name, p., 1 As QueryNbr FROM (Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID) LEFT JOIN Package AS p ON s.Package_ID = p.ID
1
1525
by: pai | last post by:
db_TBO db_TBT ----------------------------- -------------------------------------------------------- TBOID | Date TBTID | TBOID | Date ---------------------------- --------------------------------------------------------- rp01 | 01/08/2006 ap01 | rp01 | 02/08/2006 many rows ap02 | rp01 | 05/08/2006 ap03 | rp03 | 04/08/2006 I want to find TBTTD field of table db_TBT
0
1519
by: mlarson | last post by:
I have a program that worked fine then they needed to be able to also see the empty cells (inmate cells) on a housing unit when they ran the query. So what I had to do was take two tables and combine them into one with a query. The two tables are..inmtinfo and tblcelldata... inmtinfo IN_INNUM - primary key IN_NAME IN_RACE IN_BLDING IN_SECTION IN_CELLDRM IN_BED
0
2478
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in the whole. I really need MySQL wizz to give me a hand (and maybe refer me to books to get me to the wizz level myself). First off, english is a second language to me and sometimes my sentences might be a little awkward. Please forgive me. Mon...
0
10609
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
12240
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11654
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
12170
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11188
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6725
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5487
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4985
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.