Query to eliminate multiple rows based on oldest date - ms-access

Working in MS Access 2003 SP3: I have a query that I am running to find what 'cars' were sold with a date after the delivery date. I have thousands of rows. When it is all said and done, I want to just have a handful of rows for each 'car' and then the oldest date. Any suggestions?
CAR DATE ORDERED DATE DELIVERED CUSTOMER NUMBER DATE SOLD
FORD MUSTANG 20061002 20080413 ABC123 20080422
FORD MUSTANG 20061002 20080413 ABC124 20080429
CHEVY IMPALA 20061002 20080413 ABC125 20080505

This could be better if you had an ID field:
DELETE
FROM Cars
WHERE Cars.DATESOLD Not In (
SELECT TOP 5 DateSold
FROM Cars c
WHERE c.Car=Cars.Car
ORDER BY DateSold DESC)
And Cars.DATESOLD Not In (
SELECT TOP 1 DateSold
FROM Cars c
WHERE c.Car=Cars.Car
ORDER BY DateSold)
If there are duplicate dates, you will end up with more than 5 records.

Related

Aggregating data percentage wise based on date related criteria from a table

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;

SQL query with SUM and DATE_FORMAT

I hope for your help
I have three tables with relation, activity [id, amount, completed], brand_to_activity [id, activity_id, brand_id], brand [id, name] Need to get the top 3 brands selling by the sum grouped by month, for example, in January, the amount is 100 [this is the sum of the three best selling brands in January], the completed column in unixtime therefore you have to use DATE_FORMAT, i have now the following:
SELECT `activity`.*, SUM(activity.amount) as groupAmount,
DATE_FORMAT(FROM_UNIXTIME(activity.completed), '%m-%Y') as grouping
FROM `activity`
LEFT JOIN `brand_to_activity` ON `activity`.`id` = `brand_to_activity`.`activity_id`
WHERE (`activity`.`completed` BETWEEN '1546300800' AND '1577750400')
GROUP BY `grouping` ORDER BY `activity`.`completed`
Must be something like this:
Grouping | Amount | Profit | Note
January 100 50 Philips, Sony, Apple
February 100 50 Apple, Microsoft, Canon
March 50 50 Philips, Apple, AOC
version MariaDB 10
https://mega.nz/#!zFdAHACA!2CCVBVjMvwAxMqEz202a107s1CDxKc8jkvZiGQ5NJ0c
https://mega.nz/#!jNFCCQiL!MODxZR5kwCBXG6789uBZEVZhsdcZxY8W55qyIa-B4KQ
https://mega.nz/#!XRcAWAIT!xASyVxE3ucRiVtLJmjCKys7plImxLMOH99dRQVVSkDM

MS Access: Count instances over field based on record-level criteria

I need a query to count the number of orders shipped to each state for each customer. So, let's say John had 5 orders: 2 to Florida, 2 to Alaska, and 1 to Kansas. How do I get those counts for each Customer. I have a query that provides me Customer and Order delivery state, but I cannot figure out how to get the counts. I am looking for something like:
>John FL 2
>John FL 2
>John AK 2
>John KS 1
>John AK 2
>...[Next Customer]...
Thank you.
You need to group it by customer and state and count based on this:
SELECT Customer, State, Count(*) AS Amount
FROM Orders
GROUP BY Customer, State
The resulting calculated field, is named Amount in this example.

How to get value as header field in pivot table?

I've been looking for this answer for hours without finding anything at all on this subject.
I'm trying to get a pivot table from the "Sells" table that displays :
- as header fields, the name of each seller in my shop
- as rows, each month of the year
- as data the sum of their sells for each month
The only issue I have is that the headers are populated with ID instead of the name of the sellers.
For example, instead of showing this :
Month | Steve | Joe
January | 5000$ | 600$
February | 400$ | 400$
It keeps showing the ID related to each seller:
Month | 1 | 2
January | 5000$ | 600$
February | 400$ | 400$
Here is my query :
TRANSFORM Sum(Sells.Ca) AS [Monthly sells]
SELECT DISTINCTROW Format$(Sells.DateSold,'mm - mmmm') AS Month
FROM Sells
GROUP BY Sells.DateSold
PIVOT Sells.Seller
Thank you very much for your help and your time.
EDIT:
As #WolfgangKais mentionned in the comments, I forgot to mention that the Seller field is a lookup field, that's why it only shows the first value of the lookup field, hence the ID and not the name.
As stated by #June7 and #Wolfgang Kais, I had to include the Sellers Table in the query in order to get each seller's name instead of their ID, thank you very much for the lead.
This was done by :
Constructing a temporary table containing rows populated by sellers name, amount sold and date of sell:
SELECT Sellers.Name AS Seller, Sells.Ca AS Amount, Sells.DateSold AS Month
FROM Sells
INNER JOIN Sellers ON Sells.Seller = Sellers.ID
Including this temporary table into my first pivot query (In the FROM part):
TRANSFORM SUM(TempTable.Amount) AS [Monthly Sells]
SELECT Format$(TempTable.Month, 'mm - mmmm') AS Month
FROM(
SELECT Sellers.Name AS Seller, Sells.Ca AS Amount, Sells.DateSold AS Month
FROM Sells
INNER JOIN Sellers ON Sells.Seller = Sellers.ID
) AS TempTable
GROUP BY Temptable.Month
PIVOT Temptable.Seller

Complex MySQL statement to list fishing standings?

I'm trying to make a standings chart using multiple tables. One keeps tracks of meetings attended to, each meeting counting as 5 points and the other table keeps track of results of tournaments. This is a fishing club site.
I have the following so far and can show the meeting points in order but the tournament results separate from that. I'd like to find a single albeit complex SQL statement to list out current standings.
I need to show the angler name which I can grab separate from a different table, then each month's 5 points listed along with the tournament result amount from the results table, these are all added up to finally list the total from all tournaments and meetings.
SELECT aid, sum(here*5) as total
FROM rollcall GROUP BY aid ORDER BY total DESC
SELECT aid, weight, weight-penalty as fweight
FROM `results` where tid=2 order by fweight desc
So an example is:
place angler JAN FEB MARCH ... Total Points
1 name1 5 50 5 45 0 38 143
2 name2 5 49 5 47 5 31 142
...
Is that clear at all?
What if you build your query something like this?
SELECT aid,
sum(SELECT count(1) from meetings WHERE MONTH(columndatetime) = 1 * 5) AS JAN,
sum(SELECT count(1) from meetings WHERE MONTH(columndatetime) = 2 * 5) AS FEB,
/
-- add same logic to the rest of the months
/
sum(SELECT count(1) from meetings WHERE YEAR(columndatetime) = 2013 * 5) as total
FROM rollcall GROUP BY aid ORDER BY total DESC
Where columndatetime is the name of your column that has the date and time for the meetings etc...
The last one takes all for the year.
Could this help you out?