MySQL Query SELECT multiple rows - mysql

I have the following table:
surveys
comp_id question
4 What is your name?
4 How are you?
4 Where do you live?
5 Who are you?
5 What is your birthday?
I need help writing a Query that gives me the following output:
comp_id my_questions
4 What is your name?How are you?Where do you live?
5 Who are you?What is your birthday?
Thanks,

You are looking for the GROUP_CONCAT() function. Use it like this:
SELECT comp_id, GROUP_CONCAT([DISTINCT] question [ORDER BY some_field ASC/DESC] [SEPARATOR '']) as my_questions
FROM surveys
GROUP BY comp_id
Note I have shown some some optional values to pass into GROUP_CONCAT in []. To get exact like you are showing just use GROUP_CONCAT(question SEPARATOR ''). The optional items let you look for distinct question values or order them by any field(s) (including question itself).

Related

Get the sum of the numbers generated by sql

I have a table named Company which has categorized by several sector_id
SELECT sector_id,count(sector_id) FROM Company group by sector_id
I could get the company numbers like this
sector_id count(sector_id)
1 10
2 15
3 22
Then, I would like to get the sum of count(sector_id) 10 + 15 + 22
Is it possible to do this by sql only? or I need to make some code with php??
Just remove the group by:
SELECT count(*)
FROM Company ;
EDIT:
It occurs to me that you want a summary row as well as the original data. If so, you can use rollup or grouping sets in most databases. Something like this:
SELECT sector_id, count(sector_id)
FROM Company
GROUP BY sector_id WITH ROLLUP;
Yes, it is possible to calculate sum using sql by using SUM() method
Syntax :
SELECT SUM(column_name)
FROM Table_Name;
In your case it will be like this :
SELECT SUM(sector_id)
FROM Company ;
Hope it helps.

Add sum of columns to chart SSRS

I have searched all over and cannot seem to find a definitive answer for this issue! I have a simple chat here grouped on the 5 categories below detailing the Sums of their SqFt.
I want to add a Total Column to the graph ~(Total = 11M sqft). Can this only be done in SQL? It is a bit puzzling for me to do this because the query already sums the sqft for each row (as a nested query). I would need to Sum(sum(sqft)) in order to produce what I want, however, I dont believe this will work on the group level.
Sample Data set:
ID| Type| Sqft|
12| OF| 500
14| IN| 1294
99| OF| 12042
24| ME| 92043
15| IN| 13945
16| OW| 2650
Can this be done in the report builder?
Thanks!
You can add a Total row in your query by using GROUPING SETS operator. Once the total is in the dataset it is trivial to show the column in the chart.
Based on the data sample you posted you can use a similar query to the below:
SELECT
CASE
WHEN GROUPING_ID(Type) = 1 THEN 'TOTAL'
ELSE Type
END [Type],
SUM(Sqft) Sqft,
GROUPING_ID(Type) [Grouping]
FROM your_table
GROUP BY GROUPING SETS ((Type), ())
Check this Live Demo
If you are confused by the above query you can simply use the union operator to add a row to the end of your current dataset.
SELECT
ID,
[Type],
Sqft
FROM your_table
UNION ALL
SELECT
NULL,
'Total',
SUM(Sqft)
FROM your_table
Now just create your chart using the produced dataset.
Let me know if this helps.

count column items in mysql

I have a column called "WasCancelled" in a MySQL DB, it's of a Boolean type.
I save the number 0,1 and 2 to it.
I need a query that will count how many one's, two's and zeros are there.
for example:
my column:
WasCanceled
-----------
0
0
1
1
1
2
0
0
0
2
0
0
1
I need the query to output:
number | times
-------|------
"0" | 7
"1" | 4
"2" | 2
please help.
thank you.
Dave.
You can do this by grouping:
SELECT WasCancelled, COUNT(*)
FROM <Table>
GROUP BY WasCancelled
If you need more information, look here for details on Group By and Count.
Update
To include the question in the comments: to restrict on special values, you can add a WHERE clause:
SELECT WasCancelled, COUNT(*)
FROM <Table>
WHERE WasCancelled = "1"
GROUP BY WasCancelled
In further questions, please edit your overall question to include sub-questions or open new topics. Please read How To Ask Good Questions.
Update 2
SQL also allows the HAVING clause, which is like WHERE but allows the comparison of aggregated values. See here for details (e.g. you want to know which value appears more than 5 times, etc.).
Use GROUP BY with COUNT fuction:
Try this:
SELECT WasCanceled, COUNT(1) as times
FROM tableA
GROUP BY WasCanceled;
I think this might work!
select
count(WasCanceled) as CNT, WasCancelled
from
YourTableNameHere
group by
WasCanceled
This works for me
SELECT WasCanceled AS number, COUNT(WasCanceled) AS times FROM tblTest GROUP BY WasCanceled

Sorting Counts in Access

I am using Access 2013 and I have many Yes/No fields in a table named Survey. I have 25 survey questions that people check if true (yes), and I am querying the fields across the whole database to get a count of how many people entered Yes for each field. A subset of my table is:
SurveyID AutoNumber
MemberID Number
Question1 Yes/No
Question2 Yes/No
Question3 Yes/No
etc...
The following query runs against the table above and is a subset of what I have, in that it only represents three fields:
SELECT Count(IIf([Survey]![Question1]=True,1,Null)) AS CountOfQuestion1, Count(IIf([Survey]![Question2]=True,1,Null)) AS CountOfQuestion2, Count(IIf([Survey]![Question3]=True,1,Null)) AS CountOfQuestion3
FROM Survey;
This works fine (the IIF bit is due to Access and its strangeness). My question is, how do I now order this? Each result comes out as a separate field in the query, and I need to order on the results of all the fields, not just one field. For example, I might get the following:
CountOfQuestion1 34
CountOfQuestion2 7
CountOfQuestion3 11
I need to be able to sort this based on the numbers, so I know which count was the highest. I hope to get:
CountOfQuestion1 34
CountOfQuestion3 11
CountOfQuestion2 7
I feel like I'm missing something obvious, but any help would be much appreciated.
Thanks!
It would seem you want the results in rows rather than in columns. One way to accomplish this is to use separate queries for each question count and then use the union operator to merge the results together like this:
SELECT
'Q1' AS Question, Count(IIf([Survey].[Question1]=True,1,Null)) AS QuestionCount
FROM Survey
UNION ALL
SELECT
'Q2' AS Question, Count(IIf([Survey].[Question2]=True,1,Null)) AS QuestionCount
FROM Survey
UNION ALL
SELECT
'Q3' AS Question, Count(IIf([Survey].[Question3]=True,1,Null)) AS QuestionCount
FROM Survey
ORDER BY QuestionCount DESC
This would give output looking like this:
Question QuestionCount
-------- -------------
Q2 4
Q1 3
Q3 2
In Access Yes/No fields can be summed, so the following should work.
Select ABS(Sum(Question1)) as CountOfQuestion1, ABS(Sum(Question2)) as CountOfQuestion2
FROM Survey

Complicated joining on multiple id's

I have a table like this
id | user_id | code | type | time
-----------------------------------
2 2 fdsa r 1358300000
3 2 barf r 1358311000
4 2 yack r 1358311220
5 3 surf r 1358311000
6 3 yooo r 1358300000
7 4 poot r 1358311220
I want to get the concatenated 'code' column for user 2 and user 3 for each matching time.
I want to receive a result set like this:
code | time
-------------------------------
fdsayooo 1358300000
barfsurf 1358311000
Please note that there is no yackpoot code because the query was not looking for user 4.
You can use GROUP_CONCAT function. Try this:
SELECT GROUP_CONCAT(code SEPARATOR '') code, time
FROM tbl
WHERE user_id in (2, 3)
GROUP BY time
HAVING COUNT(time) = 2;
SQL FIDDLE DEMO
What you are looking for is GROUP_CONCAT, but you are missing a lot of details in your question to provide a good example. This should get you started:
SELECT GROUP_CONCAT(code), time
FROM myTable
WHERE user_id in (2, 3)
GROUP BY time;
Missing details are:
Is there an order required? Not sure how ordering would be done useing grouping, would need to test if critical
Need other fields? If so you will likely end up needing to do a sub-select or secondary query.
Do you only want results with multiple times?
Do you really want no separator between values in the results column (specify the delimiter with SEPARATOR '' in the GROUP_CONCAT
Notes:
You can add more fields to the GROUP BY if you want to do it by something else (like user_id and time).