how do i use group by in only one row?

Adeeb Mark

Correct me if the question title is wrong ,

i have table in MySQL Named sales

-------------------------------------------------------------------------------------
| PRODUCT_NAME | PRODUCT_QUANTITY | PRODUCT_TYPE | In_Box_Count_Sell | ExpiryDate |
-------------------------------------------------------------------------------------
|     kkkkk    |       4          |   Count      |         1         |   2021-02-28 |
|     kkkkk    |       4          |   Count      |         1         |   2021-02-21 |
|     yuyuyu   |       3          |   Count      |         5         |   2021-02-21 |
-------------------------------------------------------------------------------------

Another Table Named depot

    --------------------------------
    | ITEM_NAME | PRODUCT_QUANTITY | 
    --------------------------------
    |   kkkkk   |      12          |
    |   yuyuyu  |      15          |
    --------------------------------

I have used this query :

SELECT `ExpiryDate`,`PRODUCT_NAME` As pdname,sales.`PRODUCT_QUANTITY`,`In_Box_Count_Sell`,`PRODUCT_TYPE`,(`depot`.`PRODUCT_QUANTITY`) As InDepotQuent FROM ((`sales`
INNER JOIN depot ON sales.PRODUCT_NAME = depot.ITEM_NAME)
INNER JOIN bill_info ON sales.BILL_NUMBER = bill_info.BILL_NUMBER)
WHERE bill_info.BILL_TYPE = 'مشتريات' 
ORDER BY `sales`.`PRODUCT_NAME`  ASC

And its result is :

---------------------------------------------------------------------------------------------
|  ExpiryDate | pdname | PRODUCT_QUANTITY | In_Box_Count_Sell | PRODUCT_TYPE | InDepotQuent |
---------------------------------------------------------------------------------------------
|  2021-02-21 | kkkkk  |       10         |        1          |     Count    |      12      |
|  2021-02-28 | kkkkk  |       4          |        1          |     Count    |      12      |
|  2021-08-25 | yuyuy  |       3          |        5          |     Count    |      15      |
---------------------------------------------------------------------------------------------

what i need to get is the atc = the sum of PRODUCT_QUANTITY - the sum of InDepotQuent and the result will be like this :

---------------------------------------------------------------------------------------------------
|  ExpiryDate | pdname | PRODUCT_QUANTITY | In_Box_Count_Sell | PRODUCT_TYPE | InDepotQuent | atc |
---------------------------------------------------------------------------------------------------
|  2021-02-21 | kkkkk  |       10         |        1          |     Count    |      12      |  2  |
|  2021-02-28 | kkkkk  |       4          |        1          |     Count    |      12      |  2  |
|  2021-08-25 | yuyuy  |       3          |        5          |     Count    |      15      |  12 |
---------------------------------------------------------------------------------------------------

So the sum of PRODUCT_QUANTITY = (14) - sum of InDepotQuent = (12)

The atc = 2

And taking into account the name of the pdname are similar in a table !

Gordon Linoff

You can use window functions, if I understand correctly:

SELECT ExpierDate, PRODUCT_NAME As pdname, s.PRODUCT_QUANTITY, 
       In_Box_Count_Sell, PRODUCT_TIPE, d.PRODUCT_QUANTITY As InDepotQuent,
       SUM(s.PRODUCT_QUANTITY) OVER (PARTITION BY PRODUCT_NAME) - d.PRODUCT_QUANTITY as atc
FROM sales s JOIN
     depot d
     ON s.PRODUCT_NAME = d.ITEM_NAME JOIN
     bill_info bi
     ON s.BILL_NUMBER = bi.BILL_NUMBER
WHERE bi.BILL_TIPE = 'مشتريات' 
ORDER BY s.PRODUCT_NAME ASC;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In R, how do I make a plot for only one group?

From Dev

How do i change dynamically textview in one single row only

From Dev

how do i group rows by an id row in group_concat into one row string?

From Dev

How do I rewrite and use only one version of the url?

From Dev

How can I transpose my data so it only have one row per group in R?

From Dev

How do I 'squash' the values in a DataFrame that I know only has one item per row into a Series?

From Dev

How do I group by and "prefer" a row

From Dev

How do I select one row but no more from foreign key ? ---- Group BY ---

From Dev

How do I select only one div between a group of divs like a radio button?

From Dev

How do I add a table limited to only one row in mysql/django?

From Dev

How do I compare one row to the next?

From Dev

pandas: how do I select first row in each GROUP BY group?

From Dev

How do I add a totals / averages row to a crosstab row group?

From Dev

Why do I find only one group in java regular expression

From Dev

how do i use group by only on date from datetime in LINQ query

From Dev

How do I use Spring Mongo to group two fields and get a array of one fields based on the other?

From Dev

How do I use GROUP BY to show multiple objects rather than just one for each grouping?

From Dev

how can I count all row and count group by on one tables

From Dev

how can I count all row and count group by on one tables

From Dev

perl regex: how to capture multi group use only one express?

From Dev

How to use only one matching row of association table to join parents

From Dev

How do I handle only being able to use earphones with one ear?

From Dev

How do I get ssh to use only one key from ssh-agent?

From Dev

GROUP BY with only first row of sequence of one column?

From Dev

jqgrid select only one row per group

From Dev

GROUP BY with only first row of sequence of one column?

From Dev

How can I convert this excel file so that it is not only one row?

From Dev

How can I print only one row with specific id in Laravel?

From Dev

How do I update a single row in each group?

Related Related

  1. 1

    In R, how do I make a plot for only one group?

  2. 2

    How do i change dynamically textview in one single row only

  3. 3

    how do i group rows by an id row in group_concat into one row string?

  4. 4

    How do I rewrite and use only one version of the url?

  5. 5

    How can I transpose my data so it only have one row per group in R?

  6. 6

    How do I 'squash' the values in a DataFrame that I know only has one item per row into a Series?

  7. 7

    How do I group by and "prefer" a row

  8. 8

    How do I select one row but no more from foreign key ? ---- Group BY ---

  9. 9

    How do I select only one div between a group of divs like a radio button?

  10. 10

    How do I add a table limited to only one row in mysql/django?

  11. 11

    How do I compare one row to the next?

  12. 12

    pandas: how do I select first row in each GROUP BY group?

  13. 13

    How do I add a totals / averages row to a crosstab row group?

  14. 14

    Why do I find only one group in java regular expression

  15. 15

    how do i use group by only on date from datetime in LINQ query

  16. 16

    How do I use Spring Mongo to group two fields and get a array of one fields based on the other?

  17. 17

    How do I use GROUP BY to show multiple objects rather than just one for each grouping?

  18. 18

    how can I count all row and count group by on one tables

  19. 19

    how can I count all row and count group by on one tables

  20. 20

    perl regex: how to capture multi group use only one express?

  21. 21

    How to use only one matching row of association table to join parents

  22. 22

    How do I handle only being able to use earphones with one ear?

  23. 23

    How do I get ssh to use only one key from ssh-agent?

  24. 24

    GROUP BY with only first row of sequence of one column?

  25. 25

    jqgrid select only one row per group

  26. 26

    GROUP BY with only first row of sequence of one column?

  27. 27

    How can I convert this excel file so that it is not only one row?

  28. 28

    How can I print only one row with specific id in Laravel?

  29. 29

    How do I update a single row in each group?

HotTag

Archive