i have two tables one is event_invoice and payments.Here event_invoice is having invoice rows where as payments table will be having the multiple rows for single invoice.Below is the event_invoice structure
id Eventcode Currency Amount
1 E032 INR 375400
2 E032 INR 366740
3 E032 INR 283250
4 E034 USD 5000
5 E034 USD 5500
6 E034 USD 5000
7 E034 USD 5500
Here is the payments structure
invoice_id rcvamount
1 20000
2 50000
2 50000
3 40000
5 1500
6 5000
my left query is as follows
DB::table('event_invoice')
->leftjoin('payments', 'event_invoice.Id', '=', 'payments.invoice_id')->where('event_invoice.Status','=','1')
->where( DB::raw('year(DueDate)'), $year)
->select(DB::raw('
sum(event_invoice.Amount) as invoicevalue,
sum(payments.recieved_amount+payments.adjust_amount) as recvamount
'))
->groupBy('event_invoice.EventName')->groupBy('event_invoice.CurrencyType')->distinct()
->get();
results:
eventcode currency invoicevalue
E032 INR 1392130
E034 USD 21500
But the actual results should be like as follows
eventcode currency invoicevalue
E032 INR 1025390
E034 USD 21500
when i am doing left join query in the payment table that eventcode having two rows in payments table so its adding two times of that particular invoice to avoid this i have used distinct() but its not working.
Can you please help me out to solve this.
Related
I have categories that I have applied a price cart rule with coupon code that makes an discount on the products added to cart. But now I would like to exclude some products of specific category.
For example I have category: 1, 2, 3 and 4 with applied discount, so when a user add products from these categories to cart for example: product1, product2, product3 and product4 from "Category 1" and apply the coupon code he will have a discount on these products.
Where products cost: product1 - 10 usd; product2 - 10 usd; product3 - 10 usd; product 4 - 10 usd;
A total of 40 usd and if the coupon code with 10% discount is used he will have to pay 36 usd (4 usd discount)
But now am I trying to make an exclude of products to not apply discount as follow the example: user again add to his cart product1, product2, product3 and product4 but this time product3 and product4 will not be discounted, so he will get a discount only for product1 and product2.
Where products cost: product1 - 10 usd; product2 - 10 usd; product3 - 10 usd; product 4 - 10 usd;
A total of 40 usd and if the coupon code with 10% discount is used he wil have to pay 38 usd (2 usd discount as he is getting discount only for product1 and product2)
How should I configure the rule in Conditions and Action Tab to achive this goal and what is the right way to do it?
My problem is the following:
I have a table like this one below:
Date Category Value Movment Value
2019-01-01 105 1000 1000
2019-02-05 110 1200 200
2019-03-25 100 800 -400
2019-03-28 100 700 -100
2019-04-15 95 1300 600
To get the value at date, I can calculate a year-to-date on the Movment Value column (because the Value column is not additive with respect to Date column).
Things get worse when I filter on Category column. The year-to-date gives me these values per category which is correct:
Category: 95 100 105 110
YTD : 600 -500 1000 200
But the result I am looking for is:
Category: 95 100 105 110
YTD : 1300 700 1000 1200
What I tried is this formula:
IF(NOT(ISFILTERED([Category])); [Movment Value YTD]; [Movment Value Reset Per Category YTD]),
where [Movment Value Reset Per Category] is the year-to-date of reset movment, i.e.
Date Category Value Movment Value Movment Value Per Category
2019-01-01 105 1000 1000 1000
2019-02-05 110 1200 200 1200
2019-03-25 100 800 -400 800
2019-03-28 100 700 -100 -100
2019-04-15 95 1300 600 1300
It works, but if we use multiple dimensions to filter on, it could become very complicated to manage.
Do you have an idea, how I can achieve this "special" YTD per category without having to manage all cases ?
Thank you for your inputs.
Marco
I have two tables in database called purchase AND sales
Tables as follow
Table purchases
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 100
2 BANANA 200
3 GRAPES 150
4 AVACADO 110
Table Sales
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 50
2 BANANA 200
3 GRAPES 100
Here in the above tables i have purchase and sales i want to do subtraction from purchases quantity with sales quantity
and get the output like this
Output
----------------------------
ID PRODUCT QTY
----------------------------
1 APPLE 50
2 BANANA 0
3 GRAPES 50
4 AVACADO 110
i written query but not working
select id, product, (purchases.qty)-(sales.qty) from purchases left join sales on purchases.id=sales.id
Solution
SELECT purchases.id,purchases.product, (purchases.qty - IF (sales.qty IS NULL, 0, sales.qty)) as qty FROM pruchases LEFT JOIN sales ON purchases.product = sales.product
See all three Images your problem may be solved.
SALES TABLE [Same as Sales Table]1
PURCHASE TABLE [Same as Purchase Table]2
RESULTANT IMAGE [Resultant Image With Query]3
I have two tables named 'Customers' and 'Committeetables' in mysql database. They have following columns
Table: Customer
nd Name FatherName Cell# AdvanceAmount
25 Waseem Asghar 0302 2500
30 Ramzan Khan 0303 3500
35 Rana Ali 0307 2000
Table: CommitteeTables
nd Amount RecievingDate DrawDate
25 1500 2-10-15 10-10-15
30 1500 2-10-15 10-10-15
25 1500 3-10-15 10-11-15
30 1500 3-10-15 10-11-15
35 1500 3-10-15 10-11-15
A Column 'nd' in 'Customers' table is primary key and in 'CommitteeTables' table is foreign key. It means 'Customers' table joins with 'ComitteeTables' table through 'nd' column.
Expected Result table should be as under:
nd Name Father Name Cell# Advance Amount Balance
25 Waseem Asghar 0302 25000 3000 22000
30 Ramzan Khan 0303 35000 3000 32000
32 Rana Ali 0307 20000 1500 18500
----------------------------------------------------------------------------
total 80000 7500 72500
I am using Laravel 5 and HTML, Bootstrap. I need this result in Laravel 5. I shall be really thankful if someone help me to solve my problem.
Thanks
Waseem
This is not at all a Laravel question. It's a general SQL question.
Something like this:
SELECT nd, Name, FatherName, Cell#, AdvanceAmount as Advance,
(SELECT SUM(Amount) FROM CommitteeTables WHERE CommitteeTables.nd=Custoemr.nd) as Amount, Advance-Balance AS Balance
FROM Customer;
I'm not entirely sure that's correct but you can try it and fix any errors. As Bogdan suggests you should try something first then come back here with any problems that you have.
I have a huge table
startip endip country
1111 2000 in
2001 3000 in
3001 4000 in
4001 5000 chi
5001 6000 chi
I want to merge all these rows like
startip endip country
1111 4000 in
4001 6000 chi
I want do this in sqlserver.
Min(start_ip) , Max(end_ip) for DISTINCT(country)
Any help ?
Use GROUP BY.
For example:
SELECT country, MIN(start_ip), MAX(end_ip)
FROM a_table
GROUP BY country
Not sure what your table schema looks like but I think what you're trying to achieve will be accomplished using GROUP BY:
SELECT country, MIN(start_ip), MAX(end_ip) FROM your_table
GROUP BY country