Excluding email lists while selecting specific values at once - mysql

I am trying to do 2 things here within 1 SELECT query (I know I could do this in 2 steps but I want to avoid intermediary tables):
Select specific values (M and W only — there are other values possible but I just want these 2 and not other) from a TABLE 'anothertable'.
While avoiding selecting opt-outs and bounced emails (11k rows, <5 columns).
I am not sure if the syntax is correct since the query is loading for ever. Size should not be an issue though (27k rows, <5 columns and 2 core processors).
BEGIN
CREATE TABLE `mytable` AS (
SELECT * FROM `anothertable` A
WHERE NOT EXISTS (SELECT Email FROM `optout` B WHERE B.Email = A.Email)
AND NOT EXISTS (SELECT Email FROM `allbounced` C WHERE C.emailbounced = A.Email)
GROUP BY Email
HAVING
(SUM(CASE WHEN target = 'M' THEN 1 ELSE 0 END) > 0 OR -- MAN is present
SUM(CASE WHEN target = 'W' THEN 1 ELSE 0 END) > 0 AND -- WOMAN is present
SUM(CASE WHEN target NOT IN ('M', 'W') THEN 1 ELSE 0 END) = 0) -- only MAN or WOMAN
);
END

When possible you should use NOT IN instead of NOT EXISTS. This should be faster:
BEGIN
CREATE TABLE `mytable` AS (
SELECT Email FROM `anothertable` A
WHERE Email NOT IN (SELECT Email FROM `optout`)
AND Email NOT IN (SELECT emailbounced FROM `allbounced`)
GROUP BY Email
HAVING
(SUM(CASE WHEN target = 'M' THEN 1 ELSE 0 END) > 0 OR -- MAN is present
SUM(CASE WHEN target = 'W' THEN 1 ELSE 0 END) > 0 AND -- WOMAN is present
SUM(CASE WHEN target NOT IN ('M', 'W') THEN 1 ELSE 0 END) = 0) -- only MAN or WOMAN
);
END

Related

join does not return expected result in mysql

I have data that looks like,
id tag value
1 C 2000
1 C 2000
2 C 2000
1 D 1500
1 D 1200
3 D 2500
I have a requirement where i should get a net value based on the tags sum(D)- sum(C)
My query,
select 'C' as tag,t1.Debit - t.Credit as result from
(SELECT `tag` , sum(value) as Credit
FROM
test
WHERE
tag = 'C' group by `tag` ) t
join (
SELECT `tag` , sum(value) as Debit
FROM
test
WHERE
tag = 'D' group by `tag`
)t1;
gives the result as,
tag result
C -800 -- (5200 - 6000)
which the correct result given the data above.
However should all tags be D the query returns no values,but i expect the result as 11200 which is the total of D.
dbfiddle
SELECT SUM(CASE WHEN tag='D' THEN value ELSE 0 END)
- SUM(CASE WHEN tag='C' THEN value ELSE 0 END)
FROM test
fiddle
You can try something like this:
select sum(CASE WHEN tag='D' THEN value ELSE 0 END)-sum(CASE WHEN tag='C' THEN value ELSE 0 END) as val from test;
If you are going to have only 'D' or 'C' as tag.
then
select sum(CASE WHEN tag='D' THEN value ELSE -value END) as val from test;

Count different values in a column, while doing total and group by different column values

We have a table with data from different nodes and one of the column will have status report as "compliant or non-compliant", sample data as below
I want to filter the table in such a way that if any of the checks on a node shows non compliant, it should be flagged as non-compliant and rest as compliant. Using below query i am able to do it
SELECT COUNT(*) AS total_nodes,
SUM(fully_compliant = 0) AS Non_compliant_nodes,
SUM(fully_compliant = 1) AS compliant_nodes
FROM (
SELECT Node, CASE WHEN SUM(Status = 'Compliant') = COUNT(*) THEN 1 ELSE 0 END AS fully_compliant
FROM your_table GROUP BY Node
)
Now, i want to group and split the result by dept as below, how can i achieve this
I think you're looking for this:
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
sum(case when 'Non-Compliant' then 1 else 0 end) as non_compliant_chk
from your_table
group by dept,
node
) v
group by dept;
With few modifications to what Brian suggested, I am able to get the desired result
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
COUNT(CASE WHEN Compliance-Status = 'Non-Compliant' THEN 1 END) 'non_compliant_chk'
from table WHERE DOR >= DATE(NOW()) - INTERVAL 7 DAY
group by Dept,
Node
) v
group by Dept;

Return 1 or 0 in SQL depending on the multiple statements

If I find that some of the user exists with such a parameters, I want to get 1 otherwise 0. In the future I'll have to add more blocks. But it doesn't seem to work now. What am I doing wrong?
SELECT CAST(CASE WHEN EXISTS(SELECT 1
FROM Customers
WHERE Country = 'France' AND PostalCode%2 = 0)
OR (WHERE Country = 'Germany' AND PostalCode%2 = 0))
)
THEN 1
ELSE 0
END AS BIT)
You need two separate exists:
SELECT CAST(CASE WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country = 'France' AND PostalCode%2 = 0
)
THEN 1
WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country = 'Germany' AND PostalCode%2 = 0
)
THEN 1
ELSE 0
END AS BIT)
Actually, I broke this into two separate THEN clauses. This is almost equivalent to using OR, but because the logic is inside a CASE, THEN seems more natural. (The difference is that the optimizer could choose to re-arrange the OR conditions, but the THEN conditions are executed in lexical order.)
If your statements are actually this simple, you can combine them as:
SELECT CAST(CASE WHEN EXISTS (SELECT 1
FROM Customers
WHERE Country IN ('France', 'Germany') AND PostalCode%2 = 0
)
THEN 1
ELSE 0
END AS BIT)
It looks to me like you're just having issues with your bracketing:
SELECT CAST(
CASE WHEN EXISTS(
SELECT 1
FROM Customers
WHERE (Country = 'France' AND PostalCode%2 = 0)
OR (Country = 'Germany' AND PostalCode%2 = 0)
) THEN 1 ELSE 0 END AS BIT)
Building on Gordon's assumption that PostalCode%2 = 0 for all tested 'sets' of conditionals (you haven't said as much yet), you could likewise shorten this to:
SELECT CAST(
CASE WHEN EXISTS(
SELECT 1
FROM Customers
WHERE PostalCode%2 = 0
AND Country IN ('France', 'Germany')
) THEN 1 ELSE 0 END AS BIT)

Appending data from different tables - mysql

I have the following tables Job description and Candidate.
Each job description can be related to n candidates. The candidates are obtained from different sources(a, b, c d) - candidate.source.
I want a single query to list me the JD ids, with count of candidates for each source for each JD, as shown below:
JD id | candidate name | count of candidates - source a | count of candidates - source b | count of candidates - source | c..............
Try using the query as a template:
select
JobDescriptionName,
SUM(ACount) CountOfCandidatesOfA ,
SUM(BCount) CountOfCandidatesOfB,
SUM(CCount) CountOfCandidatesOfC ,
SUM(DCount) CountOfCandidatesOfD
from
( select JobDescriptionID, (CASE WHEN Source = 'a' THEN 1 ELSE 0 END) AS ACount,
(CASE WHEN Source = 'b' THEN 1 ELSE 0 END) AS BCount,
(CASE WHEN Source = 'c' THEN 1 ELSE 0 END) AS CCount,
(CASE WHEN Source = 'd' THEN 1 ELSE 0 END) AS DCount
from Candidate) AS DerivedCandidate
inner join JobDescription ON JobDescription.JobDescriptionID = DerivedCandidate.JobDescriptionID group by JobDescriptionID;
I know there is already an accepted answer but in the effort of learning myself more about SQL here is an alternative way applying the case directly in the initial select without the sub-select:
Select
jd.id,
jd.name,
sum(case when c.source = 'a' then 1 else 0 end) as sourceA,
sum(case when c.source = 'b' then 1 else 0 end) as sourceB,
sum(case when c.source = 'c' then 1 else 0 end) as sourceC,
sum(case when c.source = 'd' then 1 else 0 end) as sourceD
from JobDescription as jd
join Candidate as c on c.jobId = jd.id
group by jd.id, jd.name
SQL Fiddle Demo
SELECT JD id, COUNT(Candidate), source
FROM table1
GROUP BY JD id,source

Have Query Results Display In Different Columns

I am trying to avoid the creation of a table to have these results display (if possible). What I want to do is to essentially have the look of a table with my query results and have a column for fullitemlist, a column for 'has been ordered' and a column for 'has not been ordered'. Essentially I am trying to have a result set like such returned from my query:
> Zip = 55555
> fullitemlist ----- Has Been Ordered ---- Has Not Been Ordered
> Knife Set 1 0
> Butcher Block 0 1
> Dishwasher 0 1
> Hair Dryer 1 0
so on and so forth for all items in my tbl_itemlist. This is the 3 CTE queries I was trying to work with, but it returns everything in one giant list, not what I was after :)
Declare #zip varchar(5)
Set #zip = '55555'
;With CTE As
(
Select fullitemlist from tbl_ItemList
Where zip = #zip
)
,CTE2 As
(
Select case when hasbeenordered = 1 then 1 else 0 end as 'Has Been Ordered' from tbl_purchasehistory
WHERE itemid IN (Select itemid from tbl_ItemList where hasbeenordered = 1 and zip = #zip)
)
,CTE3 As
(
Select case when hasbeenordered = 0 then 1 else 0 end as as 'Has NEVER Been Ordered' from tbl_purchasehistory
WHERE itemid IN (Select itemid from tbl_ItemList where hasbeenordered = 0 and zip = #zip)
)
SELECT * from CTE
UNION ALL
SELECT * FROM CTE2
UNION ALL
SELECT * FROM CTE3
-- you may write your query like this according to your question instead of CTE ,union all,SubQuery this is the simplest way .
SELECT fullitemlist
,CASE
WHEN tp.hasbeenordered = 1
AND tI.hasbeenordered = 1
THEN 1
ELSE 0
END AS 'Has Been Ordered'
,CASE
WHEN tp.hasbeenordered = 0
AND ti.tp.hasbeenordered = 0
THEN 1
ELSE 0
END AS 'Has NEVER Been Ordered'
FROM tbl_ItemList ti
INNER JOIN tbl_purchasehistory tp ON ti.itemid = tp.ItemId
WHERE zip = #zip