php - Display Count of Collected and Released Blood Components

102

I'm trying to display the count of collected and released blood table entries with the same name.

I have a table like this

Blood Collection

I would like the output to be something like:

{-code-2}

I am using laravel as a framework...

484

Answer

-------- id BloodComponent Remarks 1 Whole Blood Collected 2 Platelet Released 3 Plasma Collected 4 Platelet Released 5 Plasma Collected 6 Whole Blood Released|||Name Collected Released
464

Answer

--------- Whole Blood 1 1 Platelet 0 2 Plasma 2 0
552

Answer

Solution:

In laravel:

$results = DB::table("BloodCollection")
  ->selectRaw("BloodComponent AS Name,SUM(IF(Remarks = 'Collected', 1, 0)) AS Collected,SUM(IF(Remarks = 'Released', 1, 0)) AS Released")
  ->groupBy("BloodComponent")
  ->get();

Or if you already have aBloodCollection model:

$results = BloodCollection::selectRaw("BloodComponent AS Name,SUM(IF(Remarks = 'Collected', 1, 0)) AS Collected,SUM(IF(Remarks = 'Released', 1, 0)) AS Released")
  ->groupBy("BloodComponent")
  ->get();

then:

foreach($results as $result){

    echo $results->Name . ": ".$results->Collected ." collected, ".$results->Released ." released.";

}
603

Answer

Solution:

When you require that rows become columns of data you would transform the rows using a crosstab query:

SELECT `BloodComponent` AS `Name`,
SUM(IF(`Remarks` = 'Collected', 1, 0)) AS `Collected`,
SUM(IF(`Remarks` = 'Released', 1, 0)) AS `Released`
FROM `BloodCollection`
GROUP BY `BloodComponent`

Here is an informative post showing the use of these kinds of queries in a more detailed fashion.

In Laravel:

DB::table("BloodCollection")
  ->selectRaw("`BloodComponent` AS `Name`,SUM(IF(`Remarks` = 'Collected', 1, 0)) AS `Collected`,SUM(IF(`Remarks` = 'Released', 1, 0)) AS `Released`")
  ->groupBy("BloodComponent")
  ->get();

People are also looking for solutions to the problem: php - Running Symfony2 on ISPConfig3

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.