Using GROUP BY/CASE with WHEN or IF - sql

note: edited query below.
I am looking to segment a data set according to two criteria:
If a customer has more or less than 4 txns at a specific restaurant
If a customer has more or less than 24 txns at all the other restaurants in that data set.
I am using a conjunction of GROUP BY, CASE and WHEN or IF. I am not sure which approach is best, if either?
SELECT
COUNT(Customer) AS number_of_customers,
AVG (CASE WHEN ItemPrice LIKE '-%' THEN NULL
WHEN ItemPrice LIKE '0%' THEN NULL
ELSE CAST (ItemPrice AS FLOAT) END) AS avg_item_price,
COUNT(DISTINCT(ReceiptIDDesc)) AS number_of_orders,
SUM(CAST(ItemPrice AS FLOAT)) AS total_spend
FROM Tacos
WHERE NOT (PurchaseDate > '01/01/2016 12:00' OR '03/01/2016 12:00'<
PurchaseDate)
GROUP BY
CASE
WHEN (COUNT('MerchantFamily' = %TacoTruck%)> 2) AND COUNT('MerchantFamily' != %TacoTruck%) >24)
THEN 'Fanatic'
WHEN (COUNT('MerchantFamily' = %TacoTruck%)> 2) AND COUNT('MerchantFamily' != %TacoTruck%) <24)
THEN 'Loyalist'
WHEN (COUNT('MerchantFamily' = %TacoTruck%)< 2) AND COUNT('MerchantFamily' != %TacoTruck%) <24)
THEN 'Seldom'
ELSE
'Potential'
END
OR
GROUP BY
CASE
IF(COUNT(IF( 'MerchantFamily' = 'TacoTruck', 1, 0 ) ) > 2, TRUE, FALSE)
AND
IF(COUNT(IF( 'MerchantFamily' != 'TacoTruck',1, 0) ) < 24, TRUE, FALSE), 'Loyalist', NULL )
IF(COUNT(IF( 'MerchantFamily' = 'TacoTruck', 1, 0 ) ) > 2, TRUE, FALSE)
AND
IF(COUNT(IF( 'MerchantFamily' != 'TacoTruck', 1, 0 ) ) > 24, TRUE, FALSE), 'Fanatic', NULL)
IF(COUNT(IF( 'MerchantFamily' = 'TacoTruck', 1, 0 ) ) < 2, TRUE, FALSE)
AND
IF(COUNT( IF( 'MerchantFamily' != 'TacoTruck', 1, 0 ) ) < 24, TRUE, FALSE), 'Seldom', NULL)
ELSE
'Potential'
END

Neither of those approaches will work, you need to group first then consider the aggregated count values through a having clause, or as a nested subquery ("derived table").
A case expression only evaluates values on a per row basis, it does not scan multiple rows.

Related

How to create a query to group by date order with specific value

I have this table and want to write a query and to display data as chart where for each day to display how many orders where under 50, under 100, under 250.
Can you help me how to create the graph?
This is the query I have written:
SELECT
o_id,
DATE(invoice_date) AS __timestamp,
SUM(if(amount_sale < 50,amount_sale,0)) AS `s50`,
SUM(if(amount_sale > 50 && amount_sale < 100,amount_sale,0)) AS `s100`,
SUM(if(amount_sale > 100 && amount_sale < 250,amount_sale,0)) AS `s250`,
SUM(if(amount_sale > 250 && amount_sale < 500,amount_sale,0)) AS `s500`,
SUM(if(amount_sale > 500 && amount_sale < 1000,amount_sale,0)) AS `s1000`,
SUM(if(amount_sale > 1000 && amount_sale < 2500,amount_sale,0)) AS `s2500`,
SUM(if(amount_sale > 2500,amount_sale,0)) AS `over_2500`
FROM
(SELECT
o.id as o_id,
o.ref,
o.invoice_date,
SUM(CASE
WHEN op.was_in_promo = 1
THEN op.quantity * (op.promo_price +opt.promo_amount)
ELSE op.quantity * (op.price +opt.amount)
END) AS amount_sale
FROM
thelia.order o
LEFT JOIN
order_product op ON op.order_id = o.id
LEFT JOIN
order_product_tax opt ON opt.order_product_id = op.id
LEFT JOIN
order_status os on os.id = o.status_id
LEFT JOIN
module_i18n mi ON mi.id = o.payment_module_id
AND mi.locale="de_DE"
WHERE
o.status_id IN (2, 3, 4, 5, 6, 7, 8, 11, 12, 14, 15, 16)
AND (`o`.`created_at` > (CURDATE() - INTERVAL 2 Year))
GROUP BY
o.id) AS expr_qry
GROUP BY
o_id
LIMIT 50000;

Power BI DAX - find repeatability

Given data as such:
Month ValueA
1 T
2 T
3 T
4 F
Is there a way to make a measure that would find if for each month, last three Values were True?
So the output would be (F,F,T,F)?
That would propably mean that my actual problem is solvable, which is finding from:
Month ValueA ValueB ValueC
1 T F T
2 T T T
3 T T T
4 F T F
the count of those booleans for each row, so the output would be (0,0,2[A and C],1[B])
EDIT:
Okay, I managed to solve the first part with this:
Previous =
VAR PreviousDate =
MAXX(
FILTER(
ALL( 'Table' ),
EARLIER( 'Table'[Month] ) > 'Table'[Month]
),
'Table'[Month]
)
VAR PreviousDate2 =
MAXX(
FILTER(
ALL( 'Table' ),
EARLIER( 'Table'[Month] ) - 1 > 'Table'[Month]
),
'Table'[Month]
)
RETURN
IF(
CALCULATE(
MAX( 'Table'[Value] ),
FILTER(
'Table',
'Table'[Month] = PreviousDate
)
) = "T"
&& CALCULATE(
MAX( 'Table'[Value] ),
FILTER(
'Table',
'Table'[Month] = PreviousDate2
)
) = "T"
&& 'Table'[Value] = "T",
TRUE,
FALSE
)
But is there a way to use it with unknown number of columns?
Without hard - coding every column name? Like a loop or something.
I would redo the data table in power query (upivoting the ValueX-columns) and changing T/F to 1/0. Then have a dim table with a relationship to Month, like this:
Then add a measure like this:
Three Consec T =
var maxMonth = MAX('Data'[Month])
var tempTab =
FILTER(
dimMonth;
'dimMonth'[MonthNumber] <= maxMonth && 'dimMonth'[MonthNumber] > maxMonth -3
)
var sumMonth =
MAXX(
'dimMonth';
CALCULATE(
SUM('Data'[OneOrZero]);
tempTab
)
)
return
IF(
sumMonth >= 3;
"3 months in a row";
"No"
)
Then I can have a visual like this when the slicer indicates which time window I'm looking at and the table shows if there has been 3 consecutive Ts or not.

How to query Oracle grouping?

I have such a problem and I don't know how to solve it, can you help me? t
The query returns a result that is shown on the photo and I want to get it to be shown in one line instead of many based on type of age.
https://imgur.com/a/OA6CBpa
with x as (
select ai.invoice_id, ai.invoice_num, ai.invoice_amount, ai.amount_paid,
trial.entity_id, trial.acctd_amount, trial.entered_amount, trial.gl_date,
aps.amount_remaining, aps.gross_amount, aps.due_date, aps.payment_status_flag,
trial.gl_date - aps.due_date dni_opoznienia
from ap_invoices_all ai,
xla.xla_transaction_entities xte,
(
select nvl (tr.applied_to_entity_id, tr.source_entity_id) entity_id,
tr.source_application_id application_id,
sum (nvl (tr.acctd_unrounded_cr, 0)) - sum (nvl (tr.acctd_unrounded_dr, 0)) acctd_amount,
sum (nvl (tr.entered_unrounded_cr, 0)) - sum (nvl (tr.entered_unrounded_dr, 0)) entered_amount,
max(tr.gl_date) gl_date
from xla.xla_trial_balances tr
where 1=1
and tr.definition_code = 'AP_200_1001'
and tr.source_application_id = 200
and tr.gl_date <= fnd_date.canonical_to_date('2019-12-13') -- Data KG
group by nvl (tr.applied_to_entity_id, tr.source_entity_id),
tr.source_application_id
) trial,
ap_payment_schedules_all aps
where 1=1
and ai.invoice_id = 3568325
and nvl(xte.source_id_int_1, -99) = ai.invoice_id
and xte.ledger_id = 1001
and xte.entity_code = 'AP_INVOICES'
and xte.entity_id = trial.entity_id
and xte.application_id = trial.application_id
and ai.invoice_id = aps.invoice_id
)
select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id
Based on your comment I guess you need the below
select * from (select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id)
where przedzial > 0;

Issue with select query: ORA-01861: literal does not match format string

I am facing issues while exeucting the below query as "ORA-01861: literal does not match format string". Not able to identify the actual issue.
select
*
from(
select
a.*,
rownum rnum
from(
SELECT
E1."BAMEVENT" AS "BAMEVENT",
E1."BCRMORDERID" AS "BCRMORDERID",
E1."COMPLETIONTIME" AS "COMPLETIONTIME",
E1."CONTACT" AS "CONTACT",
E1."CREATIONTIME" AS "CREATIONTIME",
E1."CURRENTOWNERID" AS "CURRENTOWNERID",
E1."CURRENTOWNERNAME" AS "CURRENTOWNERNAME",
E1."CUSTOMERNAME" AS "CUSTOMERNAME",
E1."CUSTOMERSEGMENT" AS "CUSTOMERSEGMENT",
E1."CUSTOMERSUBSEGMENT" AS "CUSTOMERSUBSEGMENT",
E1."CUSTOMERID" AS "CUSTOMERID",
E1."CUSTOMERREGION" AS "CUSTOMERREGION",
E1."ESCLATIONLEVELFIRED" AS "ESCALATIONLEVELFIRED",
E1."ESTIMATECOMPLETIONDATE" AS "ESTIMATECOMPLETETIME",
E1."OVERALLESTIMATECOMPLETIONDATE" AS "ESTIMATEDCLOSUREDATE",
E1."EVENTTYPE" AS "EVENTTYPE",
E1."EVENTWEIGHT" AS "EVENTWEIGHT",
E1."MODIFIEDBY" AS "MODIFIEDBY",
E1."MODIFIEDDATE" AS "MODIFIEDDATE",
E1."ORDERNUMBER" AS "ORDERNUMBER",
E1."ORDERSTATUS" AS "ORDERSTATUS",
E1."OWNER" AS "OWNER",
E1."PARTYID" AS "PARTYID",
E1."PARTYNAME" AS "PARTYNAME",
E1."PROCESSINSTANCEID" AS "PROCESSINSTANCEID",
E1."PRODUCTDESC" AS "PRODUCTDESCRIPTION",
E1."PRODUCTOFFERINGNAME" AS "PRODUCTOFFERING",
E1."REQUESTID" AS "REQUESTID",
E1."REQUESTEDDATE" AS "REQUESTEDDATE",
E1."REQUESTEDSYSTEM" AS "REQUESTEDSYSTEM",
E1."REVERSALREASON" AS "REVERSALREASON",
E1."SLABREACHED" AS "SLABREACHED",
E1."SMARTSEARCH" AS "SMARTSEARCH",
E1."TICKETNUMBER" AS "TICKETNUMBER",
E1."TRANSACTIONID" AS "TRANSACTIONID",
E1."PRODUCTIDENTIFIER" AS "PRODUCTIDENTIFIER",
C2."BUSINESSSEGMENT" AS "BUSINESSSEGMENT",
E1."PRODUCTIDENTIFIER" AS "COLUMN",
E1."ORDERCHANNEL" AS "ORDERCHANNEL",
E1."OVERALLPROGRESSPRCNT" AS "OVERALLPROGRESSPRCNT",
E1."PROCESSINSTANCEPRCNT" AS "PROCESSINSTANCEPRCNT",
O3."BCRMREQUESTTYPE" AS "BCRMREQUESTTYPE",
O3."OPRODUCTDETAIL_PRODUCTCATEGORY" AS "OPRODUCTDETAILPRODUCTCATEGOR",
O3."OPRODUCTDETAIL_PRODUCTLINE" AS "OPRODUCTDETAILPRODUCTLINE",
O3."OPRODUCTDETAIL_PRODUCTOFFERING" AS "OPRODUCTDETAILPRODUCTOFFERIN",
R4."CBCMREQUESTTYPE" AS "CBCMREQUESTTYPE",
R4."ORDERREFERENCENUMBER" AS "ORDERREFERENCENUMBER"
FROM
"BAMSPAPP"."ETOE_ORDER_TRACKER" E1,
"BAMSPAPP"."CUST_DETAILS" C2,
"BAMSPAPP"."ORDER_LINE_ITEMS_DETAILS" O3,
"BAMSPAPP"."REQUEST_DETAILS" R4
WHERE
(
E1."PROCESSINSTANCEID" = C2."PROCESSINSTANCEID"(+)
)
AND (
E1."PROCESSINSTANCEID" = O3."PROCESSINSTANCEID"(+)
)
AND (
E1."PROCESSINSTANCEID" = R4."PROCESSINSTANCEID"(+)
)
AND E1.CREATIONTIME >= '2019-02-01T00:00:00'
AND E1.CREATIONTIME < = '2019-05-06T00:00:00'
AND (
(
(
E1.ORDERSTATUS = 'Closed'
or E1.ORDERSTATUS = 'Cancelled'
)
and E1.PRODUCTIDENTIFIER = 'Mobile'
and NOT(E1.COMPLETIONTIME < SYSDATE -10)
)
OR (
(
E1.ORDERSTATUS = 'Closed'
or E1.ORDERSTATUS = 'Cancelled'
)
and (
E1.PRODUCTIDENTIFIER IS NULL
or E1.PRODUCTIDENTIFIER != 'Mobile'
)
and NOT(E1.COMPLETIONTIME < SYSDATE -30)
)
OR (
E1.ORDERSTATUS != 'Closed'
and E1.ORDERSTATUS != 'Cancelled'
)
)
order by
E1.CREATIONTIME
) a
where
rownum <= 5000
)
where
rnum >= 0
The error comes from comparing dates with character strings. Assuming CREATIONDATE is a date type field (which it should be) then the error is in the following conditions in your where clause:
E1.CREATIONTIME >= '2019-02-01T00:00:00'
AND E1.CREATIONTIME < = '2019-05-06T00:00:00'
You can solve it as follows:
E1.CREATIONTIME >= to_date('2019-02-01', 'YYYY-MM-DD')
AND E1.CREATIONTIME <= to_date('2019-05-06', 'YYYY-MM-DD')
Or from Oracle 11g onwards:
E1.CREATIONTIME >= date '2019-02-01'
AND E1.CREATIONTIME <= date '2019-05-06'
If however, these fields are character strings (varchar2), then the problem will be with the following condition:
NOT(E1.COMPLETIONTIME < SYSDATE -10)
Then you would need to fix it like this:
NOT(to_date(E1.COMPLETIONTIME, 'YYYY-MM-DD"T"HH24:MI:SS') < SYSDATE -10)
...where the second argument passed to the to_date function must represent the exact format of the field's content.

The multi-part identifier could not be bound

SELECT
TOP (100) PERCENT
dbo.bARCM.CustGroup, dbo.bARCM.Customer,
CASE WHEN udJobType IN ('Scheduled Maintenance', 'Unscheduled Emergency',
'Unscheduled Call Out') THEN 'Maintenance'
WHEN udJobType IN ('Scheduled Special Projects', 'UPS Internal Capital Exp')
THEN 'Capital'
WHEN udJobType LIKE '%Turnaround%' THEN 'T/A'
END AS JobType,
CASE WHEN Factor = 1.0 THEN 'ST'
WHEN Factor = 1.5 THEN 'OT'
WHEN Factor = 2.0 THEN 'OT'
END AS STOT,
SUM(dbo.bJBID.Hours) AS Hours,
DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) as SortMonth
FROM
dbo.bJBID
INNER JOIN
dbo.bJBIN ON dbo.bJBID.JBCo = dbo.bJBIN.JBCo AND dbo.bJBID.BillMonth = dbo.bJBIN.BillMonth AND dbo.bJBID.BillNumber = dbo.bJBIN.BillNumber
INNER JOIN
dbo.bARCM
INNER JOIN
dbo.bJCCM ON dbo.bARCM.CustGroup = dbo.bJCCM.CustGroup AND dbo.bARCM.Customer = dbo.bJCCM.Customer
INNER JOIN
dbo.JCJMPM ON dbo.bJCCM.JCCo = dbo.JCJMPM.JCCo AND dbo.bJCCM.Contract = dbo.JCJMPM.Contract ON dbo.bJBIN.JBCo = dbo.JCJMPM.JCCo AND
dbo.bJBIN.Contract = dbo.JCJMPM.Contract
INNER JOIN
dbo.bJCCT ON dbo.bJBID.CostType = dbo.bJCCT.CostType AND dbo.bJBID.PhaseGroup = dbo.bJCCT.PhaseGroup
INNER JOIN
dbo.budAcctMonths ON dbo.budAcctMonths.Month = dbo.bJBIN.BillMonth
WHERE
(dbo.bJCCM.JCCo = 1)
AND (dbo.bJBID.CostType IN (1, 41, 42, 43, 44, 45, 46))
AND (dbo.bJBID.CostTypeCategory = 'L')
AND (dbo.JCJMPM.udPlantLocation LIKE 'Deer%')
AND (dbo.bARCM.Name LIKE 'Dow%' OR dbo.bARCM.Name LIKE 'Rohm%')
GROUP BY
dbo.bARCM.CustGroup, dbo.bARCM.Customer,
dbo.JCJMPM.udJobType, dbo.bJBID.Factor, dbo.SortMonth
HAVING
(dbo.bARCM.CustGroup = 1) AND (SUM(dbo.bJBID.Hours) <> 0)
When I excute this query, I get
The multi-part identifier "dbo.SortMonth" could not be bound
error message. I am new to SQL, need some help.
Your SELECT is assigning a alias of SortMonth to the following DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) but you cannot use an alias in GROUP BY unless it was named in a subquery.
You will need to change the code to:
GROUP BY dbo.bARCM.CustGroup,
dbo.bARCM.Customer,
dbo.JCJMPM.udJobType,
dbo.bJBID.Factor,
DATEADD(MONTH, DATEDIFF(MONTH, 0, dbo.bJBID.JCDate), 0) -- use the DATEADD code here not the alias