Data warehouse Fact table foreign key Generation - sql-server

I am pretty new to data warehouse designing.
I have a database like below.
https://webpages.charlotte.edu/mirsad/itcs6265/group1/domain.html
I am planning to create a simple data warehouse like below.
But the problem I am facing now is I don't have ClientId and LoanId in the Fact table. Because in the original database it was lnked via Account table.
I am trying to achieve this via SQL server. Can someone show me the direction how to approach this.

If you want to create the FactTransaction fact table you don't need the Loan because it represents a fact so Transaction and Loan are two differnt actions like mentioned in the data dictionary :
TRANSACTIONS (TRANS) Each record describes one transaction on an
account
LOANS Each record describes a loan granted for a given account
the design can be like below if we follow a separate context for each :
Datamart Transaction :
Datamart Loan :
The query to populate the FactTransaction is like below :
SELECT T.account_id AS TransactionID, C.client_id AS ClientID,
A.account_id AS AccountID
FROM Transactions AS T
LEFT JOIN Account AS A ON A.account_id=T.account_id
LEFT JOIN Disposition AS D ON A.account_id=D.account_id
LEFT JOIN Client AS C ON C.client_id=D.client_id
AccountID, TransactionID, ClientID represent a composite key to uniquely identify the tuple transaction in a the fact table.
The query to populate the FactLoan is like below :
SELECT L.account_id AS LoanID, C.client_id AS ClientID,
A.account_id AS AccountID
FROM Loan AS L
LEFT JOIN Account AS A ON A.account_id=A.account_id
LEFT JOIN Disposition AS D ON A.account_id=D.account_id
LEFT JOIN Client AS C ON C.client_id=D.client_id
AccountID, LoanID, ClientID represent a composite key to uniquely identify the tuple transaction in a the fact table.
Do not forget to implement the DimDate.

Related

SQL query to join "GeneralJournalAccountEntry" to table "CustInvoiceTrans" to get column value "MainAccount"

We have a Dynamics AX 2012 environment backed by SQL Server. We load invoice line data from table "CustInvoiceTrans" into an EDW fact table. We are wanting to find what the "MainAccount" column value is for each invoice line as it relates to table "GeneralJournalAccountEntry". This would need to be done through a SQL query.
I have attempted an Internet search on the topic, but found nothing definitive. I have also searched through the AX SQL views hoping to find a solution there, but did not come across anything. It is possible that I could be missing something though.
SQL Pseudo Example:
SELECT CIT.InvoiceID
,CIT.InvoiceDate
,CIT.SalesPrice
,GJAE.MainAccount
FROM CustInvoiceTrans CIT
LEFT JOIN {..Some table(s)} ST
ON ST.{SomeColumn} = CIT.{SomeColumn}
LEFT JOIN GeneralJournalAccountEntry GJAE
ON GJAE.{SomeColumn} = ST.{SomeColumn}
If this is possible, it would assume that one invoice line relates to one general ledger account entry line to return one MainAccount. I am uncertain if an invoice line could be split among multiple general ledger account entry lines.
It sounds to me like you are worried that you might end up with duplicates when you join the GeneralJournalAccountEntry table? My example shows how you can ensure that the GeneralJournalAccountEntry table has a 1:1 relationship with the CustInvoiceTrans table. I've assumed:
That there is a primary key for your accounts and customers (AccountId and CustomerId)
That there is some table that maps customer's to accounts
SELECT
cit.InvoiceID
, cit.InvoiceDate
, cit.SalesPrice
, t.MainAccount
FROM (
SELECT
gjae.AccountId
, gjae.MainAccount
FROM GeneralJournalAccountEntry gjae
GROUP BY
gjae.AccountId
, gjae.MainAccount
) t
JOIN [SomeTable] st
ON st.AccountId = t.AccountId
JOIN CustInvoiceTrans cit
ON cit.CustomerId = st.CustomerId

Basic SQL - What’s best possible answer if you know ?

In SQL Server, you have a Customer table and an Order table. The relationship between the two tables is the CustomerId field. It’s a foreign key from the Orders back to the primary table Customer. With that in mind, how would you write a query that would retrieve Customers that don’t have Orders.
The following will return all customers on the left of the query and will show a NULL on the right where there are no matching order records.
We then filter to only show those with NULL in the right table.
SELECT *
FROM Customers c
LEFT JOIN Orders o
ON c.CustomerId = o.CustomerId
WHERE o.CustomerId IS NULL
Below is a great diagram explaining the different types of join

SQL Joining journal tables to user tables

I have a MSSQL database with 3 tables: Journals, Customers, and UserAccounts.
I'm trying to query Journals for transactions per account manager. This table has a customer ID column that links to Customers.
The Customers table has a ACC_Manager column that links to UserAccounts via UserID.
Inside the UserAcounts table are first and last name columns.
So it would be
Select
Journal.amount,
Customer.name,
UserAccounts.first
From
Tables
where
Journal.ACC_manager = 'Matt'
I'm having issues joining the tables so I can query using UserAccounts.first. Could anybody help? Thanks
Try the following. I didn't get exact column names so don't just use this code without modifying it a bit to suit your specific needs:
SELECT
j.amount,
c.name,
u.first
FROM
Journals j
JOIN Customers c ON
c.customerID = j.customerID -- Exact column names?
JOIN UserAccounts u ON
u.UserID = c.ACC_Manager
WHERE
u.first = 'Matt'
You may also need to use LEFT JOIN as opposed to JOIN. Read up on JOINs to be sure.

How to create a report based on multiple tables?

Here is the setup:
I have a database with about 7 tables. one of table (tblUsers) stores the names of the users of the database while the other six tables are used to store information inputted by the users of the database. with that I have a one-to-many relationship between the tblUsers and all the other tables in the database showing "ALL records from 'tblUsers' and only those records from 'OtherTable' where the joined fields are equal."
I have created a form (frmReport)with a combobox select a user from tblUsers and two text box that will allow me to select a date range.
Here is what I would like to accomplish: I would like to create a report that will return information from all the six tables based on criteria in the form.
PS. Im able to create the a report based on two tables eg. tblUsers and one other table, but whenever I have more that 2 table that's when I'm confused. I hope I'm clear enough.
Use one query with multiple LEFT JOINs such as:
SELECT tblUsers.*, tblA.*, tblB.*, tblC.*, tblD.*, tblE.*, tblF.*
FROM tblUsers
LEFT JOIN tblA ON tblUsers.pk=tblA.fk
LEFT JOIN tblB ON tblUsers.pk=tblB.fk
LEFT JOIN tblC ON tblUsers.pk=tblC.fk
LEFT JOIN tblD ON tblUsers.pk=tblD.fk
LEFT JOIN tblE ON tblUsers.pk=tblE.fk
LEFT JOIN tblF ON tblUsers.pk=tblF.fk
The LEFT JOIN includes only records from the first table and records that match on the second table. In this case I refer to pk as the primary key in tblUsers, and fk as the associated foreign key in the other tables. You can replace your table names in this query example, of course.
This will create one long record associated with each user in the tblUser table, but no records not matching the user.

Proper way to filter a table using values in another table in MS Access?

I have a table of transactions with some transaction IDs and Employee Numbers. I have two other tables which are basically just a column full of transactions or employees that need to be filtered out from the first.
I have been running my query like this:
SELECT * FROM TransactionMaster
Where TransactionMaster.TransID
NOT IN (SELECT TransID from BadTransactions)
AND etc...(repeat for employee numbers)
I have noticed slow performance when running these types of queries. I am wondering if there is a better way to build this query?
If you want all TransactionMaster rows which don't include a TransID match in BadTransactions, use a LEFT JOIN and ask for only those rows where BadTransactions.TransID Is Null (unmatched).
SELECT tm.*
FROM
TransactionMaster AS tm
LEFT JOIN
BadTransactions AS bt
ON tm.TransID = bt.TransID
WHERE bt.TransID Is Null;
That query should be relatively fast with TransID indexed.
If you have Access available, create a new query using the "unmatched query wizard". It will guide you through the steps to create a similar query.

Resources