I have an issue that want your help . Following is my issue :
I have a table
id | total
1_2 | 12
1_2_3 | 13
1_2 | 14
1_2_5 | 14
1_1_1 | 14
Normal group by id will result
SELECT id , sum(total) id | total
FROM mybucket -> 1_2 | 26
GROUP BY id 1_2_3 | 13
1_2_5 | 14
1_1_1 | 14
But i want group by id that format field id . Example i split 1_2_3 to 1_2 and 1_2_4 to 1_2
And final result
id | total
1_2 | 26 +13+14 = 43
1_1_1 | 14
How can I group by such method?
Assume you want group until second underscore. You get two groups 1_2, 1_1 (NOT 1_1_1)
SELECT nid AS id, SUM(total) AS total
FROM mybucket
LET nid = CASE WHEN ARRAY_LENGTH(SPLIT(id, "_")) > 2 THEN CONCAT(SPLIT(id, "_")[0],"_",SPLIT(id, "_")[1]) ELSE id END;
GROUP BY nid ;
Related
Hello I have a table here
---------------------
ID | PARTY | Name.Id
---------------------
1 | IND | 12
2 | IND | 13
3 | CUST | 14
4 | CUST | 15
5 | CUST | 16
6 | IND | 17
---------------------
I want to return the whole table but filter 'CUST' which has value 15 and 16 in the column 'Name.Id'
The result should look something like this
---------------------
ID | PARTY | Name.Id
---------------------
1 | IND | 12
2 | IND | 13
3 | CUST | 14
4 | IND | 17
---------------------
I tried using where statement on the 'Name.Id' but it returns only those rows which has a value 15 and 16.
whole table but filter 'CUST' which has value 15 and 16 in the column 'Name.Id'
It sounds like you want a where clause like:
WHERE NOT (party = 'CUST' and name.id IN(15,16))
But CUST seems redundant from your sample data, i.e. you could get away with simplifying it to just WHERE name.id NOT IN (15,16)
Can you give this a whirl?
select *
from t
where (PARTY, Name.Id) not in (
select 'CUST', 15
union
select 'CUST', 16
)
select *
from t
where Party not in
(select Party from t
where Party="CUST"
and (Name.Id=15 or Name=16));
I think I got the solution here
select *
from t
where Party <> 'CUST'
or (Party = 'CUST'
and Name.Id = 14))
I have the following tables:
purchase_tbl
id | productId | purchaseQuantity
---+-----------+-----------------
1 | 1 | 30
2 | 2 | 30
3 | 1 | 10
4 | 2 | 10
sale_tbl
id | productId | saleQuantity
---+-----------+-------------
1 | 1 | 10
2 | 2 | 10
3 | 1 | 10
4 | 2 | 10
5 | 1 | 10
6 | 2 | 10
I need to get the output as this one:
productId | totalPurchasedQuantity| totalSaleQuantity
----------+-----------------------+------------------
1 | 40 | 30
2 | 40 | 30
I'm using this query and how to get the desired result?
SELECT purchase_tbl.productId
, SUM(purchase_tbl.purchaseQuantity) AS totalPurchaseQuantity
, SUM(sale_tbl.saleQuantity) AS totalSaleQuantity
FROM purchase_tbl
JOIN sale_tbl
ON purchase_tbl.productId = sale_tbl.productId
GROUP BY purchase_tbl.productId
Current output
productId | totalPurchaseQuantity | totalSaleQuantity
----------+-----------------------+------------------
1 | 120 | 60
2 | 120 | 60
You better group then in separate query, as table have multiple records for each product, which getting cross product.
SELECT purchase.productId, totalPurchaseQuantity, totalSaleQuantity
FROM
(SELECT purchase_tbl.productId
, SUM(purchase_tbl.purchaseQuantity) AS totalPurchaseQuantity
FROM purchase_tbl
GROUP BY purchase_tbl.productId) purchase
INNER JOIN
(SELECT sale_tbl.productId
, SUM(sale_tbl.saleQuantity) AS totalSaleQuantity
FROM sale_tbl
GROUP BY sale_tbl.productId
) sale ON sale.productId= purchase.productId;
To obtain your expected result you have to do the aggregation on the individual table before joining them. Your query with be like:
SELECT A.productId, A.totalpurchaseQuantity, B.totalsaleQuantity
FROM
(SELECT productId, SUM(purchaseQuantity)
totalpurchaseQuantity FROM purchase_tbl
GROUP BY productId) A JOIN
(SELECT productId, SUM(saleQuantity)
totalsaleQuantity FROM sale_tbl
GROUP BY productId) B ON
A.productId=B.productId;
I have a problem with SQLcode
I have a table
id | content | id_user | id_store
1 | abc | 1 | 10
2 | xzy | 1 | 10
3 | abc | 1 | 10
4 | abc | 1 | 11
5 | abc | 1 | 12
My problem is how i got the result is the count of max (id_store) which is 2* value >= max(id_store)
This is a example, result will be
id_store | count(...)
10 | 3
because (3*2) > max of count = 3
Tks everyone
It's very difficult to understand your question. Try to use the next query
SELECT id_store,COUNT(*) CountOfStore
FROM `Your Table`
GROUP BY id_store
HAVING 2*COUNT(*) >= (
SELECT MAX(CountOfStore) -- max of all CountOfStore
FROM
(
SELECT COUNT(*) CountOfStore -- count of store for each id_store
FROM `Your Table`
GROUP BY id_store
)
)
Hope I understood you rightly.
SELECT TestID, Grade FROM tests_points;
Returns:
+--------+-------+
| TestID | Grade |
+--------+-------+
| 10 | 125 |
| 11 | 110 |
| 12 | 100 |
| 13 | 75 |
| 14 | 50 |
| 15 | 65 |
| 16 | 70 |
| 17 | 100 |
| 18 | 100 |
+--------+-------+
But, tests ID 17 and 18 are "bonus tests", so I need replace the two lower grades by these two ones, and return the SUM of all grades.
So, how I can "replace"the two lower grades (From TestID 14 and 15) by testID 17 and 18 grades.
The "correct grade list" would be:
+--------+-------+
| TestID | Grade |
+--------+-------+
| 10 | 125 |
| 11 | 110 |
| 12 | 100 |
| 13 | 75 |
| 14 | 100|
| 15 | 100|
| 16 | 70 |
+--------+-------+
In the end I just need the SUM of all grades, fixing the lower grades.
SELECT SUM(Grade) FROM tests_points;
How can I do that?
This is what you need!
select testID, GREATEST(
IF(
grade = (select min(grade) from test_points),(select grade from test_points where testID = 17),
grade)
,
IF(grade = (select min(grade) from test_points WHERE grade > ( SELECT min(grade) FROM test_points))
,(select grade from test_points where testID = 18),
grade)) as Score
from test_points
where testID not in(17, 18)
Demo on SQLfiddle.com
the following query will get probably get what you want..
SELECT ttal-garba FROM
(SELECT sum(c.grade) as garba FROM (SELECT * from tests_points where testid < 17 order by grade asc limit 2) as c) as a,
(SELECT sum(grade) as ttal FROM tests_points) as b
(I know.. hard coding everything is bad ..)
If your just trying to add up all the values except the bottom two you can do something lik,e this.
First add a new field lets say test_type:
UPDATE test_points
SET test_type = 'test';
Now we can just select using group by the sum. For example
SELECT test_type, (SUM(Grade) OVER (ORDER BY Grade DESC))
FROM test_Points
LIMIT 7;
This will return the sum of all values, but it will show 7 columns. If you want only the total, Throw a group by into the mix, and it will narrow it down to one field, or just select the 7th row number.
I have a MySQL table like this.
| season_id | round_1 | names | score_round_1
| 5 | 10 | John1 | 5
| 5 | 10 | John2 | 3
| 5 | 11 | John3 | 2
| 5 | 11 | John4 | 5
I want to select the records with highest score_round_1 in each round_1(10,11) group .
In this case the first and last rows would be selected.
I tried using the GROUP BY round_1 but that only returns the first row from the two.
Any advice?
Zolka
This is simple
select max(score_round_1),
name
from score
group by round_1
SELECT *
FROM table p1
WHERE score_round_1 = (
SELECT MAX( p2.score_round_1 )
FROM table p2
WHERE p1.round_1 = p2.round_1 ) ANDround_1 !=0
Use aggregate function MAX
SELECT names, MAX(score_round_1) GROUP BY round_1