How to display duplicate count from two tables where one column duplicate value separate in two column from each table - mysql

First of all I want to thank you all members of this community for posting very helpful answers. I am using this kind of plateform first so I am sorry for my grammatical mistakes and if it is a silly question.
I am having a use case as given in below image link.
Having two tables 1. is Office_1 and 2. is Office_2 and want the Desired Output
For this use case I am able to get Output separately only from one table using below query
First query is for one table (Office_1)
select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_1 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_1 Go COUNT' from Office_1 where DATE >= '2022-01-16' AND DATE <= '2022-01-18' group by UID;
Second query is for another table (Office_2)
select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_2 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_2 Go COUNT' from Office_2 where DATE >= '2022-01-16' AND DATE <= '2022-01-18' group by UID;
But I want that UID must be selected from first table and output must be combined to display. Please help me in this use case. How to get the desired output?

select* from(select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_1 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_1 Go COUNT',date as dt from Office_1
union
select EID as EID,sum(case when OP like 'come' then 1 else 0 end) as 'Office_2 Come COUNT',sum(case when OP like 'go' then 1 else 0 end) as 'Office_2 Go COUNT',date as dt from Office_2)
where dt >= '2022-01-16' AND DATE <= '2022-01-18' group by UID;
Please refer here for more information about union select https://dev.mysql.com/doc/refman/8.0/en/union.html

Related

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

MySQL count different value in same table

I am working on a database right now, and I am trying to select some special data.
so the table looks like this.
name title type
Type is including two different value, "book" and "paper".
And this is the result I would like to get
name book paper
person A 0 1
person B 1 2
person C 0 5
What is the best way to write the query it in MySQL.
You may use conditional aggregation:
SELECT
name,
SUM(CASE WHEN type = 'book' THEN 1 ELSE 0 END) AS book,
SUM(CASE WHEN type = 'paper' THEN 1 ELSE 0 END) AS paper
FROM yourTable
GROUP BY
name;

SELECT Inside a Case in MySQL

Is it possible to create an SELECT Statement within Conditional CASE, like this
SELECT la.*,
(
CASE
WHEN la.SoftDelete = 1
SELECT SUM(la.LoansAmount) FROM loans_account
ELSE
SELECT 0
END
) AS outstanding
FROM loans_account
I have tried this, but it does not work, I would like to know how to go about something like this please.
I guess you need the sum of loan amount where softdelete is 1
SELECT la.*,
SUM(
CASE
WHEN la.SoftDelete = 1
THEN la.LoansAmount
ELSE
0
END
) AS outstanding
FROM loans_account la
WHERE ...
or if you need the sum from whole where softdelete is 1 then you can do so
SELECT la.*,
SUM(la.LoansAmount) outstanding
FROM loans_account la
WHERE la.SoftDelete = 1
Note sum is aggregate function and without group by it will result as a single row assuming the whole table as one group
try this
SELECT la.*, CASE
WHEN la.SoftDelete = 1 THEN SUM(la.LoansAmount)
ELSE 0 END AS outstanding
FROM loans_account

Add 6 items together to give a total, but by not adding their values together

Bit of a newbie and probably a very basic question to you, but is driving me mad.
I have a mysql table. I have a record which has a record id, and six other fields which contain names of photographs.
So,
Record_id, photo1, photo2, photo3, photo4, photo5, photo6
I want have have the end result where I have a column called 'TOTAL' which shows the number of how many records have photographs in them as a TOTAL. So, Record 1 may have a total of 5 photographs, record 2 may have 3 photographs, and record 3 may have a total of NO records for that record etc?
Now some of the photographs names which are uploaded to the field may contain numbers, ie, photo244.jpg, or pic3993.jpg etc - so I don't want these numbers added together, I am just looking for whether the field has a name in it etc, and this would be counted as 1 to add to the total. So the end result in on the page is that record 13 (for example) has a total of 4 photographs etc - sorry if not a good explanation!
It may have something to do with SUM or COUNT, but have spent ages trying to get combination or correct key/syntax! Can anyone help please by giving an example of the mysql select statement - would be grateful!
I presume that if there is no photo, then the field photoX is null or is empty.
So use CASE for output 0 if the field is null or empty, 1 else.
SELECT Record_id
,
CASE WHEN (photo1 IS NULL OR photo1 = '') THEN 0 ELSE 1 END
+ CASE WHEN (photo2 IS NULL OR photo2 = '') THEN 0 ELSE 1 END
+ CASE WHEN (photo3 IS NULL OR photo3 = '') THEN 0 ELSE 1 END
+ CASE WHEN (photo4 IS NULL OR photo4 = '') THEN 0 ELSE 1 END
+ CASE WHEN (photo5 IS NULL OR photo5 = '') THEN 0 ELSE 1 END
+ CASE WHEN (photo6 IS NULL OR photo6 = '') THEN 0 ELSE 1 END
AS Total
FROM myTable
It will the best query:
SELECT
Record_id,
(6 - IFNULL(photo1,1) - IFNULL(photo2,1) - IFNULL(photo3,1) - IFNULL(photo4,1) - IFNULL(photo5,1) - IFNULL(photo6,1)) AS TOTAL
FROM
table
Something like this will give you a count by Photo1, Photo2, etc.:
SELECT
SUM(CASE WHEN photo1 IS NOT NULL THEN 1 END) AS Photo1Count,
SUM(CASE WHEN photo2 IS NOT NULL THEN 1 END) AS Photo2Count,
SUM(CASE WHEN photo3 IS NOT NULL THEN 1 END) AS Photo3Count,
SUM(CASE WHEN photo4 IS NOT NULL THEN 1 END) AS Photo4Count,
SUM(CASE WHEN photo5 IS NOT NULL THEN 1 END) AS Photo5Count
FROM MyTable
Something like this will count how many rows have at least one photograph:
SELECT SUM (
CASE WHEN
photo1 IS NOT NULL OR
photo2 IS NOT NULL OR
photo3 IS NOT NULL OR
photo4 IS NOT NULL OR
photo5 IS NOT NULL THEN 1 END)
FROM MyTable
Note that these queries are for grand totals. They don't show activity by row. If activity by row is needed, try the answers from Scorpi0 or Michael Sivolobov.

MySQL Query: count entries by status

I have a log table with following schema:
OperatorId - JobId - Status ( Good/Bad/Ugly )
Alex 6 Good
Alex 7 Good
James 6 Bad
Description: Whenever an operator works on a job, an entry is made along with Status. That's it.
Now I need a report like:
OperatorId - Good Count - Bad Count - Ugly Count
Alex 2 0 0
James 0 1 0
select operatorid,
sum(if(status="good",1,0)) as good,
sum(if(status="bad",1,0)) as bad,
sum(if(status="ugly",1,0)) as ugly
from table
group by operatorid
This is called a Pivot Table. It is done by setting a value 1 or 0 for each state and then summing them up:
SELECT
T.OperatorId,
SUM(T.GoodStat) AS Good,
SUM(T.BadStat) AS Bad,
SUM(T.UglyStat) AS Ugly
FROM
(
SELECT
CASE WHEN Status = 'Good' THEN 1 ELSE 0 END AS GoodStat,
CASE WHEN Status = 'Bad' THEN 1 ELSE 0 END AS BadStat,
CASE WHEN Status = 'Ugly' THEN 1 ELSE 0 END AS UglyStat,
OperatorId
FROM logTable T
)
GROUP BY T.OperatorId
If, like me, you prefer, whenever possible, to calculate counts with COUNT rather than with SUM, here's an alternative solution, which uses a method asked about in this thread:
SELECT
operatorid,
COUNT(status = 'good' OR NULL) as good,
COUNT(status = 'bad' OR NULL) as bad,
COUNT(status = 'ugly' OR NULL) as ugly
FROM table
GROUP BY operatorid