How to display MYSQL COUNT horizontally - mysql

Hi I am trying to display COUNT result in horizontal way but to no success. Is there any easyway to do this?
I have this table (created in excel for demo purposes)
Now I want to display the result like this.
Any advise will be greatly appreciated.

SELECT store_name,
SUM(CASE WHEN status = 'hold' THEN 1 ELSE 0 END) AS hold_count,
SUM(CASE WHEN status = 'ship' THEN 1 ELSE 0 END) AS ship_count,
SUM(CASE WHEN status = 'return' THEN 1 ELSE 0 END) AS return_count
FROM table
group by store_name

Use if clause inside sum-aggregate.
select store_name,
sum(if(status='hold', 1, 0)) as 'hold',
sum(if(status='ship', 1, 0)) as 'ship',
sum(if(status='return', 1, 0)) as 'return'
from mytable
group by store_name;

Related

Is it good to select the same column on multiple where condition mysql?

I'm thinking about counting the number of rows depending on each condition and put each result with alias. I don't know if it possible and good to do it this way. For example:
select
(select sum(buy) from order_db where stts=1) as buy1,
(select sum(buy) from order_db where stts=2) as buy2,
(select sum(buy) from order_db where stts=3) as buy3
from order_db
where date='2021-08-29' and user='john'
I've tried but got error in return. I'm a basic sql learner. Please help me with suggestion or a clue to find the answer.
Use conditional aggregation:
SELECT SUM(CASE WHEN stts=1 THEN buy ELSE 0 END) buy1,
SUM(CASE WHEN stts=2 THEN buy ELSE 0 END) buy2,
SUM(CASE WHEN stts=3 THEN buy ELSE 0 END) buy3
FROM order_db
WHERE date='2021-08-29' AND user='john';
If there are other values than 1, 2 and 3 for stts you may also add to the WHERE clause:
AND stts IN (1, 2, 3)
So basically, this sounds like you are in need of a "conditional count". This can be achived with the combination of SUM and the CASE WHEN operator.
Applied to your problem, the solution would be somethinge like this:
select
(sum(case when stts = 1 then buy else 0 end)) as buy1,
(sum(case when stts = 2 then buy else 0 end)) as buy2,
(sum(case when stts = 3 then buy else 0 end)) as buy3
from order_db
where date='2021-08-29' and user='john'

Multiple Count with Multiple column

I am new in sql. I want to count something like:
Select count(*) from table where col1= x and col2=x and Col3=x.
I need to count the same value in all different column.
Any help will be appreciated.
You can use conditional aggregation :
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab;
If you want to have sum of these count values, consider the above query as an inner and use the following :
Select q.*,
q.count_col1 + q.count_col2 + q.count_col3 whole_sum
from
(
Select sum(case when col1='x' then 1 else 0 end) as count_col1,
sum(case when col2='x' then 1 else 0 end) as count_col2,
sum(case when col3='x' then 1 else 0 end) as count_col3
from tab
) q
Rextester Demo

SSRS calculate % of desired colums in column group in matrix

SELECT PatchPhase.PhaseName, MAX(PatchPhase.Sequence) AS seq,
PatchState.Application, PatchState.Objectname, PatchState.Jobname,
PatchState.Streamname, COUNT(*) AS Total,
PatchState.Jobdescription,
PatchState.Status, PatchState.Timestamp
FROM PatchState
INNER JOIN PatchPhase ON PatchState.Phase = PatchPhase.PhaseTech
WHERE (PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application)) AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY PatchPhase.PhaseName, PatchState.Application, PatchState.Objectname,
PatchState.Jobname, PatchState.Streamname, PatchState.Jobdescription, PatchState.Status,
PatchState.Timestamp
ORDER BY PatchState.Application
I am working on matrix and I have column group of status which contains 3 columns (planned, running,completed). I want to sum planned+completed and divide by total column.
Total column is outside the column group.
I do find some answers but I did not get how should I use in my code
can someone please help?
Try using this to aggregate, and then use a table instead of a matrix. You will not need a column group now. As I cannot run the sql I can't promise it is perfect but perhaps you could have a little play with it if not. It should offer a guide if nothing else.
SELECT
PatchPhase.PhaseName,
MAX(PatchPhase.Sequence) AS seq,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) as 'Planned',
SUM(case when PatchState.Status = 'Running' then 1 else 0 end) as 'Running',
SUM(case when PatchState.Status = 'Completed' then 1 else 0 end) as 'Completed',
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end)) /
(SUM(case when PatchState.Status = 'Planned' then 1 else 0 end) + SUM(case when PatchState.Status = 'Running' then 1 else 0 end) + SUM(case when PatchState.Status = 'Completed' then 1 else 0 end)) as totalPercentage
PatchState.Timestamp
FROM
PatchState
INNER JOIN PatchPhase
ON PatchState.Phase = PatchPhase.PhaseTech
WHERE
(PatchPhase.PhaseName IN (#Phase))
AND (PatchState.Application IN (#Application))
AND (PatchState.Timestamp >= #StartDate)
AND (PatchState.Timestamp <= #EndDate)
GROUP BY
PatchPhase.PhaseName,
PatchState.Application,
PatchState.Objectname,
PatchState.Jobname,
PatchState.Streamname,
PatchState.Jobdescription,
PatchState.Status,
PatchState.Timestamp
ORDER BY
PatchState.Application
You can get a value displayed in a text box from expression =Reportitems!Textbox133.Value
instead of textbox133 in the above expression you have to give the textbox name of the textbox from which you want the value. In your case the total column

Grouping similar values into new select statement

I have a table which looks something like this...
supplier_name status count
supplier_a Unavailable 1
supplier_a Refunded 5
supplier_a Shipped 10
supplier_z Refunded 2
supplier_z Shipped 4
I know exactly what columns I want and I would like to use the values from the first table to return values structured like the following...
supplier_name unavailable refunded shipped
supplier_a 1 5 10
supplier_z 0 2 4
(note: in cases where there is no value for a specific column it should be set to 0)
Would this be possible to achieve in a query?
I think something like this should work:
SELECT Supplier_Name,
MAX(CASE WHEN STATUS = 'Unavailable' THEN Count ELSE 0 END) as Unavailable,
MAX(CASE WHEN STATUS = 'Refunded' THEN Count ELSE 0 END) as refunded,
MAX(CASE WHEN STATUS = 'Shipped' THEN Count ELSE 0 END) as shipped
FROM YourTable
GROUP BY Supplier_Name
And here is the SQL Fiddle.
Good luck.
Try this:
SELECT
supplier_name,
SUM(COALESCE(CASE WHEN status = 'unavailable' THEN `count` END, 0)) unavailable,
SUM(COALESCE(CASE WHEN status = 'refunded' THEN `count` END, 0)) refunded,
SUM(COALESCE(CASE WHEN status = 'shipped' THEN `count` END, 0)) shipped
FROM tbl
GROUP BY supplier_name
SQL FIDDLE DEMO

How to combine two count queries to their ratio?

I have two queries:
select count(*) from my_table where status="accepted"
and
select count(*) from my_table where status="rejected"
I was to find the ratio of accepted/reject so I Was wondering if it's possible to combine the two queries so I don't have to execute two queries
Putting this answer since none offered so far is correct
select count(case when status = "accepted" then 1 end) /
count(case when status = "rejected" then 1 end) as Ratio
from my_table
where status in ("accepted","rejected")
If you also need the individual counts
select count(case when status = "accepted" then 1 end) Accepted,
count(case when status = "rejected" then 1 end) Rejected,
count(case when status = "accepted" then 1 end) /
count(case when status = "rejected" then 1 end) as Ratio
from my_table
where status in ("accepted","rejected")
Note: MySQL does not have a divide by zero problem. It returns NULL when Rejected is 0.
select accepted_count, rejected_count, accepted_count/rejected_count ratio
from (
select sum(CASE WHEN status="accepted" THEN 1 ELSE 0 END) accepted_count,
sum(CASE WHEN status="rejected" THEN 1 ELSE 0 END) rejected_count
from my_table
) A
I think this will work:
SELECT (Select count(*) from my_table where status="accepted") / (select count(*) from my_table where status="rejected") AS ratio
select status, count(*) from my_table
where status in ("rejected", "accepted")
group by status;
select sum(case when status='accepted' then 1 else 0 end) as AcceptedCount,
sum(case when status='rejected' then 1 else 0 end) as RejectedCount
from my_table