Please help to to get out of situation
I have Product A with 3 different batch numbers
Product Batch Qty
A 1 10
A 2 10
A 3 10
If I put sell value 25 items then automatically sales report should be seen like this
Product Batch Sale
A 1 10
A 2 10
A 3 5
TIA.
This query will do that:
SELECT
ProductBadge.Product,
ProductBadge.Batch,
ProductBadge.Qty,
(Select Sum(Qty)
From ProductBadge As T
Where
T.Product = ProductBadge.Product And
T.Batch <= ProductBadge.Batch) AS Total,
IIf([Total]-[Order]<=0, [Qty], [Order]-([Total]-[Qty])) AS Sale
FROM
ProductBadge;
Related
I have a table as shown below. I want to do a partition and then subtract the values in the same column and different rows to get the difference using group by.
id
type
name
amount
1
sale
sam
2
2
sale
sam
15
3
return
lilly
20
4
sale
lilly
25
5
return
sam
3
6
sale
anju
20
And Need to return:
name
amount
sam
14
lilly
5
anju
20
Your sample data is a little suspect but I think you basically need to apply a case expression to your sale type:
select name,
Sum(amount * case when type='return' then -1 else 1 end) Amount
from t
group by name;
I have legacy tables which tracks flight and had to extract data. We have three tables named booking, airlines and flighttype. Note this is a dummy samples
booking :
id
customer
request_date
airline
flightType
price
currency
1
1
11-20-2020 10:23
1
1
120
Eur
2
1
11-21-2020 10:24
1
2
110
CHF
3
2
11-01-2020 11:25
2
2
120
Eur
4
1
15-01-2020 10:23
1
1
100
Eur
5
1
11-01-2020 11:23
1
2
60
Eur
6
1
12-01-2020 10:23
1
3
35
Eur
airline :
id
airline
1
French
2
Swiss
type :
id
flightType
1
domestic
2
international
Now the data we are trying to figure out is number of bookings consecutively within x days (let say if two days it would mean how many bookings were made in 2 days) for various parameters like
airline
flightType
airline & flightype
currency
price total price
For example lets say I wish to see what is the percentage of customer who have made multiple bookings within x days across multiple airline I should be able to do so or if I want to see the total revenue of customers who have made multiple booking within x days or customers who have made multiple booking with different set of currencies with x days
I am trying to make self join to extract such data and then group it but I am always reaching a dead end
SELECT
t1.customer, t1.request_date, t1.airline, count(*)
FROM booking t1
JOIN booking t2
ON t1.customer= t2.customer
WHERE t2.request_date > t1.request_date and DATEDIFF(t2.request_date, t1.request_date) > 0 and DATEDIFF(t2.request_date, t1.request_date) <=2
GROUP BY t1.customer, t1.request_date
The problem I am facing is the date has time and it gives me wrong results. And I am not sure what is the right way to get %share of customers who make booking in such way like
% share of customers who book more than one flight / more than one type of flight within span of x days.
Sorry if the question is too vague or if this violates any rules.
By the way I am using mysql 5.5
I want to see the total revenue of customers who have made multiple booking within x days or customers who have made multiple booking with different set of currencies with x days
You can answer questions like this using window functions. For the first question, this looks like:
select count(distinct customer)
from (select b.*,
lag(request_date) over (partition by customer order by request_date) as prev_request_date
from booking b
) b
where request_date <= prev_request_date + interval <n> day;
I am trying to pull a few reports for a client and I'm having trouble nailing one of queries down. I'd like to count the amount of times each customer has visited each location.
Sales Table
CustNo|StoreNo
2 10
2 10
3 5
3 10
2 20
Return Query
CustNo|StoreNo|Count
2 10 2
2 20 1
3 5 1
3 10 1
Thank you in advance for the help!
You need to use group by keyword like:
select CustNo,StoreNo,Count(*) as VisitCount
from Sales
group by CustNo, StoreNo
An example of my Progress database, opdetail table
invoice invline article size qty
----------------------------------------
905155 1 Shoe 10 5
905155 2 Slipper 3 2
905155 2 Slipper 4 6
905155 2 Slipper 5 1
905156 1 Boot 10 1
905156 1 Boot 11 1
905157 1 Slipper 5 4
905157 2 Shoe 8 6
a simple SQL select statement, run from the OpenEdge editor returns just what I need, a list of invoices with their total quantities:-
SELECT invoice, sum(qty) FROM opdetail GROUP BY qty ORDER BY invoice ASC
905155 14
905156 2
905157 10
HOWEVER:-
When run from an ASP page via DSN I have to list both fields in the GROUP BY otherwise progress returns a GROUP BY error
SELECT invoice, sum(qty) FROM opdetail GROUP BY qty, invoice ORDER BY invoice ASC
905155 5
905155 9
905156 2
905157 4
905157 6
Its not summarizing the qty, and seems to be taking into account the line number even though the line number plays no part in my sql statement. Can anyone throw any light on this or how I can do a sum of the total qty taking into account the line number? Thanks!
You are using qty in the aggregate function and then using on the group by this makes no sense and you should group by on some other column something as
SELECT
invoice,
sum(qty) FROM opdetail
GROUP BY invoice ORDER BY invoice ASC
I'm trying to find a way to sum amounts that match a specific term, and also amounts that don't match it. For example, if my table looks like this
user amount description
1 34 bike
1 78 toys
2 3 fuel
2 12 bike
I'm trying to get a table that will look like this in the end:
user amount spent on bike amount spent total
1 34 112
2 12 15
I'm using mysql
You can use a CASE statement within a SUM grouping:
SELECT user,
SUM(CASE WHEN description = 'bike' THEN amount ELSE 0 END) bike_amount,
SUM(amount) total_amount
FROM mytable
GROUP BY user