Calculating using Variables in MYSQL query - sql

hope you can help, this is driving me up the wall
I need to calculate the percentage of times a question has been failed, but this needs to be narrowed down by the geographical area, and product these questions are being asked against.
I have :
$CA002 = "( SELECT ROUND(100 * (SELECT count(CA002Result) from Data_Table where (CA002Result='Fail'))/count(CA002Result),2) from Data_Table) AS 'CA002 %'";
Which 'works' but just calculates against the whole set of records as an 'overall'
I'm trying :
$CA001 = "( SELECT ROUND(100 * (SELECT count(CA001Result) from Data_Table where (CA001Result='Fail' AND Area ='$Area'))/count(CA001Result) from Data_Table WHERE (Area='$Area'),2) AS 'CA001 %'";
And Also :
$CA001 = "( SELECT ROUND(100 * (SELECT count(CA001Result ) from Data_Table where (CA001Result='Fail' AND Product='$product' AND Area='$Area'))
/ count(CA001Result WHERE Product = '$product' AND Area='$Area'),2) from Data_Table) AS 'CA001 %'";
and am just getting errors no matter what I try, I just can't seem to work out what I need to put where.
Any great GREATLY apprteciated, thankyou.

Try this
//Filter by Area
create table t( id int, answer varchar(10),Area varchar(10));
insert into t select 1 , 'pass' , 'Area1';
insert into t select 2 , 'pass' , 'Area1';
insert into t select 3 , 'fail' , 'Area1';
insert into t select 4 , 'fail' , 'Area1';
insert into t select 5 , 'fail' , 'Area1';
insert into t select 6 , 'fail' , 'Area2';
SELECT
(x.TotalFailedAnswerRecord * 100) /y.TotalRecord AS Fail_percent
FROM
( SELECT Area,TotalFailedAnswerRecord = COUNT(answer)
FROM t
WHERE answer='fail' AND Area = 'Area1'
GROUP BY Area
)x
INNER JOIN
( SELECT Area,TotalRecord = COUNT(answer)
FROM t
WHERE Area = 'Area1'
GROUP BY Area
)y ON x.Area =y.Area
//Result
Fail_percent
-------------
60
//Filter by Area,Product
create table t( id int, answer varchar(10),Area varchar(10),Product varchar(10));
insert into t select 1 , 'pass' , 'Area1' ,'Product1';
insert into t select 2 , 'fail' , 'Area1' ,'Product1';
insert into t select 3 , 'fail' , 'Area1' ,'Product1';
insert into t select 4 , 'fail' , 'Area1' ,'Product1';
insert into t select 5 , 'fail' , 'Area1' ,'Product2';
insert into t select 6 , 'fail' , 'Area2' ,'Product2';
SELECT
(x.TotalFailedAnswerRecord * 100) /y.TotalRecord AS Fail_percent
FROM
( SELECT Area,Product,TotalFailedAnswerRecord = COUNT(answer)
FROM t
WHERE answer='fail' AND Area = 'Area1' AND Product = 'Product1'
GROUP BY Area,Product
)x
INNER JOIN
( SELECT Area,Product,TotalRecord = COUNT(answer)
FROM t
WHERE Area = 'Area1' AND Product = 'Product1'
GROUP BY Area,Product
)y ON x.Area =y.Area AND x.Product = y.Product
//Result
Fail_percent
-------------
75
Hope this helps

Related

Trying to add a syntax in existing query (Custom SQL)

I am trying to add a condition to existing SQL Query but I am no expert at SQL & requesting some help here.
I have the below SQL query & I currently have PARNT_SLS_ORDER_LINE_KEY that I am pulling for two BUSINESS_UNIT_ID ('CSPBU', 'TMGBU'). In the existing query I am trying to add a condition like:
If PARNT_SLS_ORDER_LINE_KEY exists in both CSPBU & TMGBU then output or ignore.
SELECT
BV_PRODUCTS.PRODUCT_ID,
BV_FISCAL_DAY_TO_YEAR.FISCAL_YEAR_NUMBER_INT,
BV_FISCAL_DAY_TO_YEAR.FISCAL_QUARTER_ID,
SUM(CASE WHEN DT_MT_RSTD_BKGS_MEASURE_IE_BE.SERVICE_FLG = 'N' THEN DT_MT_RSTD_BKGS_MEASURE_IE_BE.EXTENDED_QUANTITY*COALESCE(BV_BE_HIER_PRDT_FAM_ALLOC_INT.PRDT_FAMILY_ALLOCATION_PCT,1)*COALESCE(BV_BE_HIER_PRDT_FMLY_ALLOC_EXT.PRDT_FAMILY_ALLOCATION_PCT,1) ELSE 0 END) Quanity
,
sum(CASE WHEN DT_MT_RSTD_BKGS_MEASURE_IE_BE.SERVICE_FLG = 'N' THEN DT_MT_RSTD_BKGS_MEASURE_IE_BE.COMP_US_NET_PRICE_AMOUNT*COALESCE(BV_BE_HIER_PRDT_FAM_ALLOC_INT.PRDT_FAMILY_ALLOCATION_PCT,1)*COALESCE(BV_BE_HIER_PRDT_FMLY_ALLOC_EXT.PRDT_FAMILY_ALLOCATION_PCT,1) ELSE 0 END) Net_Price,
BV_SALES_ORDER_LINE.PARNT_SLS_ORDER_LINE_KEY,
BV_PRODUCTS.BUSINESS_UNIT_ID
FROM
FINANCEBOBVDB.BV_PRODUCTS,
FINANCEBOBVDB.BV_FISCAL_DAY_TO_YEAR,
FINANCEBOBVDB.BV_BE_HIER_PRDT_FAMILY_ALLOC BV_BE_HIER_PRDT_FAM_ALLOC_INT,
FINANCEBOBVDB.BV_BE_HIER_PRDT_FMLY_ALLOC_EXT,
(
SELECT BV_MT_RSTD_BKGS_MEASURE.* , BV_FISCAL_DAY_TO_YEAR.FISCAL_YEAR_QUARTER_NUMBER_INT ,
BV_FISCAL_DAY_TO_YEAR.CALENDAR_DATE
FROM FINANCEBOBVDB.BV_MT_RSTD_BKGS_MEASURE , FINANCEBOBVDB.BV_FISCAL_DAY_TO_YEAR
WHERE BV_MT_RSTD_BKGS_MEASURE.BOOKINGS_PROCESS_DATE = BV_FISCAL_DAY_TO_YEAR.CALENDAR_DATE
AND BV_FISCAL_DAY_TO_YEAR.FISCAL_YEAR_MONTH_INT >= 201001
) DT_MT_RSTD_BKGS_MEASURE_IE_BE,
BV_SALES_ORDER_LINE
WHERE
( BV_BE_HIER_PRDT_FAM_ALLOC_INT.ITEM_KEY=DT_MT_RSTD_BKGS_MEASURE_IE_BE.PRODUCT_KEY AND BV_BE_HIER_PRDT_FAM_ALLOC_INT.FISCAL_YEAR_QUARTER_NUMBER_INT=DT_MT_RSTD_BKGS_MEASURE_IE_BE.FISCAL_YEAR_QUARTER_NUMBER_INT )
AND ( BV_BE_HIER_PRDT_FMLY_ALLOC_EXT.ITEM_KEY=DT_MT_RSTD_BKGS_MEASURE_IE_BE.PRODUCT_KEY AND BV_BE_HIER_PRDT_FMLY_ALLOC_EXT.FISCAL_YEAR_QUARTER_NUMBER_INT=DT_MT_RSTD_BKGS_MEASURE_IE_BE.FISCAL_YEAR_QUARTER_NUMBER_INT )
AND ( DT_MT_RSTD_BKGS_MEASURE_IE_BE.BOOKINGS_PROCESS_DATE=BV_FISCAL_DAY_TO_YEAR.CALENDAR_DATE )
AND ( BV_PRODUCTS.ITEM_KEY=DT_MT_RSTD_BKGS_MEASURE_IE_BE.PRODUCT_KEY )
AND ( BV_SALES_ORDER_LINE.SALES_ORDER_LINE_KEY=DT_MT_RSTD_BKGS_MEASURE_IE_BE.DV_SALES_ORDER_LINE_KEY )
AND ( DT_MT_RSTD_BKGS_MEASURE_IE_BE.PRODUCT_KEY IN ( SELECT ITEM_KEY FROM FINANCEBOBVDB.BV_PRODUCTS H JOIN FINANCEBOBVDB.BV_IAM_TECHNOLOGY_GROUP_LINK S ON H.TECHNOLOGY_GROUP_ID = S.TECHNOLOGY_GROUP_ID WHERE cec_id='meramesh' AND IAM_LEVEL_NUM=1 ) )
AND ( DT_MT_RSTD_BKGS_MEASURE_IE_BE.DV_ATTRIBUTION_CD IN ('ATTRIBUTED','STANDALONE') )
AND
(
DT_MT_RSTD_BKGS_MEASURE_IE_BE.REVENUE_RECOGNITION_FLG IN ( 'Y' )
AND
BV_FISCAL_DAY_TO_YEAR.FISCAL_YEAR_NUMBER_INT IN ( 2018, 2019, 2020 )
AND
BV_PRODUCTS.BUSINESS_UNIT_ID IN ( 'cspbu', 'tmgbu' )
)
GROUP BY
1,
2,
3,
6,
7
How are you grouping your rows -- by product_id? If you want to only return rows where a given product_id has PARNT_SLS_ORDER_LINE_KEY with both values of TMGBU and CSPBU, then you can add this to the end of your query:
QUALIFY
MAX(CASE PARNT_SLS_ORDER_LINE_KEY WHEN 'CSPBU' THEN 1 WHEN 'TMGBU' THEN 2 END)
OVER(PARTITION BY BV_PRODUCTS.PRODUCT_ID) <>
MIN(CASE PARNT_SLS_ORDER_LINE_KEY WHEN 'CSPBU' THEN 1 WHEN 'TMGBU' THEN 2 END)
OVER(PARTITION BY BV_PRODUCTS.PRODUCT_ID)
Give it a try and let me know.

always get a "not a GROUP BY expression" exception

The select part is fine, I can run it and get result, but if I insert the query result into a table, the "not a group by expression" exception was thrown, see my sql statement below:
INSERT INTO RAWDATA_FACT
(
LINEITEMID
,CALENDARYEAR
,CALENDARQUARTER
,CALENDARMONTH
,DEPARTMENTID
,PRODUCTID
,DEALERID
,ACTUALVALUE
,TARGETVALUE
,AGGREGATION
,TODATE
,CREATEDATE
,BATCH_ID
)
select parentid
,calendaryear
,calendarquarter
,calendarmonth
,departmentid
,productid
,dealerid
,sum(case when unaryoperator = '-' then actualvalue * (-1)
when unaryoperator = '~' then actualvalue * 0
else actualvalue
end
) actualvalue
,sum(case when unaryoperator = '-' then targetvalue * (-1)
when unaryoperator = '~' then targetvalue * 0
else targetvalue
end
) targetvalue
,aggregation
,todate
,sysdate createdate
,'201808' batch_id--v_batch_ID
from
(
select
x.parentid
, x.unaryoperator
, y.calendaryear
, y.calendarquarter
, y.calendarmonth
, y.departmentid
, y.productid
, y.dealerid
, y.actualvalue
, y.targetvalue
, y.aggregation
, y.todate
from
(select substr(lineitemid,instr(lineitemid,'_')+1) lineitemid,
parentid, unaryoperator from lineitem_temp where levelid = 14 /*v_cur_level*/) x
inner join
(select lineitemid,calendaryear, calendarquarter, calendarmonth, departmentid, productid, dealerid,
coalesce(actualvalue,0) actualvalue,coalesce(targetvalue,0) targetvalue,
aggregation, todate
from RAWDATA_FACT where BATCH_ID = '201808'/*v_batch_ID*/) y --eg. 201809
on x.lineitemid = y.lineitemid
--parent node's id contains "_" will not take part in calculation from current level to parent level
where regexp_like(x.parentid , '^\d+$') and not exists
--parent node contains formula will not participate calculation from current level to parent level
(select 1 from LINEITEM_TEMP where lineitemid = x.parentid and custommember is not null)
) t
GROUP BY
t.parentid
, t.calendaryear
, t.calendarquarter
, t.calendarmonth
, t.departmentid
, t.productid
, t.dealerid
, t.aggregation
, t.todate
;
who can tell me why? is there a way to insert the result into that table? If I run the select parts, it works fine, but if I want to insert the result into that table, it reports that exception.

How to merge two columns from CASE STATEMENT of DIFFERENT CONDITION

My expected result should be like
----invoiceNo----
T17080003,INV14080011
But right now, I've come up with following query.
SELECT AccountDoc.jobCode,AccountDoc.shipmentSyskey,AccountDoc.docType,
CASE AccountDoc.docType
WHEN 'M' THEN
JobInvoice.invoiceNo
WHEN 'I' THEN
(STUFF((SELECT ', ' + RTRIM(CAST(AccountDoc.docNo AS VARCHAR(20)))
FROM AccountDoc LEFT OUTER JOIN JobInvoice
ON AccountDoc.principalCode = JobInvoice.principalCode AND
AccountDoc.jobCode = JobInvoice.jobCode
WHERE (AccountDoc.isCancelledByCN = 0)
AND (AccountDoc.docType = 'I')
AND (AccountDoc.jobCode = #jobCode)
AND (AccountDoc.shipmentSyskey = #shipmentSyskey)
AND (AccountDoc.principalCode = #principalCode) FOR XML
PATH(''), TYPE).value('.','NVARCHAR(MAX)'),1,2,' '))
END AS invoiceNo
FROM AccountDoc LEFT OUTER JOIN JobInvoice
ON JobInvoice.principalCode = AccountDoc.principalCode AND
JobInvoice.jobCode = AccountDoc.jobCode
WHERE (AccountDoc.jobCode = #jobCode)
AND (AccountDoc.isCancelledByCN = 0)
AND (AccountDoc.shipmentSyskey = #shipmentSyskey)
AND (AccountDoc.principalCode = #principalCode)
OUTPUT:
----invoiceNo----
T17080003
INV14080011
Explanation:
I want to select docNo from table AccountDoc if AccountDoc.docType = I.
Or select invoiceNo from table JobInvoice if AccountDoc.docType = M.
The problem is what if under same jobCode there have 2 docType which are M and I, how I gonna display these 2 invoices?
You can achieve this by using CTE and FOR XML. below is the sample code i created using similar tables you have -
Create table #AccountDoc (
id int ,
docType char(1),
docNo varchar(10)
)
Create table #JobInvoice (
id int ,
invoiceNo varchar(10)
)
insert into #AccountDoc
select 1 , 'M' ,'M1234'
union all select 2 , 'M' ,'M2345'
union all select 3 , 'M' ,'M3456'
union all select 4 , 'I' ,'I1234'
union all select 5 , 'I' ,'I2345'
union all select 6 , 'I' ,'I3456'
insert into #JobInvoice
select 1 , 'INV1234'
union all select 2 , 'INV2345'
union all select 3 , 'INV3456'
select *
from #AccountDoc t1 left join #JobInvoice t2
on t1.id = t2.id
with cte as
(
select isnull( case t1.docType WHEN 'M' THEN t2.invoiceNo WHEN 'I' then
t1.docNo end ,'') invoiceNo
from #AccountDoc t1 left join #JobInvoice t2
on t1.id = t2.id )
select invoiceNo + ',' from cte For XML PATH ('')
You need to pivot your data if you have situations where there are two rows, and you want two columns. Your sql is a bit messy, particularly the bit where you put an entire select statement inside a case when in the select part of another query. These two queries are virtually the same, you should look for a more optimal way of writing them. However, you can wrap your entire sql in the following:
select
Jobcode, shipmentsyskey, [M],[I]
from(
--YOUR ENTIRE SQL GOES HERE BETWEEN THESE BRACKETS. Do not alter anything else, just paste your entire sql here
) yoursql
pivot(
max(invoiceno)
for docType in([M],[I])
)pvt

How to add a count/sum and group by in a CTE

Just a question on displaying a row on flight level and displaying a count on how many crew members on that flight.
I want to change the output so it will only display a single record at flight level and it will display two additional columns. One column (cabincrew) is the count of crew members that have the 'CREWTYPE' = 'F' and the other column (cockpitcrew) is the count of crew members that have the `'CREWTYPE' = 'C'.
So the query result should look like:
Flight DepartureDate DepartureAirport CREWBASE CockpitCrew CabinCrew
LS361 2016-05-19 BFS BFS 0 3
Can I have a little help tweaking the below query please:
WITH CTE AS (
SELECT cd.*, c.*, l.Carrier, l.FlightNumber, l.Suffix, l.ScheduledDepartureDate, l.ScheduledDepartureAirport
FROM
(SELECT *, ROW_NUMBER() OVER(PARTITION BY LegKey ORDER BY UpdateID DESC) AS RowNumber FROM Data.Crew) c
INNER JOIN
Data.CrewDetail cd
ON c.UpdateID = cd.CrewUpdateID
AND cd.IsPassive = 0
AND RowNumber = 1
INNER JOIN
Data.Leg l
ON c.LegKey = l.LegKey
)
SELECT
sac.Airline + CAST(sac.FlightNumber AS VARCHAR) + sac.Suffix AS Flight
, sac.DepartureDate
, sac.DepartureAirport
, sac.CREWBASE
, sac.CREWTYPE
, sac.EMPNO
, sac.FIRSTNAME
, sac.LASTNAME
, sac.SEX
FROM
Staging.SabreAssignedCrew sac
LEFT JOIN CTE cte
ON sac.Airline + CAST(sac.FlightNumber AS VARCHAR) + sac.Suffix = cte.Carrier + CAST(cte.FlightNumber AS VARCHAR) + cte.Suffix
AND sac.DepartureDate = cte.ScheduledDepartureDate
PLEASE TRY THIS.
SELECT Flight,
DepartureDate,
DepartureAirport,
CREWBASE,
SUM(CASE WHEN CREWTYPE = 'F' THEN 1 ELSE 0 END) AS CabinCrew ,
SUM(CASE WHEN CREWTYPE = 'C' THEN 1 ELSE 0 END) AS CockpitCrew
FROM #Table
GROUP BY Flight, DepartureDate, DepartureAirport, CREWBASE
Please Try This:
select Flight, DepartureDate, DepartureAirport,CREWBASE,
count(case when CREWTYPE='F' then 1 end ) as CabinCrew,count(case when CREWTYPE='C' then 1 end ) as CockpitCrew
from Staging.SabreAssignedCrew
group by Flight, DepartureDate, DepartureAirport,CREWBASE

SQL divide data in rows

I have the following table named
Table: activity
userid appid type
1 a imp
2 a imp
2 a click
3 a imp
4 a imp
4 a click
5 b imp
5 b click
I am trying to calculate the click-through rate for each appid. In this instance we define a click-through as the (number of clicks) / (number of impressions). I have written the following SQL:
SELECT appid, type, count(*) from activity group by appid, type
and got the following outcome:
Output:
appid type count(*)
a click 2
a imp 4
b click 1
b imp 1
The next step is to do a row-wise division. Ultimately, I would like to achieve the following:
Goal:
appid click-through
a .5 # 2/4 = .5
b 1 # 1/1 = 1
How is this achieved? Ideally I want this to be done in one query, is that possible?
You can use conditional aggregation to do this:
select appid
, SUM(CASE WHEN type = 'click' THEN 1 END)*1.0
/ SUM(CASE WHEN type = 'imp' THEN 1 END) AS click_through
from activity
group by appid
Demo: SQL Fiddle
If using MySQL you can further simplify with:
select appid
, SUM(type = 'click')*1.0
/ SUM(type = 'imp') AS click_through
from activity
group by appid
Just count the impressions and clicks in subqueries and join them together:
select appid,
num_impressions,
num_clicks,
cast(num_clicks as float) / num_impressions as ctr
from(
select appid, count(1) as num_impressions
from activity
where type = 'imp'
group by appid
)a
join(
select appid, count(1) as num_clicks
from activity
where type = 'click'
group by appid
)b
on (a.appid = b.appid);
Note the type cast on num_clicks in ctr to avoid integer division.
CREATE TABLE #activity
(
userid INT ,
appid VARCHAR(1),
[type] VARCHAR(5)
)
INSERT INTO #activity
VALUES
(1, 'a' , 'imp'),
(2, 'a', 'imp'),
(2, 'a', 'click'),
(3, 'a', 'imp'),
(4, 'a', 'imp'),
(4, 'a', 'click'),
(5, 'b', 'imp'),
(5, 'b', 'click')
SELECT A.appid, CAST(a.Clicks AS FLOAT)/B.Imp
FROM
( SELECT appid, COUNT(1) Clicks
FROM #activity
WHERE [type] ='CLICK'
GROUP BY appid
) A
INNER JOIN
( SELECT appid, COUNT(1) Imp
FROM #activity
WHERE [type] ='imp'
GROUP BY appid
) B ON A.appid = B.appid
DROP TABLE #activity
You could do this with sub-queries since you are dealing with two different aggregates using different criteria:
SELECT d1.appid, (
SELECT count(*)
FROM activity d2
WHERE d2.appid = d1.appid
d2.type = 'click'
) / (
SELECT count(*)
FROM activity d3
WHERE d3.appid = d1.appid
d3.type = 'imp'
) AS click_through
FROM activity d1;