SQL Joining journal tables to user tables - sql-server

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.

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

Need help on a sql query to get data from multiple tables

I have a couple of tables that have data in them that I am looking to get information from. Here is the rundown....In table 1 I have bunch of columns that I am pulling data from, one of the columns is a user ID (which is a number)that was the last userID to modify a record. In table 2 I want to pull in the name of that user based on the ID that is pulled from the other table (this table has both the userID and the username).
so my final query would have the columns in table 1 as well as the username from table 2 to show that was the user to last edit the record. I assume this has to be done in a nested select statement but for the life of me I cannot come up with the correct syntax.
Can anyone help me out?
Thanks
Jeff
Yes, you need a very basic join that link both tables together.
Select t1.UserID,
t2.UserName
FROM table1 t1 INNER JOIN
table2 t2 ON t1.userid=t2.userid
select t1.*, t2.{username} from table1 as t1
join table2 as t2 on t1.{userId}=t2.{userid};
change {username} with the actual column name of user
similarly {userId} with appropriate column name in tables.
Hope it helps you.
this is standard inner join query, to learn more consider reading: http://www.w3schools.com/sql/

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.

Getting repetitive column names by adding a prefix to the repeated column name in SQL Server 2005

How can I write a stored procedure in SQL Server 2005 so that i can display the repeated column names by having a prefix added to it?
Example: If I have 'Others' as the column name belonging to a multiple categories mapped to another table having columns as 'MyColumn','YourColumn'. I need to join these two tables so that my output should be 'M_Others' and 'Y_Others'. I can use a case but I am not sure of any other repeated columns in the table. How to write that dynamically to know the repetitions ?
Thanks In Advance
You should use aliases in the projection of the query: (bogus example, showing the usage)
SELECT c.CustomerID AS Customers_CustomerID, o.CustomerID AS Orders_CustomerID
FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID
You can't dynamically change the column names without using dynamic SQL.
You have to explicitly alias them. There is no way to change "A_Others" or "B_Others" in this query:
SELECT
A.Others AS A_Others,
B.Others AS B_Others
FROM
TableA A
JOIN
TableB B ON A.KeyCol = B.KeyCol
If the repeated columns contain the same data (i.e. they are the join fields), you should not be sending both in the query anyway as this is a poor practice and is wasteful of both server and network resources. You should not use select * in queries on production especially if there are joins. If you are properly writing SQL code, you would alias as you go along when there are two columns with the same name that mean different things (for instance if you joined twice to the person table, once to get the doctor name and once to get the patient name). Doing this dynamically from system tables would not only be inefficient but could end up giving you a big security hole depending on how badly you wrote the code. You want to save five minutes or less in development by permanently affecting performance for every user and possibly negatively impacing data security. This is what database people refer to as a bad thing.
select n.id_pk,
(case when groupcount.n_count > 1 then substring(m.name, 1, 1) + '_' + n.name
else n.name end)
from test_table1 m
left join test_table2 n on m.id_pk = n.id_fk
left join (select name, count(name) as n_count
from test_table2 group by name)
groupcount on n.name = groupcount.name

Resources