R generate mysql select statements - r

I need to generate complex select statements that change based on an input file each time my script is ran.
I have a list of names that serves as my filter list, and I have a select statement that has been suggested to me in another thread. I'm not sure how to generate what I need without sitting on google for the next 6 hours piecing it together.
I need to create a MAX(CASE... line for each item in the list.
list <- df$names
list:
1 square
2 ball
3 dog
4 triangle
5 hamster
6 circle
7 yellow
8 cat
suggested SELECT format:
SELECT
data.loc
, data.type
, MAX(CASE WHEN data.name = 'cat' THEN 1 ELSE 0 END) AS cat
, MAX(CASE WHEN data.name = 'hamster' THEN 1 ELSE 0 END) AS hamster
FROM
data
GROUP BY
data.loc
, data.type
I believe the desired output would be:
SELECT
data.loc
, data.type
, MAX(CASE WHEN data.name = 'square' THEN 1 ELSE 0 END) AS square
, MAX(CASE WHEN data.name = 'ball' THEN 1 ELSE 0 END) AS ball
, MAX(CASE WHEN data.name = 'dog' THEN 1 ELSE 0 END) AS dog
, MAX(CASE WHEN data.name = 'triangle' THEN 1 ELSE 0 END) AS triangle
, MAX(CASE WHEN data.name = 'hamster' THEN 1 ELSE 0 END) AS hamster
, MAX(CASE WHEN data.name = 'circle' THEN 1 ELSE 0 END) AS circle
, MAX(CASE WHEN data.name = 'yellow' THEN 1 ELSE 0 END) AS yellow
, MAX(CASE WHEN data.name = 'cat' THEN 1 ELSE 0 END) AS cat
FROM
data
GROUP BY
data.loc
, data.type

simplest approach that I could think of is
l <- list("Cat", "dog", "bird")
sqlqry <- "SELECT
data.loc
, data.type"
for (i in 1:length(l)) {
sqlqry <- paste0(sqlqry, sprintf(", MAX(CASE WHEN data.name = '%s' THEN 1 ELSE 0 END) AS %s",l[i], l[i]))
print(sqlqry)
}
sqlqry <- paste0(sqlry, "your where clause")

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 ;

update single record with results from multiple case query

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.

How to use the MAX and Case function over three tables?

I have joined three tables using Max and Case functions.
When I used two tables it working fine but when I added the third table I got this error.
Ambiguous column name 'Month'.
SELECT Category ,
KPI ,
TDTargetValue ,
MAX(CASE WHEN Month = 'Jan' THEN Input
END) Jan ,
MAX(CASE WHEN Month = 'Feb' THEN Input
END) Feb ,
MAX(CASE WHEN Month = 'Mar' THEN Input
END) Mar ,
MAX(CASE WHEN Month = 'Apr' THEN Input
END) Apr ,
MAX(CASE WHEN Month = 'May' THEN Input
END) May ,
MAX(CASE WHEN Month = 'Jun' THEN Input
END) Jun ,
MAX(CASE WHEN Month = 'Jul' THEN Input
END) Jul ,
MAX(CASE WHEN Month = 'Aug' THEN Input
END) Aug ,
MAX(CASE WHEN Month = 'Sep' THEN Input
END) Sep ,
MAX(CASE WHEN Month = 'Oct' THEN Input
END) Oct ,
MAX(CASE WHEN Month = 'Nov' THEN Input
END) Nov ,
MAX(CASE WHEN Month = 'Dec' THEN Input
END) Dec
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
INNER JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[UpdateData]
WHERE PeriodId = '1'
) UD ON UD.Cid = NC.Id
LEFT JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[TargetData]
) TD ON UD.Cid = TD.CId
WHERE NC.Grade = 'A'
AND IsActived = '0'
GROUP BY Category ,
KPI ,
TDTargetValue
ORDER BY 1
Any help would be appreciated.
Add alias also for the column Month...
Assuming NewCriteria table has the column Month
SELECT Category ,
KPI ,
TDTargetValue ,
MAX(CASE WHEN NC.Month = 'Jan' THEN Input
END) Jan ,
MAX(CASE WHEN NC.Month = 'Feb' THEN Input
END) Feb ,
MAX(CASE WHEN NC.Month = 'Mar' THEN Input
END) Mar ,
MAX(CASE WHEN NC.Month = 'Apr' THEN Input
END) Apr ,
MAX(CASE WHEN NC.Month = 'May' THEN Input
END) May ,
MAX(CASE WHEN NC.Month = 'Jun' THEN Input
END) Jun ,
MAX(CASE WHEN NC.Month = 'Jul' THEN Input
END) Jul ,
MAX(CASE WHEN NC.Month = 'Aug' THEN Input
END) Aug ,
MAX(CASE WHEN NC.Month = 'Sep' THEN Input
END) Sep ,
MAX(CASE WHEN NC.Month = 'Oct' THEN Input
END) Oct ,
MAX(CASE WHEN NC.Month = 'Nov' THEN Input
END) Nov ,
MAX(CASE WHEN NC.Month = 'Dec' THEN Input
END) Dec
FROM [NEWSEMAKPI].[dbo].[NewCriteria] NC
INNER JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[UpdateData]
WHERE PeriodId = '1'
) UD ON UD.Cid = NC.Id
LEFT JOIN ( SELECT *
FROM [NEWSEMAKPI].[dbo].[TargetData]
) TD ON UD.Cid = TD.CId
WHERE NC.Grade = 'A'
AND IsActived = '0'
GROUP BY Category ,
KPI ,
TDTargetValue
ORDER BY 1

SQL count all of the column that has a value greater than 10 and sum it up

I want to count all of the column in my table that has value >= 10.
here is my table :
Date ##### || Value1 || Value2 || Value3
23/04/2014 || __ 1,2 || __ 12,3 ||__ 10 ||
23/04/2014 ||__ 11,2 || ____ 3 || __ 10,3 ||
24/04/2014 || __ 10,9 || ____ 3 || __ 1 ||
I want it to display:
Date ##### || Count ||
23/04/2014 || __ 4 ||
24/04/2014 || __ 1 ||
Assume that I have a lot of date, I want it to display only the last 3 rows.
here is my first code :
Dim strCommand As String = "Select Date, count(*) as tcount from tbBooth having count(*) >= 10 group by date"
already changed based on the solution from Collapsar into this:
Dim strCommand As String = "Select t.d, sum(t.valcount) cnt from (select [date] AS d, CASE WHEN t1.ManualAssists1 >= 10 THEN 1 ELSE 0 END + CASE WHEN t1.ManualAssists2 >= 10 THEN 1 ELSE 0 END + CASE WHEN t1.ManualAssists3 >= 10 THEN 1 ELSE 0 END AS valcount from tbBooth t1) t group by t.d"
it's works, but I want to display only the last 3 row based on ASC order.
Is there anyway how to do it?
Thanks in advances....
try
select t.d
, sum(t.valcount) cnt
from (
select [date] AS d
, CASE WHEN t1.value1 >= 10 THEN 1 ELSE 0 END
+ CASE WHEN t1.value2 >= 10 THEN 1 ELSE 0 END
+ CASE WHEN t1.value3 >= 10 THEN 1 ELSE 0 END
AS valcount
from table t1
) t
group by t.d
;
SELECT
D,
SUM(one + two + three) AS tcount
FROM
(
SELECT
[Date] AS D,
CASE WHEN Value1 >= 10 THEN 1 ELSE 0 END AS one,
CASE WHEN Value2 >= 10 THEN 1 ELSE 0 END AS two,
CASE WHEN Value3 >= 10 THEN 1 ELSE 0 END AS three
FROM
tbBooth
)
GROUP BY
D

Resources