A
Arvi Laanemets
Hi
When designing phone bills processing database, I need a little help. I
explain my problem step-by-step.
At start I have 3 tables:
Bills:
BillID; Month; Provider; PhoneNum; ServiceID; CallTime; CallLength; Sum;
TargetNum
Services:
ServiceID; Service; TargetCountry; NetID
TargetNets:
NetID; TargetNet
The relations are Bills.ServiceID->Services.ServiceID and
Services.NetID->TargetNets.NetID
Based on those 3 tables I create a query:
SELECT Bills.BillID, Bills.Month, Bills.Provider, Bills.PhoneNum,
Services.Service, Bills.CallTime, Bills.CallLength, Bills.Sum,
Bills.TargetNum, Services.TargetCountry, TargetNets.TargetNet FROM
(TargetNets RIGHT JOIN Bills ON TargetNets.NetID = Bills.NetID) RIGHT JOIN
Bills ON Services.ServiceID = Bills.ServiceID;
So long it's all OK. Now I have 2 additional tables
NumTypesX:
ID; PhoneNum; NumTypeID, FromDate
NumTypes:
NumTypeID; NumType
The relation between those tables is
NumTypesX.NumTypeID->NumTypes.NumTypeID. There is no direct relationship to
table Bills estabilished.
To every PhoneNum in table Bills is given NumType, which can change. Which
NumType is valid for which date is estabilished in table NumTypesX. What
I'm searching for, is a way do have for every record in query according
NumType displayed.
I have designed an UDF, which returns any value from a table, for which
certain field is equal to some value, and for which a date field is nearest
to >= to some value - i.e. it can return according ID or NumTypeID from
table NumTypeX into query. But I can't find a way to implement it into any
RIGHT JOIN clause, so that right NumType is returned. I.e. I have working
queries:
SELECT Bills.BillID, Bills.Month, Bills.Provider, Bills.PhoneNum,
Services.Service, Bills.CallTime, Bills.CallLength, Bills.Sum,
Bills.TargetNum, Services.TargetCountry, TargetNets.TargetNet,
MyFunction(...) As ID FROM (TargetNets RIGHT JOIN Bills ON TargetNets.NetID
= Bills.NetID) RIGHT JOIN Bills ON Services.ServiceID = Bills.ServiceID;
, or
SELECT Bills.BillID, Bills.Month, Bills.Provider, Bills.PhoneNum,
Services.Service, Bills.CallTime, Bills.CallLength, Bills.Sum,
Bills.TargetNum, Services.TargetCountry, TargetNets.TargetNet,
MyFunction(...) As NumTypeID FROM (TargetNets RIGHT JOIN Bills ON
TargetNets.NetID = Bills.NetID) RIGHT JOIN Bills ON Services.ServiceID =
Bills.ServiceID;
, but I need an according NumTypes.NumType as last column instead!
Thanks in advance for any help!
When designing phone bills processing database, I need a little help. I
explain my problem step-by-step.
At start I have 3 tables:
Bills:
BillID; Month; Provider; PhoneNum; ServiceID; CallTime; CallLength; Sum;
TargetNum
Services:
ServiceID; Service; TargetCountry; NetID
TargetNets:
NetID; TargetNet
The relations are Bills.ServiceID->Services.ServiceID and
Services.NetID->TargetNets.NetID
Based on those 3 tables I create a query:
SELECT Bills.BillID, Bills.Month, Bills.Provider, Bills.PhoneNum,
Services.Service, Bills.CallTime, Bills.CallLength, Bills.Sum,
Bills.TargetNum, Services.TargetCountry, TargetNets.TargetNet FROM
(TargetNets RIGHT JOIN Bills ON TargetNets.NetID = Bills.NetID) RIGHT JOIN
Bills ON Services.ServiceID = Bills.ServiceID;
So long it's all OK. Now I have 2 additional tables
NumTypesX:
ID; PhoneNum; NumTypeID, FromDate
NumTypes:
NumTypeID; NumType
The relation between those tables is
NumTypesX.NumTypeID->NumTypes.NumTypeID. There is no direct relationship to
table Bills estabilished.
To every PhoneNum in table Bills is given NumType, which can change. Which
NumType is valid for which date is estabilished in table NumTypesX. What
I'm searching for, is a way do have for every record in query according
NumType displayed.
I have designed an UDF, which returns any value from a table, for which
certain field is equal to some value, and for which a date field is nearest
to >= to some value - i.e. it can return according ID or NumTypeID from
table NumTypeX into query. But I can't find a way to implement it into any
RIGHT JOIN clause, so that right NumType is returned. I.e. I have working
queries:
SELECT Bills.BillID, Bills.Month, Bills.Provider, Bills.PhoneNum,
Services.Service, Bills.CallTime, Bills.CallLength, Bills.Sum,
Bills.TargetNum, Services.TargetCountry, TargetNets.TargetNet,
MyFunction(...) As ID FROM (TargetNets RIGHT JOIN Bills ON TargetNets.NetID
= Bills.NetID) RIGHT JOIN Bills ON Services.ServiceID = Bills.ServiceID;
, or
SELECT Bills.BillID, Bills.Month, Bills.Provider, Bills.PhoneNum,
Services.Service, Bills.CallTime, Bills.CallLength, Bills.Sum,
Bills.TargetNum, Services.TargetCountry, TargetNets.TargetNet,
MyFunction(...) As NumTypeID FROM (TargetNets RIGHT JOIN Bills ON
TargetNets.NetID = Bills.NetID) RIGHT JOIN Bills ON Services.ServiceID =
Bills.ServiceID;
, but I need an according NumTypes.NumType as last column instead!
Thanks in advance for any help!