I've set up a database to record the performance of a group of people picking sports events.
So every day around 40 people choose an team or person or horse or whatever they think will win that day.
The tables I have are Tipsters, Selections & Choices.
Tipsters is a list of all those involved.
Selections is a list of all the choices made for each day. More than one tipster can choose the same selection.
Choices is the 'choice' of the tipster - also shows running totals of current win seq, current lose seq and running total to 1 pt stakes.
My SQL isn't great but I've got enough to do the basics, join the tables etc.
BUT ... I need to be able to do some 'what happened next' type of queries e.g I need to select those people who had a winning streak of 2 (or whatever) and find out what happened next. So if tipster #1 had selected a winning choice on 28th and 29th October, I need to have a query to be able to show what happened after that. I can do it programmatically (join the tables, select those with the relevant sequence, then take that result and select all later selections etc.) but could do with some SQL. Thanks in advance ... sample data below:
Tipsters
tid tname
1 RCFO
2 DMN
3 RPNK
4 WPKY
5 SSBG
6 MEGJ
7 SSCEKK
Selections
sid sdate sname
1 2016-09-28 MT-name1
2 2016-09-28 CCC
3 2016-09-28 BDF
4 2016-09-28 MA
5 2016-09-28 EV
6 2016-09-28 AQQ
7 2016-09-28 ERAA
8 2016-09-28 SL
9 2016-09-28 SHWM
10 2016-09-28 OM
748 2016-10-21 MD
Choices
tid sid sqw sqf running
1 1 0 3 -7.56
2 2 0 3 -13.57
3 3 0 4 -18.96
4 4 0 3 36.75
5 5 0 4 -0.33
6 6 3 0 8.41
7 7 0 4 5.23
8 7 0 16 -27.49
1 88 0 1 -5.56
2 89 0 5 -15.57
7 97 0 7 2.23
3 97 0 1 -19.86
SELECT selections.seldate, selections.selection, selections.rtime, selections.result, selections.sp
FROM choices
JOIN selections ON selections.id = choices.selid AND sqw = 2
JOIN tipsters ON tipsters.id = choices.tipid
ORDER BY selections.seldate asc, rtime asc, selection ASC
Related
So I am using Visual Studios 2015 and MySQL 5.7.
First, I have a table called "items" which contain the following columns and rows:
BranchNo itemNo itemName Qty Pkey
1 1 Chicken 99 11
1 2 Coke 99 12
1 3 Applie Pie 99 13
Then I have another table called "setmeal" which uses the Pkey (itemNo in "setmeal" table) of table "items":
setmealno branchno itemNo Name Price SetMealID entryNo
1 1 11 1pc Fried Chicken 69 11 1
1 1 12 1pc Fried Chicken 69 11 2
2 1 13 Apple Pie Single 50 12 3
3 1 12 Coke Drink 20 13 4
Basically what table "setmeal" does is, is if there are 2 itemNo's with same "SetMealID", then they belong to the same set.
Then I have a "cart" which consists of (assuming I ordered):
TransactionNo UserNo SetMealNo Name Price Date BranchNo
1 1 11 1pc Fried Chicken 69 2015 1
So what I want to do in Visual Studios (2015) is whatever you put in the "cart", there's a code that's automatically going to deduct 1 in "Qty" in table "items" if the SetmealNo is the same with table "setmeal".
I need a code in visual studios to successfully deduct 1 from Qty whenever the conditions above are met.
Although, I think joining the 3 tables is the first step. (Although I also have no idea how to do it in MySQL Select Statements)
EDIT: I found how to merge the 3 tables
SELECT s.setmealid, t.setmealNo, i.pKey, s.itemNo, i.qty
FROM items i, setmeal s, cart t
WHERE s.itemNo = i.pKey and t.setmealno != 0
AND s.setmealID = t.setmealNo
AND t.transactionNo = '" & Form2.TextBoxTransNo.Text & "';
It Produces this table
setmealid setmealNo pKey itemNo qty
11 11 11 11 99
11 11 12 12 99
All that's left is a Visual Studios code to deduct from "qty" although I do not know how to do it.
I'm a bit confused when trying to create a specific query with the following data tables:
**table 1 - referral_data:**
ID attribution_name
------------------------
1 Category
2 Brand
3 Size
4 Color
5 Processor
6 OS
7 Screen Size
.....
**table 2 - referral_values:**
ID ref_data_id attribution_value
---------------------------------------------
1 1 Cell Phones
2 1 Tablets
3 1 Laptops
4 1 Computers
5 1 LCD Monitors
6 2 Nokia
7 2 Motorola
8 2 Samsung
9 2 Lenovo
10 2 Philips
11 3 10x10x11
12 3 100x100x20
13 3 10x200x200
14 3 2x2x3
15 4 Black
16 4 Cyan
17 4 Magenta
18 4 White
19 4 Blue
20 5 ARM Cortex A11
21 5 Snapdragon 11
22 5 Intel I3 XXXXX
23 5 Exynos XXXX
24 6 Android 4.1
25 6 Android 3.0
26 6 Windows Phone 3
27 6 Windows 8 Professional
28 7 18.5"
29 7 11.8"
30 7 7.0"
31 7 5.0"
32 7 3.5"
......
**table 3 - product_specs:**
ID product_id referral_data_id referral_value_id
--------------------------------------------------------
1 1050 1 1 // <-- Product 1 - Category: Cell Phones
1 1050 2 8 // <-- Product 1 - Brand: Samsung
1 1050 4 19 // <-- Product 1 - Color: Blue
1 1050 6 24 // <-- Product 1 - Processor: Exynos XXXX
1 1050 7 30 // <-- Product 1 - Screen Size: 7.0"
1 1068 1 4 // <-- Product 2 - Category: Computers
1 1068 2 9 // <-- Product 2 - Brand: Samsung
1 1068 6 22 // <-- Product 2 - Processor: Intel Core I3
1 1068 7 28 // <-- Product 2 - Screen Size: 18.5"
......
These tables consists in a "Product Catalog" that I'm planning to use in a e-commerce website.
This is intended to optimize "client side" search functions and organize internal product data information, turning the "content-administrators" tasks in a simplest and easiest environment as possible. (Letting them, for example, choosing an "already-entered" data values instead of re-entering an "already entered data", avoiding duplicated data or typo errors).
The "content administrators" will have options, according to that "table dynamics", to insert new attribution data (product characteristics) and or new attribution values to them (attribution values).
Info: the product code field named "product_id", is outside the tables relation, this is just used to create a link to attach informations to products that they belong to.
In general SQL Joins to get data over the tables, I'm Ok. But there some kind of informations that I need to get / manage, I'm getting nuts. I've spent A LOT of hours and I just have found a headache.
My question is about how to build, in a single query, to get commonly used referral data, based on a CATEGORY.
(when the contents admin choose, for a example, "Cell Phones" in "Category" field, they will get a commonly used "data table information" about that Category, like Brand, Color, Screen Size, etc .... to just choose their category attribution ) and to create a similar query to highlight or order by the commonly used attribution values ( i.e. commonly used screen sizes ).
In a single query?
SELECT ps2.product_id,rv2.attribution_value,rd.attribution_name
FROM ((
(select ps.* from product_specs ps JOIN referral_values rv
ON rv.ref_data_id=ps.referral_value_id
WHERE rv.attribution_value = 'Cell Phones'
) ps1
JOIN product_specs ps2 on ps1.product_id=ps2.product_id)
JOIN referral_values rv2 ON ps2.referral_value_id = rv2.id)
JOIN referral_data rd ON rd.id = rv2.ref_data_id;
The inner select is used to get the right product_id based on criteria 'Cell Phones'. The others are used to populate this value with all details. To do this the first JOIN is a self-JOIN of product_specs to get all data, the following two joins are used to get the string values of them.
By the way: The column product_specs.referral_data_id is redundant and can/should be removed
Here is a problem. I need to find working days between two dates (without weekends). I currently found and was able to successfully use this approach:
SELECT 5 * (DATEDIFF(DateClosed, DateOpened) DIV 7) + MID('0123455501234445012333450122234501101234000123450',
7 * WEEKDAY(DateOpened) + WEEKDAY(DateClosed) + 1, 1) AS TotalResolutionTimeBusinessDays
FROM table1
This is a complex calculation based on a matrix:
| M T W T F S S
-|--------------
M| 0 1 2 3 4 5 5
T| 5 0 1 2 3 4 4
W| 4 5 0 1 2 3 3
T| 3 4 5 0 1 2 2
F| 2 3 4 5 0 1 1
S| 0 1 2 3 4 0 0
S| 0 1 2 3 4 5 0
In the matrix, the intersection of any given x and y value pair (WEEKDAY(#S) and WEEKDAY(#E) yields the difference in work days between the two values. The 49 values in the table are concatenated into the following string: 0123455501234445012333450122234501101234000123450.
This works fine for me but now I need to present difference between dates in another format. E.g.: Let's say we have two dates: StartDate = '2013-06-28 01:27:35' and EndDate = '2013-07-08 16:47:21'. If we use method described above we get 7 working days which is correct. But I need to count all the difference between dates(including hours and minutes) so it could look like
SELECT TIME_TO_SEC(TIMEDIFF('2013-07-08 16:47:21','2013-06-28 01:27:35')) / 3600 / 24
which without weekends should be value like 7.64 days.
Any suggestions how to write a calculation based on that format? Any help would be much appreciated.
I have a table which contains the following columns:
ID = unique identifier in ascending order
ProductId = Id of a product
rate1 to rate5 = number of times that product has recived a 1 star, 2
star, 3 star, 4 star or 5 star rating
aveRate = average rating for that product
lastSubDate = last time a review was submitted for that product
And I'm trying to write an SQL statement which selects the ProductId's which have the highest number of 5 star ratings also with an average rating of 5 and the last review submission was within the last three months.
The table looks like this example:
ID ProductId rate1 rate2 rate3 rate4 rate5 aveRate lastSubDate
18 9996637 0 0 0 0 1 5 2011-08-10 12:00:34
26 9996628 1 0 0 0 0 1 2010-05-06 05:45:05
34 9996618 0 0 0 1 0 4 2011-10-09 09:00:45
36 9996614 5 0 0 0 0 1 2011-01-05 09:30:32
48 9996592 5 0 1 0 3 3 2012-11-28 19:00:06
66 9996566 0 0 0 1 3 5 2011-04-06 06:45:34
70 9996562 0 0 0 1 1 5 2011-05-17 18:30:03
This is the query I've got so far:
SELECT `ProductId`,`rate5`,`aveRate`,`lastSubDate`
FROM ratings
WHERE `aveRate` = 5 AND `lastSubDate` > '24 Feb 2013'
ORDER BY `rate5` DESC
LIMIT 3
This returns the products with the most 5 star reviews which also have an average rating of 5, however it doesn't limit the results to the last three months as I want. How can I amend this statement to also select data from only the last three months?
Your date constant is in the wrong format. Try this if you're using a constant date:
... AND `lastSubDate` > '20130224'
You can also use 20130224 (without the quotes) or '2013-02-24' above - see the MySQL Date and Time Literals documentation.
Or to calculate "three months ago" just do this:
... AND `lastSubDate` > CURDATE() - INTERVAL 3 MONTH
Finally, you don't need the backticks here. If you find them distracting you can drop them:
... AND lastSubDate > whatever
OK this is the data I am working with:
category_child_id category_parent_id
1 0
2 0
3 1
4 1
5 3
6 3
7 4
8 0
9 8
10 8
11 0
12 11
13 11
14 0
15 14
16 14
17 14
18 0
19 18
20 18
21 18
0 19
It's basically categories with sub categories etc etc.
If I
SELECT category_child_id FROM category_xref WHERE category_parent_id = 1
it returns 3 & 4 which is correct. However there are no products in this category only in the category below so the results I actually want are 5 & 6 as well. However this is not always the same so it does need to be a query.
So basically I need to run a query to get all connected(nested) categories from the table. I've tried many ways with failed results so any help would be great.
If you are using PostgreSQL you could have used "WITH RECURSIVE" but MySQL does not support dynamic, recursive queries. You have to do it with your accessing language (e.g. PHP, Java).
There you can iterate over your recordset and perform a query for each returned child row until no more child rows are returned.
As TRD says, there are extensions to SQL supporting recursive searching of self-joins (Oracle uses 'CONNECT BY') however not only are these not available in MySQL, they're also not the most efficient nor flexible solution.
Have a google for adjacency list model - I did and found this.