Remove Duplicate Records From Left Join - sql-server-2014

I work with Microsoft Dynamics Navision 2013 and SQL 2014 as the data source. The Value entry table is the main table that host all sales transaction for customers / Items.
For some of the items we pay Excise (Tax), a certain rate we pay for all items being transferred from one warehouse to another which we call (Transfer Excise).
I work for a wine farm and for each year we enter a unit rate for each item per location(Location being the warehouse where the wine is stored before transfer out of the warehouse]. Our unit rate was created on the 28-02-28.
In order to get the calculation for the Transfer Excise I took data from various tables from Microsoft Dynamics Navision. The tables are containing the columns I took:
Location : Code, also contains a filter “NONBOND”
Item: No(PK) , Excise Type
Excise Rate: Starting Date (The starting date of the Unit Rate), Excise Type Code
Item Unit Of Measure: Item No(PK), Qty_ per unit of measure as (Litre Conversion Factor)
The data was entered into a new table called Transfer Excise Tbl 1. To simplify my example, my Transfer Excise table contains: No_ and Location Code (FK) my Value Entry Table contains Item No_(PK) , Location Code(PK). Of course I will join Transfer Excise and Value Entry table on (No_=Item No_) and Location code = Location code.
I'm trying to get all item transactions which match the Value Entry table for each unit rate for that specific starting date and the posting date when the transaction were posted using LEFT JOIN with filters.
In the Value Entry table I use Gen_ Bus_ Posting Group to filter only (LOCA & EXSA), LOCA meaning local and EXSA meaning Export South Africa. This is the query I am trying, however I get duplicates with no starting dates and posting dates. I tried using DISTINCT as seen in my query but it does not work.
SELECT DISTINCT a.[Starting Date],
b.[Posting Date],
b.[Item No_],
b.[Invoiced Quantity],
a.[Litre Conversion Factor],
a.[Unit Rate] ,
a.[Location Code],
a.[Excise Location],
a.[Excise Type Code],
a.[Unit Of Measure Code]
FROM [Transfer Excise Tbl] a
LEFT JOIN [Spier Live$Value Entry] b
ON a.[No_] = b.[Item No_]
WHERE b.[Posting Date] > '2014-03-01'
AND b.[Item No_] = 'F00335'
AND b.[Location Code] = a.[Location Code]
AND b.[Gen_ Bus_ Posting Group] IN ('LOCA','EXSA')
AND b.[Posting Date] >= a.[Starting Date]
AND b.[Invoiced Quantity] <>0
order by b.[Posting Date]

Related

What criteria can I use in MS Access to have monthly sales data appear only if it is between specified start and end dates?

I am using MS Access 2013.
The query I am working on is called "Bookings Query." Within the Bookings Query, I have two tables (Initialization and Bookings) joined together by Assignment Code. The Initialization table contains data for Sales Reps and their assignments with start/end dates and the Bookings table contains data for assignments and their monthly quota.
I need to have monthly quota data from the Bookings table only appear if it's within the Start and End Dates which come from the Initialization table. For Example: if we look at Assignment "ABC", and quota is $150,000 for January but the start and end dates for "ABC" are 2/1/21 - 12/31/21, then I need January to be $0 since quota for this assignment does not start until February.
Thanks in advance. It's a bit confusing I know.
Below is a sample I have used regularly
SELECT DISTINCTROW Accounts.AccountNumber, Accounts.AccountName, Sum(Transactions.[DEBIT AMOUNT]) AS DEBIT, Sum(Transactions.[CREDIT AMOUNT]) AS CREDIT
FROM Accounts LEFT JOIN Transactions ON Accounts.AccountID = Transactions.AccountID
WHERE (((Transactions.TransactionDate)>=[forms]![Report Date Range]![Beginning Rpt Date] And (Transactions.TransactionDate)<=[Forms]![Report Date Range]![Ending Rpt Date]))

Access SQL Query: Get specific field of a previous record looked up by date

I am pretty new to Access databases and I am trying to create an inventory database for a gas distribution company with multiple gas stations.
Each station has an inventory of more products/gas types.
The gas stations report their inventory including
TransactionDate
GallonsSold
GallonsDelivered
ActualGallonsInventory
BookedInventory
The difference between ActualGallonsInventory and BookedInventory is the overage/shortage on gas.
In my query I want to display the stock level (ActualGallonsInventory) of the previous day for the station/gas combination. How can I look up a certain entry of the previous day and display it in my query as a field?
There is only one entry for each station/gas/date combination.
Try this:
Select
*,
(Select Top 1 ActualGallonsInventory
From YourTable As T
Where T.TransactionDate < YourTable.TransactionDate
Order By T.TransactionDate Desc) As
PreviousActualGallonsInventory
From
YourTable

SQL Adding and Subtracting Data From Two Different Tables

I am new in SQL, and this is my first SQL program ever my life. I want to make a program that has the following:
A table that has the record of number of loads that a customer delivered in a week, the rate(fee) for each load to deliver, the dispatch fee.
Another table that holds expenses, such us Fuel, Insurance, Trailer rent, etc.
I need to get the total sum of all expenses of a customer, and sum of all rates (earned money for each load), and then subtract the total expenses from the total net pays for each customer.
Note:
QuickPay= Rate*0.05
NetPay=Rate-(DispatchFee+QuickPay),
TotalExpenses= Fuel+Advance+Insurance+Trailer
FinalPay=NetPay-TotalExpenses
I have created one query for each table, to make the calculation, but I need to combine all in one query. Here is the code for the queries:
Query For TblLInfo
SELECT CustName, sum(Rate) AS RateFee, sum(Disp_Fee) AS DispFee, sum(Rate*0.05) AS QuickPay, sum((Rate)-((Rate*0.05)+(Disp_Fee))) AS NetPay
FROM TblLInfo
GROUP BY CustName;
Query For TblExp
SELECT CustName, sum(Fuel) AS FuelFee, sum(Advance) AS AdvanceFee, sum(Insurance) AS InsuranceFee, sum(Trailer) AS TrailerFee,
sum((Advance)+(Insurance)+(Trailer)+(Fuel)) AS TotalExp
FROM TblExp
GROUP BY CustName;
When I tried to combine them, the result is 9 (3*3), records as I expected only 3 records.
[TblLInfo,(Table Load Info), For collecting weekly loads transported one customer][1]
[TblExp (Table Expenses), For collecting weekly expenses for each customer][2]
[Query For TblLInfo, to get the sum of all loads carried specific customer and subtract the dispatch fee, so we can get the net pay][3]
[Query for TblExp, to get total expenses for specific customer][4]
![enter image description here][1]
![enter image description here][2]
SELECT a.CustName,a.NetPay,b.TotalExp,(b.TotalExp-a.NetPay) AS overall_profit
FROM
(SELECT CustName, sum(Rate) AS RateFee, sum(Disp_Fee) AS DispFee, sum(Rate*0.05) AS QuickPay, sum((Rate)-((Rate*0.05)+(Disp_Fee))) AS NetPay FROM TblLInfo GROUP BY CustName) a
INNER JOIN
(SELECT CustName, sum(Fuel) AS FuelFee, sum(Advance) AS AdvanceFee, sum(Insurance) AS InsuranceFee, sum(Trailer) AS TrailerFee, sum((Advance)+(Insurance)+(Trailer)+(Fuel)) AS TotalExp FROM TblExp GROUP BY CustName)b

Finding the sum of a set of calculated sums

I am developing a php/mysql database.
I have a table called ‘actions’ which (amongst others) contains fields hrs, mins, actiondate, invoiceid and staffid.
For any particular actiondate there could be any number of actions carried out by various staff who would enter their time as hrs and mins.
What I need to do is produce a table which for each date and for a specific member of staff and invoice, adds up all of the hrs and mins for each date as a decimal, rounds it up to the nearest quarter and displays that result. I also need to be able to add up all of those results and display that total.
For example, if on March 1st, person with staffid=23 had carried out 4 actions for invoiced 121 lasting, 1h2m, 23m, 10m and 20m the total for that day would be 62+23+10+20 = 115m = 115/60 = 1.92 which would be rounded up to 2.00.
I can get each day’s total (maybe not very elegantly) and display it against the date using the code below
SELECT actions.`actiondate`,
(FORMAT((((CEIL((((60*SUM(hrs))+SUM(mins))/60)*4))/4)),2)) AS dayfeeqtr
FROM actions
WHERE staff.staffid=’23’
AND invoiceid=‘121’
GROUP BY actions.`actiondate`
However, what I can’t work out, is how can I add up all of these rounded up results for that invoice and that member of staff.
Can anyone help please?
If I understand correctly, you can use a subquery:
SELECT sum(dayfeeqtr)
FROM (SELECT a.`actiondate`,
FORMAT((((CEIL((((60*SUM(hrs))+SUM(mins))/60)*4))/4)), 2) AS dayfeeqtr
FROM actions a
WHERE s.staffid = '23' AND invoiceid = '121'
GROUP BY a.`actiondate`
) a;
I do note that your query is not correct -- for instance, there is a reference to staff, which is not in a from clause. However, you say that this is working, so I assume the errors are a transcription problem.

Create Customer Statement in MS Access

I have tables one is invoice & other is payment, I want to make customer statement related to it
Invoice table:
Invoice Date:
Invoice Id:
Customer Id:
quantity:
Rate:
Amount:
Payment table
Payment Date
Customer Id:
Payment Method:
Reference No:
Payment Amount:
I want to create a customer statement like
Date | Invoice Detail | Debit Amount | Credit Amount|
Date From Invoice or Payment Column according to data
Invoice amount should be debit & payment amount should be credit to form the ledger, How could i do with that Please help me out
I got the balance invoice sheet etc , but cant able to make ledger / statement of customer
Database file https://www.dropbox.com/s/hnra30rpgdlzz56/invoice-test.accdb
EDIT: This answer was constructed on the basis of the requirements and table definitions posted in the question. Upon inspection of the Access DB provided, the table structures described in the question bear little resemblance to the real implementation, however the approach still solves what was requested.
The base query is something along these lines.
SELECT [Customer ID], 'DEBIT' AS Transaction_Type, [Invoice Date], [Invoice Id] As [Ref_Num], [Amount] as [Debit], Null AS [Credit]
FROM Invoice
UNION
SELECT [Customer ID], 'CREDIT' AS Transaction_Type, [Invoice Date], [Reference No] As [Ref_Num], Null as [Debit], [Payment Amount] AS [Credit]
FROM Payment
If you want to aggregate all debit/credit transactions for a particular date it becomes fiddly as you need to merge the references, but in essence this is what you need.
I've not been able to test this so please advise if it works for you.