Solved

Relation ship build query

Posted on 2014-10-19
5
142 Views
Last Modified: 2014-10-23
I have 2 tables
one is Fact Table and Customer
Fact Table fields are



Originator nvarchar(60),
[Customer Number] nvarchar(7) ,
Cust_Seq int FOREIGN KEY REFERENCES Customers(cust_num),

[Item code] nvarchar(10) FOREIGN KEY REFERENCES Item([Item code]),
[DateKey] INT FOREIGN KEY REFERENCES DimDate(DateKey)

and
Customer Table
cust_num nvarchar(7),
cust_seq INT PRIMARY KEY,
Company_name  nvarchar(60),
Address1  nvarchar(60),
Address2  nvarchar(60),
Address3  nvarchar(60),
Address4  nvarchar(60),
City  nvarchar(60),
Zip_Code  nvarchar(60),
County_State  nvarchar(60),
Country  nvarchar(60),
Telephone  nvarchar(60),
Email  nvarchar(60)


My question is

Customer Number is can't take unique,but custpmer sequence number can take unique.
becuase one customer has many orders and but customer sequence number will generate unique number according to orders(0,1,2,3...etc)

if i comapre with Fact table cust_seq field and customer table cust_seq is can't equal,becuase i will get wrong results
for example customer1 and customer 2 has same sequence number.

So can some one show me how to combine Customer table and Fact Table using which Fields(This for Dataware House).
and i need custo_seq becuase some time some time same customer address is different according cust_seq number.
0
Comment
Question by:ukerandi
5 Comments
 
LVL 23

Assisted Solution

by:David
David earned 250 total points
ID: 40390364
I admit I do not clearly understand the question.

"for example customer1 and customer 2 has same sequence number."
-- In general, when a SEQUENCE object is used to populate a unique value,  it would be illogical and a design flaw.  DML with INSERT INTO ... (customer_seq#.nextval, ....) will always return a unique number (within the constraints of the object's definition).

So, your primary key on the table is the customer sequence#, yes?

The customer sequence# is entered on the fact table as a foreign key (relational parent-child integrity), yes?
With a difference sequence generator for orders, all order# will be unique.
0
 
LVL 10

Author Comment

by:ukerandi
ID: 40390690
"for example customer1 and customer 2 has same sequence number.

Example :
Customer No                 cust_seq
E0001                             0
E0001                             1
E0001                             2
E0002                             0
E0002                             1
E0002                             2
0
 
LVL 10

Author Comment

by:ukerandi
ID: 40390694
cust_seq also can't be unique
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40391154
Since the combination of the customer_no and cust_seq is uinique you can join the tableson those two columns, i.e.

select colums_you_need
from Fact_table f left join customer_table c on f.[Customer Number] =c.cust_num and f.Cust_Seq =c.cust_seq

Open in new window

0
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 250 total points
ID: 40391473
Your Customer Table should have a composite primary key on (cust_num, cust_seq).
So you can have a composite foreign key referencing both fields:
ALTER TABLE Customer ADD CONSTRAINT PK
  PRIMARY KEY (cust_num, cust_seq)
GO

ALTER TABLE Fact ADD CONSTRAINT FK
  FOREIGN KEY ([Customer Number] , cust_seq) REFERENCES Customer (cust_num, cust_seq)

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
the term "Features Not Supported in the Next Version of SQL Server" 12 30
SQL Field Length for Email Address 3 18
SQL VIEW 7 24
Sql Query 4 19
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now