sql query problem

Поиск
Список
Период
Сортировка
Искать

sql query problem

От:
Alok Thakur <alokthakur1987@gmail.com>
Дата:
Dear All,

I have two tables one contains details of user and other contains
result. The details are:
1. UserTable - id, name, phone
2. result - id, question_id, user_id, status (0 or 1)

I want the list like this:
User Id   Name   Attended   Failed   Passed

but i could not find the way to do this.

Please help

Re: sql query problem

От:
Oliveiros <oliveiros.cristina@gmail.com>
Дата:
What does a 0 state mean? Failed? And a 1 state? Passed?

Best,
Oliveiros

2012/1/14 Alok Thakur <alokthakur1987@gmail.com>
Dear All,

I have two tables one contains details of user and other contains
result. The details are:
1. UserTable - id, name, phone
2. result - id, question_id, user_id, status (0 or 1)

I want the list like this:
User Id   Name   Attended   Failed   Passed

but i could not find the way to do this.

Please help

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Re: sql query problem

От:
Misa Simic <misa.simic@gmail.com>
Дата:
It seems question is not clear...

I could not determine what should be in column Attended, and based on
what should define passed/failed

But quick tip would be

SELECT name, CASE status WHEN 1 THEN 'Passed' ELSE 'Failed' END FROM
UserTable INNER JOIN result ON UserTable.id = result.user_id

Sent from my Windows Phone
From: Alok Thakur
Sent: 15/01/2012 22:08
To: pgsql-sql@postgresql.org
Subject: [SQL] sql query problem
Dear All,

I have two tables one contains details of user and other contains
result. The details are:
1. UserTable - id, name, phone
2. result - id, question_id, user_id, status (0 or 1)

I want the list like this:
User Id   Name   Attended   Failed   Passed

but i could not find the way to do this.

Please help

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Re: sql query problem

От:
Misa Simic <misa.simic@gmail.com>
Дата:
It seems question is not clear...

I could not determine what should be in column Attended, and based on
what should define passed/failed

But quick tip would be

SELECT name, CASE status WHEN 1 THEN 'Passed' ELSE 'Failed' END FROM
UserTable INNER JOIN result ON UserTable.id = result.user_id

Sent from my Windows Phone
From: Alok Thakur
Sent: 15/01/2012 22:08
To: pgsql-sql@postgresql.org
Subject: [SQL] sql query problem
Dear All,

I have two tables one contains details of user and other contains
result. The details are:
1. UserTable - id, name, phone
2. result - id, question_id, user_id, status (0 or 1)

I want the list like this:
User Id   Name   Attended   Failed   Passed

but i could not find the way to do this.

Please help

-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Re: sql query problem

От:
Alok Thakur <alokthakur1987@gmail.com>
Дата:
Dear,

I am trying to provide you as much details as possible.

answer` ( `id` int(10) NOT NULL AUTO_INCREMENT, `question_id` int(10) NOT NULL, `user_id` int(10) NOT NULL, `answer` int(10) NOT NULL,  -> `status` tinyint(1) NOT NULL,  --> Status will be 0 or 1 means wrong
or right answer `date` datetime NOT NULL, PRIMARY KEY (`quiz_result_id`)
)

user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user` varchar(255) NOT NULL, `username` varchar(255) NOT NULL, `user_email` varchar(255) NOT NULL, `user_gender` varchar(255) NOT NULL, `refrence` varchar(255) NOT NULL, `join_date` varchar(255) NOT NULL, `status` tinyint(1) NOT NULL, `banned` tinyint(1) NOT NULL, PRIMARY KEY (`user_id`)
)

Now I want the report like this:

UserID    UserName   Attended(questions)   Wrong   Correct
1             A

On Jan 16, 3:49 am, misa.si...@gmail.com (Misa Simic) wrote:
> It seems question is not clear...
>
> I could not determine what should be in column Attended, and based on
> what should define passed/failed
>
> But quick tip would be
>
> SELECT name, CASE status WHEN 1 THEN 'Passed' ELSE 'Failed' END FROM
> UserTable INNER JOIN result ON UserTable.id = result.user_id
>
> Sent from my Windows Phone
> From: Alok Thakur
> Sent: 15/01/2012 22:08
> To: pgsql-...@postgresql.org
> Subject: [SQL] sql query problem
> Dear All,
>
> I have two tables one contains details of user and other contains
> result. The details are:
> 1. UserTable - id, name, phone
> 2. result - id, question_id, user_id, status (0 or 1)
>
> I want the list like this:
> User Id   Name   Attended   Failed   Passed
>
> but i could not find the way to do this.
>
> Please help
>
> --
> Sent via pgsql-sql mailing list (pgsql-...@postgresql.org)
> To make changes to your subscription:http://www.postgresql.org/mailpref/pgsql-sql
>
> --
> Sent via pgsql-sql mailing list (pgsql-...@postgresql.org)
> To make changes to your subscription:http://www.postgresql.org/mailpref/pgsql-sql


Re: sql query problem

От:
Andreas Kretschmer <akretschmer@spamfence.net>
Дата:
Alok Thakur  wrote:

> Dear,
> 
> I am trying to provide you as much details as possible.
> 
> answer` (
>   `id` int(10) NOT NULL AUTO_INCREMENT,
>   `question_id` int(10) NOT NULL,
>   `user_id` int(10) NOT NULL,
>   `answer` int(10) NOT NULL,  ->
>   `status` tinyint(1) NOT NULL,  --> Status will be 0 or 1 means wrong
> or right answer
>   `date` datetime NOT NULL,
>   PRIMARY KEY (`quiz_result_id`)
> )
> 
> user` (
>   `user_id` int(11) NOT NULL AUTO_INCREMENT,
>   `user` varchar(255) NOT NULL,
>   `username` varchar(255) NOT NULL,
>   `user_email` varchar(255) NOT NULL,
>   `user_gender` varchar(255) NOT NULL,
>   `refrence` varchar(255) NOT NULL,
>   `join_date` varchar(255) NOT NULL,
>   `status` tinyint(1) NOT NULL,
>   `banned` tinyint(1) NOT NULL,
>   PRIMARY KEY (`user_id`)
> )

That's MySQL (i guess), please join a mysql-list.



Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°

Re: sql query problem

От:
David Johnston <polobo@yahoo.com>
Дата:
On Jan 17, 2012, at 8:35, Andreas Kretschmer  wrote:

> Alok Thakur  wrote:
> 
>> Dear,
>> 
>> I am trying to provide you as much details as possible.
>> 
>> answer` (
>>  `id` int(10) NOT NULL AUTO_INCREMENT,
>>  `question_id` int(10) NOT NULL,
>>  `user_id` int(10) NOT NULL,
>>  `answer` int(10) NOT NULL,  ->
>>  `status` tinyint(1) NOT NULL,  --> Status will be 0 or 1 means wrong
>> or right answer
>>  `date` datetime NOT NULL,
>>  PRIMARY KEY (`quiz_result_id`)
>> )
>> 
>> user` (
>>  `user_id` int(11) NOT NULL AUTO_INCREMENT,
>>  `user` varchar(255) NOT NULL,
>>  `username` varchar(255) NOT NULL,
>>  `user_email` varchar(255) NOT NULL,
>>  `user_gender` varchar(255) NOT NULL,
>>  `refrence` varchar(255) NOT NULL,
>>  `join_date` varchar(255) NOT NULL,
>>  `status` tinyint(1) NOT NULL,
>>  `banned` tinyint(1) NOT NULL,
>>  PRIMARY KEY (`user_id`)
>> )
> 
> That's MySQL (i guess), please join a mysql-list.
> 
> 

DB aside the query you are looking for is very simple SQL.  The only real trick is using

SUM(case when status = 1/0 then 1 else 0 end 

to obtain the proper counts.  

Any reference materials covering table joining and group by will give you the syntax and examples needed to write your query.

The lack of response is because most people are not going to bother answering very simple queries that beginner reference materials cover adequately.  Plus, you didn't display any effort in attempting to solve the question yourself; you can do this by showing and and asking what you did wrong as opposed to simply asking for an answer.

David J.
FAQ