i use this query below to get the data
SELECT
tsi.shipping_service,
tsi.shipment_amount as shipping_cost,
(
SELECT sum(shipment_cost) FROM tabPacking Slip packages shadow sps
WHERE parent =
(SELECT name FROM tabPacking Slip pslip
WHERE pslip.purchase_order = tsi.po_no LIMIT 1
)
GROUP BY tsi.shipping_service
)
as cobb_charge
FROM tabSales Invoice as tsi;
OUTPUT
+-------------------------------------+---------------+-------------+
| shipping_service | shipping_cost | cobb_charge |
+-------------------------------------+---------------+-------------+
| UPS-Ground | 32.150000 | 32.150000 |
| UPS-Ground | 0.000000 | 18.150000 |
| UPS-Ground | 53.740000 | 0.000000 |
| UPS-Ground | 20.240000 | 20.240000 |
| UPS-Ground | 14.710000 | 14.710000 |
| UPS-Ground | 18.410000 | 18.410000 |
| UPS-Ground | 21.740000 | 21.740000 |
i need to group this data by shipping service and sum all the cost and charge
If your query produce the result in your output you can try:
select a.shipping_service,
sum(a.shipping_cost),
sum(a.cobb_charge)
from
(
SELECT
tsi.shipping_service,
tsi.shipment_amount as shipping_cost,
(
SELECT sum(shipment_cost) FROM tabPacking Slip packages shadow sps
WHERE parent =
(SELECT name FROM tabPacking Slip pslip
WHERE pslip.purchase_order = tsi.po_no LIMIT 1
)
GROUP BY tsi.shipping_service
)
as cobb_charge
FROM tabSales Invoice as tsi
) as a group by a.shipping_service ;
Related
I have a column (share_2pp) that needs to be updated with a calculated result from the table. This select query produces the column (share_2pp) I would like.
WITH cte
AS (
SELECT recipe
, SUM(meal_nr) AS meal_sum
FROM w03_forecast
GROUP BY recipe
)
SELECT w03_forecast.recipe
, w03_forecast.meal_nr
, meal_sum
, (meal_nr / meal_sum) AS share_2pp
FROM w03_forecast
INNER JOIN cte
ON w03_forecast.recipe = cte.recipe;
+--------+---------+----------+-----------+
| recipe | meal_nr | meal_sum | share_2pp |
+--------+---------+----------+-----------+
| 1 | 3842 | 4593 | 0.8365 |
| 2 | 4284 | 5130 | 0.8351 |
| 3 | 4166 | 4926 | 0.8457 |
| 4 | 2830 | 3382 | 0.8368 |
| 5 | 2495 | 2935 | 0.8501 |
| 1 | 751 | 4593 | 0.1635 |
| 2 | 846 | 5130 | 0.1649 |
| 3 | 760 | 4926 | 0.1543 |
| 4 | 552 | 3382 | 0.1632 |
| 5 | 440 | 2935 | 0.1499 |
+--------+---------+----------+-----------+
However, when I try to update the table I get a syntax error at FROM.
WITH cte
AS (
SELECT recipe
, SUM(meal_nr) AS meal_sum
FROM w03_forecast
GROUP BY recipe
)
UPDATE w03_forecast
SET w03_forecast.share_2pp = (meal_nr / meal_sum)
FROM w03_forecast
INNER JOIN cte
ON w03_forecast.recipe = cte.recipe;
I think you can just use JOIN:
UPDATE w03_forecast f JOIN
(SELECT recipe, SUM(meal_nr) AS meal_sum
FROM w03_forecast
GROUP BY recipe
) r
USING (recipe)
SET f.share_2pp = (meal_nr / meal_sum);
That said, there is no reason to store this in the table. It can easily be calculated on-the-fly using window functions:
select f.*,
(meal_nr / sum(meal_nr) over ()) as share_2pp
from w03_forecast f
I want to join three tables respectively from the below SQLFiddle
http://sqlfiddle.com/#!9/5dd558/4
Now I want to create one table from this table based on date and Brand.
Like, I want data in this manner
Date, Brand, Series, Table_1_Viewers, Table_2_Viewers, Table_2_Viewers
and if data is not matched on the table then the field should be nulled.
What I have done
SELECT h.*,
a.`amazon_viewers` AS "Table_1_Viewers",
n.`views` AS "Table_2_Viewers",
FROM `Table-1` h
LEFT JOIN `Table-2` a
ON h.`date` = a.`date`
AND h.`brand` = a.`brand`
LEFT JOIN `Table-3` n
ON h.`date` = n.`date`
AND h.`brand` = n.`brand`
Obviously I am selecting data from table-1 so it will display brand column only from table-1 but how can I get all table's brand column name in one column and merge these tables.??
The output I want...
| Date | Brand | Series | Table_1_Viewers | Table_2_Viewers | Table_3_Viewers |
|:----------:|:--------:|:--------------:|:---------------:|:---------------:|:---------------:|
| 12/1/2018 | TEST | TEST_SERIES | 100 | | |
| 10/15/2018 | MTV | GOT | 1000 | | 1000 |
| 12/1/2018 | TEST | Viking | 485632 | 856325 | |
| 12/1/2018 | TEST | Another Series | | 200 | |
| 10/15/2018 | POGO | GOT | | 1000 | |
| 7/1/2019 | No_Match | TEST_SERIES | | | 100 |
| 12/1/2018 | TEST-5 | Viking | | | 953022 |
You can do union all with aggregation :
select t.Date, t.Brand, t.Series_name,
sum(case when table_view = 't1' then amazone_viewer else 0 end) as Table_1_Viewers,
. . .
from (select h.date, h.brand, h.series_name, h.amazone_viewer, 't1' as table_view
from `Table-1` h union all
select h1.date, h1.brand, h1.series, h1.viewes, 't2'
from `Table-2` h1 union all
. . .
) t
group by t.Date, t.Brand, t.Series_name;
I have two queries that pull data from two different tables, but I need them to pull in the same report. I have a shared key between them, and the first table has one entry that corresponds to many entries in the second table.
My first query:
SELECT Proposal_ID,
substr(Proposal_Name, 1, 3) AS Prefix,
substr(Proposal_Name, 4, 6) AS `Number`,
Institution,
CollegeCode,
DepartmentCode,
Proposer_FirstName,
Proposer_LastName
FROM proposals.proposal
WHERE Institution = 'T';
Sample Data:
+----+--------+--------+-------+----------+----------+-----------+----------+
| ID | Prefix | Number | Inst. | CollCode | DeptCode | FirstName | LastName |
+----+--------+--------+-------+----------+----------+-----------+----------+
| 18 | SYP | 4675 | T | AS | SOC | Linda | McGaff |
+----+--------+--------+-------+----------+----------+-----------+----------+
| 20 | GEO | 4340 | T | AS | SGS | Teddy | Graham |
+----+--------+--------+-------+----------+----------+-----------+----------+
My second query:
SELECT Parent_Proposal,
SUBSTRING_INDEX(GROUP_CONCAT(`status`.`Status_Code` ORDER BY `status`.`Status_Time` DESC), ',', 1) AS status_code,
SUBSTRING_INDEX(GROUP_CONCAT(`status`.`Status_Time` ORDER BY `status`.`Status_Time` DESC), ',', 1) AS status_timestamp
FROM proposals.`status`
GROUP BY `status`.Parent_Proposal
Sample Data:
+-----------------+-------------+----------------------+
| Parent_Proposal | Status_Code | Status_Time |
+-----------------+-------------+----------------------+
| 18 | 40 | 2016-11-09 06:30:35 |
+-----------------+-------------+----------------------+
| 20 | 11 | 2017-03-20 10:26:31 |
+-----------------+-------------+----------------------+
I basically need to pull the most recent Status_Code and Status_Timestamp based on the Status_Timestamp and then relate that to the first table with the Parent_Proposal column.
Is there a way to group a subset of results without grouping all of the data together?
Expected Result:
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| ID | Prefix | Number | Inst. | CollCode | DeptCode | FName | LName | Status_Code | Status_Time |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| 18 | SYP | 4675 | T | AS | SOC | Linda | McGaff | 40 | 2016-11-09 06:30:35 |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| 20 | 11 | GEO | 4340 | AS | SGS | Teddy | Graham | 11 | 2017-03-20 10:26:31 |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
Thanks for any help and insight!
I think you want this. Just join your two tables together, and then do an additional join to a subquery on the status table to find the latest record for each parent proposal.
SELECT
p.Proposal_ID,
SUBSTR(p.Proposal_Name, 1, 3) AS Prefix,
SUBSTR(p.Proposal_Name, 4, 6) AS Number,
p.Institution,
p.CollegeCode,
p.DepartmentCode,
p.Proposer_FirstName,
p.Proposer_LastName,
s1.Status_Code,
s1.Status_Time
FROM proposals.proposal p
LEFT JOIN proposals.status s1
ON p.ID = s1.Parent_Proposal
INNER JOIN
(
SELECT Parent_Proposal, MAX(Status_Time) AS Max_Status_Time
FROM proposals.status
GROUP BY Parent_Proposal
) s2
ON s1.Parent_Proposal = s2.Parent_Proposal AND s1.Status_Time = s2.Max_Status_Time
WHERE
p.Institution = 'T';
msyql query
SELECT id,student_user_id,MIN(start_time) FROM appoint_course
WHERE student_user_id IN(
931,2034,2068,2111,2115,2173,2181,2285,2500,2505,2507,
2518,2594,2596,2600,2608,2637,2652,2654
)
AND course_type=3 and disabled=0 GROUP BY student_user_id;
result
[query result]
+-------+-----------------+-----------------+
| id | student_user_id | MIN(start_time) |
+-------+-----------------+-----------------+
| 8356 | 931 | 1500351000 |
| 9205 | 2034 | 1501733400 |
| 9246 | 2068 | 1501649100 |
| 9755 | 2111 | 1502943000 |
| 9585 | 2115 | 1502595300 |
| 10820 | 2173 | 1503545700 |
| 9594 | 2181 | 1502852400 |
| 10324 | 2285 | 1502852400 |
| 11204 | 2500 | 1504839600 |
| 11152 | 2507 | 1504064100 |
| 12480 | 2594 | 1505707800 |
| 11521 | 2608 | 1504494000 |
| 11818 | 2652 | 1504753200 |
+-------+-----------------+-----------------+
but right start time is:
id: 9594
start_time: 1503284400
9594 right start_time is 1503284400 not 1502852400.In fact 1502852400 is a record of 9597
I do not know why.
In any other database your query would return an error, because id is not in the group by. The correct query is:
SELECT student_user_id, MIN(start_time)
FROM appoint_course
WHERE student_user_id IN (931,2034,2068,2111,2115,2173,2181,2285,2500,2505,2507,2518,2594,2596,2600,2608,2637,2652,2654) AND
course_type = 3 and disabled = 0
GROUP BY student_user_id;
In your case, adding a simple MIN(id) to the SELECT might work, assuming that ids increase with the start time.
More generally, you appear to want:
SELECT ac.*
FROM appoint_course ac
WHERE ac.student_user_id IN (931,2034,2068,2111,2115,2173,2181,2285,2500,2505,2507,2518,2594,2596,2600,2608,2637,2652,2654) AND
ac.course_type = 3 AND ac.disabled = 0 AND
ac.start_time = (SELECT MIN(ac2.start_time)
FROM appoint_course ac2
WHERE ac2.student_user_id = ac.student_user_id AND
ac2.course_type = ac.course_type AND
ac2.disabled = ac.disabled
);
No GROUP BY is necessary.
I should add that there is a MySQL hack that often works:
SELECT student_user_id, MIN(start_time),
SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY start_time), ',', 1) as id_at_min_start_time
FROM appoint_course
WHERE student_user_id IN (931,2034,2068,2111,2115,2173,2181,2285,2500,2505,2507,2518,2594,2596,2600,2608,2637,2652,2654) AND
course_type = 3 and disabled = 0
GROUP BY student_user_id;
This uses string manipulations and the GROUP_CONCAT() can overflow internal buffer sizes.
I have a Mysql table with the following data.
|ID | Date | BillNumber|BillMonth | Amount | Name |AccNum |
| 2 |2015-09-25| 454345 | 092015 | 135.00 |Andrew Good| 735976|
| 3 |2015-09-26| 356282 | 092015 | 142.00 |Peter Pan | 123489|
| 4 |2015-08-11| 312738 | 082015 | 162.00 |Andrew Good| 735976|
| 5 |2015-07-12| 287628 | 072015 | 220.67 |Andrew Good| 735976|
| 6 |2015-06-12| 100756 | 062015 | 556.34 |Andrew Good| 735976|
What I wanted to achieve is to retrieve the data of Andrew Good with AccNum 735976 for the BillMonth of 092015, provided that the user can entry any of his BillNumber(past/current).
If the reason that that row is of interest is because it is the latest of his rows, try:
select *
from tbl t
where name = ( select name
from tbl
where billnumber = 100756 -- can be any of his
)
and date = ( select max(date)
from tbl x
where x.name = t.name
)
(the billnumber can be any of his)