update single record with results from multiple case query - case

Found related type solutions, but nothing to match my need;
I want to update an existing table record (fields pu_cnt, pu_ot, pu_ltc etc.) with the result from the query (pu_cnt, pu_ot, pu_ltc etc.) etc.
Here's my query:
SELECT
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('C','D','H','F') THEN 1 END) AS
pu_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE '' THEN 1 END) AS
pu_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'C' THEN 1 END)
AS pu_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actpu_delay_code,1) LIKE 'U' THEN 1 END)
AS pu_ltu,
COUNT(CASE WHEN vuWO_CHC_ALL.status IN('D','H','F') THEN 1 END) AS del_cnt,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE '' THEN 1 END) AS
del_ot,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'C' THEN 1 END)
AS del_ltc,
COUNT(CASE WHEN LEFT(vuWO_CHC_ALL.actdel_delay_code,1) LIKE 'U' THEN 1 END)
AS del_ltu
FROM vuWO_CHC_ALL WHERE
RTRIM(vuWO_CHC_ALL.SHIPPER) LIKE #pVendor AND
RTRIM(vuWO_CHC_ALL.ORIG) LIKE #pOrigin AND
RTRIM(vuWO_CHC_ALL.Dest) LIKE #pDestin
..

Figured it out using #variables.

Related

I want to create sub query in stored proc in oracle the following query giving error too many values

I want to add this query in stored procedure but it gives an error: ORA-00913: too many values
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
( SELECT COUNT(CASE WHEN SRM_DISCON_STATUS_ID = 3 THEN 1 END) AS ACTIVE_CONNECTIONS,
COUNT(CASE WHEN SRM_DISCON_STATUS_ID = 2 THEN 1 END) AS DISC_CONNECTIONS
from Connection_master)
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;
The problem is that you are selecting multiple values in the sub-query. You can only select a single value in a sub-query in the SELECT clause.
However, you do not need the sub-query and appear to want:
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
COUNT(CASE SRM_DISCON_STATUS_ID WHEN 3 THEN 1 END) AS ACTIVE_CONNECTIONS,
COUNT(CASE SRM_DISCON_STATUS_ID WHEN 2 THEN 1 END) AS DISC_CONNECTIONS
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;
If you did want the sub-queries then:
Select SUM(CONNMASS.CONN_BILLAMOUNT) as Revenuebilled,
count(CONNMASS.CONN_BILLEDUNITS) AS volumebilled,
count(MASSBILL.BM_lo_id) as normalbilled,
( SELECT COUNT(CASE SRM_DISCON_STATUS_ID WHEN 3 THEN 1 END)
from Connection_master) AS ACTIVE_CONNECTIONS,
( SELECT COUNT(CASE SRM_DISCON_STATUS_ID WHEN 2 THEN 1 END)
from Connection_master) AS DISC_CONNECTIONS
from CONNECTION_MASTER CONNMASS
left join BILLING_MASTER MASSBILL
on MASSBILL.CONN_SERVICE_NO = CONNMASS.CONN_SERVICE_NO;

derive 2 fields SPEED_UPGRADE and TV_PACKAGE from 3 tables

I need to derive 2 fields SPEED_UPGRADE and TV_PACKAGE from 3 tables. I have written code below. I am not getting correct count of 'SPEED' and 'TV' which are falling between start_dt and END_DT. rather it is giving me count of all 'SPEED' and 'TV'. Kindly optimise my query .
select <few columns>,
count (case WHEN Cast('2021-11-09 00:00:00' AS TIMESTAMP(0))
BETWEEN a.START_DT AND Coalesce(a.END_DT,Cast('9999-12-31 00:00:00' AS TIMESTAMP FORMAT 'Y4-MM-DDBHH:MI:SS')) AND b.VOUCHER_TYPE_CD='SPEED'
then 1
ELSE 0
end) SPEED_UPGRADE,
count(case WHEN Cast('2021-11-09 00:00:00' AS TIMESTAMP(0))
BETWEEN a.START_DT AND Coalesce(a.END_DT,Cast('9999-12-31 00:00:00' AS TIMESTAMP FORMAT 'Y4-MM-DDBHH:MI:SS')) AND b.VOUCHER_TYPE_CD='TV'
then 1
ELSE 0
end) TV_PACKAGE
FROM
(sel * from P0_view.edw_v_fct_subscriber_household_base where subscriber_status_cd ='Active' and billed_customer_id ='-1' and household_id >0 and household_base_dt='2021-11-09' ) f
right join P0_VIEW.EDW_V_FCT_FIXED_VOUCHER_REDEEMED a on f.CUSTOMER_ID=a.CUSTOMER_ID
left join P0_VIEW.EDW_V_DIM_FIXED_VOUCHER b on a.FIXED_VOUCHER_ID = b.FIXED_VOUCHER_ID
group by 1,2,3,4,5,6,7,8,9 ) q ;

Bigquery Google Analytics Users Not Counting Unique

I'm having issues with the below query in BigQuery for Google Analytics. For some reason I'm not able to count the number of users as unique, it essentially counts the number of rows so the numbers are very similar to sessions. I've also tried EXACT_COUNT_DISTINCT() but gives me the same answer.
SELECT
date AS Day,
MAX(CASE
WHEN hits.sourcePropertyInfo.sourcePropertyTrackingId CONTAINS '778****' THEN 'MUG'
WHEN hits.sourcePropertyInfo.sourcePropertyTrackingId = 'Social' THEN 'Social'ELSE 'Website' END) AS Property,
geoNetwork.country AS Country,
SUM(totals.visits) AS visits,
COUNT (DISTINCT(fullVisitorId), 1000000) AS Users,
SUM(IFNULL(totals.newVisits,0)) AS NEW,
(SUM(IFNULL(totals.screenviews,0))+SUM(IFNULL(totals.pageviews,0))) AS PAGEVIEWS,
IFNULL(SUM(CASE
WHEN totals.screenviews = 1 THEN SUM(IFNULL(totals.screenviews,0))
ELSE 0 END)+ SUM(IFNULL(totals.bounces,0)),0) AS BOUNCES,
SUM(CASE
WHEN REGEXP_MATCH(hits.eventInfo.eventAction,'register$|registersuccess|new registration|account signup|registro') THEN 1
ELSE 0 END) AS NewRegistrations,
SUM(CASE
WHEN REGEXP_MATCH(hits.eventInfo.eventAction, 'add to cart|add to bag|click to buy|ass to basket|comprar') OR hits.eventInfo.eventAction CONTAINS 'addtobasket::' THEN 1
ELSE 0 END) AS ClickToBuy,
SUM(IFNULL(totals.transactions,0)) AS Transactions,
SUM(IFNULL(totals.transactionRevenue,0))/1000000 AS Revenue
FROM (TABLE_DATE_RANGE([****.ga_sessions_], TIMESTAMP('2017-03-15'), TIMESTAMP('2017-03-31'))),
GROUP BY
Day,
Country,
geoNetwork.country,
totals.screenviews;
I just tested this query and it seems a bit simpler:
SELECT
date,
MAX(CASE
WHEN hits.sourcePropertyInfo.sourcePropertyTrackingId CONTAINS '778****' THEN 'MUG'
WHEN hits.sourcePropertyInfo.sourcePropertyTrackingId = 'Social' THEN 'Social'ELSE 'Website' END) AS Property,
geoNetwork.country AS Country,
SUM(totals.visits) AS visits,
COUNT(DISTINCT(fullVisitorId), 1000000) AS Users,
SUM(totals.newVisits) AS NEW,
SUM(totals.pageviews) AS PAGEVIEWS,
SUM(totals.bounces) AS BOUNCES,
SUM(CASE
WHEN REGEXP_MATCH(hits.eventInfo.eventAction,'register$|registersuccess|new registration|account signup|registro') THEN 1
ELSE 0 END) AS NewRegistrations,
SUM(CASE
WHEN REGEXP_MATCH(hits.eventInfo.eventAction, 'add to cart|add to bag|click to buy|ass to basket|comprar|addtobasket::') THEN 1
ELSE 0 END) AS ClickToBuy,
SUM(totals.transactions) AS Transactions,
SUM(totals.transactionRevenue) /1000000 AS Revenue
FROM (TABLE_DATE_RANGE([project_id:dataset_id.ga_sessions_], TIMESTAMP('2017-03-15'), TIMESTAMP('2017-03-31'))),
GROUP BY
date, Country
It did work in our database (not sure why you summed screenviews with pageviews though).
In Standard SQL (highly recommended that you use this version) maybe this already solves for you:
SELECT
date,
MAX(CASE
WHEN exists(select 1 from unnest(hits) hits where regexp_contains(hits.sourcePropertyInfo.sourcePropertyTrackingId, r'778\*\*\*\*')) THEN 'MUG'
WHEN exists(select 1 from unnest(hits) hits where hits.sourcePropertyInfo.sourcePropertyTrackingId = 'Social') THEN 'Social'ELSE 'Website' END) AS Property,
geoNetwork.country AS Country,
SUM(totals.visits) AS visits,
COUNT(DISTINCT(fullVisitorId)) AS Users,
SUM(totals.newVisits) AS new_,
SUM(totals.pageviews) AS PAGEVIEWS,
SUM(totals.bounces) AS BOUNCES,
SUM(CASE
WHEN exists(select 1 from unnest(hits) hits where REGEXP_contains(hits.eventInfo.eventAction,'register$|registersuccess|new registration|account signup|registro')) THEN 1
ELSE 0 END) AS NewRegistrations,
SUM(CASE
WHEN exists(select 1 from unnest(hits) hits where REGEXP_contains(hits.eventInfo.eventAction, 'add to cart|add to bag|click to buy|ass to basket|comprar|addtobasket::')) THEN 1
ELSE 0 END) AS ClickToBuy,
SUM(totals.transactions) AS Transactions,
SUM(totals.transactionRevenue) /1000000 AS Revenue
FROM `project_id.dataset_id.ga_sessions*`
where 1 = 1
and parse_timestamp("%Y%m%d", regexp_extract(_table_suffix, r'.*_(.*)')) between TIMESTAMP('2017-03-15') and TIMESTAMP('2017-03-31')
GROUP BY
date, Country

use the same parameters multiple times

I have an sql-select (or insert) which uses the same two parameters several times.
Is there a way to avoid using multiple the same parameter for every "?,?,?,?,..." in the list ?
cursor.execute(statement, list)
I could think of two named parameters but without the possibility of code-injection.
In the example below every left "?" resp. right "?" is the same string. I used the seven counts in one statement in order to get one result.
select count(case (aart like "1%") and (adatum between ? and ?) when 1 then 1 else null end) as AufExt,
count(case (aart like "1%E") and (adatum between ? and ?) when 1 then 1 else null end) as AufExtE,
count(case (aart like "1%K") and (adatum between ? and ?) when 1 then 1 else null end) as AufExtK,
count(case (aart like "2S%") and (adatum between ? and ?) when 1 then 1 else null end) as AufInt,
count(case (eart like "3%") and (edatum between ? and ?) when 1 then 1 else null end) as EntExt,
count(case (eart like "3%K") and (edatum between ? and ?) when 1 then 1 else null end) as EntExtK,
count(case (eart like "2S%") and (edatum between ? and ?) when 1 then 1 else null end) as EntInt
from tabelle
Related question to this problem: It looks as if no index is used in "case". Correct?
The documentation says:
? A question mark that is not followed by a number creates a parameter with a number one greater than the largest parameter number already assigned. [...]
?NNN A question mark followed by a number NNN holds a spot for the NNN-th parameter. [...]
:AAAA A colon followed by an identifier name holds a spot for a named parameter with the name :AAAA. Named parameters are also numbered. [...] To avoid confusion, it is best to avoid mixing named and numbered parameters.
you can also add with clause like this:
with
parms as (select ? as parm1, ? as parm2)
select count(case (aart like "1%") and (adatum between parms.parm1 and parms.parm2) when 1 then 1 else null end) as AufExt
...

How to do get data out of multiple `countof`s on single table in SQLite?

I have a table with data and having a column with a date value and an affinity column, which can have 5 different values (i.e. a,b,c,d,e).
Table A
table_id date affinity
I need to count how many entries there is per month of the year per affinity. I initially created an sql query for each month per affinity, so the database gets opened for about 60 times, which is too much for most android phones to handle and is super slow.
How I can condense this in a single query and then how I can get the values? Ideally I would create a temporary table that looks like this with sample values.
Jan Feb Mar Apr May ...
a 2 4 6 4 1
b 4 1 3 4 0
c 2 2 4 2 0
d 7 3 6 0 5
e 9 5 1 9 8
I am not well versed with advanced sql querying, but I do know of JOINS and nested SELECTS. So I just need a little push in the right direction. How can I achieve this?
You can use conditional aggregation to do this by using a case expression in conjunction with the count function:
select
affinity
, count(case when month(`date`) = 1 then affinity end) as "Jan"
, count(case when month(`date`) = 2 then affinity end) as "Feb"
, count(case when month(`date`) = 3 then affinity end) as "Mar"
, count(case when month(`date`) = 4 then affinity end) as "Apr"
, count(case when month(`date`) = 5 then affinity end) as "May"
-- ... etc.
from a -- this is your table, which I assumed is called 'a'
group by affinity;
Sample SQL Fiddle
As SQLite doesn't have any month function, you would have to use the strftime function instead: strftime('%m', date)
For SQLite the query should probably look lie this:
select
affinity
, count(case when strftime('%m', date) = '01' then affinity end) as "Jan"
, count(case when strftime('%m', date) = '02' then affinity end) as "Feb"
, count(case when strftime('%m', date) = '03' then affinity end) as "Mar"
, count(case when strftime('%m', date) = '04' then affinity end) as "Apr"
, count(case when strftime('%m', date) = '05' then affinity end) as "May"
from a -- this is your table, which I assumed is called 'a'
group by affinity;
Investigate 'GROUP BY' and its aggregate functions. Something like:
SELECT COUNT() AS C, affinity, date
FROM ...
GROUP BY affinity, date
Gives you the list of records. Reorder to matrix if necessary.

Resources