GROUP BY addition of product multiple order tables - mysql

Table 1
ProductID
HSN CODE
GST
1
JJKK
5
2
J1
12
3
JJKK
5
4
J2
18
5
J1
12
6
JJKK
5
7
J2
18
8
J3
18
9
JJKK
5
TABLE 2
PRODUCT ID
PRODUCT PRICE
2
20
3
30
4
40
5
30
6
40
7
20
9
30
TABLE 3
PRODUCT ID
QUANTITY
2
2
3
4
4
6
6
4
7
2
9
1
What I need is the following output
HSN CODE
TOTAL OF QUANTITY AS PER HSN
GST of HSN(B)
GROSS TOTAL OF PRICE(PRODUCT PRICE * QUANTITY)(A)
TOTAL PRICE (A+(B percent of A))
J1
2
12
(2x20) = 40
40+(12% of 40) = 44.8
JJKK
9
5
(4x30)+(4x40)+(1x30) = 310
310+(5% of 310) = 325.5
J2
8
18
(6x40)+(2x20) = 280
280+(18% of 280) = 330.4
TABLE 3 is the base table, ie we need to look for product ids 2,3,4,6,7,9.
The logic for output table is described below:
FOR HSN LOGIC (COLUMN 1 LOGIC)
So for HSN we will look for HSN of these ids from table 1 & then Group by it with HSN & remove duplicate HSN. The answer is written in column 1 of output.
FOR TOTAL OF QUANTITY AS PER HSN (COLUMN 2 LOGIC)
We now add quantity as per product id from table 2 and display as total as per HSN ie. HSN of 3,6 and 9 are same so we will add quantity of table 2 of these id and place it in frnt of JJKK of HSN in column 2. Similar logic for other hsn value will apply.
FOR GST OF HSN (COLUMN 3 LOGIC)
We will write unique gst from table 1 which is written in same row value of HSN, ie HSN JJKK has GSt of 5%. So we will write 5.and same logic goes for other HSN.
FOR GROSS TOTAL OF PRICE (COLUMN 4 LOGIC)
Here we will take product price from table 2 for product id mentioned in table 3. We will multiply that price with quantity of table 3 and the result will be added as per HSN. ie. for product id 3,6 and 9 the HSN is same then we will add (430)+(440)+(1*30)
TOTAL PRICE (A+(B percent of A)) (COLUMN 5 LOGIC)
Here we will add Gross total pf price marked as A with the number mentioned under GST of HSN. ie for J1 HSN the value for column 4 will be (40+(12% of 40))

Related

Mysql Select Product attribute price

Sorry for my bad english)
Need help with mysql query.
There is a products table, which stores the base price for products that do not have attributes that the price depends on.
Table: product
product_id
product_name
price
1
T-shirt_1
10
2
T-shirt_2
12
10
T-shirt_10
18
15
Hat
10
There is a table of attribute values by which products are filtered.
Table: eav_ent
ent_id
product_id
attr_id
attr_value_id
1
1
1
2
2
1
1
4
3
1
1
19
4
1
2
25
5
1
2
34
6
2
2
23
7
2
3
56
Table: eav_ent_product_price
ent_product_price_id
product_id
price_name
price
1
1
T-shirt_1 size S
10
2
1
T-shirt_1 size M
15
3
1
T-shirt_1 size M with a zipper
20
4
2
T-shirt_2 size M
12
5
2
T-shirt_2 size M with a zipper
25
Table: eav_ent_product_price_attr
ent_product_price_attr_id
product_id
attr_value_id
ent_product_price_id
1
1
2
1
2
1
19
2
3
1
4
3
4
1
19
3
5
2
19
4
6
2
4
5
7
2
19
5
For each product, you can separately create prices depending on the combination of attributes, for example:
T-shirt_1 size S - 10 $
T-shirt_1 size M - 15 $
T-shirt_1 size M with a zipper - 20 $
Hat - $ 10 (base price)
How can I correctly filter products and display prices depending on the selected attributes, if they exist, or just display the base price?
What I tried:
SELECT a.* FROM(
SELECT p.* FROM product p
JOIN eav_ent en1 ON en1.product_id=p.product_id AND en1.attr_value_id=19
JOIN eav_ent en2 ON en2.product_id=p.product_id AND en2.attr_value_id=4
LEFT JOIN eav_ent_product_price ppi ... I don't know
WHERE 1 GROUP BY p.product_id
) a WHERE 1

Sum of all child and assign to parent in hierarchical data in MySql

I have a 2 Mysql tables like 1.Product(This table is hierarchical or recursive) and 2.Sales table in this will store sale id and product id. I need to show parent product with the sum of all hierarchical child product.
Product table having data like,
id name parent_id
1 Necklace NULL
2 Ring NULL
3 Earing NULL
4 Choker 1
5 Long Necklace 1
6 Short Necklace 1
7 2-Line 5
8 3-Line 5
9 Mango 5
10 Green 7
11 Red 7
12 White 7
13 Stud 3
Sales table will have data like,
id product_id no_of_pcs weight rate
1 10 5 40 35000
2 12 8 50 50000
3 9 2 20 25000
4 6 1 8 25000
5 13 2 16 22000
Now I'm trying the result as,
Product sale_pcs tot_pcs sale_wt sale_rate
1 Necklace 16 118 135000
3 Earing 2 16 22000
Now I'm using mysql query inside php recursive function by using product table. By using this even recursive function all the products need to check with sales table. This will happening performance issue. Could you please help any one to solve this issue whether this possible by doing in query itself

Sum of column for distinct rows

I have the following MySQL table structure
id product_id order_id order_shipping shipping_code
1 23 1 2.00 GB
2 12 1 2.00 GB
3 5 2 3.50 GB
4 6 2 3.50 GB
5 34 3 3.00 FR
6 8 3 3.00 FR
7 4 4 5.00 IN
I'd like to group the rows by the shipping_code and include the total orders and total shipping cost per code. Something like this:
Code Orders Cost
GB 2 5.50
FR 1 3.00
IN 1 5.00
I'm struggling with the total cost part. The shipping cost is for the order as a whole, not the product. It's just duplicated in each row relating to the same order. So for order #1 the shipping cost was £2, not £4.
How do I do a sum of that column but only for the distinct order rows?
To get around the duplicated shipping costs for each order you need to group your data by order_id first, and then COUNT the number of orders and SUM the shipping costs:
SELECT code, COUNT(o.shipping) AS orders, SUM(o.shipping) AS cost
FROM (SELECT MIN(shipping_code) AS code, MIN(order_shipping) AS shipping
FROM orders
GROUP BY order_id) o
GROUP BY code
Output:
code orders cost
GB 2 5.5
FR 1 3
IN 1 5
Demo on dbfiddle

Find lowest value from the rows which contains similar foreign key value

Table:
pk fk price
1 1 23
2 1 12
3 1 3
4 2 53
5 2 75
6 3 95
7 3 113
8 3 63
9 3 73
10 3 93
11 4 113
11 4 150
11 4 105
In the above table:
How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.
I want a single SQL statement which could find lowest price for each common fk value.
You just want a basic aggregate per group.
select fk, min(price)
from your_table
group by fk;

Repeat records according to a quantity field

I have an Excel sheet which I receive from my customer and I get imported to Access and I would call that table [tblCustomer] and that would look something like this:
ProductID Name Expire date SumofQty
------------ ----------- --------------- --------
3 Flour 13-Dec-2013 6
6 Meat 20-Jan-2014 10
So the table contain maybe 100 items. I want in the same table or another table to copy the same record 6 times as per SumofQty and than the next record copy it 10 times and so on.
I need this cause I will create labels for each product right now I'm doing manually.
You can do that in a Query quite easily by using a "Numbers table". Create a table named [Numbers] in your database consisting of a single field named [n] that has a field type of Numeric (Long Integer). Create rows in that table with values 1, 2, 3, ... up to a number that well exceeds the largest value you ever expect to see in [tblCustomer].[SumofQty]. In my test I used 2500, so my [Numbers] table looks like
n
----
1
2
3
...
2499
2500
Then, for sample data in table [tblCustomer]
ProductID Name Expire date SumofQty
--------- ----- ----------- --------
3 Flour 2013-12-13 6
6 Meat 2014-01-20 10
the query
SELECT tblCustomer.*
FROM
tblCustomer
INNER JOIN
Numbers
ON Numbers.n <= tblCustomer.SumofQty
returns
ProductID Name Expire date SumofQty
--------- ----- ----------- --------
3 Flour 2013-12-13 6
3 Flour 2013-12-13 6
3 Flour 2013-12-13 6
3 Flour 2013-12-13 6
3 Flour 2013-12-13 6
3 Flour 2013-12-13 6
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
6 Meat 2014-01-20 10
Here's the solution using Erik von Asmuth's comment which pointed to an answer originally created by Gustav.
SELECT t.*
FROM tblCustomer AS t
INNER JOIN (SELECT DISTINCT
[Hundreds]+[Tens]+[Ones] AS Factor,
100*Abs(Hundo.id Mod 10) AS Hundreds,
10*Abs(Deca.id Mod 10) AS Tens,
Abs(Uno.id Mod 10) AS Ones
FROM
msysobjects AS Uno,
msysobjects AS Deca,
msysobjects As Hundo
WHERE Abs(Deca.id Mod 10) <> 0
or Abs(Uno.id Mod 10) <> 0
or Abs(Hundo.id Mod 10) <> 0) AS sq on sq.Factor <= t.SumofQty
I added the hundreds column as well because I think I'm probably going to need it. My method also filters out 0 in the sub query, otherwise I was getting 2 rows for things with a quantity of 1.