I created a spare list that have Part In and Part Out. As illustrate in image below.
As you can see there is Part In, Part Out and Stock Balance. Which when I enter the Part In value=1, the Stock balance value is equal to 1 and when I enter the Part Out value it will deduct the stock balance value. The problem is how to capture the Stock balance value from row 1 into row 2? The idea when entered new value Part In :
[Stock Balance row 2 = Stock Balance from row 1 + Part In]
I hope you understand my explanation and will help me out. Thank you.
As it's too long for a comment I'll add this as an answer to flesh out my comment of
it may be better just to have three fields in the table - date/time of
transaction, direction of transaction (in or out) and number of parts
in transaction. You can then add all the parts in, minus all the parts
out and get the balance at any point in time
Run the following VBA to create a table containing the three fields and add some sample data:
Sub Test()
With DoCmd
.SetWarnings False
.RunSQL "CREATE TABLE Stock (" & _
"Date_Time DateTime, Direction CHAR, Unit INTEGER)"
.RunSQL "INSERT INTO Stock VALUES(#2017/11/20 07:00:00#, 'In', 100)"
.RunSQL "INSERT INTO Stock VALUES(#2017/11/20 07:00:01#, 'Out', 5)"
.RunSQL "INSERT INTO Stock VALUES( #2017/11/20 07:00:02#, 'In', 3)"
.RunSQL "INSERT INTO Stock VALUES(#2017/11/20 07:00:05#, 'In', 2)"
.RunSQL "INSERT INTO Stock VALUES(#2017/11/20 07:10:00#, 'Out', 15)"
.SetWarnings True
End With
End Sub
This SQL will give you a running total of the balance:
SELECT T1.Date_Time
, T1.Direction
, T1.Unit
, (
SELECT SUM(Unit)
FROM Stock
WHERE Direction='IN' AND Date_Time<=T1.Date_Time
) -
NZ((
SELECT SUM(Unit)
FROM Stock
WHERE Direction='OUT' AND Date_Time<=T1.Date_Time
),0) AS Balance
FROM Stock T1
This SQL will give the balance at a specific point in time:
SELECT Date_Time AS TimeFrame
, (
SELECT SUM(Unit) AS Stock_In
FROM Stock
WHERE Direction='In' AND Date_Time<=#2017/11/20 07:00:05#
) -
(
SELECT SUM(Unit) AS Stock_Out
FROM Stock
WHERE Direction='Out' AND Date_Time<=#2017/11/20 07:00:05#
) AS Balance
FROM Stock
WHERE Date_Time=#2017/11/20 07:00:05#
Related
I have a "CONTRACTS" table in which the user can select whether a Contract is "ANUAL" or "MONTHLY" (working on MariaDB/phpmyadmin)
The data is stored in the following manner:
CONTRACT
PERIOD
CICLE
SALE PRICE
CATEGORY
001
1
YEARLY
12000
CAT1
002
1
MONTHLY
1000
CAT2
I want to make a report that tells me the SUM of monthly contracts by CATEGORY
RIGHT NOW, THIS QUERY BELOW WORKS but its useless, since its doing SUM of "yearly" contracts along with monthly contracts
SELECT SUM(contracts.salesprice), `categories`.*
FROM `contracts`
LEFT JOIN `categories` ON `contratos`.`cat_id` = `categories`.`id_cat`
GROUP BY categorias.descripcion_cat;1
I'm a newbie and so far I was fine with INSERT, SELECT, UPDATE, DELETE;
I tried reading all documentation about CASE or IF, but I cant figure how to tell mysql to SUM based AND calculate on conditions
when CICLE = YEARLY then SALEPRICE /12 (to get the monthly value)
You were on the correct track with CASE.
The following code snippet will convert your yearly sales prices into monthly:
SUM(
CASE
WHEN contracts.cicle = 'YEARLY' THEN (contracts.salesprice / 12)
WHEN contracts.cicle = 'MONTHLY' THEN contracts.salesprice
ELSE 0
END
)
To use it in your query, simply replace your SUM(...) with that one.
To explain what it is doing, the CASE statement has several WHEN conditions. It uses the value of the first one that is true, if none are true, it will use the ELSE value (which you can change if you don't like 0). All of those resulting values are then summed up with SUM.
The benefit of CASE over IF is that CASE can be expanded as needed if you need more calculations for bi-annual, quarter, etc.
I have a table called membership. One column is called amount_paid and the other is called valid_membership.
I have another table called m_type with a column called price.
The linking clause is WHERE membership.type_id = m_type.type_id
I want a trigger before insert on the membership table to check if amount paid is >= 1/2 of price. If it is greater i want 1 to be placed in valid_membership else if it isn't true then 0 to be placed in valid_membership.
Just having a little trouble with the correct syntax,
This is what i have tried already--
DELIMITER
//
CREATE TRIGGER valid_membership
BEFORE INSERT ON membership
FOR EACH ROW
BEGIN
IF (NEW.amount_paid >= 1/2 price) THEN
SET valid_membership = '1' ELSE '0'
WHERE membership.type_id = m_type.type_id
END IF;
END
//
DELIMITER ;
Thanks
1/3 price should coded as price / 3, but even better you could use plain SQL:
update membership set
valid_membership = (
select amount_paid
from price
where membership.type_id = m_type.type_id
) >= price / 3
which can be executed as a plain update query, to both test the query and also backfill existing data.
For the trigger:
set new.valid_membership = (
select amount_paid
from price
where new.type_id = m_type.type_id
) >= price / 3
This does use a small “trick” special to MySQL: In MySQL, true is 1 and false is 0.
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]
DECLARE
v_in auto_service.vin%TYPE;
v_first auto_service.service_date%TYPE;
v_last auto_service.service_date%TYPE;
v_max auto_service.price%TYPE;
v_total auto_service.price%TYPE;
v_n NUMBER;
CURSOR c_auto
IS
SELECT vin,
COUNT(*) AS no,
MIN(SERVICE_DATE) AS FIRSTprice,
MAX(SERVICE_DATE) AS lastprice,
max(price) as maxprice,
sum(price) as totalprice
FROM auto_service
GROUP BY vin;
BEGIN
OPEN c_auto;
FETCH c_auto INTO v_in,v_n,v_first,v_last,v_total,v_max;
IF c_auto%notfound THEN
dbms_output.put_line('No output');
ELSE
dbms_output.put_line('vin No firstprice lastprice maximumprice totalprice');
LOOP
dbms_output.put_line(rpad(v_in,10) || rpad(v_n,10) || rpad(v_first,10) || rpad(v_last,12) || rpad(v_max,15) || rpad(v_total,5));
FETCH c_auto INTO v_in,v_n,v_first,v_last,v_max,v_total;
EXIT
WHEN c_auto%notfound;
END LOOP;
END IF;
CLOSE c_auto;
END;
To find
the number of services, the first service date and the price for the first service,the last service date and the price for the last service, the maximum price and the service date for the maximum price, and the prices for all services
I got all the other things except price for the first service date and last date of all VIN.
select q.*,
(
select sum(price)
from auto_service x
where x.vin = q.vin
and x.service_date = q.FIRST_DATE
) as FIRST_PRICE,
(
select sum(price)
from auto_service x
where x.vin = q.vin
and x.service_date = q.LAST_DATE
) AS LAST_PRICE
from
(
SELECT vin,
COUNT(*) AS no,
MIN(SERVICE_DATE) AS FIRST_DATE,
MAX(SERVICE_DATE) AS LAST_DATE,
max(price) as maxprice,
sum(price) as totalprice
FROM auto_service
GROUP BY vin
) q
Notice that I renamed the columns you named as FIRSTprice and lastprice to first_date and last_date, since this name is more related to what the column does actually contain.
in second place: i used "select sum(price)" in the subqueries only to handle the possibility that the same car has been serviced twice on the same day. if this should happen, without the sum(), the subquery would extract more than a value and would give you a runtime error. This much less likely to happen if if the date field contains also the time part, not only the date, but it could still happen if your DB contains bad data.
it is up to you if you want to keep that sum() call or if you prefer the db to reveal errors if there are duplicate rows you don't expect
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.