Converting a google sheet formula into a custom function - google-apps-script

This is a real working formula in my google sheet. I have 5 cells in which the user chooses values from data validated drop down lists. This formula then chooses the sheet to filter then filters on those values. Obviously, my issue is speed. So I'm hoping if I create a custom function or functions I will improve performance of my calculations. My question is, what would be the best way to proceed. create one function or should I break it down into the various functions and build a formula from multiple custom functions. Will I see an improvement? I'm also considering not setting the formula until the user clicks an image button (to avoid the formula constantly recalculating while the user is making their picks.
=if(E4= "View All Contacts", filter('View All Contacts'!$A$1:$L$3999,if(I8<>"",'View All Contacts'!A:A =I8,'View All Contacts'!A:A <>""),if(E7<>"",'View All Contacts'!M:M =E7,'View All Contacts'!A:A <>""),if(E9<>"",'View All Contacts'!O:O =E9,if(E8 = "All Mile Stone Anniversaries",('View All Contacts'!N:N = (year(NOW()) - 1 )) + ('View All Contacts'!N:N = (year(Now()) - 5 ) )+ ('View All Contacts'!N:N = (year(Now()) - 10 ) )+ ('View All Contacts'!N:N = (year(Now()) - 15 ) )+ ('View All Contacts'!N:N = (year(Now()) - 20 )) + ('View All Contacts'!N:N = (year(Now()) - 25 ) )+ ('View All Contacts'!N:N = (year(Now()) - 30 ) )+ ('View All Contacts'!N:N = (year(Now()) - 40 ) )+ ('View All Contacts'!N:N = (year(Now()) - 50 )) + ('View All Contacts'!N:N = (year(Now()) - 60 ) )+ ('View All Contacts'!N:N = (year(Now()) - 70 )) + ('View All Contacts'!N:N = (year(Now()) - 75 ) )+ ('View All Contacts'!N:N = (year(Now()) - 80 ) )+ ('View All Contacts'!N:N = (year(Now()) - 90 )) + ('View All Contacts'!N:N = (year(Now()) - 100 )) + ('View All Contacts'!N:N = (year(Now()) - 125 ) )+ ('View All Contacts'!N:N = (year(Now()) - 150 ) )+ ('View All Contacts'!N:N = (year(Now()) - 175 )) + ('View All Contacts'!N:N = (year(Now()) - 200 )),If(E8 = ">100", ('View All Contacts'!N:N < (year(Now())) - 100) * ('View All Contacts'!N:N <>""), If(E8 >0, 'View All Contacts'!N:N = (year(NOW()) - E8 ),'View All Contacts'!A:A <>""))))),if(E4 = "View Active Contacts",filter('View Active Contacts'!$A$1:$L$3999,if(I8<>"",'View Active Contacts'!A:A =I8,'View Active Contacts'!A:A <>""),if(E7<>"",'View Active Contacts'!M:M =E7,'View Active Contacts'!A:A <>""),if(E9<>"",'View Active Contacts'!O:O =E9,if(E8 = "All Mile Stone Anniversaries",('View Active Contacts'!N:N = (year(NOW()) - 1 )) + ('View Active Contacts'!N:N = (year(Now()) - 5 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 10 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 15 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 20 )) + ('View Active Contacts'!N:N = (year(Now()) - 25 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 30 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 40 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 50 )) + ('View Active Contacts'!N:N = (year(Now()) - 60 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 70 )) + ('View Active Contacts'!N:N = (year(Now()) - 75 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 80 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 90 )) + ('View Active Contacts'!N:N = (year(Now()) - 100 )) + ('View Active Contacts'!N:N = (year(Now()) - 125 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 150 ) )+ ('View Active Contacts'!N:N = (year(Now()) - 175 )) + ('View Active Contacts'!N:N = (year(Now()) - 200 )),If(E8 = ">100", ('View Active Contacts'!N:N < (year(Now())) - 100) * ('View Active Contacts'!N:N <>""), If(E8 >0, 'View Active Contacts'!N:N = (year(NOW()) - E8 ),'View Active Contacts'!A:A <>""))))),if(E4 = "View Contacts from Companies Pending Current Payments",filter('View Contacts from Companies Pending Current Payments'!$A$1:$L$3999,if(I8<>"",'View Contacts from Companies Pending Current Payments'!A:A =I8,'View Contacts from Companies Pending Current Payments'!A:A <>""),if(E7<>"",'View Contacts from Companies Pending Current Payments'!M:M =E7,'View Contacts from Companies Pending Current Payments'!A:A <>""),if(E9<>"",'View Contacts from Companies Pending Current Payments'!O:O =E9,if(E8 = "All Mile Stone Anniversaries",('View Contacts from Companies Pending Current Payments'!N:N = (year(NOW()) - 1 )) + ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 5 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 10 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 15 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 20 )) + ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 25 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 30 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 40 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 50 )) + ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 60 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 70 )) + ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 75 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 80 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 90 )) + ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 100 )) + ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 125 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 150 ) )+ ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 175 )) + ('View Contacts from Companies Pending Current Payments'!N:N = (year(Now()) - 200 )),If(E8 = ">100", ('View Contacts from Companies Pending Current Payments'!N:N < (year(Now())) - 100) * ('View Contacts from Companies Pending Current Payments'!N:N <>""), If(E8 >0, 'View Contacts from Companies Pending Current Payments'!N:N = (year(NOW()) - E8 ),'View Contacts from Companies Pending Current Payments'!A:A <>""))))),if(E4 = "View Inactive Companies Contacts",filter('View Inactive Companies Contacts'!$A$1:$L$3999,if(I8<>"",'View Inactive Companies Contacts'!A:A =I8,'View Inactive Companies Contacts'!A:A <>""),if(E7<>"",'View Inactive Companies Contacts'!M:M =E7,'View Inactive Companies Contacts'!A:A <>""),if(E9<>"",'View Inactive Companies Contacts'!O:O =E9,if(E8 = "All Mile Stone Anniversaries",('View Inactive Companies Contacts'!N:N = (year(NOW()) - 1 )) + ('View Inactive Companies Contacts'!N:N = (year(Now()) - 5 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 10 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 15 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 20 )) + ('View Inactive Companies Contacts'!N:N = (year(Now()) - 25 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 30 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 40 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 50 )) + ('View Inactive Companies Contacts'!N:N = (year(Now()) - 60 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 70 )) + ('View Inactive Companies Contacts'!N:N = (year(Now()) - 75 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 80 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 90 )) + ('View Inactive Companies Contacts'!N:N = (year(Now()) - 100 )) + ('View Inactive Companies Contacts'!N:N = (year(Now()) - 125 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 150 ) )+ ('View Inactive Companies Contacts'!N:N = (year(Now()) - 175 )) + ('View Inactive Companies Contacts'!N:N = (year(Now()) - 200 )),If(E8 = ">100", ('View Inactive Companies Contacts'!N:N < (year(Now())) - 100) * ('View Inactive Companies Contacts'!N:N <>""), If(E8 >0, 'View Inactive Companies Contacts'!N:N = (year(NOW()) - E8 ),'View Inactive Companies Contacts'!A:A <>""))))),if(E4 = "View Contacts from Companies with Expired Memberships",filter('View Contacts from Companies with Expired Memberships'!$A$1:$L$3999,if(I8<>"",'View Contacts from Companies with Expired Memberships'!A:A =I8,'View Contacts from Companies with Expired Memberships'!A:A <>""),if(E7<>"",'View Contacts from Companies with Expired Memberships'!M:M =E7,'View Contacts from Companies with Expired Memberships'!A:A <>""),if(E9<>"",'View Contacts from Companies with Expired Memberships'!O:O =E9,if(E8 = "All Mile Stone Anniversaries",('View Contacts from Companies with Expired Memberships'!N:N = (year(NOW()) - 1 )) + ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 5 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 10 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 15 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 20 )) + ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 25 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 30 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 40 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 50 )) + ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 60 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 70 )) + ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 75 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 80 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 90 )) + ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 100 )) + ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 125 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 150 ) )+ ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 175 )) + ('View Contacts from Companies with Expired Memberships'!N:N = (year(Now()) - 200 )),If(E8 = ">100", ('View Contacts from Companies with Expired Memberships'!N:N < (year(Now())) - 100) * ('View Contacts from Companies with Expired Memberships'!N:N <>""), If(E8 >0, 'View Contacts from Companies with Expired Memberships'!N:N = (year(NOW()) - E8 ),'View Contacts from Companies with Expired Memberships'!A:A <>""))))),if(E4 = "View Contacts from Potential Companies",filter('View Contacts from Potential Companies'!$A$1:$L$3999,if(I8<>"",'View Contacts from Potential Companies'!A:A =I8,'View Contacts from Potential Companies'!A:A <>""),if(E7<>"",'View Contacts from Potential Companies'!M:M =E7,'View Contacts from Potential Companies'!A:A <>""),if(E9<>"",'View Contacts from Potential Companies'!O:O =E9,if(E8 = "All Mile Stone Anniversaries",('View Contacts from Potential Companies'!N:N = (year(NOW()) - 1 )) + ('View Contacts from Potential Companies'!N:N = (year(Now()) - 5 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 10 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 15 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 20 )) + ('View Contacts from Potential Companies'!N:N = (year(Now()) - 25 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 30 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 40 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 50 )) + ('View Contacts from Potential Companies'!N:N = (year(Now()) - 60 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 70 )) + ('View Contacts from Potential Companies'!N:N = (year(Now()) - 75 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 80 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 90 )) + ('View Contacts from Potential Companies'!N:N = (year(Now()) - 100 )) + ('View Contacts from Potential Companies'!N:N = (year(Now()) - 125 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 150 ) )+ ('View Contacts from Potential Companies'!N:N = (year(Now()) - 175 )) + ('View Contacts from Potential Companies'!N:N = (year(Now()) - 200 )),If(E8 = ">100", ('View Contacts from Potential Companies'!N:N < (year(Now())) - 100) * ('View Contacts from Potential Companies'!N:N <>""), If(E8 >0, 'View Contacts from Potential Companies'!N:N = (year(NOW()) - E8 ),'View Contacts from Potential Companies'!A:A <>""))))),""))))))

It's better if you'll break it down into the various functions and build a formula from multiple custom functions. As you've said, the performance will be slow if you're working on a sheet with lots of filter formulas.
Here are some references which may help:
https://www.quora.com/How-do-I-set-up-a-function-in-a-Google-Sheets-cell-to-take-the-input-value-as-variable
Using a Custom Function in a Google Spreadsheet?
https://productforums.google.com/forum/#!topic/docs/0uUV9PKSVpU

Yikes! My eyes glaze just looking at that formula. I really can't figure out what you are trying to do. In general a spreadsheet formula more than one line long is a write-only language.
Break it down into chunks. You can create multiple sheets with various stages of calculation. Generally there isn't a performance penalty for splitting this up.
Avoid constructs of the form
if(E4= "View All Contacts"...
Instead use
if (E4=C2, filter(FilterRange,C2)
or if your tag isn't the same as what you are filtering...
if(E4= C2, filter(vlookup(FilterRange,C2,2)...
Filter can take multiple ranges that have to evaluate to true or false.
This allows you to set up the matching conditions outside of the formula.
The pink box uses a regexmatch. This is very powerful, but can be slow on big arrays.
The green box uses a simple match, faster, but limited to only one item
The orange box shows the results: Either Critter starts with a or A OR class is fish.
By using Named ranges throughout, I have a MUCH more maintainable spreadsheet. Named ranges do the Right Thing most of the time when you expand them. If you used a named range in multiple places, you only have to edit it once.
It's good practice to put auxiliary tables on a separate tab, at least out of sight of users (including yourself.
You can do more of the logic in the auxiliary sheet. E.g. Add a 3rd range after fish with a an arrayformula (OR(FTest_CritFilt,FTest_ClassFilt))
When you are creating this sheet, you can have it open in two browser windows with the view on different tabs.

Related

Count number of users that purchased in certain stores at certain dates

I have two tables:
"purchases" table has 3 columns: "user_id", "store_id", "purchase_date"
(so I can know that client_id=XXX made a purchase in store_id=YYY on
date=YYYY-MM-DD).
"stores" table has "store_id", "latitude", "longitude", "date" (so at that date=YYYY-MM-DD, the store_id=YYY was located in latitude=AAA and longitude=BBB).
I would like to count the number of clients that purchased in stores located at certain geographical regions at some date, and another region in another date.
For example, I want to count the number of users that purchased in stores in USA in May 2022 and also purchased in stores in Japan during June 2022.
I tried this:
SELECT count(distinct user)
FROM (
"purchases" WHERE store_id IN (
SELECT store_id FROM "stores" WHERE ( latitude BETWEEN 23 AND 50 ) AND ( longitude BETWEEN -127 AND -66 ) AND ( date BETWEEN '2022-05-01' AND '2022-05-31'
)
AND store_id IN (
SELECT store_id FROM "stores" WHERE ( latitude BETWEEN 30 AND 45 ) AND ( longitude BETWEEN 130 AND 150 ) AND ( date BETWEEN '2022-06-01' AND '2022-06-30'
)
)
But does not work, I don't know how to do it...
Use 2 levels of aggregation:
SELECT COUNT(*) count
FROM (
SELECT p.user_id
FROM purchases p INNER JOIN stores s
ON s.store_id = p.store_id
GROUP BY p.user_id
HAVING SUM(s.latitude BETWEEN 23 AND 50 AND s.longitude BETWEEN -127 AND -66 AND p.purchase_date BETWEEN '2022-05-01' AND '2022-05-31') > 0
AND SUM(s.latitude BETWEEN 30 AND 45 AND s.longitude BETWEEN 130 AND 150 AND p.purchase_date BETWEEN '2022-06-01' AND '2022-06-30') > 0
) t;
Step by step with the help of CTEs (WITH clauses):
with us_stores as
(
SELECT *
FROM stores
WHERE latitude BETWEEN 23 AND 50
AND longitude BETWEEN -127 AND -66
)
, jp_stores as
(
SELECT *
FROM stores
WHERE latitude BETWEEN 30 AND 45
AND longitude BETWEEN 130 AND 150
)
, us_purchasers as
(
select distinct user_id
from purchases
where store_id in (select store_id from us_stores)
and date >= date '2022-05-01'
and date < date '2022-06-01'
)
, jp_purchasers as
(
select distinct user_id
from purchases
where store_id in (select store_id from jp_stores)
and date >= date '2022-06-01'
and date < date '2022-07-01'
)
select count(*)
from us_purchasers
join jp_purchasers using (user_id);

SIngle Query from Multiple Table MySql

I have tables that contain same field, for example:
p_central_ticket p_south_ticket p_west_ticket
=====================================================================
- t_id - t_id - t_id
- t_open_by - t_open_by - t_open_by
- t_closed_by - t_closed_by - t_closed_by
- t_open_time - t_open_time - t_open_time
- t_closed_time - t_closed_time - t_closed_time
One thing that i expect is output just like this, but definitely for 3 table above in single query:
Name today weekly monthly yearly
=================================================================
test1#random.com 2 10 70 1000
test2#random.com 5 14 60 1234
But, my query right now just for calculate 1 table.
SELECT t_closed_by As Username, ixt_user_type.user_owner As Role,
COUNT( case when t_closed_time > curdate() - interval 1 day THEN 1 END ) as today,
COUNT( case when t_closed_time > curdate() - interval 7 day THEN 1 END ) as weekly,
COUNT( case when t_closed_time > curdate() - interval 1 month THEN 1 END ) as monthly,
COUNT( case when t_closed_time > curdate() - interval 1 year THEN 1 END ) as yearly
FROM p_central_ticket
LEFT JOIN m_event_type ON p_central_ticket.t_req_type = m_event_type.ev_type
LEFT JOIN ixt_user_type ON m_event_type.ev_user_type_target = ixt_user_type.user_type
WHERE t_status = 9
GROUP BY t_closed_by;
My question is, how should i do, to make my query calculate from 3 tables but in single query ?
You can use UNION to merge three tables and apply all your join on that merged table like below -
SELECT t_closed_by As Username, ixt_user_type.user_owner As Role,
COUNT( case when t_closed_time > curdate() - interval 1 day THEN 1 END ) as today,
COUNT( case when t_closed_time > curdate() - interval 7 day THEN 1 END ) as weekly,
COUNT( case when t_closed_time > curdate() - interval 1 month THEN 1 END ) as monthly,
COUNT( case when t_closed_time > curdate() - interval 1 year THEN 1 END ) as yearly
FROM
( select * from p_central_ticket
union
select * from p_south_ticket
union
select * from p_west_ticket
)A
LEFT JOIN m_event_type ON A.t_req_type = m_event_type.ev_type
LEFT JOIN ixt_user_type ON m_event_type.ev_user_type_target = ixt_user_type.user_type
WHERE t_status = 9
GROUP BY t_closed_by

SQL - Limit all Except TOP 6

the SQL I got at the moment and it is working :
SELECT Count(CATEGORY), COUNT(INCIDENT_ID), CATEGORY,
ROUND(Count(CATEGORY) * 100 / (SELECT Count(*)
FROM incident_view
WHERE
( create_month = Month(Now() -
INTERVAL 1 month) )
AND ( create_year = Year(
Now() - INTERVAL 1 month) )
AND customer_company_name = "Company"
), 1) AS Percentage
FROM incident_view
WHERE ( create_month = Month(Now() - INTERVAL 1 month) )
AND ( create_year = Year(Now() - INTERVAL 1 month) )
AND customer_company_name = "Company"
GROUP BY CATEGORY
ORDER BY COUNT(INCIDENT_ID) DESC
limit 6
This query results in the top 6 Categorys and their number of Incidents with the Percentage.
Now I want to generate a second query, which shows me all Category and their Total Number + Percentage , !EXCEPT! the 6 Category I retrieved in the first query.
So if I have let's say 20 Categorys, I now want number 7-20 Displayed, under one name "Other" and the total Number Sumed up of all Categories (7-20) + their Percentage.
The result should look like this for example:
Category| Number of Inicdents | Percentage
Other 200 20%
Other should Contain all categories, except the from the first query..
Is this Possible?
Would appreciate any help.
------------EDIT-------------
SELECT Count(CATEGORY), COUNT(INCIDENT_ID), CATEGORY,
ROUND(Count(CATEGORY) * 100 / (SELECT Count(*)
FROM incident_view
WHERE
( create_month = Month(Now() -
INTERVAL 1 month) )
AND ( create_year = Year(
Now() - INTERVAL 1 month) )
AND customer_company_name = "Company"
), 1) AS Percentage
FROM incident_view
WHERE ( create_month = Month(Now() - INTERVAL 1 month) )
AND ( create_year = Year(Now() - INTERVAL 1 month) )
AND customer_company_name = "Company"
GROUP BY CATEGORY
ORDER BY COUNT(INCIDENT_ID)DESC
limit 6, 1295852105
Cheers
As you don't know the last record so use just a large number as below code
SELECT Count(CATEGORY), COUNT(INCIDENT_ID), CATEGORY,
ROUND(Count(CATEGORY) * 100 / (SELECT Count(*)
FROM incident_view
WHERE
( create_month = Month(Now() -
INTERVAL 1 month) )
AND ( create_year = Year(
Now() - INTERVAL 1 month) )
AND customer_company_name = "Company"
), 1) AS Percentage
FROM incident_view
WHERE ( create_month = Month(Now() - INTERVAL 1 month) )
AND ( create_year = Year(Now() - INTERVAL 1 month) )
AND customer_company_name = "Company"
GROUP BY CATEGORY
ORDER BY COUNT(INCIDENT_ID) DESC
limit 6, 18446744073709551615;

SQL - Percentage

I need to make a query, which results in a percentage.
The query I have at the moment looks like this:
select COUNT(CREATE_WEEKDAY),
CREATE_WEEKDAY,
COUNT(CREATE_WEEKDAY) * 100 /
from call_view
WHERE (create_month = MONTH(NOW() - INTERVAL 1 MONTH))
AND (create_year = YEAR(NOW() - INTERVAL 1 MONTH))
AND CUSTOMER_COMPANY_NAME = "Company"
group by CREATE_WEEKDAY
If I start the query the result shows:
COUNT(CREATE_WEEKDAY) | CREATE_WEEKDAY | COUNT(CREATE_WEEKDAY) * 100
111 Friday 11100
225 MONDAY 22500
and so on....
I want the last column to display the
"COUNT(CREATE_WEEKDAY) * 100 / SUM(COUNT(CREATE_WEEKDAY))"
->> this last part should be the SUM OF all the CREATE_WEEKDAY. In the example give = 111 + 225 = 336
But this Code isn't working. I appreciate any help.
Cheers
Here is one way using Sub-query
First identify the total count for the given filter's(where clause)
SELECT Count(*)
FROM call_view
WHERE ( create_month = Month(Now() - INTERVAL 1 month) )
AND ( create_year = Year(Now() - INTERVAL 1 month) )
AND customer_company_name = 'Company'
Then use this query in denominator to find percentage
SELECT Count(create_weekday),
create_weekday,
Count(create_weekday) * 100 / (SELECT Count(*)
FROM call_view
WHERE
( create_month = Month(Now() -
INTERVAL 1 month) )
AND ( create_year = Year(
Now() - INTERVAL 1 month) )
AND customer_company_name =
'Company')
FROM call_view
WHERE ( create_month = Month(Now() - INTERVAL 1 month) )
AND ( create_year = Year(Now() - INTERVAL 1 month) )
AND customer_company_name = "Company"
GROUP BY create_weekday
Join with a subquery that calculates the total count.
select COUNT(*),
CREATE_WEEKDAY,
COUNT(*) * 100 / total_count
from call_view
CROSS JOIN (SELECT COUNT(*) AS total_count
FROM call_view
WHERE create_month = MONTH(NOW() - INTERVAL 1 MONTH)
AND create_year = YEAR(NOW() - INTERVAL 1 MONTH)
AND customer_company_name = "Company") AS x
WHERE (create_month = MONTH(NOW() - INTERVAL 1 MONTH))
AND (create_year = YEAR(NOW() - INTERVAL 1 MONTH))
AND CUSTOMER_COMPANY_NAME = "Company"
group by CREATE_WEEKDAY

Result not getting sorted out as per requirement

I have two database. One oracle and one sql server.
For oracle.
WITH cycleset AS (
SELECT
c.CYCLE_OID
, UPPER(m.NAME) AS MACHINE_NAME, c.PAYLOAD
FROM mshist.CYCLE c
INNER JOIN msmodel.MACHINE m ON m.machine_oid = c.primarymachine
WHERE (c.CYCLE_OID BETWEEN 1705497113111 AND 1705497596716)),
eventset AS (
SELECT cs.* ,
CAST((
EXTRACT(SECOND FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) +
EXTRACT(MINUTE FROM ( a.END_TIME_UTC- a.START_TIME_UTC) DAY TO SECOND) * 60 +
EXTRACT(HOUR FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 +
EXTRACT(DAY FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 * 24
) AS NUMBER(19,0)) AS DURATION
FROM cycleset cs
INNER JOIN mshist.CYCLEACTIVITYCOMPONENT a ON cs.CYCLE_OID = a.OID
WHERE a.NAME NOT IN ('Machine.Delay')
UNION ALL
SELECT cs.* ,
CAST((
EXTRACT(SECOND FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) +
EXTRACT(MINUTE FROM ( a.END_TIME_UTC- a.START_TIME_UTC) DAY TO SECOND) * 60 +
EXTRACT(HOUR FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 +
EXTRACT(DAY FROM ( a.END_TIME_UTC - a.START_TIME_UTC) DAY TO SECOND) * 60 * 60 * 24
) AS NUMBER(19,0)) AS DURATION
FROM cycleset cs
INNER JOIN mshist.CYCLEDELAY a ON cs.CYCLE_OID = a.OID)
SELECT MACHINE_NAME, SUM(DURATION) AS EVENT_DURATION, SUM(PAYLOAD) AS PAYLOAD
FROM eventset
GROUP BY
MACHINE_NAME
For SQL.
SELECT
UPPER(MACH_NAME) AS MACHINE_NAME
, CAST(SUM(f.SPLIT_DURATION) AS NUMERIC(19,0) ) AS DURATION,
SUM(f.EVENT_PAYLOAD) AS PAYLOAD
FROM [mssol2015pdw].[dbo].[F_CYCLE_SHIFT_HR_EVENT_LU] f
INNER JOIN [mssol2015pdw].[dbo].D_MACHINE m ON f.MACHINE_1_DIM_KEY = m.DIMENSION_KEY
INNER JOIN [mssol2015pdw].[dbo].D_CYCLE c ON f.[CYCLE_DIM_KEY] = c.DIMENSION_KEY
WHERE m.DIMENSION_KEY > 0
AND c.CYCLE_OID BETWEEN 1705497113111 AND 1705497596716
GROUP BY UPPER(MACH_NAME)
The data should get sorted out by machine name, it is getting sorted out in oracle but not in sql.
I am trying to compare the data in both the columns are same or not.
Also the count of data is not same for both the queries.
just add Order by MACHINE_NAME to the end of your both queries, you can also add Order by 1 at the end of queries, as MACHINE_NAME is first field