Check data condition with multiple row and get only single output value
Status
SUCCESSFULL
SUCCESSFULL
SUCCESSFULL
SUCCESSFULL
Then need Output "SUCCESSFULL"
IF any record have "Failed" status then output will be "Failed" as below
Status
SUCCESSFULL
SUCCESSFULL
FAILED
SUCCESSFULL
Then need Output "FAILED"
Are you merely looking for this:
select min(status) from mytable;
('FAILED' comes before 'SUCCESSFULL' in the alphabet, so with MIN you get 'FAILED' when at least one row is 'FAILED' and 'SUCCESSFULL' only if all rows are 'SUCCESSFULL'.)
You can use aggregation functions count() and sum() to achieve this.
select
case when
sum(case when status = 'SUCCESSFULL' then 1 else 0 end) = count(1)
then 'SUCCESSFULL'
else 'FAILED'
end
from tableA
SELECT DISTINCT Status
FROM Your_table
ORDER BY status
Related
As you can see in the screenshot below, there are multiple account numbers that have either a "successful" or "fail" outcome. Some account numbers have multiple entries like account_number "413655". For accounts like "413655", where one outcome is Success, and the other outcome is fail, I want to create a new column that shows "success" for both entries. Only accounts that have all fail outcomes should display "fail" in this new column "Distinct_count". The rest should display "success".
SCREENSHOT: https://i.stack.imgur.com/NQZmY.jpg
Please find my query below.(I have bolded the part that needs to be edited)
-- #WbResult v_tcci_collection_activity_fact
SELECT date_dim_id,
B.user_key,
B.product_type,
B.dealer_nbr,
(CASE B.make
WHEN 'TOYOTA' THEN 'TOYOTA'
WHEN 'SUBARU' THEN 'SUBARU'
WHEN 'LEXUS' THEN 'LEXUS'
ELSE 'OTHER'
END),
C.dealer_name,
C.zone,
activity_date,
activity_time,
activity_code,
(CASE WHEN len(b.loan_nbr) > 3
THEN b.loan_nbr
ELSE b.lease_nbr END)
AS account_number,
(CASE WHEN activity_code IN ('SHPS','SBPS','SOPS','SHCS','SBCS') THEN 'Successful'
ELSE 'Fail' END)
AS outcome,
**(CASE WHEN outcome = 'Successful' AND outcome = 'Fail' OR outcome = 'Successful' THEN 'Successful'
ELSE 'Fail' END)
AS distinct_count**
FROM dm_business_ops_tcci.v_tcci_collection_activity_fact A
left join dm_business_ops_tcci.v_tcci_collection_account_dim B
on A.collection_account_dim_id = B.collection_account_dim_id
left join dm_business_ops_tcci.v_tcci_dealer_dim C
on A.dealer_dim_id = C.dealer_dim_id
where activity_code IN ('SBCS','SOPF','SBPS','SOPS','SHPF','SHPS','SBCF','SBPF','SHCF','SHCS')
It's difficult to parse through the whole query in your example, but in your scenario a Windowing Function is a good way to achieve this scenario.
Your query won't work as it's only doing things row-by-row, so you need to be able to take in the details from the other row to make it happen.
In your case you want to use a Windowing Function like the following (simplified from your specific example):
MAX(outcome) OVER (PARTITION BY account_number) AS outcome_overall
Here I am being a little cheap and nothing that Successful comes after Fail in the dictionary to utilize MAX. But with this, outcome_overall is calculated by:
Partitioning the dataset into separate chunks based on account_number.
Within each partition, it finds the MAX(outcome).
outcome_overall is that value from #2, repeated for each row in that partition.
Example data and what outcome_overall would be:
account_number
outcome
outcome_overall
A1
Successful
Successful
A1
Fail
Successful
A2
Fail
Fail
A2
Fail
Fail
A3
Successful
Successful
A4
Fail
Fail
So I have a database here with a table that lists off whether or not certain processes have failed. There are two columns, IsProcessed, and IsFailed. A failed process can still be considered processed if the error was handled, but I still need to recognize that it failed. They're both bit values, and so I have to try and grab and separate them despite that they may depend on one another. After they've been separated out, I need to count the relative successes and relative failures.
I utilize an AND statement in my WHERE clause to try and separate out the successes from the failures. I honestly have no idea where to go from here.
SELECT CAST(PQ.ProcessedDate AS Date) AS Date, COUNT(PQ.IsProcessed) AS Successes
FROM PQueue PQ
WHERE PQ.ProcessDate BETWEEN '2019-10-1' AND '2019-10-31' AND PQ.IsFailed = 0 AND PQ.IsProcessed = 1
GROUP BY CAST(PQ.ProcessDate AS Date)
ORDER BY CAST(PQ.ProcessDate AS Date) ASC
Because a failed process can still be processed in the system, we have to do a check first to try and grab the data that was processed but didn't flag a failure. Now I need to try and find a way to not exclude the failures, but include them and place them in a group. I can do the group part, but I'm relatively new to SQL so I don't know whether or not I can place something in an IF statement somewhere or try to use variables to get this done. Thank you in advance.
You seem to want conditional aggregation:
SELECT CAST(PQ.ProcessedDate AS Date) AS Date,
SUM(CASE WHEN PQ.IsFailed = 0 AND PQ.IsProcessed = 1 THEN 1 ELSE 0 END) as Successes,
SUM(CASE WHEN PQ.IsFailed = 1 AND PQ.IsProcessed = 1 THEN 1 ELSE 0 END) as Fails
FROM PQueue PQ
WHERE PQ.ProcessDate BETWEEN '2019-10-1' AND '2019-10-31'
GROUP BY CAST(PQ.ProcessDate AS Date)
ORDER BY CAST(PQ.ProcessDate AS Date) ASC
If SQL Server then maybe a CASE statement would help you out.
eg
SELECT ...........
CASE
WHEN IsFailed = 1 AND IsProcessed = 1 THEN "Processed But Failed"
WHEN IsFailed = 0 AND IsProcessed = 0 THEN "Not Processed"
WHEN IsFailed = 0 AND IsProcessed = 1 THEN "Processed Succesfully"
WHEN IsFailed = 1 AND IsProcessed = 0 THEN "Failed"
END as REsult
I have a table incident that has a status that is a string.
I want to query all incidents with a custom sort order.
A status can be one of the following: inProgress, completed, canceled
I want to be able to have a sort that is custom. Let the client specify the sort order. I am having problems with the query itself though.
I've tried a few things:
SELECT *
FROM incident as i
ORDER BY array_position(array["inProgress", "completed", "canceled"], i.status)
SELECT *
FROM incident as i
ORDER BY case when status = "inProgress" then 0
case when status = "completed" then 1
case when status = "canceled" then 2
else 3
I get the error Unhandled rejection SequelizeDatabaseError: column "inProgress" does not exist on all of my attempts.
I'm expecting inProgress to be a value of status, but I'm not sure what I'm doing wrong.
Check documentation for the right sintaxis. And text use single quotes. Double quotes is for fieldnames
ORDER BY case
when status = 'inProgress' then 0
when status = 'completed' then 1
when status = 'canceled' then 2
else 3
end
I'm trying to create a SQL Server query based on the following criteria:
The query focuses on three columns: Report_Status, Error_Message, and Create_Date. The purpose of the query is to filter the top 100 most recent results based on the Create_Date. Once that's done, it needs to see if EVERY row in Report_Status in that top 100 says 'Failed' AND that Error_Message does not contain 'Placement is missing%'.
If it meets those conditions, then it needs to output the message "Potential service failure." If it doesn't meet those conditions, then it either needs to do nothing or output something normal, like "No problems found."
I figured a Case might be the best way to do this, so I tried it out. I'm having trouble getting it to work, though:
select Report_Status, Error_Message, Create_Date,
case
when Report_Status = 'Failed' and Error_Message not like 'Placement is missing%' then 'Potential service failure.'
ELSE 'No problems found.'
end
from [AUDIT_TABLE] limit 100
Is this the best way to approach this problem? If so, what do I need to change so this works? If it's not a good way, what's a better way to tackle the problem?
You would appear to want something like this:
select (case when count(*) = 100 then 'Potential service failure.'
else 'No problems found.'
end) as summary
from (select a.*
from [AUDIT_TABLE]
order by date desc
fetch first 100 rows only
) t100
where Report_Status = 'Failed' and
Error_Message not like 'Placement is missing%'
I ended up working with a coworker to solve this. Gordon Linoff's CASE section was great, but we changed how we searched for the most recent 100 records by also using the Report_ID field.
select
(case when count(*) = 100 then 'Potential failure.'
else 'No problems found.'
end) as Result
from Audit_Table
where Report_Status = 'fail'
and Error_Message not like 'Placement is missing%'
and Report_ID >= (select min(Report_ID) from (select top 100 * from Audit_Table order by Report_ID desc ) t100)
Trying to automatically produce a piechart in access which will show me 3 values (completed, pending and queued)
However in my status field I have several values for pending (sent, monitoringStage1, monitoringStage2, finalApproval)
My query at present gives me the count of each item individually:
SELECT Status, Count(*) AS [Count]
FROM StatusTable
GROUP BY Status
ORDER BY Status;
How would I edit it to count sent, monitoringStage1, monitoringStage2, and finalApproval as one item called pending?
Also on a side note, does anybody know how to put a line at a certain point on a piechart by percentage? So in the piechart created I could have a line to indicate the target number of completed items to compare against current progress.
You can't use CASE in a query, only in VBA.
This will take care of it:
SELECT qryTestx.ThisStatus, Count(qryTestx.Status) AS CountOfStatus
FROM (SELECT (IIf([StatusTable.Status] in ("sent","monitoringStage1","monitoringStage2","finalApproval"),"Pending",[StatusTable.Status])) AS ThisStatus, StatusTable.Status
FROM StatusTable) qryTestx
GROUP BY qryTestx.ThisStatus;
Try this and don't forget to vote ;)
select
count(*),
case
when Status in ( 'sent', 'monitoringStage1', 'monitoringStage2', 'finalApproval') then 'pending'
else status
end as status
from StatusTable
group by
case
when Status in ( 'sent', 'monitoringStage1', 'monitoringStage2', 'finalApproval') then 'pending'
else status
end