0

In my database, i have a list of responses collected from customers that i want to gather and generate a pie chart for. (like that of google forms).

In my database, i have responses that are the same and other not like below

Client 1   Client 2    Client
  Yes        Yes         No

So in the column above, i have Yes(2) and No(1). I want to gather, count and display my query response like below

Yes : 2  , No: 1

But with my code below

 public function answers_chart(Survey $survey)
   {
         $response = DB::select( DB::raw(" select * from (
          SELECT questionnaire_id, COUNT(answer) as e_count
          FROM Answer
          GROUP BY answer
          )a where  a.e_count > 1)); 
   }

I get the response like below, meaning it is only counting the Yes

[{"e_count":2}]

How can i achieve something like this?

Table

public function up()

 {
        Schema::create('Answer', function (Blueprint $table) {
            $table->increments('id');         
            $table->integer('question_id');
            $table->integer('questionnaire_id');
            $table->string('answer');
            $table->timestamps();
        });
    }
2
  • add the table's schema in the question. Commented Jul 9, 2018 at 10:07
  • @DsRaj, i have added the schema Commented Jul 9, 2018 at 10:12

3 Answers 3

1

You have a.e_count > 1. This means that the GROUP BY answer has only 1 'No' in it and you only want the ones that has more than 1 of 'No' or 'Yes'. You should remove a where a.e_count > 1).

I highly discourage doing queries like this though. Laravel has a powerful built in Eloquent ORM. Take a look at it here for better use of the Laravel framework!

Sign up to request clarification or add additional context in comments.

3 Comments

Advocate of the devil: I highly encourage doing queries like this. It makes your code framework independent and probably faster since you don't need to load an entire ORM for a relatively easy select.
If this is the only table in the database I would have to agree, but using this schema for bigger projects makes it inconvenient in the long term. I do not know the size of Switz' project though.
That's taste I think. Very valid point but it doesn't weigh up to my need of being independent from frameworks :)
0

may be using case statments will be better, for example

SELECT
COUNT(CASE WHEN answer = '1' THEN 1 END) AS yes,
COUNT(CASE WHEN answer= '0' THEN 1 END) AS no
FROM ANSWER

1 Comment

How would this query stand if the response are not Yes or No response? I added the Yes or No as an example of responses that could be the same
0

Try this:

Model::select(DB::raw("COUNT(CASE WHEN answer = 'Yes' THEN 1 END) AS e_count"))->first(); // Eloquent approach

DB::table('answer')->select(DB::raw("COUNT(CASE WHEN answer = 'Yes' THEN 1 END) AS e_count"))->first(); // Query Builder approach

1 Comment

You can add the count of the 'no' answer by own so you will get better idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.