SQL avg of comma occurrencies with condition - mysql

I have a table of events where partecipants are listed as a comma separated list of IDs:
0,4,21,33,41
I'm trying to perform the following query to retrieve the average partecipants number for event only when status is equal to 1.
I prepared the following but doesn't work, anyone can help me?
SELECT avg(case when (status = 1 then LENGTH(REPLACE(listofPartecipants, ',', '')) end) avgPartecipants FROM events;
Many thanks

This is the expression if you want to count the number of elements in a list:
SELECT avg(case when status = 1
then LENGTH(REPLACE(listofPartecipants, ',', 'XX')) - length(listofPartecipants) + 1
end) as avgPartecipants
FROM events;
More important than getting this arcane string logic right is fixing your data model. You should not be storing list of ids in a comma-delimited list. You should have a table with one row per event and per participant.

Related

SQL : SELECT SUM WHERE CONDITION

I've got some troubles about SQL request :
I have a table like this table data image
I would like to create a view from this table to get :
Time_A : SUM of a column (total_time_taken) WHERE column (is_radiant)=1
Time_B : SUM of the same column (total_time_taken) WHERE column (is_radiant)=0
Time_AB : SUM of the column (total_time_taken) WHERE column (is_radiant)=0 OR (is_radiant)=1
SELECT
SUM(`matchpickban_radiant`.`total_time_taken`) AS `draft_time_radiant`,
SUM(`matchpickban_dire`.`total_time_taken`) AS `draft_time_radiant`
FROM
(`matchpickban` AS `matchpickban_radiant`
JOIN `matchpickban` AS `matchpickban_dire` ON ((`matchpickban_dire`.`idmatchpickban` = `matchpickban_radiant`.`idmatchpickban`)))
WHERE
`matchpickban_radiant`.`is_radiant` = 1
AND `matchpickban_dire`.`is_radiant` = 0
Actually I can run this request without syntax error but the result is NULL cause no data can be equal to 0 AND equal to 1 in the same time, obviously...
Also, I don't know if it's possible to make a JOIN the table to itself as I did (matchpickban JOIN matchpickban).
If syntax is correct I need to place my WHERE CONDITION away but don't know how, is it possible to replace it with 2 IF statement (IF is_radiant=0 SUM(...))
Thx for reading and helping me about this issue I got !
If you need more info about table or request I will give you all you need !
No need for a self-join or complex logic, you can just use conditional aggregation, which consists in using conditional expression within aggregate functions.
In MySQL, you could go:
select
sum(is_radiant * total_time_taken) time_a,
sum((1 - is_radiant) * total_time_taken) time_b,
sum(total_time_taken) time_ab
from matchpickban
where is_radiant in (0, 1)
This works because is_radiant is made of 0/1 values only - so this simplifies the logic. A more canonical way to phrase the conditional sums would be:
sum(case when is_radiant = 1 then total_time_taken else 0 end) time_a,
sum(case when is_radiant = 0 then total_time_taken else 0 end) time_b,

To get total count based on condition

I am struggling to get the count of a particular person. As I am new to tableau I don't know how to write the condition for this query.
SELECT COUNT(*) as cnt
FROM
Expert_CollaborationsRequests
WHERE
ExpertID=3 AND
IsAccepted = 1
Generate a calculated field as:
Calculation1: IF ExpertID=3 AND IsAccepted = 1 THEN 1 ELSE 0 END
Then, place this field on a shelf (row, columns, etc.) and select Measure (Sum).
You can use LOD (Level of Detail) like so:
{ FIXED [ExpertID]: SUM(IF [IsAccepted]= 1 THEN 1 ELSE 0 END) }
This will give you aggregated values for each user.
For more information on LOD, please follow official Tableau help link here
SELECT COUNT(ExpertID),COUNT(IsAccepted ) as cnt
FROM
Expert_CollaborationsRequests
WHERE
ExpertID=3 OR
IsAccepted = 1

Get Multi Columns Count in Single Query

I am working on a application where I need to write a query on a table, which will return multiple columns count in a single query.
After research I was able to develop a query for a single sourceId, but what will happen if i want result for multiple sourceIds.
select '3'as sourceId,
(select count(*) from event where sourceId = 3 and plateCategoryId = 3) as TotalNewCount,
(select count(*) from event where sourceId = 3 and plateCategoryId = 4) as TotalOldCount;
I need to get TotalNewCount and TotalOldCount for several source Ids, for example (3,4,5,6)
Can anyone help, how can I revise my query to return a result set of three columns including data of all sources in list (3,4,5,6)
Thanks
You can do all source ids at once:
select source_id
sum(case when plateCategoryId = 3 then 1 else 0 end) as TotalNewCount,
sum(case when plateCategoryId = 4 then 1 else 0 end) as TotalOldCount
from event
group by source_id;
Use a where (before the group by) if you want to limit the source ids.
Note: The above works in both Vertica and MySQL, and being standard SQL should work in any database.

Counting comma separated values in TSQL

SCHEMA / DATA for TABLE :
SubscriberId NewsletterIdCsv
------------ ---------------
11 52,52,,52
We have this denormalized data, where I need to count the number of comma separated values, for which I am doing this :
SELECT SUM(len(newsletteridcsv) - len(replace(rtrim(ltrim(newsletteridcsv)), ',','')) +1) as SubscribersSubscribedtoNewsletterCount
FROM TABLE
WHERE subscriberid = 11
Result :
SubscribersSubscribedtoNewsletterCount
--------------------------------------
4
The problem is some of our data has blanks / spaces in between the comma separated values, if I run the above query the expected result should be 3 (as one of the value is blank space), how do I check in my query to exclude the blank spaces?
EDIT :
DATA :
SubscriberId NewsletterIdCsv
------------ ---------------
11 52,52,,52
12 22,23
I need to get an accumulative SUM instead of just each rows sum, so for this above data I need to have just a final count i.e. 5 in this case, excluding the blank space.
Here's one solution, although their may be a more efficient way:
SELECT A.[SubscriberId],
SUM(CASE WHEN Split.a.value('.', 'VARCHAR(100)') = '' THEN 0 ELSE 1 END) cnt
FROM
(
SELECT [SubscriberId],
CAST ('<M>' + REPLACE(NewsletterIdCsv, ',', '</M><M>') + '</M>' AS XML) AS String
FROM YourTable
) AS A
CROSS APPLY String.nodes ('/M') AS Split(a)
GROUP BY A.[SubscriberId]
And the SQL Fiddle.
Basically it converts your NewsletterIdCsv field to XML and then uses CROSS APPLY to split the data. Finally, using CASE to see if it's blank and SUM the non-blank values. Alternatively, you could probably build a UDF to do something similar.

Select data which have same letters

I'm having trouble with this SQL:
$sql = mysql_query("SELECT $menucompare ,
(COUNT($menucompare ) * 100 / (SELECT COUNT( $menucompare )
FROM data WHERE $ww = $button )) AS percentday FROM data WHERE $ww >0 ");
$menucompare is table fields names what ever field is selected and contains data bellow
$button is the week number selected (lets say week '6')
$ww table field name with row who have the number of week '6'
For example, I have data in $menucompare like that:
123456bool
521478bool
122555heel
147788itoo
and I want to select those, who have same word in the last of the data and make percentage.
The output should be like that:
bool -- 50% (2 entries)
heel -- 25% (1 entry)
itoo -- 25% (1 entry)
Any clearness to my SQL will be very appreciated.
I didn't find anything like that around.
Well, keeping data in such format probably not the best way, if possible, split the field into 2 separate ones.
First, you need to extract the string part from the end of the field.
if the length of the string / numeric parts is fixed, then it's quite easy;
if not, you should use regular expressions which, unfortunately, are not there by default with MySQL. There's a solution, check this question: How to do a regular expression replace in MySQL?
I'll assume, that numeric part is fixed:
SELECT s.str, CAST(count(s.str) AS decimal) / t.cnt * 100 AS pct
FROM (SELECT substr(entry, 7) AS str FROM data) AS s
JOIN (SELECT count(*) AS cnt FROM data) AS t ON 1=1
GROUP BY s.str, t.cnt;
If you'll have regexp_replace function, then substr(entry, 7) should be replaced to regexp_replace(entry, '^[0-9]*', '') to achieve the required result.
Variant with substr can be tested here.
When sorting out problems like this, I would do it in two steps:
Sort out the SQL independently of the presentation language (PHP?).
Sort out the parameterization of the query and the presentation of the results after you know you've got the correct query.
Since this question is tagged 'SQL', I'm only going to address the first question.
The first step is to unclutter the query:
SELECT menucompare,
(COUNT(menucompare) * 100 / (SELECT COUNT(menucompare) FROM data WHERE ww = 6))
AS percentday
FROM data
WHERE ww > 0;
This removes the $ signs from most of the variable bits, and substitutes 6 for the button value. That makes it a bit easier to understand.
Your desired output seems to need the last four characters of the string held in menucompare for grouping and counting purposes.
The data to be aggregated would be selected by:
SELECT SUBSTR(MenuCompare, -4) AS Last4
FROM Data
WHERE ww = 6
The divisor in the percentage is the count of such rows, but the sub-stringing isn't necessary to count them, so we can write:
SELECT COUNT(*) FROM Data WHERE ww = 6
This is exactly what you have anyway.
The divdend in the percentage will be the group count of each substring.
SELECT Last4, COUNT(Last4) * 100.0 / (SELECT COUNT(*) FROM Data WHERE ww = 6)
FROM (SELECT SUBSTR(MenuCompare, -4) AS Last4
FROM Data
WHERE ww = 6
) AS Week6
GROUP BY Last4
ORDER BY Last4;
When you've demonstrated that this works, you can re-parameterize the query and deal with the presentation of the results.