I have the following statement:
SELECT
ROUND(SUM(invoicetitle.unitpricegross*invoicetitle.suppliedquantity),2) as Costs,
SUM(invoicetitle.suppliedquantity) AS Unitamounts
FROM invoicetitle
WHERE
((SELECT invoice.state
FROM invoice where invoicetitle.invoiceid = invoice.invoiceid
and (invoice.invoicedate >= 1609459200000 and invoice.invoicedate <= 1640908800000)) = (1 or 4))
GROUP BY invoicetitle.invoicetitle_number
note that = (1 or 4) refers to two statements in the database where 1 is sold and 4 is a refund.
With = (1)) I get the following results:
Costs - Unitamounts
3.281,10 - 582
With = (4)) I get the following results:
Costs - Unitamounts
-115,2 - -32
With = (1 or 4)) I get the following results:
Costs - Unitamounts
3.281,10 - 582
But I expect as a correct SUM() of it:
Costs - Unitamounts
3.165,9 - 550
What am I doing wrong that the results are not subtracted correctly?
You probably meant to do:
SELECT
ROUND(SUM(invoicetitle.unitpricegross*invoicetitle.suppliedquantity),2) as Costs,
SUM(invoicetitle.suppliedquantity) AS Unitamounts
FROM invoicetitle
INNER JOIN invoice ON invoicetitle.invoiceid = invoice.invoiceid
and (invoice.invoicedate BETWEEN 1609459200000
and 1640908800000)
WHERE invoice.state IN (1,4)
GROUP BY invoicetitle.invoicetitle_number
Related
I have one table and trying to subtract the total where a condition is True from the full total.
Ticket
Amount
Code
11
5.00
12
3.00
X
13
10.00
14
2.00
X
My query was
SELECT SUM(AMOUNT)
FROM Table
MINUS
SELECT SUM(Amount)
FROM Table
WHERE Code = 'X'
So the answer should be 20 - 5= 15
Below two possible queries:
-- Use IF operator
SELECT SUM(amount) - SUM(IF(code = 'X', amount, 0)) FROM tbl;
-- Use implicit MySQL conversion boolean to int (true => 1)
SELECT SUM(amount) - SUM(amount * (code = 'X')) FROM tbl;
SQL editor online
I have three tables tblinventory, tbldisbursement, tblmissinglost. I want to calculate the total stock of books after deducting disbursements and returns for the current day.
select bn.No_of_books_procured
- (count(mis.Unr_ret_donated_discareded))
- (count(case when dis.dis_disbursed_return = 1
then dis.dis_disbursed_return end ) )as Stock
from tbl_inventorylibbooks bn
right
join tbl_limgt_booksmissing_lost_adjust mis
on mis.Book_Name_missingbk = bn.Id_inventory
left
join tbllibmange_disbursement dis
on dis.dis_book_name_fk_id = bn.Id_inventory
where bn.Id_inventory = 14
On few entries I get the correct answer and the other results entries the query is showing wrong answers. Can you kindly provide a solution for this?
tblinventory
id Name
----------
1 xyz
consider total books procured=10
tbldisbursement
**booknameFK disbursed/returned**
----------------------------------------
Booknafk1 1
Booknafk2 0
Booknafk3 1
where 1=disbursed and 0=returned
tblmissinglost
**id BooknameFk missng**
----------------------------------------
1 Booknafk1 lost
2 Booknafk1 lost
----------------------------------------
Also check this query
select (bn.No_of_books_procured) as procured,
count(distinct mis.Id_missingbooks_pid) as missing,
count( case when bs.dis_disbursed_return!=0 then
bs.dis_disbursed_return end) as disbursed
from tbl_limgt_booksmissing_lost_adjust mis
join tbllibmange_disbursement bs on
mis.book_name_missingbk=bs.dis_book_name_fk_id
join tbl_inventorylibbooks bn on bs.dis_book_name_fk_id=bn.Id_inventory
where bn.Id_inventory=14 group by bn.Id_inventory
I am getting duplicate entries
Select Sum(BookCount) Stock from (
Select Name , 1 BookCount from tblinventory
Union
select booknameFK Name , Case when disbursedORreturned=1 then -1 else 1 end as BookCount from tbldisbursement
Union
select booknameFK Name , -1 BookCount from tblmissinglost
)T
This will give you total number of books in stock as of now.
I have a spendings table and a dates table, that are joined by date_id and id...
What I'm trying to do, is get from 1 query all the info from spendings, plus the sum of all the spendings but with a limit and/or offset
This is the query right now
SELECT spendings.id, spendings.price, spendings.title,
dates.date, users.username, currencies.value,
( SELECT SUM(sum_table.price)
FROM (
SELECT s.price
FROM spendings s, dates d
WHERE s.date_id = d.id
AND day(d.date) = 25
LIMIT 2 OFFSET 0
) as sum_table
) AS sum_price
FROM spendings, dates, users, currencies
WHERE spendings.date_id = dates.id
AND day(dates.date) = 25
AND spendings.user_id = users.id
AND spendings.curr_id = currencies.id
LIMIT 2 OFFSET 0
Output
id price title date username value sum_price
3 6.00 title1 2013-11-25 alex € 21.00
4 15.00 title2 2013-11-25 alex € 21.00
It works, but only if the date here day(d.date) = 25 is the same as the outer one here day(dates.date) = 25
If instead I put day(d.date) = day(dates.date) which seems the logic thing to do, I get #1054 - Unknown column 'dates.date' in 'where clause'
If anyone has an idea to make this simpler let me know :)
Try to join instead of using nested correlated subqueries:
SELECT spendings.id, spendings.price, spendings.title,
dates.date, users.username, currencies.value,
y.sum_price
FROM spendings, dates, users, currencies
JOIN (
SELECT day, SUM(sum_table.price) As sum_price
FROM (
SELECT day(d.date) As day,
s.price
FROM spendings s, dates d
WHERE s.date_id = d.id
AND day(d.date) = 25
LIMIT 2 OFFSET 0
) sum_table
GROUP BY day
) y
ON y.day = day(dates.date)
WHERE spendings.date_id = dates.id
-- AND day(dates.date) = 25 <== commented since it's redundant now
AND spendings.user_id = users.id
AND spendings.curr_id = currencies.id
Some remarks:
Using old join syntax with commas is not recommended: FROM table1,table2,table2 WHERE
The recommended way of expressing joins is "new" ANSI SQL join syntax:
FROM table1
[left|right|cross|[full] outer|natural] JOIN table2 {ON|USING} join_condition1
[left|right|cross|[full] outer|natural] JOIN table3 {ON|USING} join_condition2
....
Actually this "new syntax" is quite old now, since is has been published, as I remember, in 1992 - 22 years ago. In IT industry 22 years is like 22 ages.
I have a table which has below structure. I'm using phpmyadmin 3.4.5, mysql version 5.5.16.
Table
Invoice_ID - PO_ID- Product - Quantity Invoice_Qty - Amount
Inv1 - PO1 - P1 - 100 - 50 - 1000
Inv2 - PO1 - P1 - 100 - 50 - 1000
Inv3 - PO2 - P2 - 50 - 20 - 500
Inv4 - PO2 - P2 - 50 - 20 - 500
Inv5 - PO2 - P3 - 50 - 10 - 250
What I'm really want to do is that
If Previous Row of PO_ID and and Product Name is
Same as current Row PO_ID and Product then Quantity of current row should be zero?
Sum of Invoice_Quantity = Quantity. So Required like below
My Expected Output given below:
Out Put:
Invoice_ID - PO_ID- Product - Quantity Invoice_Qty - Amount
Inv1 - PO1 - P1 - 100 - 50 - 1000
Inv2 - PO1 - P1 - 0 - 50 - 1000
Inv3 - PO2 - P2 - 50 - 20 - 500
Inv4 - PO2 - P2 - 0 - 20 - 500
Inv5 - PO2 - P3 - 0 - 10 - 250
I tried the How to get result set like Oracle lag function. But It not worked for me.
And tried to write a procedure for that. I'm stuck with export resutlset.
That is I don't know how to assign and get the result set.
Please help me out this problem.
Refer:
http://sqlfiddle.com/#!2/5c0b0/4
Your sqlfiddle was confusing. Please don't provide sample data here and then use different sample data in the sqlfiddle. And your desired result here is wrong, since you said in the description
If Previous Row of PO_ID and and Product Name is Same as current Row PO_ID and Product then Quantity of current row should be zero
Anyway, used my own...
select
t.*,
if(#previd = po_id and #prevprod = Product, 0, Quantity) AS new_quantity,
#previd := po_id,
#prevprod := product
from
t
, (select #previd:=null, #prevprod:=null) var_init
order by po_id, product
sqlfiddle
Note, that the order in the select clause is important, as well as the order by clause.
The previous ID is the maximum ID of all lower IDs. So the statement can be written as:
select
invoice_id, po_id, product,
case when mytable.po_id = prev_mytable.po_id and mytable.product = prev_mytable.product
then 0
else mytable.quantity
end as qty,
invoice_qty, amount
from mytable
left join mytable prev_mytable on prev_mytable.id =
(
select max(id)
from mytable all_prev_mytable
where all_prev_mytable.id < mytable.id
)
order by invoice_id;
And here is the SQL fiddle: http://sqlfiddle.com/#!2/5c0b0/11.
This is Standard SQL and should thus work with about any dbms.
this works :):) :
select Invoice_ID,PO_ID,product,
case when
decode(lead(Quantity) over (order by PO_ID),Quantity,'SAME','DIFF') = 'SAME'
then Quantity
else 0
end Quantity, Amount
from <table-name>
I have a table of multiple transactions. I am trying to get the row of the last transaction.
I am using the following:
select n.AccountNumber, max(PostDate), f.TransAmt
from mp_cycle n, fintrans f
where n.AccountNumber = f.AccountNumber
and TransCode >= '50'
and TransCode <= '59'
group by n.AccountNumber
This is returning the last date for a particular account, but the TransAmt is not for the same record.
ie:
Acct # Date Amt
1 1/1 10.00
1 1/2 11.00
1 1/3 12.00
2 1/2 20.00
2 1/3 21.00
2 1/4 22.00
My select will return the last date for each account, so 1/3 for act # 1 and 1/4 for act # 2, but the Amt field is not the amt that goes with that record.
Any assistance will be greatly appreciated.
There are many ways to solve this problem, one is by joining extra subquery which separate gets the latest PostDate for every AccountNumber. The result of the subquery will then be joined on the other table provided that it should match on two columns: AccountNumber and PostDate.
SELECT a.*, b.*
FROM mp_cycle a
INNER JOIN fintrans b
ON a.AccountNumber = b.AccountNumber
INNER JOIN
(
SELECT AccountNumber, MAX(PostDate) max_date
FROM fintrans
GROUP BY AccountNumber
) c ON b.AccountNumber = c.AccountNumber AND
b.PostDate = c.max_date
-- WHERE ..your conditions here..