473,883 Members | 1,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query looking back

I have two tables, WIDGETS and VERSIONS. The WIDGETS table has
descriptive information about the widgets while the VERSIONS table
contains IDs relating to different iterations of those widgets over the
years. The idea is that from any widget in the database you can look
forward and backward to see different versions of the widget through
the years. Here are the tables:

WIDGETS
widget_id
name
description
year

VERSIONS
v_id
new_id
old_id
Now my query gets me somewhere in the vicinity, but not where I want to
be.

select w.widget, w.widget_id, v.new_id, v.old_id
from widgets w, versions v
where w.widget_id = 712
and w.widget_id = v.new_id
order by w.widget;

.... and I get ...

WIDGET WIDGET_ID NEW_ID OLD_ID

Widget 1 712 712 535
Widget 1 712 712 287
Widget 1 712 712 1
Widget 1 712 712 205
Widget 1 712 712 458

What I would like is a list that shows the widget names related to
OLD_ID, rather than NEW_ID. But I'm not sure how to get there. Help
is appreciated.

Jul 23 '05 #1
4 2157
Diamondback wrote:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has
descriptive information about the widgets while the VERSIONS table
contains IDs relating to different iterations of those widgets over the
years. The idea is that from any widget in the database you can look
forward and backward to see different versions of the widget through
the years.

Now my query gets me somewhere in the vicinity, but not where I want to
be.

select w.widget, w.widget_id, v.new_id, v.old_id
from widgets w, versions v
where w.widget_id = 712
and w.widget_id = v.new_id
order by w.widget;

... and I get ...

WIDGET WIDGET_ID NEW_ID OLD_ID

Widget 1 712 712 535
Widget 1 712 712 287
Widget 1 712 712 1
Widget 1 712 712 205
Widget 1 712 712 458

What I would like is a list that shows the widget names related to
OLD_ID, rather than NEW_ID. But I'm not sure how to get there. Help
is appreciated.


Maybe there is a plain sql solution out there, but it will definitely
not be an easy one. Maybe it's possible with a join/subquery
combination or stuff in that direction.

For code transparency and general ease of use I'ld suggest an API
approach. You could execute 2 separate queries where the first one ties
each [v]![old_id] to its corresponding [w]![name]. The second query
could then use these stored values from the first query.

I set up the following test environment:

SELECT * FROM w;

---------|--------|-----------|----
widget_id|name |description|ye ar
---------|--------|-----------|----
40|Widget 0| |
400|Widget 1| |
500|Widget 2| |
600|Widget 3| |
700|Widget 4| |
712|Widget 5| |
---------|--------|-----------|----

SELECT * FROM v;

----|------|------
v_id|new_id|old _id
----|------|------
1|712 |400
2|712 |500
3|712 |600
4|712 |700
5|713 |50
6|713 |80
7|714 |40
----|------|------

Now the API could first perform the following query:

SELECT v.old_id, w.name
FROM v
LEFT JOIN w
ON v.old_id = w.widget_id

And then tie those results to, let's say, a hash:

$widgetname{"$v ar_old_id"} = $var_name;

(supposed that $var_old_id is the first field and $var_name the second
one from your query result set)

Then, doing a query like:

SELECT v.old_ID AS name, w.widget_id, v.new_id, v.old_id
FROM w, v
WHERE w.widget_id =712
AND w.widget_id = v.new_id
ORDER BY w.name

will give you:

----|---------|------|------
name|widget_id| new_id|old_id
----|---------|------|------
400 |712 |712 |400
500 |712 |712 |500
600 |712 |712 |600
700 |712 |712 |700
----|---------|------|------

Finally, before showing results to screen, replace the first column by
its descriptive name that was stored in your hash.

I realise this all sounds rather cryptic. Here is a small perl program
(tested):

#!/usr/bin/perl
use DBI;
# uncomment the following 2 lines if CGI
# print "Content-Type: text/html\n\n<html>\ n<pre>\n";
# use CGI::Carp qw(fatalsToBrow ser);
$db = DBI->connect("DBI:m ysql:NAME:HOST" ,"USR", "PW");
$query = $db->prepare("SELEC T
v.old_id, w.name
FROM v
LEFT JOIN w
ON v.old_id = w.widget_id
");
$query->execute;
$numrows = $query->rows;
while (@array = $query->fetchrow_array )
{
my ($var_old_id, $var_name)= @array;
$widgetname{"$v ar_old_id"} = $var_name;
}
$query->finish;
$db->disconnect;
print "w.name|w.widge t_id|v.new_id|v .old_id|\n";
$db = DBI->connect("DBI:m ysql:NAME:HOST" ,"USR","PW") ;
$query = $db->prepare("SELEC T
v.old_ID, w.widget_id,
v.new_id, v.old_id
FROM w, v
WHERE w.widget_id =712
AND w.widget_id = v.new_id
ORDER BY v.old_ID");
$query->execute;
$numrows = $query->rows;
while (@array = $query->fetchrow_array )
{
print "$widgetname{$a rray[0]}|";
print $array[1]."|";
print $array[2]."|";
print $array[3]."|\n";
}
$query->finish;
$db->disconnect;
# uncomment this line if CGI
print "</pre>\n</html>";

Output result (when ran as non-CGI):

w.name|w.widget _id|v.new_id|v. old_id|
Widget 1|712|712|400|
Widget 2|712|712|500|
Widget 3|712|712|600|
Widget 4|712|712|700|

The same script can be made in your favourite api language if you're
not a perl man.

Hope this helps.

--
Bart

Jul 23 '05 #2
You're right, it does sound cryptic. But I'm giving it a try ... I'll
let you know how it turns out.

Jul 23 '05 #3
On 26/05/2005, Diamondback wrote:
select w.widget, w.widget_id, v.new_id, v.old_id
from widgets w, versions v
where w.widget_id = 712
and w.widget_id = v.new_id
order by w.widget;
...
What I would like is a list that shows the widget names related to
OLD_ID, rather than NEW_ID. But I'm not sure how to get there. Help
is appreciated.


Something like this?

select w.name, ow.name as oldname, w.widget_id, v.new_id, v.old_id
from widgets w, versions v, widgets ow
where w.widget_id = 712
and w.widget_id = v.new_id
and ow.widget_id = v.old_id
order by w.name;

--
felix
Jul 23 '05 #4
That did it exactly. Thank you.

Jul 23 '05 #5

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

Similar topics

2
2939
by: lawrence | last post by:
A very strange bug. www.monkeyclaus.org is run by a cms I'm developing. One of types of users we allow is "justTestingTheSite", a type of user I developed to give demo's to prospective clients. The purpose of this level of security is to let someone log in and see everything as if they were root, and yet not be able to update or delete anything, as they have no real priveledges at all. I just logged in as root and created such an...
14
9307
by: Bob | last post by:
Hi there, Need a little help with a certain query that's causing a lot of acid in my stomach... Have a table that stores sales measures for a given client. The sales measures are stored per year and there could be multiple sales measures every year per client. There is another field called last update date. If there are multiple sales measures then need to select the one that's been entered last based on this field. Also, if there
22
3082
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20 3, NULL, 9, 82, 25
6
1939
by: Steven D.Arnold | last post by:
I have a query which does not use column indexes that it should use. I have discovered some interesting behaviors of Postgres which may indicate a bug in the database's query planning. Take a look at the query below. There is a btree index on both m.account_id and a.account_id. Query (1) does not use the index on the messages table, instead opting for a full table scan, thus killing performance. The messages table can contain...
2
3555
by: Pete | last post by:
Before I get started with the question, does anyone have a (single) good book recommendation for database design? Not an Access-specific book, but something geared toward helping me figure out *what the user wants*. I've had brief formal education about data flow diagramming, but I'm looking for ... more, now that I'm actually running into problems I think stem from the fact that my users can't explain what they need done, compounded by...
4
2263
by: dp | last post by:
After looking and looking, it appears that Access ADPs graphic query designer won't display: update customer set = . from customer, where customer. = .; It comes up with the "Query Definitions Differ" dialog box. Anybody know anything about this? I can live this with I guess, however it was sure
3
2301
by: brian | last post by:
I have a problem that probably has a simple solution but I don't have much SQL experience... My company's database is pretty crummy, based on Access97 .MDB files with a VB front-end. My company sends updates to our clients every month via e-mail, over the past 7 years a lot of the addresses have gone out of use and we get more and more bounce-backs. We are looking to update our contact database but our old database does not allow us...
2
1446
by: John Hoge | last post by:
I'm looking for that elusive .net time saving magic. I have an application that presents a list of items which the user can select from. A CheckBoxList works great for that, but I want to query a database based on their selections. I'm forced to dynamically building a SQL statement in a method I could have done easily in classic ASP: SELECT fields FROM tablename WHERE ID IN (value1,value2....)
3
2169
by: Phil Stanton | last post by:
I have a number of queries which use code for the output of 1 or more fields. For example Address:GetAddress(AddressID, True, 60) Address ID Points to an Address in a table - Address Line1, Line 2, Line 3, Town, County, Post Code, Country. The True is whether to include the country and the 60 is the maximum line length (after which a line feed is inserted). All this is to output to a formatted RTF file for a handbook. In addition, I...
0
9942
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
9792
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10745
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
10848
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,...
1
7972
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
5798
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...
0
5992
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4614
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
4221
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.