Visual Studio code to manipulate MySql Data - mysql

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.

Related

Is there a trigger which can help me to update a report table from a daily records table?I

I have two tables as shown below: Table_Report needs to be updated from Daily_Records under these conditions:
Sold_Summary column needs to be updated by concatenating all Sold_Item where PIN is the same. Excluding special sold { Laptop, Smartphone, Tablets} which needs to be recorded in the Special_sold column.
Dest_Summary column needs to be updated by concatenating all Dest when PIN is the same.
Special_sold column needs to be updated the same way when PIN is the same.
Special_sold column only looks in the Sold_Item column if It finds any special sold { Laptop, Smartphone, Tablets} it records that under Special_sold when PIN is the same.
Table1: Daily_Records
**ID
Date
PIN
Sold_Item
Dest**
1
2021-04-20
110
Bag
Dawa
2
2021-04-21
131
Table
Harare
3
2021-04-22
110
Chair
Gondar
4
2021-04-23
120
Smartphone
Dawa
5
2021-04-24
111
Laptop
Adama
6
2021-04-25
120
Chair
Dawa
7
2021-04-26
111
Book
Harare
Table2: Table_Report
**ID
PIN
Sold_Summary
Dest_Summary
Special_sold**
1
110
Bag, Chair
Dawa, Gondar
null
2
111
Book
Adama, Harare
Laptop
3
120
Chair
Dawa
Smartphone
4
131
Table
Harare
null
I wouldn't use a trigger for this. Just run a query:
select pin,
group_concat(distinct case when sold_item not in ('Laptop', 'Smartphone', 'Tablet') then sold_item end) as sold_items,
group_concat(distinct case when sold_item in ('Laptop', 'Smartphone', 'Tablet') then sold_item end) as sold_items_special,
group_concat(distinct dest) as dest
from daily_records
group by pin;
You don't explain what id is. You can add such a column as:
row_number() over (order by pin) as id

Get records from a SQL table that should be there but are not

Is it possible to get a list of records that should be in a table but are not?
Here is the scenario: a survey question (caled certificate) is assigned to a number of facilities for a set period of time. Each facility is supposed to respond daily to this survey. The easy way to do this is to create/pre-populate the compliance records in a table and each time a facility responds, mark it as complete (set its ResponseDate). But because I am dealing with thousands of facilities over hundreds of days, it is not feasible. So right now I am doing this in DAL in a very inefficient way, converting the resulting datatable into JSON string and passing it to jQuery DataTable, and I am wondering if this can be achieved using a SQL query.
Here is an example:
Certificate: "Did you do this today"
5 facilities assigned to this certificate spanning May 1 to May 10. So there should be 50 compliance records in total.
This is how I have it set up in DB(just the releveant fields):
Table name: CertItems
CertiItemID CertificateName StartDate EndDate
1 Cert1 05/01/2018 05/10/2018
Table name: FacilityCertificate:
FacCertificateID FacilityID CertItemID
1 100 1
2 200 1
3 300 1
.... ... ...
10 1000 1
Table name: CertComplaince (theoretically, these records should be here, with some facilities having responded to some of the surveys)
CertComplianceID FacCertificateID ResponseDueDate ResponseDate
1 1 05/01/2018 05/01/2018
2 1 05/02/2018
3 1 05/03/2018 05/03/2018
... ... ...
10 1 05/10/2018
11 2 05/01/2018
12 2 05/02/2018 05/02/2018
13 2 05/03/2018
... ... ...
20 2 05/10/2018
... ... ...
98 10 05/08/2018 05/08/2018
99 10 05/09/2018
100 10 05/10/2018
But becuase I cannot pre-populate this table, I enter a record each time a facility responds, so in reality it looks like this:
CertComplianceID FacCertificateID ResponseDueDate ResponseDate
1 1 05/01/2018 05/01/2018
2 1 05/03/2018 05/03/2018
3 2 05/02/2018 05/02/2018
... ... ... ...
15 10 05/08/2018 05/08/2018
Question is how do I get all the records that would be in "theoretical" version of the compliance table having the table above, for a given date range. Something that includes facility ID, CertItemID/Name, ResponseDuedate and responseDate. I amusing SQL Server 2016.

MYSQL - Joins and what happened next query?

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

How to create a query based on condition with 3 joined tables in MySQL

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

TSQL How do I add the same identifying number to all rows in the same group, then increment for the next group, etc

If I have an output dataset from a CTE that looks like
PERIOD FT GROUP DEPT VALUE
1 Actual KINDER MATH 200
2 Actual KINDER MATH 363
3 Actual KINDER MATH 366
1 Budget KINDER MATH 457
2 Budget KINDER MATH 60
3 Budget KINDER MATH 158
1 Actual HIGHSCH ENGLISH 456
2 Actual HIGHSCH ENGLISH 745
3 Actual HIGHSCH ENGLISH 125
1 Budget HIGHSCH ENGLISH 364
2 Budget HIGHSCH ENGLISH 158
3 Budget HIGHSCH ENGLISH 200
6 Budget HIGHSCH ENGLISH 502
7 Budget HIGHSCH ENGLISH 650
1 Actual COLL ENGLISH 700
2 Actual COLL ENGLISH 540
3 Actual COLL ENGLISH 160
1 Budget COLL ENGLISH 820
2 Budget COLL ENGLISH 630
3 Budget COLL ENGLISH 800
but I want to add a column that will have an identifier for each group (the grouping being by FT, Group and Dept) like this:
PERIOD FT GROUP DEPT VALUE GroupID
1 Actual KINDER MATH 200 1
2 Actual KINDER MATH 363 1
3 Actual KINDER MATH 366 1
1 Budget KINDER MATH 457 2
2 Budget KINDER MATH 60 2
3 Budget KINDER MATH 158 2
1 Actual HIGHSCH ENGLISH 456 3
2 Actual HIGHSCH ENGLISH 745 3
3 Actual HIGHSCH ENGLISH 125 3
1 Budget HIGHSCH ENGLISH 364 4
2 Budget HIGHSCH ENGLISH 158 4
3 Budget HIGHSCH ENGLISH 200 4
1 Budget HIGHSCH ENGLISH 502 5
2 Budget HIGHSCH ENGLISH 650 5
3 Budget HIGHSCH ENGLISH 336 5
1 Actual COLL ENGLISH 700 6
2 Actual COLL ENGLISH 540 6
3 Actual COLL ENGLISH 160 6
1 Budget COLL ENGLISH 820 7
2 Budget COLL ENGLISH 630 7
3 Budget COLL ENGLISH 800 7
Please do you know how to go about it?
EDIT:
I feel like something in this direction may be useful
SELECT *,
CASE WHEN FT = 'Actual' THEN <something_incremental_to_do_with_row_num> OVER (PARTITION DEPT, GROUP, FT) END as GROUPID
FROM cte
I can't use ORDER BY in the OVER clause because I am on 2008
It's hard to say without seeing the SQL for the query, but I would think a variant on 'row_number() (partition by [some fields])' would give you this. Have you looked into that?
This existing question might give you what you need:
How to add sequence number for groups in a SQL query without temp tables
Chen, I believe your answer is already available in another thread:
How to return a incremental group number per group in SQL
Additionally, you could just create a unique_id for each group by concatenating fields. For example, if you wanted to group by FT, GROUP, and DEPT, then just put them all together. Ex:
SELECT *,
CAST(FT AS VARCHAR) +
CAST([GROUP] AS VARCHAR) +
CAST(DEPT AS VARCHAR) AS UNIQUE_ID
FROM MYTABLE
If you wanted to use it for a while to do a more complex query, then just throw it into a temp table:
SELECT *,
CAST(FT AS VARCHAR) +
CAST([GROUP] AS VARCHAR) +
CAST(DEPT AS VARCHAR) AS UNIQUE_ID
INTO #MYTEMPTABLE
FROM MYTABLE
You only need the CAST function if there are different data types in the fields you want to join. Best to keep it in to avoid any unnecessary headaches. Hope this helps.