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

Home Posts Topics Members FAQ

Conceptual ideas - 2 tables one changes other complete Cursors?

I think cursors might help me, but I'm not sure. I'm looking for ideas
on how to solve a problem I have.

Consider two tables, one table contains student information (very wide
100 fields) , the other historical changes of the student information,
(narrow, just fields that record changes).

As an example Table one has STUDENT_ID, STUDENT_MAJOR, STUDENT_NAME,
RECORD_DT and has one student in it.

Table two contains STUDENT_ID, STUDENT_MAJOR , CHANGE_DT and contains 2
records, since the student changed their major 2 times.

I want to end up with a table the contains 3 rows, the 2 changes to the
Major and the current student record. I want each row to be complete.
Everything that I have tried (joins, outer joins, union) I end up with
some field being null (in my example, the STUDENT_NAME would on be in
the original row, and null for the two changes)
I know this is pretty vague, but I am wondering if this is a place to
use CURSORS?
(Some of you may recognize this as a type 2 dimension or slowly
changing dimension as used in a data warehouse, which it is. I need to
build up my historical changes to I can feed it to my warehouse. I have
the current student record, and all the descreet changes made to the
student.)
TIA
Rob

Jul 23 '05 #1
18 1527
Stu
How about:

--represents current status
SELECT STUDENT_ID, STUDENT_MAJOR, RECORD_DT, STUDENT_NAME
FROM Table1
UNION ALL
SELECT t2.STUDENT_ID, t2.STUDENT_MAJO R, t2.CHANGE_DT, t1.STUDENT_NAME
FROM Table2 t2 JOIN Table1 t1 ON t2.STUDENT_ID = t1.STUDENT_ID

Or am I missing something?

Stu

Jul 23 '05 #2
Stu
How about:

--represents current status
SELECT STUDENT_ID, STUDENT_MAJOR, RECORD_DT, STUDENT_NAME
FROM Table1
UNION ALL
SELECT t2.STUDENT_ID, t2.STUDENT_MAJO R, t2.CHANGE_DT, t1.STUDENT_NAME
FROM Table2 t2 JOIN Table1 t1 ON t2.STUDENT_ID = t1.STUDENT_ID

Or am I missing something?

Stu

Jul 23 '05 #3

"rcamarda" <rc******@cable speed.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I think cursors might help me, but I'm not sure. I'm looking for ideas
on how to solve a problem I have.

Consider two tables, one table contains student information (very wide
100 fields) , the other historical changes of the student information,
(narrow, just fields that record changes).

As an example Table one has STUDENT_ID, STUDENT_MAJOR, STUDENT_NAME,
RECORD_DT and has one student in it.

Table two contains STUDENT_ID, STUDENT_MAJOR , CHANGE_DT and contains 2
records, since the student changed their major 2 times.

I want to end up with a table the contains 3 rows, the 2 changes to the
Major and the current student record. I want each row to be complete.
Everything that I have tried (joins, outer joins, union) I end up with
some field being null (in my example, the STUDENT_NAME would on be in
the original row, and null for the two changes)
I know this is pretty vague, but I am wondering if this is a place to
use CURSORS?
(Some of you may recognize this as a type 2 dimension or slowly
changing dimension as used in a data warehouse, which it is. I need to
build up my historical changes to I can feed it to my warehouse. I have
the current student record, and all the descreet changes made to the
student.)
TIA
Rob


Hi Rob,

Cursors are the devils toenails. There has to be a join that will do what
you want. Can you identify specifically what your primary key is? Once we
have this we might move forward.

regards

SYM.
Jul 23 '05 #4

"rcamarda" <rc******@cable speed.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
I think cursors might help me, but I'm not sure. I'm looking for ideas
on how to solve a problem I have.

Consider two tables, one table contains student information (very wide
100 fields) , the other historical changes of the student information,
(narrow, just fields that record changes).

As an example Table one has STUDENT_ID, STUDENT_MAJOR, STUDENT_NAME,
RECORD_DT and has one student in it.

Table two contains STUDENT_ID, STUDENT_MAJOR , CHANGE_DT and contains 2
records, since the student changed their major 2 times.

I want to end up with a table the contains 3 rows, the 2 changes to the
Major and the current student record. I want each row to be complete.
Everything that I have tried (joins, outer joins, union) I end up with
some field being null (in my example, the STUDENT_NAME would on be in
the original row, and null for the two changes)
I know this is pretty vague, but I am wondering if this is a place to
use CURSORS?
(Some of you may recognize this as a type 2 dimension or slowly
changing dimension as used in a data warehouse, which it is. I need to
build up my historical changes to I can feed it to my warehouse. I have
the current student record, and all the descreet changes made to the
student.)
TIA
Rob


Hi Rob,

Cursors are the devils toenails. There has to be a join that will do what
you want. Can you identify specifically what your primary key is? Once we
have this we might move forward.

regards

SYM.
Jul 23 '05 #5
CREATE TABLE "dbo"."F_Studen t_Sample"
(
"STUDENT_ID " VARCHAR(20) NOT NULL,
"STUDENT_LEAD_I D" VARCHAR(10) NULL,
"RECORD_DT" DATETIME NULL,
"STUDENT_LASTNA ME" VARCHAR(40) NULL,
"STUDENT_FIRSTN AME" VARCHAR(40) NULL,
"STUDENT_CAMPUS _ID" VARCHAR(10) NULL,
"STUDENT_ADMREP _ID" VARCHAR(10) NULL,
"STUDENT_MARKET CODE_ID" VARCHAR(10) NULL
)
;

insert into [F_Student_Sampl e] VALUES
('100','900','2 005-05-01','CAMARDA',' ROBERT','HOST*0 01','TLS*123',' I20')
CREATE TABLE "dbo"."Student_ Changes_Sample"
(
"STUDENT_ID " VARCHAR(20) NOT NULL,
"CHANGE_COD E" NUMERIC(19) NULL,
"CHANGE" VARCHAR(100) NULL,
"RECORD_DT" DATETIME NULL,
"STUDENT_CAMPUS _ID" VARCHAR(10) NULL,
"STUDENT_ADMREP _ID" VARCHAR(10) NULL
)
;
-- The addtion of the two columns my be redundant, (STUDENT_CAMPUS _ID
and STUDENT_ADMREP_ ID)
-- CHANGE_CODE = 7, CHANGE will contain the new value for
STUDENT_CAMPUS_ ID
-- CHANGE_CODE = 10, CHANGE will contain the new value for
STUDENT_ADMREP_ ID
-- STUDENT_ID is my "primary key" but it is not unique in this case,
since I need all the rows.

INSERT INTO [Student_Changes _Sample] VALUES
('100',7,'HOST* 002','2001-01-03','HOST*002', NULL)
INSERT INTO [Student_Changes _Sample] VALUES
('100',7,'HOST* 003','2002-04-03','HOST*003', NULL)
INSERT INTO [Student_Changes _Sample] VALUES
('100',7,'HOST* 004','2003-02-13','HOST*004', NULL)
INSERT INTO [Student_Changes _Sample] VALUES
('100',7,'DMI10 ','2003-02-13',NULL,'DMI10 ')

I need to end up with 5 rows of information, the current record found
in F_STUDENT_SAMPL E, and the 4 changes in the apporiate columns with
all the fields populated.
Thanks

Jul 23 '05 #6
Thanks Stu,
I'm ending up with null data again.
Using you example, I created:
select
student_id,
student_campus_ id,
'' as student_lastnam e
from student_changes where student_id = '1000139200'
union
select
t2.student_id,
t2.student_camp us_id,
t2.student_last name
from
student t2 join student_changes t1 on t2.student_id = t1.student_id
WHERE T2.STUDENT_ID = '1000139200'
I get:
1000139200 NULL
1000139200 003
1000139200 006
1000139200 016
1000139200 HOST*006 Iverson Iii

I need the last name (Iverson Iii) to be on all rows

Jul 23 '05 #7
Thanks Stu,
I'm ending up with null data again.
Using you example, I created:
select
student_id,
student_campus_ id,
'' as student_lastnam e
from student_changes where student_id = '1000139200'
union
select
t2.student_id,
t2.student_camp us_id,
t2.student_last name
from
student t2 join student_changes t1 on t2.student_id = t1.student_id
WHERE T2.STUDENT_ID = '1000139200'
I get:
1000139200 NULL
1000139200 003
1000139200 006
1000139200 016
1000139200 HOST*006 Iverson Iii

I need the last name (Iverson Iii) to be on all rows

Jul 23 '05 #8
Try this:

SELECT S.student_id, S.student_lead_ id, C.record_dt,
S.student_lastn ame, S.student_first name,
COALESCE(C.stud ent_campus_id,S .student_campus _id) AS student_campus_ id,
COALESCE(C.stud ent_admrep_id,S .student_admrep _id) AS student_admrep_ id,
S.student_marke tcode_id
FROM f_student_sampl e AS S,
student_changes _sample AS C

--
David Portas
SQL Server MVP
--
Jul 23 '05 #9
Try this:

SELECT S.student_id, S.student_lead_ id, C.record_dt,
S.student_lastn ame, S.student_first name,
COALESCE(C.stud ent_campus_id,S .student_campus _id) AS student_campus_ id,
COALESCE(C.stud ent_admrep_id,S .student_admrep _id) AS student_admrep_ id,
S.student_marke tcode_id
FROM f_student_sampl e AS S,
student_changes _sample AS C

--
David Portas
SQL Server MVP
--
Jul 23 '05 #10

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

Similar topics

3
10874
by: Paul | last post by:
Hi, I'm trying to add a modified datetime and userid to all 72 tables in my SQL 2000 database. I have the script to do one table, and a cursor, but it won't run across all tables. Any help would be appreciated. Thanks... DECLARE @tName varchar(40) DECLARE C1 CURSOR FOR select name from sysobjects where type = 'U' OPEN C1 FETCH NEXT FROM C1 INTO @tName -- Check @@FETCH_STATUS to see if there are any more rows to fetch
2
4586
by: Csaba2000 | last post by:
The following code has me so confused, I don't even know the right questions to ask. Opera 7.01 and IE 5.5 both exhibit behaviours I don't understand while NN 6.1 seems to ignore me altogether. The upshot: .style.cursor seems to be ignored under certain circumstances. Let's take something that's mostly repeatable and maybe someone can explain to me what's happening. In Opera 7.01
4
1753
by: Daniel Ladd | last post by:
Hi, I have a problem with a conceptual graph in c++. I have a oist of structures like this: typedef struct Conceptual { char* Name;//Rappresenta la parola da mettere nel grafo Conceptual* Next; Conceptual() {Next=NULL; Name=NULL; }
10
2064
by: heromull | last post by:
We have an asp.net app with about 200 data entry forms. Customers may enter data into any number of forms. Each form's data is persisted in a corresponding sql table. When data entry is complete, it needs to be processed. Here's where the questions start. How can we easily determine in which tables a customer has data and how best to select that data? We're not opposed to putting all the data in a single table. This table would...
1
1400
by: kuhni | last post by:
Hi everybody! Writing this time, I'm really desperate. Basically, I have a conceptual problem of how to solve a certain "problem" in MS Access 97. General objective: In order to categorise certain products, I need to know whether a specific product has been promoted in a specific week. To find this out, I have a linked table, which has all promotions for each period
3
1615
by: kaosyeti | last post by:
hello. i want to first say that i welcome anyone's ideas for this post. please feel free to throw your 2 cents in. i have a db that i'm finishing that i'm going to be giving to others to use. the ONLY thing they're going to need to manipulate in this db are 3 forms and 1 simple table (1 field which is a list of names). what is the best way to secure or lock these items, as well as the other 6 or so tables, queries and reports that i...
7
3056
by: Frank | last post by:
Hi there, I'm trying to generate a report for an old database and I'm having trouble coming up with an elegant way of going about it. Using cursors and other 'ugly' tools I could get the job done but 1) I don't want the report to take ages to run, 2) I'm not a big fan of cursors! Basically there are tables that track history and each table tends to track only a specific value housed within a date range. I'm trying to combine the tables...
4
2391
by: Troels Arvin | last post by:
Hello, I've run into situations where a table was accidentally dropped. The related database contained lots of other tables in lots of other schemas used by many different users. So I couldn't simply recover to a previous point in time (the other users' changes would be lost and/or there would be periods of no database availability). It seems that my only option was a complete, redirected restore. (Compete, because redirected restore...
13
2687
by: AliRezaGoogle | last post by:
Dear Members, I have a problem in the concepts of Monit.Enter and Monitor.Exit (Before everything I should say that I know how to solve this problem by using Monitor.Wait and I do not need a solution. But this question sticks to my head as a conceptual problem) Suppose there are two threads T1, T2 running concurrently:
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
11145
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
10748
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
10854
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
10418
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...
0
9577
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5994
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4617
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
4223
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.