How would you round the following output to 3 decimal places in Vertica using ROUND(). I get an error, "Relationship TOTAL does not exist"
WITH TOTAL AS
(
SELECT
SUM(ADV_CPC) AS TOTAL_SPEND,
COUNT(DISTINCT cc.ID) AS TOTAL_CLICKS,
COUNT(DISTINCT cv.CLICK_ID) AS TOTAL_CONVERSIONS
FROM
clickcache.click cc
LEFT JOIN clickcache.CONVERSION cv ON
cv.CLICK_ID = cc.ID
WHERE
cc.ADV_ACCOUNT_ID = 14102
AND AMP_CLICK_DAY = '07-06-2016'
AND AMP_CLICK_STATUS_ID = 1
)
SELECT
ROUND( COUNT(DISTINCT cc.ID) / (SELECT TOTAL_CLICKS FROM TOTAL), 3.0) * 100 || '%' AS CLICKS_BY_AGENT
Related
I need select values from a table and returns the total hours for all categories and their occurrences. The challenge is that there are different totals for each occurrence.
My query:
SELECT c.Category,
c.HrsFirstOccur,
c.HrsAddlOccur,
COUNT(*) AS Occurrences
FROM dbo.Categories sc
INNER JOIN dbo.Categories c
ON sc.CategoryID = c.CategoryID
INNER JOIN dbo.OrderHistory oh
ON sc.GONumber = oh.OrderNumber
AND sc.Item = oh.ItemNumber
WHERE sc.BusinessGroupID = 1
AND oh.OrderNumber = 500
AND oh.ItemNumber = '100'
GROUP BY c.Category, c.HrsFirstOccur, c.HrsAddlOccur
returns the following results:
Category
HrsFirstOccur
HrsAddlOccur
Occurrences
Inertia
24
16
2
Lights
1
0.5
4
Labor
10
0
1
The total is calculated based on the number of occurrences. The first one is totaled then for each additional occurrence, the HrsAddlOccur is used.
My final result should be (24 + 16) + (1 + 0.5 + 0.5 + 0.5) + 10 for a grand total of 52.5.
How do I loop and process the results to total this up?
The total is calculated based on the number of occurrences. The first one is totaled then for each additional occurrence, the HrsAddlOccur is used.
SQL databases understand arithmetic. You can perform the computation on each row. As I understand, the logic you want is:
SELECT
c.Category,
c.HrsFirstOccur,
c.HrsAddlOccur,
COUNT(*) AS Occurrences,
c.HrsFirstOccur + ( COUNT(*) - 1 ) * HrsAddlOccur As Total
FROM ... < rest of your query > ..
Later on you can aggregate the whole resultset to get the grand total:
SELECT SUM(Total) GrandTotal
FROM (
... < above query > ..
) t
you can sum them simply up
WITH CTE as(SELECT c.Category,
c.HrsFirstOccur,
c.HrsAddlOccur,
COUNT(*) AS Occurrences
FROM dbo.Categories sc
INNER JOIN dbo.Categories c ON sc.CategoryID = c.CategoryID
INNER JOIN dbo.OrderHistory oh ON sc.GONumber = oh.OrderNumber
AND sc.Item = oh.ItemNumber
WHERE sc.BusinessGroupID = 1
AND oh.OrderNumber = 500
AND oh.ItemNumber = '100')
SELECT SUM(HrsFirstOccur + (CAST((Occurrences -1) AS DECIMAL(8,2)) * HrsAddlOccur)) as total FROM CTE
it would do it like the example
CREATE TABLE CTE
([Category] varchar(7), [HrsFirstOccur] int, [HrsAddlOccur] DECIMAL(8,2), [Occurrences] int)
;
INSERT INTO CTE
([Category], [HrsFirstOccur], [HrsAddlOccur], [Occurrences])
VALUES
('Inertia', 24, 16, 2),
('Lights', 1, 0.5, 4),
('Labor', 10, 0, 1)
;
3 rows affected
SELECT SUM(HrsFirstOccur + (CAST((Occurrences -1) AS DECIMAL(8,2)) * HrsAddlOccur)) as total
FROM CTE
total
52.5000
fiddle
I need SUM of this result:
SELECT
ROUND(
(invoicesitems.pscost / invoicesitems.inmedunit -
AVG(ISNULL(mainexstock.peacecost,0)) / invoicesitems.inmedunit), 2)
*
CASE WHEN units.unitqty=3 THEN (invoicesitems.bigqty *
invoicesitems.inbigunit * invoicesitems.inmedunit) +
(invoicesitems.medqty * invoicesitems.inmedunit) +
invoicesitems.smallqty
ELSE (invoicesitems.bigqty * invoicesitems.inmedunit)
+ invoicesitems.smallqty
END AS PROFITS
FROM invoicesitems
INNER JOIN mainexstock ON mainexstock.pid = invoicesitems.pid
INNER JOIN units ON units.[uid] = invoicesitems.punits
WHERE invoicesitems.bid = 'B-0480580'
GROUP BY
invoicesitems.pid, invoicesitems.inbigunit,
invoicesitems.inmedunit, invoicesitems.bigqty,
invoicesitems.medqty, invoicesitems.smallqty,
invoicesitems.pscost, units.unitqty
You can use it as a CTE and get the sum. For example:
with a as (
-- your big query here
)
select sum(profits) as profits from a;
I have a table with few columns and I am trying to concatenate the code column with rank. But it throws me an error by saying code not found, this is in the concatenation I am doing with rank and code. The code column is a calculated column as you can see from the query. Could anyone please point me where I am doing a mistake?
table mm
ID cal_dtm rank milestone_hier_onb tr
CNT 1/31/2020 1 RFR EEZ
table dd
ID frs lst frst lst_s cal
CNT 6/20/2018 6/28/2018 6/28/2018 1/31/2020
Query
WITH r
AS (
SELECT dd.ID,
dd.frs,
dd.lst,
dd.frst,
dd.lst_s,
dd.cal_dtm,
mm.tr,
mm.ipf_rank,
mm.milestone_hier_onb
FROM dos dd
LEFT JOIN plw mm ON dd.ID = mm.ID
AND dd.cal_dtm = mm.cal_dtm
)
SELECT *
FROM (
SELECT r.ID,
r.cal_dtm,
'lp' AS code,
r.lst_s AS planned,
r.lst AS actual,
r.tr,
r.ipf_rank AS rank,
r.milestone_hier_onb AS onb,
* * CONCAT (
r.ipf_rank,
code
) AS milestone_label * *
FROM r
WHERE r.lst IS NOT NULL
UNION
SELECT r.ID,
r.cal_dtm,
'fp' AS code,
r.frst AS planned,
r.frs AS actual,
r.tr,
r.ipf_rank AS rank,
r.milestone_hier_onb AS onb,
* * CONCAT (
r.ipf_rank,
code
) AS milestone_label * *
FROM r
WHERE r.frs IS NOT NULL
)
ORDER BY ID,
cal_dtm,
code
Expected output
ID Cal code pl Al tr milestone_label rank onb
CNT 1/31/2020 lp 6/28/2018 6/28/2018 lp 1 1
CNT 1/31/2020 fp 6/20/2018
You can't use CODE like that; it is a constant so - concatenate that constant, e.g.
... 'lp' as code,
r.ipf_rank || 'lp' as milestone_label
...
How to add space?
r.ipf_rank ||' '|| 'lp' as milestone_label
or
r.ipf_rank ||' lp' as milestone_label
Let try:
remove order by
add alias **concat( r.ipf_rank, code) => **concat( r.ipf_rank, r.code)
The odr cte gets num where code is 1. The adv cte gets num where code is 2. comm and medi cte's do the same where code is 3 and 4 respectively. The total cte gets counts of all nums regardless of the code.
with odr (num, odr_count) as
(
SELECT num, count(*)
FROM pay t
where
code = '1'
group by num
),
adv (num, adv_count) as
(
SELECT num, count(*)
FROM pay t
where
code = '2'
group by num
),comm (num, comm_count) as
(
SELECT num, count(*)
FROM pay t
where
code = '3'
group by num
),medi (num, medi_count) as
(
SELECT num, count(*)
FROM pay t
where
code = '4'
or
code = '5'
group by num
),
total (num, tot_count) as
(
SELECT num, count(*)
FROM pay t
group by num
)
select t.num, tot_count, o.num,
o.odr_count, adv.num, adv_count,
c.num, comm_count,
medi.num, medi_count
FROM total t
left join odr o
on t.num = o.num
left join adv
on o.num = adv.num
left join comm c
on medicaid.num = c.num
left join medi
on c.num = medi.num
This is the output of this query -
Num tot_count num odr_count adv.num adv_count c.num comm_count medi.num medi_count
14476 10
15082 156
Why do all the other columns not have data?
I would expect a result like this
Num tot_count num odr_count adv.num adv_count c.num comm_count medi.num medi_count
14476 10 14476 35345 14476 234 14476 343246 14476 8
15082 156 15082 4354 NULL NULL 15082 3432
I am expecting NULL and empty columns in the second row because 15082 does not have all the codes from 1 through 4.
in SQL when I want to compute Sum of one column that is also Sum base on grouping, the total value is not correct.here I want to compute sum of Mand that is Sum(Qty) but the final result isn't correct.
Select Sum(Mand) from (Select TrackingFactor1 As Number ,SUM(Qty) As Mand from(
Select t.TrackingFactor1,SUM(
CASE
WHEN Direction = 1 THEN t.MajorUnitQuantity
WHEN Direction = 2 THEN -t.MajorUnitQuantity
ELSE 0
END
) AS Qty,
ROW_NUMBER() OVER(ORDER BY i.Date, i.VoucherCreationDate, v.InventoryVoucherID, i.InventoryVoucherItemID) AS
RowNumber
from LGS3.InventoryVoucher AS v
INNER JOIN LGS3.InventoryVoucherItem i ON v.InventoryVoucherID = I.InventoryVoucherRef
LEFT OUTER JOIN LGS3.InventoryVoucherItemTrackingFactor t ON i.InventoryVoucherItemID = t.InventoryVoucherItemRef
GROUP BY
v.InventoryVoucherID,
v.Number,
i.Date,
i.VoucherCreationDate,
i.InventoryVoucherItemID,
i.CounterpartEntityText,
t.TrackingFactor1 ,
v.FiscalYearRef
)As A
group by TrackingFactor1
Having SUM(Qty)>0) As AA