Simply query with calculated fields - sql

I have 2 tables in my sqlite3 database. Can someone help me with the sql command below:
tbltrans:
transid | transdate | discountpercentage | Bank
12345 10/09/2011 5 20.00
tbltrans2:
transid | itemnr |price | btwpercentage | qty
12345 205 10.11 12 5
12345 302 15.00 6 7
12345 501 20.00 21 3
SO I want to get a query table with total amount of sale for each transid's and calculated cash column, Like:
Select
Sum(tbltrans2.qty * tbltrans2.price) as TotalAmount,
(Totalamount - tbltrans.Bank) as Cash
where
tbltrans.transid = tbltrans2.transid and transdate = '10/09/12'
Can someone please correct this sql satement ?

Select
Sum(ifnull((tbltrans2.qty * tbltrans2.price),0))-tbltrans.Bank as cash
from tbltrans,tbltrans2
where
tbltrans.transid = tbltrans2.transid and tbltrans.transdate = '10/09/12'
group by tbltrans.transid
try this
If you want to select total amount also then include this in
select,
Sum(ifnull((tbltrans2.qty * tbltrans2.price),0)) as TotalAmount

Related

Access SQL query update calculation for duplicates

I have a query that filters results for products which have had orders sent after an user-input date, and calculates what the quantity becomes if the order was sent after that date.
SELECT *, [OnHand]+[OrderJoin.Quantity] AS Qty After
FROM Query3
WHERE (((Query3.ShippedDate)>[Enter End Date] And (Query3.ShippedDate) Is Not Null));
However, I need a way for it to recognise duplicates and update it based on those.
e.g. I have this
ID | Product Name | Qty Before | Qty Shipped | Qty After
11 | Chocolate | 80 | 20 | 100
11 | Chocolate | 80 | 10 | 90
And I'd need a way for it to show Qty After as 110 (after the 10 and 20 shipped)
If I understand correctly, you want an aggregation query. This would be something like this:
SELECT id, ProductName,
OnHand]+ SUM([OrderJoin.Quantity]) AS Qty After
FROM Query3
WHERE Query3.ShippedDate > [Enter End Date] And
Query3.ShippedDate) Is Not Null
GROUP BY id, ProductName, OnHand;
I note that OrderJoin is not defined, but that is the structure of your original query.

How to join 2 columns as one and order by date?

I'm using SQL Server Compact Edition 4.0 and there are 2 tables called debit and credit as below.
tbl_debit
invoice | dealer | price| purchasedate
=========================================
001 | AAA | 1000 | 2/9/2016 8:46:38 PM
002 | AAA | 1500 | 2/20/2016 8:46:38 PM
tbl_credit
dealer | settlement| purchasedate
=========================================
AAA | 800 | 2/12/2016 8:46:38 PM
AAA | 400 | 2/22/2016 8:46:38 PM
I want to create single table that should include 4 column..
Invoice, Dealer, Amount, date
Amount should include both settlement from tbl_credit and price from tbl_debit and need to order by date.
I really appreciate if anyone can help me.
Here's a script to logically approach the problem based on the limited information presented to us:
SELECT A.invoice, A.dealer, A.amount, A.purchasedate
FROM (SELECT A.invoice, A.dealer, A.price [amount], A.purchasedate
WHERE tbl_debit A
UNION
SELECT ' ', B.dealer, B.settlement, B.purchasedate
FROM tbl_credit B) A
ORDER BY 4

What is SELECT for SUM, GROUPed and DISTINCTed by another columns in SQL?

This is content of my table:
date cat. price
----|----|------|
0101| 1 | 100
0101| 1 | 200
0101| 2 | 300
0102| 2 | 400
0102| 2 | 500
0102| 1 | 600
0102| 1 | 700
0203| 1 | 800
0203| 2 | 900
0203| 2 | 100
I would like to SUM all prices in same category of same day.
The result of select query should look like this:
date cat. price
----|----|------|
0101| 1 | 300
0101| 2 | 300
0102| 1 | 1300
0102| 2 | 900
0203| 1 | 800
0203| 2 | 1000
I tired several selects which came on my mind, but not a single one which would work. I just do not know to SUM grouped by something and distinted by something else from same table in same select. Can you help me?
Have you tried something like...
select date, categories, sum(price)
from tablename
group by date, categories
If the table above was a orders table with columns OrderDate and Amount then, I would do it like this:
SELECT ORDERS.ORDER_DATE
, SUM(ORDERS.AMOUNT_BILLED)
FROM ORDERS ORDERS
GROUP BY ORDERS.ORDER_DATE
Hope this gives you a starting point.
Yes, it would really help if you have your table and column names labelled. Best way would be to start writing and show some work...
Best

how can i have my sql show the sum of 2 tables at the same time?

I've tried to show the result of sum of bill total and vat total (that customer paid VAT) and the sum of all(bill_total + vat) for monthly. However, my sql command show nothing after it executed. So could you kindly please help me as my sql knowledge was so limit.
My tables are below
tbl_bill_total
bill_id | bill_total | cust_id | showndate
1 | 1000 | 12 | 12/10/13
2 | 1200 | 13 | 1/11/13
3 | 500 | 12 | 3/11/13
tbl_vat_bill_total
vat_id | vat_total | if_paid| showndate | cust_id
1 | 400 | false | 13/10/13 | 14
2 | 500 | true | 14/11/13 | 12
3 | 100 | false | 15/11/13 | 11
4 | 200 | true | 20/11/13 | 12
The expected result should be like this
bill_total | vat_total | Sum_of_all | month
1000 | 0 | 1000 | 10
1700 | 700 | 2400 | 11
Thank you very much
** sorry I've changed the expected result… As it is a yearly report, so the outcome should list all sales in one year. *
below is my attempt that failed...
SELECT
Sum(tbl_bill_total.bill_total) AS bill_totalOfSum,
Sum(tbl_vat_bill_total.vat_total) AS vat_totalOfSum,
tbl_vat_bill_total.if_paid,
Month([tbl_bill_total.showndate]) AS month1
FROM
tbl_bill_total
INNER JOIN
tbl_vat_bill_total
ON tbl_bill_total.cust_id = tbl_vat_bill_total.cust_id
GROUP BY
tbl_vat_bill_total.if_paid,
Month([tbl_bill_total.showndate])
HAVING (((tbl_vat_bill_total.if_paid)=True));
Ok, I haven't used Access in awhile, but I think the following should work:
select
sum(tbt.bill_total) as bill_total_
,iif(sum_vat_total is null, 0, sum_vat_total) as vat_total_
,iif(sum_vat_total is null, 0, sum_vat_total) + sum(bill_total) as sum_of_all
,month(showndate) as month
from tbl_bill_total tbt
left join (
select
sum(vat_total) as sum_vat_total
,month(showndate) as month
from tbl_vat_bill_total
where if_paid = true
and year(showndate) = 2013
group by
month(showndate)
) tvt
on tvt.month = month(tbt.showndate)
where year(showndate) = 2013
group by
month(showndate)
,sum_vat_total
Of course, this assumes that you will at least have one record per month in tbl_bill_total since that is the source of our month field.
If you have months where there are tbl_vat_bill_total records but no tbl_bill_total records (or no records in either table), you will need a more complex solution - likely with a date table.
Create a query like this call it say q_bill_sum
Select Sum(bill_total) as total, 'Bill' as Total_Type, Month(showndate) as bill_Month
From tbl_bill_total
Where Year(showndate) = 2013
group by Total_Type, Bill_month
Union
Select Sum(vat_total), 'Vat' as Total_Type, Month(showndate) as bill_month
From tbl_vat_bill_total
Where Year(showndate) = 2013 and if_paid = true
group by Total_Type, Bill_month
You end up with
1000 Bill 10
1700 Bill 11
0 Vat 10
700 Vat 11
then
Select b.Total, ifnull(v.total,0), b.bill_Month
From q_bill_sum b
left join q_bill_sum v On v.Bill_month = b.Bill_month
should be close, but you never did answer my question about having vat in a month with no bill in a month.
I think anyway, access isn't my strong suit, but when I have to do a query that does a lot, I always break it down into bits, then when it works I try to streamline if it seems horrible...

SQL - Average number of records within a time period

I'm trying to compile some lifetime value information for customers within one of our databases.
We have an MS SQL Server database which stores all of our customer/transactional information.
My issue is that I don't have much experience when it comes to MS SQL Server (or SQL in general) - I'd like to be able to run a query against the database that pulls AVG number of loans, and AVG revenue based on three criteria:
1.) Loans be counted if they are 'approved'
2.) Loans from a customer_id only be counted if the first loan (first identified by date_created field) be on or after a certain 'mm/yyyy'
3.) I'm able to specify for how many months after the first 'mm/yyyy' to tally the number of loans / revenue to be included within the AVG
Here is what the database would look like:
customer_id | loan_status | date_created | revenue
111 | 'approved' | 2010-06-20 17:17:09 | 100.00
222 | 'approved' | 2010-06-21 09:54:43 | 255.12
333 | 'denied' | 2011-06-21 12:47:30 | NULL
333 | 'approved' | 2011-06-21 12:47:20 | 56.87
222 | 'denied' | 2011-06-21 09:54:48 | NULL
222 | 'approved' | 2011-06-21 09:54:18 | 50.00
111 | 'approved' | 2011-06-20 17:17:23 | 100.00
... loads' of records ...
555 | 'approved' | 2012-01-02 09:08:42 | 24.70
111 | 'denied' | 2012-01-05 02:10:36 | NULL
666 | 'denied' | 2012-02-05 03:31:16 | NULL
555 | 'approved' | 2012-02-17 09:32:26 | 197.10
777 | 'approved' | 2012-04-03 18:28:45 | 300.50
777 | 'approved' | 2012-06-28 02:42:01 | 201.80
555 | 'approved' | 2012-06-21 22:16:59 | 10.00
666 | 'approved' | 2012-09-30 01:17:20 | 50.00
If I wanted to find the avg transaction count (approved transactions), and average revenue per approved transaction for all customer's who's first loan was in/after 2012-01, and for a period of 4 months after then, how would I go about querying the database?
Any help is greatly appreciated.
something like this (there maybe a few typos here and there)...
you could first calculate the minimum loan date:
select customer_id, min(date_created) from table t where loan_status = 'approved' group by customer_id
then you can join to it:
select customer_id, count(date_created), avg(revenue) from table t
join (
select customer_id, min(date_created) as min_date from table t where loan_status = 'approved' group by customer_id ) s
on t.customer_id = s.customer_id
where t.date_created between s.min_date and DATEADD(month, 4, s.min_date) and t.loan_status = 'approved'
Rename tbl to your table name.
Specify dates in the format YYYYMMDD.
select customer_id, AVG(revenue) average_revenue
from
(
select customer_id
from tbl
group by customer_id
having min(date_created) >= '20120101'
) fl
join tbl t on t.customer_id = fl.customer_id
where t.loan_status = 'approved'
and date_created < '20120501' -- NOT including May the first, so Jan through Apr (4 months)
If you mean 4 months after each customer's first loan, leave me a comment, state whether it's 4 calendar months (e.g. 15-Jan to 15-May) or up to the last day of the 4th month (15-Jan to 30-Apr), and I'll update the answer.