For some reason, my client does not track what goes out of his store, what he does is counting the products he has every time, some products daily weekly and monthly, anyway, I have three tables that look like this
PRODUCT
------------------
| idPro | ProName|
------------------
| 1 | P1 |
| 2 | P2 |
| 3 | P3 |
------------------
STOCK
-------------------------
| idPro | idCount | Qty |
-------------------------
| 1 | 1 | 10 |
| 2 | 2 | 30 |
| 1 | 4 | 60 |
| 2 | 5 | 10 |
-------------------------
Count
------------------
| idCount | Date |
------------------
| 1 | 1100 |
| 2 | 1109 |
| 3 | 1505 |
| 4 | 1806 |
------------------
I want to get the last product count along with Qty, in other words, I want the products to be suspended for the account (I check whether the last account was more than a week or never been counted) as
----------------------------
|ProName | LastCount | Qty |
----------------------------
| 1 | 1505 | 60 |
| 2 | 1806 | 10 |
| 3 | NULL | NULL|
----------------------------
What I came up with is
USE StoTrackerBasic
GO
SELECT P.ProCode, P.ProName, MAX(R.CountDate) LastCount
FROM Product P
LEFT JOIN Stock as S ON P.idProduct = S.idProduct
INNER JOIN Count R ON R.idCount = S.idCount
GROUP BY P.ProName, P.ProCode
ORDER BY P.ProName
but cannot add Qty
, , PRODUCT 1000 , COUNT , 3 10 , Count.
PS: Count -