Sql loop over unique elements of a column - sql

I am trying to get data of rows into columns. This is my data before query.
This is my query.
SELECT
Date,
SUM(CASE WHEN (Terms="Art") THEN 1 ELSE 0 END) AS Art,
SUM(CASE WHEN (Terms="Action") THEN 1 ELSE 0 END) AS Action,
SUM(CASE WHEN (Terms="Board") THEN 1 ELSE 0 END) AS Board,
SUM(CASE WHEN (Terms="Puzzle") THEN 1 ELSE 0 END) AS Puzzle,
SUM(CASE WHEN (Terms="Adventure") THEN 1 ELSE 0 END) AS Adventure,
SUM(CASE WHEN (Terms="Others") THEN 1 ELSE 0 END) AS Others
FROM
__table__
GROUP BY
Date
After Query this is my data.
Which is fine and intended data. But problem is in query as you can see I have to explicitly write each term. I want to automate that using loop. Theoretical solution is to write subquery to get all unique terms and then loop over them. But I don't know how.

Related

Optimization of text parsing - Oracle SQL

I am working on a SQL query that will count the appearances of certain words in "long text", or a huge text field that is a CLOB data type.
My dataset (which is massive, ~5M+ rows) looks something like this:
http://sqlfiddle.com/#!4/2c13d/1
I have a query, like this:
SELECT
TheTask AS Tasking,
SUM(CASE WHEN TRIM(UPPER(TheTaskText)) LIKE '%LONG%' THEN 1 ELSE 0 END) AS LongCount,
SUM(CASE WHEN TRIM(UPPER(TheTaskText)) LIKE '%TEXT%' THEN 1 ELSE 0 END) AS TextCount,
SUM(CASE WHEN TRIM(UPPER(TheTaskText)) LIKE '%ENGLISH%' THEN 1 ELSE 0 END) AS EnglishCount
FROM
example
GROUP BY
TheTask
However, it takes an extremely long time to run on the complete dataset (~3 hours or so). I believe this is due to LIKE optimization issues, but I am unsure of how else to achieve this goal dataset. I have tried researching other articles on how to optimize like, but is it possible that REGEX or something would be quicker? I am looking to optimize this query by evaluating LIKE performance.
The CONTEXT index type is used to index long texts. You can use :
CREATE INDEX idx_TheTaskTxt ON example(TRIM(UPPER(TheTaskText))) INDEXTYPE IS CTXSYS.CONTEXT;
and collect statistics for the optimizer to take effect :
EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'EXAMPLE', cascade=>TRUE);
and call
SELECT
TheTask AS Tasking,
SUM(CASE WHEN CONTAINS(TRIM(UPPER(TheTaskText)), 'LONG', 1) > 0 THEN 1 ELSE 0 END) AS LongCount,
SUM(CASE WHEN CONTAINS(TRIM(UPPER(TheTaskText)), 'TEXT', 1) > 0 THEN 1 ELSE 0 END) AS TextCount,
SUM(CASE WHEN CONTAINS(TRIM(UPPER(TheTaskText)), 'ENGLISH', 1) > 0 THEN 1 ELSE 0 END) AS EnglishCount
FROM example
GROUP BY TheTask
HAVING
SUM(CASE WHEN CONTAINS(TRIM(UPPER(TheTaskText)), 'LONG', 1) > 0 THEN 1 ELSE 0 END) *
SUM(CASE WHEN CONTAINS(TRIM(UPPER(TheTaskText)), 'TEXT', 1) > 0 THEN 1 ELSE 0 END) *
SUM(CASE WHEN CONTAINS(TRIM(UPPER(TheTaskText)), 'ENGLISH', 1) > 0 THEN 1 ELSE 0 END)
IN (0,1)

How to add columns from another table using a SELECT

I have a table named Action, which I want to add new columns to it.
The columns that I want to add are the result of this query :
SELECT DATE_AFFECTATION ,
SUM(CASE WHEN STATUT_Ticket = 'Clôturé' THEN 1 ELSE 0 END) AS TotalCloturé,
SUM(CASE WHEN STATUT_Ticket = 'En cours' THEN 1 ELSE 0 END) AS TotalEncours,
SUM(CASE WHEN STATUT_Ticket = 'Gelé' THEN 1 ELSE 0 END) AS TotalGelé,
SUM(CASE WHEN STATUT_Ticket = 'Hors délai' THEN 1 ELSE 0 END) AS TotalHorsdélai,
SUM(CASE WHEN STATUT_Ticket = 'Nouveau' THEN 1 ELSE 0 END) AS TotalNouveau,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Clôturé' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalCloturé,
SUM(Sum(CASE WHEN STATUT_Ticket = 'En cours' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalEncours,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Gelé' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalGelé,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Hors délai' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalHorsdélai,
SUM(Sum(CASE WHEN STATUT_Ticket = 'Nouveau' THEN 1 ELSE 0 END)) Over (ORDER BY DATE_AFFECTATION) AS cumTotalNouveau
FROM DIM_Ticket
GROUP BY DATE_AFFECTATION
ORDER BY DATE_AFFECTATION
Is this possible?
This is too long for a comment.
If you want to add columns to an existing table, then the syntax is alter table. For instance:
alter table add column TotalCloturé int;
The default values are NULL or you can provide a constant default (such as 0 in this case).
You don't add columns with a query.
Hence, your question is quite unclear. Are you simply trying to assign values to existing columns? Do you just want a result set with these column values in addition to the columns in the original table? Do you need triggers (or some other mechanism) so the values remain synchronized?
Your query return more than one column.
You can't add a column and fill it in a single Query.

SQL percentage with rows same table with different where condition

I want to do a query like:
select
count(asterisk) where acción='a'/count(asterisk) where acción='b' * 100
from
same_table
grouped by day
but I don't want use subquery, is it possible with joins?
I`m not sure the syntax is correct, but you can use something like this:
SELECT day,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A,
SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) AS SUM_B,
SUM(CASE WHEN "acción" = 'a' THEN 1 ELSE 0 END) AS SUM_A / SUM(CASE WHEN "acción" = 'b' THEN 1 ELSE 0 END) * 100 AS result
FROM your_table
GROUP BY day
The concept is to actually sum the the values that you need, instead of count.

SQL Do not return column if value is zero

This query returns one row with columns Ready, Processing, Complete, Failed and Error with totals for each. Is there a way to rewrite this query so that columns that have a total of zero are not returned?
I'm using this to populate the mschart control and I don't wan't labels on the chart if there are 0 instances of that category.
SELECT
SUM(CASE WHEN Status = 'R' THEN 1 ELSE 0 END) AS Ready,
SUM(CASE WHEN Status = 'P' THEN 1 ELSE 0 END) AS Processing,
SUM(CASE WHEN Status = 'C' THEN 1 ELSE 0 END) AS Complete,
SUM(CASE WHEN Status = 'F' THEN 1 ELSE 0 END) AS Failed,
SUM(CASE WHEN Status = 'E' THEN 1 ELSE 0 END) AS Error
FROM MailDefinition
No, because the shape of the query (the fields it contains) has to be known. Only the data can change, and that is what you should be looking for. You can dynamically remove or hide labels based on 0 or null data in a column.
What I would do is take what you have, throw it into an unpivot, then remove all of the 0 records.
select
Type,
Sum
from
(
SELECT
SUM(CASE WHEN Status = 'R' THEN 1 ELSE 0 END) AS Ready,
SUM(CASE WHEN Status = 'P' THEN 1 ELSE 0 END) AS Processing,
SUM(CASE WHEN Status = 'C' THEN 1 ELSE 0 END) AS Complete,
SUM(CASE WHEN Status = 'F' THEN 1 ELSE 0 END) AS Failed,
SUM(CASE WHEN Status = 'E' THEN 1 ELSE 0 END) AS Error
FROM MailDefinition
) a
unpivot
(
Sum for Type in ([Ready],[Processing],[Complete],[Failed],[Error])
) u
where Sum>0
That does, of course, entail changing your chart some.

select sum statement

I created this select statement I will convert to a view. I need help with this. I need to be able to add the total of Minority that = Yes and No show total on report pages.
select
ps.BidPackage_ID,
ps.Project_ID,
SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotal,
SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
ps.SubContractor
from
PersonnelSummary ps
group by
ps.BidPackage_ID,
ps.Project_ID,
ps.SubContractor
You nearly have it:
...
SUM (case ps.Minority when 'Yes' then 1 else 0 end) AS MinorityYes,
SUM (case ps.Minority when 'No' then 1 else 0 end) AS MinorityNo,
COUNT(*) AS Total,
...
With the Total I'm assuming that every row should be counted. This is what you want if:
The only values that exist in the column are 'Yes' and 'No' or
Values different from 'Yes' and 'No' should also be counted in the total.
You're forcing us to guess what you want. You have a count of the people who said that they were in a minority; do you want a count of the people who said No? Or do you want a count of the number who said 'either "Yes" or "No"' and excluding those who gave 'decline to say' or simply no answer at all?
select
ps.BidPackage_ID,
ps.Project_ID,
SUM (case ps.Minority when 'Yes' then 1 else 0 end) MinorityTotalYes,
SUM (case ps.Minority when 'No' then 1 else 0 end) MinorityTotalNo,
SUM (case ps.Minority when 'Yes' then 1 when 'No' then 1 else 0 end)
AS StatedMinorityTotal,
SUM (case ps.Gender when 'Female' then 1 else 0 end) FemaleTotal,
SUM(case ps.Cleveland_Resident when 1 then 1 else 0 end) ClevelandResidents,
ps.SubContractor
from
PersonnelSummary ps
group by
ps.BidPackage_ID,
ps.Project_ID,
ps.SubContractor