MS Access how to check start date and first sales - sql

I'm having some difficulties trying to figure out when a sales rep makes their first sales.
I have two tables
The first table is the sales rep with their information setup like so
ID | First Name | Last Name | Start Date |
My second table is a table of all the sales combined from every rep like so
Order Number | Order Date | REP ID | Sales Amount |
I'm trying to create a query where I can list the Rep information, and when their first sales date was.
Some help would be great!
Thanks

You have to JOIN the two tables to get the information. Since you are only interested in the First Sale for every employee, you need to make use of the Min. Something like
SELECT
ID,
[First Name],
[Last Name],
[Start Date],
Min([Order Date]) As [First Sale]
FROM
firstTable INNER JOIN secondTable
ON
firstTable.ID = secondTable.[Rep ID]
GROUP BY
ID,
[First Name],
[Last Name],
[Start Date]

Related

Query to Sum values from one table based on values from another table, considering null values

I have two tables in MS Access. One for contracts, with contract id and total value. Another one for the payments made in each contract.
So for example when a client gets a new contract for a total value of USD 100.00 then he can pay it in any amount he want up to the total value, like two payments of 50.00 or one of 30.00 and another of 70.00 or even one hundred payments of 1.00.
And those payments go to the payments table with the contract id and the payment amount.
I need to make a query to find how much do they still have to pay for each contract. What is the net amount for each contract.
I managed to make a query that gives me the result I need, but if the contract is new and there are no payments on the payments table for this new contract, the net amount shown in my query is zero (blank field) and I needed it to show the total value of the contract instead.
For example
Contract of 100.00 with two payments of 10.00 and 15.00 should show the net value as 75.00
Contract of 100.00 with no payments should show net value as 100.00
Here is what I have so far:
'Query 1
SELECT Contracts.[Contract ID],
Sum(Payments.[Payment Value]
FROM Contracts
LEFT JOIN Payments
ON Contracts.[Contract ID] = Payments.[Contract ID]
GROUP BY Contracts.[Contract ID]
Query 2:
SELECT Contracts.[Contract ID],
Contracts.[Contract Value],
Query1.[SumOfValue Payment Value],
[Contract Value] - [SumOfValue Payment Value]
INTO NetValues
FROM Contracts
INNER JOIN Query1
ON Contracts.[Contract ID] = Query1.[Contract ID]
Basically the Query 1 , sums the payments for each contract and Query 2 calculates the net value (total value - paid amount)
If there is any better way to achieve all of this please let me know.
You can do it as a single query as follows:
SELECT c.[Contract ID],
c.[contract value],
p.Paid,
c.[contract value]-nz(p.paid) AS Remaining
FROM Contracts AS c
LEFT JOIN
(SELECT [Contract ID], sum([Payment Value]) as paid
FROM Payments
group by [Contract ID]) AS p
ON c.[Contract ID] = P.[Contract ID];
Given these two tables:
The query will produce this result:
" ... if the contract is new and there are no payments on the payments table for this new contract, the net amount shown in my query is zero (blank field)"
Actually, when you have no payments recorded for a given contract, Query1 gives you Null, not zero, as the sum of the payments. And any number minus Null gives you Null, not that first number.
If you want the Null treated as zero, use the
Nz Function
Here's an Immediate window example of those issues:
Contract_Value = 100.00
SumOfValue_Payment_Value = Null
? Contract_Value - SumOfValue_Payment_Value
Null
' use Nz() to substitute zero for Null
? Contract_Value - Nz(SumOfValue_Payment_Value, 0)
100
You can do the same in Query2 by including this:
[Contract Value] - Nz([SumOfValue Payment Value], 0)

Analysis of customer along time

I have the task of developing a SQL query for analysing the behaviour of a customer through time.
So, I started with two tables; a calendar table (with each year-month-day of some years), and a sales table (with an ID and a purchase date of my interest) This is the query:
SELECT [Spice Id], FORMAT([Fecha venta],'yyyyMM') AS Purchase_Date
INTO #Sale_date
FROM SALES
WHERE [Spice Id] IS NOT NULL
GROUP BY [Spice Id], [Fecha venta]
enter image description here and enter image description here
Then I use a cross join to have all dates available even though the customer has no purchase in dates, I use a where clause to limit the table as I want to. Query below
SELECT [Spice Id], year, YearMonth, Purchase_Date, (Purchase_Date) AS First_purchase, (Purchase_Date) AS Last_purchase
INTO #Sorted
FROM #calendar
CROSS JOIN #Sale_date
WHERE year > 2019
AND Purchase_Date > 202001
AND Purchase_Date < FORMAT(GETDATE(),'yyyyMM')
AND YearMonth BETWEEN purchase_date AND FORMAT(GETDATE(), 'yyyyMM')
GROUP BY
[Spice Id], Year, YearMonth, Purchase_Date
ORDER BY
[Spice Id], Year ASC, YearMonth, Purchase_date
enter image description here
Then as you can see first purchase and lastpurchase are just the same as purchase date, so I update both values with the following:
----------UPDATE MIN
UPDATE #Sorted
enter code here`SET #SORTED.First_purchase = t1.minimo
FROM #Sorted
INNER JOIN
(SELECT [SPICE ID],MIN([First_purchase]) AS minimo
FROM #Sorted
GROUP BY [Spice Id])
AS t1 on t1.[spice id] = #Sorted.[spice id]
--------------Update Max
UPDATE #Sorted
SET #SORTED.Last_purchase = t1.maximo
FROM #SORTED
INNER JOIN
(SELECT [SPICE ID],MAX([Last_purchase]) AS maximo
FROM #Sorted
where Purchase_Date <= YearMonth
GROUP BY [Spice Id])
AS t1 on t1.[spice id] = #Sorted.[spice id]
So once I updated both values I got the following result, I'll use a specific ID to be more clear:
enter image description here
AS you can see, I have some mistakes Purchase_Date is not correctly ordered, I really don't mind that much because i can drop that column and leave just Start and Last, my big trouble is with last purchase, it should change overtime and update as in the following example I don't know if you find any mistakes in my logic or a better way to get to it, I wish to see the final table as this
enter image description here
I hope it is clear enough, thank you very much for your help!!
I'm not entirely sure your desired output but the updating logic to get the first and last purchase date is entirely unneccessary. You can use something like the below to always obtain these values
min(Purchase_date) over (partition by spice_id) as first_purchase
max(Purchase_date) over (partition by spice_id) as last_purchase
If you wish for purchase date to be ordered correctly then you need to evalute your order by clause, since this is last in the clause it is ordered after the first several columns have been ordered

MS ACCESS: Unable to retrieve LAST Price Paid based on Maximum Date

OBJECTIVE
Develop a sales catalog for a COMPANY ID based on ITEM ID and latest PRICE paid (based on LAST SHIP DATE).
APPROACH
Pull in CUSTOMER, SALES, ITEM Tables
Run Query Link tables based off of CUSTOMER ID and ITEM to understand purchase history
Export a table showing a COMPANY ID, ITEM ID, LAST SALES PRICE, LAST SHIP DATE
CODE
SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
(CCur([SDLPRC])/10000) AS PRICE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM,
(CCur([SDLPRC])/10000);
ISSUE/QUESTION
CUSTOMER A bought ITEM ABC # 3 different prices on 3 different dates. I've taken the Max of Ship Date in hope to show the LAST PRICE PAID (resulting in one single value for price). However, for some reason, I am still receiving the three different prices on three different dates. How can I have MS Access only display the latest price based off of the latest ship date?
NOTE: SDLPRC = "Sold Price". I have to convert SLDPRC into a Currency and then divide by 1000; this is due to our current database setup. Also, SDAITM is an "Abbreviated Item Number" that is more customer-friendly.
The problem is that you're grouping by your Price variable (CCur([SDLPRC])/10000). When you use GROUP BY, Access/SQL will split the rows by all the variables in the GROUP BY statement. So you need to not group by price.
Change your query to use a subquery that finds the last date of a sale grouped by [Company ID], [Item ID] and Style. The use an outer query to grab the price for that particular record. Something like:
SELECT b.[COMPANY ID], b.[ITEM ID], b.STYLE, b.[LAST SHIP DATE], CCur(a.[SDLPRC])/10000 as PRICE
INTO [Table - Sales Details]
FROM [Sales Order Details (F42119)] as a
INNER JOIN
(SELECT
[Sales Order Details (F42119)].SDAN8 AS [COMPANY ID],
[Sales Order Details (F42119)].SDITM AS [ITEM ID],
[Sales Order Details (F42119)].SDAITM AS STYLE,
Max([Sales Order Details (F42119)].SDDRQJ) AS [LAST SHIP DATE]
FROM [Sales Order Details (F42119)]
GROUP BY
[Sales Order Details (F42119)].SDAN8,
[Sales Order Details (F42119)].SDITM,
[Sales Order Details (F42119)].SDAITM
) as b
ON a.SDAN8 = b.[COMPANY ID]
and a.SDITM = b.[ITEM ID]
and a.SDAITM = b.STYLE
and a.SDDRQJ = b.[LAST SHIP DATE]

Extract a specific line from a SELECT statement based on the last Trasanction date

Good day,
I have an SQL code that return to me all quantities that I received over time, but I want to display only the latest one
SELECT * FROM
(SELECT DISTINCT
[dbo].[ttcibd001110].[t_cmnf] AS [Manufacturer],
[dbo].[ttcibd001110].[t_item] AS [Item code],
[dbo].[ttcibd001110].[t_dsca] AS [Description],
[dbo].[ttcibd001110].[t_seak] AS [Search key 1],
[dbo].[twhinr110110].[t_trdt] AS [Transaction date],
[dbo].[twhinr110110].[t_cwar] AS [Warehouse],
[dbo].[twhinr110110].[t_qstk] AS [Quantity Inventory Unit]
FROM [dbo].[twhinr110110] LEFT JOIN [dbo].[ttcibd001110]
ON [dbo].[twhinr110110].[t_item]=[dbo].[ttcibd001110].[t_item]
WHERE [dbo].[twhinr110110].[t_koor]='2' AND [dbo].[ttcibd001110].[t_cmnf]='ManufacturerX') AS tabel
WHERE ltrim(tabel.[Item code])='1000045'
Now, from this selection I want to select only the line with the latest Transaction date, but I am stuck.
Can somebody help me in this way?
Thank you!
Change your beginning to
SELECT TOP 1
and after where use
ORDER BY [Transaction date] DESC

SSRS Report - Complex Layout?

I need to create a report, but I'm unsure how to get the layout correct.
The way the report should look is Here
The data is currently formatted like so:
Customer No | Sale Price | Cost Price | Margin | Date
Customer A | 200 | 100 | 100 | 1/1/14
Is it possible to design a report with this layout? I suspect so, but I haven't encountered this issue yet.
Any hints and tips to get me on my way?
Thanks!
Since what you want is actually a cross-tab, it is easier if your dataset is structured like this:
Customer No Price Type Date Amount
----------- ---------- ------- ------
Customer A Sale 1/1/14 200
Customer A Cost 1/1/14 100
Customer A Margin 1/1/14 100
...
To achieve this, simply use UNION ALL in the SQL statement of your dataset, like so:
SELECT [Customer No], 'Sale' AS [Price Type], [Date], [Sale Price]
FROM MyTable
UNION ALL
SELECT [Customer No], 'Cost' AS [Price Type], [Date], [Cost Price]
FROM MyTable
UNION ALL
SELECT [Customer No], 'Margin' AS [Price Type], [Date], [Margin]
FROM MyTable
With a dataset like this, it is straightforward to get the report layout you want, for example using the Tablix Wizard.
I managed to get it working. It actually wasn't too difficult, to my surprise. This probably isn't the best way to do things, but it worked well for me!
Query
SELECT sa.[Document No_]
,[sa.Customer No_]
,DATEPART(m, sa.[Posting Date]) AS MonthName
,sa.Quantity
,sa.[Amount (LCY)]
,sa.[Cost (LCY)]
,sa.[Profit (LCY)]
,c.[Salesperson Code]
,c.NAME
FROM [Sales Analysis] AS sa
INNER JOIN [Customer] AS c ON c.[No_] = sa.[customer no_]
WHERE [Posting Date] BETWEEN '2014-01-01' AND '2014-05-31'
AND [Customer No_] IS NOT NULL
AND [Customer No_] <> ''
Matrix Layout
Results
Thanks for the help!