SQL query to show updated account balances - ms-access

I have two tables, [Party] and [Invoice], in an Access database. I want to summarize all party balances. Here is my query so far:
select
Party.AccountCode,
Party.AccountDescription,
OpeningBalance+sum(invoiceAmount) as balance,
invoiceDate
from Party,Inovoice
where party.accountCode=Inovice.AccountCode
and invoiceDate>=01-01-2013
and invoiceDate<=30-06-2013
group by
Party.AccountCode,
Party.AccountDescription,
invoiceDate
This query returns the balance for only the two parties that appear in the [Invoice] table: TOM and JHONS. I want to display the balance for all 4 parties:

Use two queries. The first to calculate the total invoice amount and the second for the party balances
qrySumInvoiceAmounts:
SELECT
AccountCode
sum(invoiceAmount) as InvoiceBalance,
FROM Invoice
group by
AccountCode,
WHERE invoiceDate>= #01-01-2013#
and invoiceDate<= #30-06-2013#
qryPartyBalance:
SELECT
Party.AccountCode,
Party.AccountDescription,
OpeningBalance + InvoiceBalance,
from Party LEFT JOIN qrySumInvoiceAmounts
ON party.accountCode=qrySumInvoiceAmounts.AccountCode

Related

How to use left join in this context (If that is even the correct way to accomplish my task)

I am confused how to complete this problem that i currently have infront of me. I am very new to SQL however, so I apologize if there is not much help from my side.
https://i.stack.imgur.com/Gv80l.png
These are my 2 tables. For info on the problem, for incoming calls a charge of 1 call unit/second is levied. For outgoing calls a charge of 500 call units is charged for the first 2 minutes of a call, then 2 units/second after that, for example a 3 minute outgoing call is (500+60*2) 620.
I want to have my output give me names, phone numbers, and then the total billed units. For example, based on the tables in the photo,
output would look like:
Geoffrey Chase 8052564516 363
Mollie Mcguire 9997156377 1288
Geoffrey Chase with phone number 8052564516 received 2 incoming calls of 264 and 99 seconds each. The billing for this is 363 call units in total.etc. Any ideas?
Simplified example fiddle
Another possible solution: Two Derived Tables/CTEs to calculate the incoming/outgoing sums, which are both Left Joined to customer_detail:
SELECT
customer.cname,
COALESCE(incoming.charge, 0),
COALESCE(outgoing.charge, 0),
COALESCE(incoming.charge, 0) + COALESCE(outgoing.charge, 0)
FROM cust_det AS customer
LEFT JOIN
(
SELECT
inc_no AS phone_no
,duration AS charge
FROM call_rec
GROUP BY inc_no
) as incoming
ON incoming.phone_no=customer.phone_no
LEFT JOIN
(
SELECT
out_no AS phone_no
--500 call units is charged for the first 2 minutes of a call, then 2 units/second after that
,500 + greatest(duration-120,0) * 2 AS charge
FROM call_rec
) as outgoing
ON outgoing.phone_no=customer.phone_no
Using the column names from the fiddle, I created a subquery that contains the charge per number and the I joined it with the customer details table. The charge table was created using a UNION to combine incoming charges with outgoing charges that are calculated differently.
SELECT
customer.cname,
SUM(charge_per_number.charge)
FROM cust_det AS customer
LEFT JOIN (
SELECT
inc_no AS phone_no
,duration AS charge
FROM call_rec
UNION ALL
SELECT
out_no AS phone_no
,CASE WHEN duration > 120
THEN 500+(duration-120)*2
ELSE 500
END AS charge
FROM call_rec
) AS charge_per_number
ON charge_per_number.phone_no=customer.phone_no
GROUP BY customer.cname
The most recent versions of MySQL support lateral joins, which allows:
select cu.name, sum(charge)
from cust_det cu left join
(call_rec cr cross join lateral
(select cr.in_no as phone_no, cr.duration as charge union all
select cr.out_no as phone_no, 500 + 2 * greatest(duration - 120, 0)
)
on cu.phone_no = cr.phone_no
group by cu.name;

Multiple Periodic Charges within a billing cycle

Each department is supposed to have only one statement per monthly billing cycle.

My goal is to figure out how many departments have been hit with more than one billing statement (having a value greater than $0.00) within the same billing cycle.  

CONTEXT: 
My current query is set to check only departments that are supposed to be billed monthly.
Department table has a one-to-many relationship with Statement table (a department is expected to have one or more statements)
Statement table has a one-to-many relationship with Trans table (a statement is expected to have one or more transactions)
Each billing statement has its own statement_close_date
c.type=‘periodic’ refers to a bill that only occurs once per billing cycle
d.period=1 represents accounts that are billed monthly
s.status=‘closed’ represents only closed statements
d.exp_date represents billing expiration date
v.status=‘active’ and d.status=‘active’ are meant to ensure that only active departments are being queried.
I also tried searching between specific expire dates on a per account basis.

The problem with my query is that it outputs the total number of billing statement_close_dates with a value greater than $0.00, instead of only checking for multiple occurrences within a billing cycle. 

How can this query be modified to only output departments with more than one instance of a bill consisting of a periodic type charge greater than $0.00 within each department’s billing cycle?  

QUERY:
SELECT s.department_id, s.statement_id, COUNT(s.statement_close_date) AS sd,
t.trans_id, from_unixtime(s.statement_close_date)
FROM statement s
INNER JOIN trans t
ON t.statement_id=s.statement_id
INNER JOIN cde c
ON t.cde=c.cde
INNER JOIN department d
ON s.department_id=d.department_id
WHERE from_unixtime(s.statement_close_date) BETWEEN
DATE_SUB(from_unixtime(d.exp_date), INTERVAL 1 MONTH) AND
from_unixtime(d.exp_date)  
AND d.period=‘1’
AND s.status='closed'
AND t.dollars>0
AND c.type='periodic'
GROUP BY s.statement_id DESC
HAVING sd>1
SAMPLE OUTPUT:
department_id statement_id sd trans_id statement_close_date
1719712 9351464 3 98403043 2018-09-24
1719709 9351463 2 98403026 2018-09-24
1719708 9351462 2 98403010 2018-09-24
1719665 9351457 3 97374764 2018-09-24

find Sum of Sub-Query

Query need to find a client named Yossi Cohen, who purchased a number of items and the amount billed(sum) of purchase
For example: Yossi Cohen bought three items costing 40 shekels and we want to know the amount of the order.
example:
namecustomer namemodel quantity sum
Yossi Cohen iphone6 3 120
I try to write this:(not working)
SELECT nameCustomer,
(SELECT idCustomer,nameCustomer,nameModel,idOrders,Quantity,
SUM(price*Quantity)AS OrderTotal
FROM Customers,OrdersItems,Products GROUP BY idOrders)
FROM Customer
where Customer = 'Yossi Cohen';
This should do the work:
SELECT idCustomer,nameCustomer,nameModel,idOrders,Quantity
From Customer
Where sum=price*Quantity
AND Customer = 'Yossi Cohen';

Query to get the output as shown in designation table

EmpId EmpDeptName
1 Account
2 Sales
3 IT
4 HR
i want output in the format
EmpId Account Sales IT HR
1
2
3
4
If anybody knows how to write query to get this output , please provide me that query.
This type od data transformation is a PIVOT, where you take row data and convert it to columns.
You did not provide many details, but if you wanted the count of employees in each department, you could use something like this:
select Account, Sales, IT, HR
from
(
select EmpDeptName
from employees
) d
pivot
(
count(EmpDeptName)
for EmpDeptName in (Account, Sales, IT, HR)
) p;
This will display the total number of employees in each department as column data instead of rows.

MySQL reporting query

I have the following db schema (i'm only mentioning the columns for each table necessary for the query in mind):
bills
-------
id
amount (decimal)
collection_case_id
collection_case
------------------
id
payments
----------
id
bill_id
amount (decimal)
type (char(2)) - "CH" stands for check payment, field used for polymorphism
check_payments
---------------
payment_id
For a set of collection cases, I need to get the total interest earned from check payments.
I am currently doing these calculations in memory after retrieving all the bill records for my set of collection case criteria. For each bill I simply add the sum of checks received - bill amount if sum of checks received is greater than bill amount.
for instance my total received query looks like this (collection cases 1 & 2):
select sum(payment.amount) as received
from bills bill
cross join payments payment inner join check_payments checkpayment
on
payment.id=checkpayment.payment_id
where payment.type='CH'
and payment.bill_id=bill.id
and (bill.collection_case_id in (1 , 2))
Not really sure if this is what you meant.
select IF(sum(payment.amount) > bill.amount, sum(payment.amount)-bill.amount, 0) as interest
from bills bill
cross join payments payment inner join check_payments checkpayment
on
payment.id=checkpayment.payment_id
where payment.type='CH'
and payment.bill_id=bill.id
and (bill.collection_case_id in (1 , 2))