I am developing a database using MySQL. Database (shopping) consists of one table (basket). When the customer comes in and purchase groceries those to be stored in the table by unique customer Id. For example, a customer named ABC came to my store, purchased 10 apples, and made bill. After 4 hours, he returned four apples to the shop, took money of them, and left. Therefore, I need to version control those records in my table, to see the track of the customer.
MySQL code:
SHOW DATABASES;
CREATE DATABASE shopping;
USE shopping;
CREATE TABLE basket (Customer_ID VARCHAR (20) NOT NULL, Phone INT, grocery VARCHAR (10), QTY INT, Timestamp, Version VARCHAR (10));
The table looks like
Customer_ID Phone Grocery QTY Timestamp Version
ABC 34567 Apple 10 1/20/2020 7:00 am A1
ABC 34567 banana 5 1/20/2020 7:00 am B1
ABC 34567 oranges 4 1/20/2020 7:00 am O1
DEF 12345 jelly 10 1/20/2020 8:00am J1
DEF 12345 pineapple 6 1/20/2020 8:00am P1
GHI 67854 juice 4 1/20/2020 9:00 am J1
GHI 67854 icecream 6 1/20/2020 9:00 am I1
ABC 34567 Apple -4 1/20/2020 11:00 am A2
No I need to get the second instance of customer ABC when he return four apples to the shop and version it as A2, so i could track all the changes on each customer.
Related
I want for given date time range to get:
Airport name,
Number of flights of that airport (number of times where that airport is arrivalAirport in Flight, while departureDate of that same Flight is in the given datetime range),
Number of sold tickets of that airport (dateOfSale has to be in the given datetime range) and
Total price of those sold tickets of that airport.
I have 3 tables (Airport, Flight and Ticket):
Airport data:
id | name
1 , Madrid Airport
2 , Amsterdam Airport
3 , Belgrade Airport
----------------------------------------------
Flight data:
id | number | departureDate | arrivalDate | departureAirport | arrivalAirport | price
1 , 101 ,2019-01-29 19:21:44,2019-01-29 22:21:44, Madrid Airport , Amsterdam Airport, 600
2 , 102 ,2019-01-29 22:21:44,2019-01-30 00:21:44, Madrid Airport , Belgrade Airport , 450
3 , 103 ,2019-01-30 20:21:44,2019-01-30 22:21:44, Belgrade Airport , Amsterdam Airport, 555
4 , 104 ,2019-02-10 20:21:44,2019-02-10 22:21:44, Belgrade Airport , Madrid Airport , 555
----------------------------------------------
Ticket data:
id | FlightId | dateOfSale
1 , 3 , 2019-01-23 00:00:00
2 , 3 , 2019-01-27 10:00:00
3 , 1 , 2019-01-27 13:00:00
Example datetime range:
Minimal: 2019-01-25 19:21:44
Maximal: 2019-02-02 00:00:00
With the given datetime range, only fourth flight will not pass condition because its departureDate is not in the given range, other three flights will pass.
So now, we have Amsterdam Airport (x2) and Belgrade Airport (x1) as an arrivalAirports.
So first two columns should be represented like this:
Name of Airport | Number of Flights |
Amsterdam Airport, 2
Belgrade Airport , 1
Third one represents number of sold tickets while dateOfSale is also in the given datetime range.
In the ticket's table 3 tickets are sold, first two are bought for flight with id=3 and third ticket is bought for flight with id=1.
Since first ticket's dateOfSale is not in the given datetime range, only 2nd and 3rd ticket will pass, and they both represent arrivalAirport called Amsterdam Airport.
So end result should be:
Name of Airport | Number of Flights | Number of sold tickets | Total price
Amsterdam Airport, 2 , 2 , 1155
Belgrade Airport , 1 , 0 , 0
I tried something like this:
select a.name, count(f.arrivalAirport) as 'number of flights',
count(t.dateOfSale) as 'number of sold tickets', sum(f.price) from airport a,
flight f, ticket t where a.name = f.arrivalAirport and f.departureDate >
'2019-01-25 19:21:44' and f.departureDate < '2019-02-02 00:00:00' and
t.flightId = f.id and t.dateOfSale > '2019-01-25 19:21:44' and t.dateOfSale
< '2019-02-02 00:00:00' group by a.name;
Doing only this, it counts number of sold tickets exactly like it should but number of flights are wrong.
I am not sure how to proceed any further. What am I missing in this query and can it be done like this or it has to include join (with which I have problems also)?
I have a DB which is an Attendance List for a Training Program. In this list - First Subscriptions gets Priority - and LOCAL USERS have priority too.
DB:
ID NAME SUBSCRIPTION DATE COUNTRY
1 JOHN 2018-04-05 12:00:00 USA
2 MARY 2018-04-05 12:30:00 CANADA
3 CARL 2018-04-05 13:00:00 USA
I need a way to order the table like this:
ID NAME SUBSCRIPTION DATE COUNTRY
1 JOHN 2018-04-05 12:00:00 USA
3 CARL 2018-04-05 13:00:00 USA
2 MARY 2018-04-05 12:30:00 CANADA
CARL is from USA then I need to give priority to him, even Mary makes his subscription early.
any idea?
** IMPORTANT: Country priority changes according to the location of the training. (here the location is "USA", but can be any country)
I tried this:
SELECT * FROM SUBSCRIPTION_TABLE ORDER BY COUNTRY = 'LOCAL_COUNTRY_VAR', SUBSCRIPTION_DATE
but it did not work.
You appear to want to order first by being in the USA and then by the date:
In MySQL, you can do:
order by (country = 'USA') desc, date
I would recommend creating a separate table with the location and a priority code (integer). Inner join the two and then order by the priority code ascending. USA would have priority 1, CANADA would be 2.
I have a table with columns (id,gp, village, year) id as primary key.
Table name is files_dev columns are id, gp, village, year
id gp village year
1 a1 xyz 2000
2 b1 abc 2001
3 c1 def 1999
i want like this to display, but the id column should start with serial order 1 2 3 4. I know since it is primary key and set it to auto increment, i have seen setting row numbers will work but how to do it.
id gp village year
4 a4 def 1999
1 a1 xyz 2000
2 a2 abc 2001
3 a3 def 1999
I am working on employee attrition analysis with a table having rowwise data for a (employee like Id, name, Date_Join Date_Relieving Dept Role etc)
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11 (ie she is available)
I can run regression on this data to find the beta coefficient
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11
But I've seen other approach too.. where employee have multiple entries depending on their difference between joining date and current month or relieving month(say Employee A joined in Jan and Left in Dec so he'll have 12 entries updating corresponding columns like experience and marriage etc)
eID eName Dept Married Experience
123 John Doe HR No 0
123 John Doe HR No 1
123 John Doe HR Yes 2
123 John Doe HR Yes 3
can someone tell what differentiate two approaches.. and what is the outcome of this second approach.
I'm having difficulties with a query that absolutely has me stumped. I have a mysql database for a restaurant chain that keeps track of menu item prices from year to year. In this particular query I'm trying to obtain only the most recent price for an item at each store.
ItemMenu
pk storeNum itemNum vendorNum size price year
1 5555 2000 3150 Large 3.99 2015
2 5555 2000 3150 Large 3.75 2014
3 3333 2000 3153 Large 3.69 2014
4 2222 2000 3150 Large 3.89 2014
5 2222 2000 3150 Large 3.69 2013
ItemList
itemNum item categoryNum
2000 Mashed Potatoes 2000
2001 Green Beans 2000
2002 Coleslaw 2000
2003 Baked Beans 2000
2004 Corn 2000
ItemCategory
categoryNum type
2000 Side
2001 Dessert
2002 Drink
2003 Sauce
ItemVendor
vendorNum vendorName
3150 Acme Foods
3152 John's Vegetables
3153 Smith's Wholesale
Stores
storeNum franchisee address phone
5555 David Smith 9999 Main st 555-1212
3333 James Bond 123 Baker 867-5309
2222 Mark Jones 450 21st Ave 888-5411
What I would like to have returned is
storeNum, franchisee, item, type, vendorName, size, price, year
But only for the most recent year.
5555, David Smith, Mashed Potatoes, Side, Acme Foods, Large, 3.99, 2015
3333, James Bond, Mashed Potatoes, Side, Smith's Wholesale, 3.69, 2014
2222, Mark Jones, Mashed Potatoes, Side, Acme Foods, Large, 3.89, 2014
I hope that made sense, I'm at a complete loss of how to join the multiple tables and only pulling data for the most recent year.
Thanks,
Kevin
I have this working but have run into another issue where I may have multiple prices for a given year due to a mid-year price increase. How can I go about adding an additional sub-query to grab the max price after I've selected the max year?
My current query
SELECT m.storeNum, m.itemNum,size,m.price,year FROM ItemMenu m,
(SELECT storeNum, itemNum, MAX(year) maxYear FROM ItemMenu
GROUP BY storeNum, itemNum) yt, (SELECT storeNum, itemNum, MAX(price)
maxPrice FROM ItemMenu) mp
WHERE m.storeNum=yt.storeNum AND m.itemNum=yt.itemNum
AND m.year=yt.maxYear AND m.itemNum=5000 AND m.storeNum=205706;
Returns valid results for max year (I've selected a specific store and item to reduce the number of results).
+----------+---------+------------+-------+------+
| storeNum | itemNum | size | price | year |
+----------+---------+------------+-------+------+
| 205706 | 5000 | Individual | 1.59 | 2014 |
| 205706 | 5000 | Large | 3.69 | 2014 |
| 205706 | 5000 | Large | 3.59 | 2014 |
| 205706 | 5000 | Individual | 1.79 | 2014 |
+----------+---------+------------+-------+------+
I need to further reduce this so I only get the values of $1.79 and 3.69.
Thanks
-Kevin
You'll need to use a subquery: 1st get a set of the most recent year for a given (item,store) pairing. Next, select the price for that (item,store,year) triplet:
SELECT m.storeNum, m.itemNum,price,year FROM ItemMenu m,
(SELECT storeNum, itemNum, MAX(year) maxYear FROM ItemMenu
GROUP BY storeNum, itemNum) yt
WHERE m.storeNum=yt.storeNum AND m.itemNum=yt.itemNum
AND m.year=yt.maxYear;
You can, of course, join the various ID->name tables onto this to get the human-readable data, but I suspect your issue was figuring out how to get the most recent prices.
It should be also noted that this could be done with a JOIN rather than including the subquery in the FROM section; that may be faster.