I have this:
SELECT A.salesperson, A.TDP, A.Units, T.new_target as Target
FROM
(
SELECT a.new_salespersonidname as Salesperson,
a.new_salespersonid,
SUM(b.new_profits_sales_totaldealprofit) as TDP,
COUNT (a.new_dealsheetid) as Units
FROM new_dealsheet a
LEFT JOIN salesorder B ON a.new_dsheetid = B.salesorderid
WHERE MONTH(a.New_actualdate) = 7
AND YEAR(a.new_actualdate) = 2016
AND a.New_PassedToAdmin = 1
GROUP BY a.new_salespersonidname, a.new_salespersonid
) A
LEFT JOIN new_salespersontarget T
on T.new_salespersonid = A.new_salespersonid
AND T.new_month = 7 and T.new_year = 2016 AND T.new_type = 2
ORDER BY A.TDP desc
This works fine and gives me the total sales for a month (July 2016) along with the salespersons target for the month.
I need a second version of this which shows me all 2016. So I can easily change:
WHERE MONTH(a.New_actualdate) = 7 AND YEAR(a.new_actualdate) = 2016
to:
WHERE YEAR(a.new_actualdate) = 2016
But, can I get the sum of all 2016 targets?
Thanks
just group by
SELECT A.salesperson, sum(A.TDP) as TDP, sum(A.Units) as Units, T.new_target as Target
FROM
....
LEFT JOIN new_salespersontarget T
on T.new_salespersonid = A.new_salespersonid
AND T.new_year = 2016 AND T.new_type = 2
GROUP BY A.salesperson, T.new_target
ORDER BY ...
Related
I am working on a custom stock valuation module and in one model I am trying to get adjustment value for a lot - product - warehouse wise of the previous day.
QUERY 1
SELECT COUNT(*)
FROM
(
SELECT stock_inventory.date AS stock_adjustment_date,
stock_move_line.lot_id,
stock_move_line.product_id,
SUM(stock_move_line.qty_done) total_stock_adjustment
FROM stock_move_line
LEFT JOIN stock_move ON stock_move_line.move_id = stock_move.id
LEFT JOIN stock_inventory ON stock_move.inventory_id = stock_inventory.id
WHERE stock_move.inventory_id IS NOT NULL
AND stock_move_line.location_id = 5
AND stock_move_line.location_dest_id = 13
AND stock_move_line.lot_id IS NOT NULL
GROUP BY stock_move_line.lot_id, stock_move_line.product_id, stock_inventory.date
ORDER BY total_stock_adjustment DESC
)
testTable;
QUERY 2
SELECT COUNT(*)
FROM
(
SELECT stock_inventory.date AS stock_adjustment_date,
stock_move_line.lot_id,
stock_move_line.product_id,
SUM(stock_move_line.qty_done) total_stock_adjustment
FROM stock_move_line
LEFT JOIN stock_move ON stock_move_line.move_id = stock_move.id
LEFT JOIN stock_inventory ON stock_move.inventory_id = stock_inventory.id
WHERE stock_move.inventory_id IS NOT NULL
AND stock_move_line.location_id = 13
AND stock_move_line.location_dest_id = 5
AND stock_move_line.lot_id IS NOT NULL
GROUP BY stock_move_line.lot_id, stock_move_line.product_id, stock_inventory.date
ORDER BY total_stock_adjustment DESC
)
testTable;
Why these both queries returning same count 14,849 ?
13 is the warehouse ID and 5 is the virtual location used for adjustment. What I am doing wrong here?
I've been trying hopelessly to get the following SQL statement to return the query results and default to 0 if there are no rows matching the query.
This is the intended result:
vol | year
-------+------
0 | 2018
Instead I get:
vol | year
-----+------
(0 rows)
Here is the sql statement:
select coalesce(vol,0) as vol, year
from (select sum(vol) as vol, year
from schema.fact_data
join schema.period_data
on schema.fact_data.period_tag = schema.period_data.tag
join schema.product_data
on schema.fact_data.product_tag =
schema.product_data.tag
join schema.market_data
on schema.fact_data.market_tag = schema.market_data.tag
where "retailer"='MadeUpRetailer'
and "product_tag"='FakeProductTag'
and "year"='2018' group by year
) as DerivedTable;
I know the query works because it returns data when there is data. Just doesn't default to 0 as intended...
Any help in finding why this is the case would be much appreciated!
Using your subquery DerivedTable, you could write:
SELECT coalesce(DerivedTable.vol, 0) AS vol,
y.year
FROM (VALUES ('2018'::text)) AS y(year)
LEFT JOIN (SELECT ...) AS DerivedTable
ON DerivedTable.year = y.year;
Remove the GROUP BY (and the outer query):
select 2018 as year, coalesce(sum(vol), 0) as vol
from schema.fact_data f join
schema.period_data p
on f.period_tag = p.tag join
schema.product_data pr
on f.product_tag = pr.tag join
schema.market_data m
on fd.market_tag = m.tag
where "retailer" = 'MadeUpRetailer' and
"product_tag" = 'FakeProductTag' and
"year" = '2018';
An aggregation query with no GROUP BY always returns exactly one row, so this should do what you want.
EDIT:
The query would look something like this:
select v.yyyy as year, coalesce(sum(vol), 0) as vol
from (values (2018), (2019)) v(yyyy) left join
schema.fact_data f
on f.year = v.yyyy left join -- this is just an example. I have no idea where year is coming from
schema.period_data p
on f.period_tag = p.tag left join
schema.product_data pr
on f.product_tag = pr.tag left join
schema.market_data m
on fd.market_tag = m.tag
group by v.yyyy
However, you have to move the where conditions to the appropriate on clauses. I have no idea where the columns are coming from.
From the code you posted it is not clear in which table you have the year column.
You can use UNION to fetch just 1 row in case there are no rows in that table for the year 2018 like this:
select sum(vol) as vol, year
from schema.fact_data innrt join schema.period_data
on schema.fact_data.period_tag = schema.period_data.tag
inner join schema.product_data
on schema.fact_data.product_tag = schema.product_data.tag
inner join schema.market_data
on schema.fact_data.market_tag = schema.market_data.tag
where
"retailer"='MadeUpRetailer' and
"product_tag"='FakeProductTag' and
"year"='2018'
group by "year"
union
select 0 as vol, '2018' as year
where not exists (
select 1 from tablename where "year" = '2018'
)
In case there are rows for the year 2018, then nothing will be fetched by the 2nd query,
I want to display result of 3 selects together in one window, in the same time.
First:
SELECT sum(r.punkty)
,r.id_kierowcy
FROM wyniki AS r
INNER JOIN wyscigi AS w ON r.id_wyscigu = w.id_wyscigu
WHERE w.rok = 2006
GROUP BY r.id_kierowcy
ORDER BY sum(r.punkty) DESC limit 1
Second:
SELECT sum(r.punkty)
,r.id_kierowcy
FROM wyniki AS r
INNER JOIN wyscigi AS w ON r.id_wyscigu = w.id_wyscigu
WHERE w.rok = 2000
GROUP BY r.id_kierowcy
ORDER BY sum(r.punkty) DESC limit 1
Third:
SELECT sum(r.punkty)
,r.id_kierowcy
FROM wyniki AS r
INNER JOIN wyscigi AS w ON r.id_wyscigu = w.id_wyscigu
WHERE w.rok = 2012
GROUP BY r.id_kierowcy
ORDER BY sum(r.punkty) DESC limit 1
In brief there is one difference in the selects, that is WHERE [2000, 2006, 2012]
Is the way to connect these selects? I must display them as one result set
EDIT: I will try to explain my exercise.
I have 2 tables:
Results and races.
Results contains 3 columns:
id_driver
/points
/id_race
Races contains 3 columns:
id_race
/year
/place
Races are between 2000 and 2012 years
I need to do classification of races in the 2000, 2006, 2012 and establish which one driver(id_driver) won each year. So I'm trying to do like that
SELECT sum(r.points)
,r.id_driver
FROM results AS r
INNER JOIN races AS x ON r.id_race = x.id_race
WHERE x.year = 2012
GROUP BY r.id_driver
ORDER BY sum(r.points) DESC limit 1
and the same for 2006 and 2000, but i need to display that as one result set, that is my problem I dont know how to connect it.
Intro Details:
I have an issue with a query for Oracle SQL Developer using client 12c. I've researched several other questions on SO as well as searched Google for an answer to this, but ultimately the answers to all have been to include all columns without aggregate functions in the GROUP BY clause.
What I want is to get a single result for each category (PG.CAT) per year, week, and day (FD.YEAR, FD.WEEK, and FD.DT respectively). I want to sum the units, hours, errors (GE.QTY), and total hours. I also perform multiplication and division on two columns and join up to four other tables.
Query:
`SELECT
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
SUM(PG.UNITS) AS UNITS,
SUM(PG.HOURS) AS HOURS,
PM.MTM,
PM.MTM * PG.HOURS AS ADJ_MTM,
(PG.UNITS / (PM.MTM * PG.HOURS)) AS PERC_STANDARD,
SUM(CASE WHEN GE.QTY IS NULL THEN 0 ELSE GE.QTY END) AS QTY,
SUM(WH.TOTALHOURS) AS TOTALHOURS
FROM
PROD_GUNS PG
INNER JOIN PROD_MTM PM
ON PG.CAT = PM.CATEGORY
AND PM.DEPTNO = '018'
AND PG.DT BETWEEN PM.START_DT AND PM.END_DT
INNER JOIN FISCAL_DATES_DAYS FD
ON PG.DT = FD.DT
LEFT OUTER JOIN PROD_GUNS_ERRORS GE
ON PG.EID = GE.EID
AND PG.DT = GE.DT
INNER JOIN WEEKLY_HOURS WH
ON FD.WEEK = WH.DT_WEEK
AND FD.YEAR = WH.DT_YEAR
AND PG.EID = WH.EEXX31
GROUP BY
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
PM.MTM,
PM.MTM * PG.HOURS,
(PG.UNITS / ( PM.MTM * PG.HOURS))
HAVING
FD.YEAR = '2015'
AND FD.WEEK = '1'
AND PG.DT = '29-DEC-14'
AND PG.CAT = 'Picking'
ORDER BY
PG.DT;`
Actual Result:
2015 1 29-DEC-14 Picking 46 0.5 68 34 1.35294117647058823529411764705882352941 0 32.21
2015 1 29-DEC-14 Picking 831 7.72 68 524.96 1.58297775068576653459311185614142029869 0 29.35
Intended Result:
2015 1 20-Dec-14 Picking 877 8.22 68 558.96 1.21654501216545 0 61.59
Question:
With the aggregates and grouping that I have above, why would this not be giving me the intended result? Thank you all in advance for any guidance provided.
Try to SUM/AVG (depending on what you need) PM.MTM * PG.HOURS AS ADJ_MTM and (PG.UNITS / (PM.MTM * PG.HOURS)) AS PERC_STANDARD, not group by them:
SELECT
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
SUM(PG.UNITS) AS UNITS,
SUM(PG.HOURS) AS HOURS,
PM.MTM,
SUM(PM.MTM * PG.HOURS )AS ADJ_MTM,
SUM((PG.UNITS / (PM.MTM * PG.HOURS))) AS PERC_STANDARD,
SUM(CASE WHEN GE.QTY IS NULL THEN 0 ELSE GE.QTY END) AS QTY,
SUM(WH.TOTALHOURS) AS TOTALHOURS
FROM
PROD_GUNS PG
INNER JOIN PROD_MTM PM
ON PG.CAT = PM.CATEGORY
AND PM.DEPTNO = '018'
AND PG.DT BETWEEN PM.START_DT AND PM.END_DT
INNER JOIN FISCAL_DATES_DAYS FD
ON PG.DT = FD.DT
LEFT OUTER JOIN PROD_GUNS_ERRORS GE
ON PG.EID = GE.EID
AND PG.DT = GE.DT
INNER JOIN WEEKLY_HOURS WH
ON FD.WEEK = WH.DT_WEEK
AND FD.YEAR = WH.DT_YEAR
AND PG.EID = WH.EEXX31
GROUP BY
FD.YEAR,
FD.WEEK,
PG.DT,
PG.CAT,
PM.MTM
HAVING
FD.YEAR = '2015'
AND FD.WEEK = '1'
AND PG.DT = '29-DEC-14'
AND PG.CAT = 'Picking'
ORDER BY
PG.DT;
So far I have this query
SELECT
COUNT(f.code_id) as item_count,
f.code_desc
FROM
foo f
INNER JOIN foohistory fh ON f.history_id = fh.history_id
WHERE
MONTH(fh.create_dt) = 6
AND YEAR(fh.create_dr) = 2010
GROUP BY
f.code_desc
UNION ALL
SELECT
COUNT(b.code_id) as item_count,
b.code_desc
FROM
bar b
INNER JOIN barhistory bh ON b.history_id = bh.history_id
WHERE
MONTH(bh.create_dt) = 6
AND YEAR(bh.create_dr) = 2010
GROUP BY
b.code_desc
My goal is to UNION these two queries add SUM the 'item_count' columns foreach code_desc. Is this possible?
Without more information about the codes, like if it's possible that codes are mutually exclusive between the two tables, use:
SELECT x.code_desc,
SUM(x.item_count)
FROM (SELECT f.code_desc,
COUNT(f.code_id) as item_count
FROM foo f
JOIN foohistory fh ON f.history_id = fh.history_id
WHERE MONTH(fh.create_dt) = 6
AND YEAR(fh.create_dr) = 2010
GROUP BY f.code_desc
UNION ALL
SELECT b.code_desc,
COUNT(b.code_id) as item_count
FROM bar b
JOIN barhistory bh ON b.history_id = bh.history_id
WHERE MONTH(bh.create_dt) = 6
AND YEAR(bh.create_dr) = 2010
GROUP BY b.code_desc) x
GROUP BY x.code_desc
Yeah, doing something like this
SELECT Sum(unionedTable.item_count)
FROM
(
//your query
) as unionedTable