here i have a table named as test2. i need to get the below output from a single mysql query.i have tried it using a subquery but i was fail. kindly help me to sort this.
pending status- 154
completed status - 159
required output
query that i have tried
SELECT
(
test2.doc_no,
SELECT
(
Count(test2.esn) AS pending_quantity,
test2.doc_no
FROM
test2
WHERE
test2.sttus = 154
GROUP BY
test2.doc_no
),
SELECT
(
Count(test2.esn) AS completed_quantity,
test2.doc_no
FROM
test2
WHERE
test2.sttus = 159
GROUP BY
test2.doc_no
)
)
SELECT doc_no,
SUM(STATUS=154) AS pending_quantity,
SUM(STATUS=159) AS completed_quantity
FROM
test2 GROUP BY doc_no
Try above query.
Related
I need mysql query to display the order when i have subquery in it without any sorting
Query which i am using
SELECT * FROM attributes WHERE AttributesID IN (SELECT AttributesID
FROM processattributes WHERE ProcessID = 166)
result i am getting as below
ID NAME
218 AA-Delays
219 AA-Internal
220 AA-External Errors
Am supposed to GET as below
ID Name
219 AA-External Errors
220 AA-Internal
218 AA-Delays
As sub query returns correctly 219 220 218 but final is not correctly please help out in the query
The inner select does not return the data in a garantied way. So you have to define the order you want. You can join and then define the order like this (but I don't know on which column in the other table you want to sort)
SELECT a.*
FROM attributes a
JOIN processattributes p on p.AttributesID = a.AttributesID
WHERE p.ProcessID = 166
ORDER BY <some column in table processattributes>
Hey guys i have did some coding in mysql to add a new line value to a row..
SELECT
babe
FROM
(SELECT
concat_ws(' ', 'assword \n') AS babe,
) test;
When i did like this i get an output like
BABE
assword name
What i need is an output like
BABE
assword
name(this would be below assword)
Is there any mysql functions to do this ??...or can i UPDATE the row ??..
I am a newbie in mysql. Hope you guys can help me out..Thanks in advance..
The statement includes a newline character in the babe column. You can confirm this by using the HEX() function to view the character encodings.
For example:
SELECT HEX(t.babe)
FROM ( SELECT CONCAT_WS(' ', 'assword \n') AS babe ) t
On my system, that Will output:
617373776F7264200A
It's easy enough to understand what was returned
a s s w o r d \n
61 73 73 77 6F 72 64 20 0A
(In the original query, there's an extraneous comma that will prevent the statement from running. Perhaps there was another expression in the SELECT list of the inline view, and that was returning the 'name' value that's shown in the example output. But we don't see any reference to that in the outer query.
It's not clear why you need the newline character. If you want to return:
BABE
-----------
asssword
name
That looks like two separate rows to me. But it's valid (but peculiar) to do this:
SELECT t.babe
FROM ( SELECT CONCAT_WS(' ', 'assword \nname') AS babe ) t
FOLLOWUP
Q: i just wanted to know how to add a new row below the assword ..if u know please edit the answer
It's not clear what result you are trying to achieve. The specification, divorced from the context of a use-case, is just bizarre.
A: If I had a need to return two rows: one row with the literal 'assword' and another row "below" it with the literal 'name', I could do this:
( SELECT 'assword' AS some_string )
UNION ALL
( SELECT 'name' AS some_string )
ORDER BY some_string
In this particular case, we can get the ordering we need by a simple reference to the column in the ORDER BY clause.
In the more general case, when there isn't a convenient expression for the ORDER BY clause, I would add an additional column, and perform a SELECT on the resultset from the UNION ALL operation. In this example, that "extra" column is named seq:
SELECT t.some_string
FROM ( SELECT 'assword' AS some_string, 1 AS seq
UNION ALL SELECT 'name', 2
)
ORDER BY t.seq
As another example:
( SELECT 'do' AS tone, 1 AS seq )
UNION ALL ( SELECT 're', 2 )
UNION ALL ( SELECT 'mi', 3 )
UNION ALL ( SELECT 'fa', 4 )
ORDER BY seq
I'd only need to add an outer SELECT if I needed a projection operation (for example, to remove the seq column from the returned resultset.
SELECT t.tone
FROM ( SELECT 'do' AS tone, 1 AS seq
UNION ALL SELECT 're', 2
UNION ALL SELECT 'mi', 3
UNION ALL SELECT 'fa', 4
)
ORDER BY t.seq
I have data like
column
1
1
57
57
57
1
1
57
57
I need to generate count data like
count
2
3
2
2
using mysql query. I have tried group by but it groups all the values also i couldn't group by any other fields. Is there any easy way to achieve this?
find the screenshot in this link
Update:
My end goal is to get the time spend by the user on each course . like the user who has ID 1 have spend time from 1177924991 to 1177925038 ( here 1177925038 is one second minus the next course visit time) on course id 1
Use this query:
SELECT COUNT(*)
FROM `tablename`
GROUP BY `info`
try using
SELECT COUNT(columnname)
FROM `tablename`
GROUP BY columnname
This will do the needful.
UPDATE
I am getting following result set on running this query :
Use This Query:
Select `course`, count(*) from `tablename`
group by `course`
I thought I had a very simple query to perform, but I can't seem to make it work.
I have this table with 2 columns:
version_id trim_id
1 15
1 25
1 28
1 30
1 35
2 12
2 25
2 33
2 48
3 11
3 25
3 30
3 32
I am trying to get any version-id's that have say a sub-set of trim_id's. Let's say all version_id's that have trim_id's 25 and 30. My obvious attempt was :
SELECT * FROM table WHERE trim_id=25 AND trim_id=30
I was expecting to have version_id 1 and 3 as a result, but instead I get nothing.
I am working with the latest version of MAMP, which has some odd behavior, like in this case it just tells me its 'LOADING' and never gives me an error message or something. But that's normally the case when there is no data to return.
This is InnoDB, if that helps.
Thanks for your input.
Your query does not work because you are using AND and the trim_id cannot have two different values at the same time, so you need to apply Relational Division to get the result.
You will need to use something similar to the following:
SELECT version_id
FROM yourtable
WHERE trim_id in (25, 30)
group by version_id
having count(distinct trim_id) = 2
See SQL Fiddle with Demo.
This will return the version_id values that have both 25 and 30. Then if you wanted to include additional columns in the final result, you can expand the query to:
select t1.version_id, t1.trim_id
from yourtable t1
where exists (SELECT t2.version_id
FROM yourtable t2
WHERE t2.trim_id in (25, 30)
and t1.version_id = t2.version_id
group by t2.version_id
having count(distinct t2.trim_id) = 2);
See SQL Fiddle with Demo
SELECT *
FROM table
WHERE trim_id IN(25,30)
I have a table color_balls entries as below
=====================================================
Name Red_Ball Green_Ball Yellow_Ball
=====================================================
John 27 56 66
Mathew 37 45 15
=====================================================
I want to write a sql or mysql query to get the output as below.
=====================================
Color_of_Balls No_of_balls
=====================================
Red_Ball 64
Green_Ball 101
Yellow_Ball 81
=====================================
This type of query is called UNPIVOT, which unfortunately isn't natively supported by MySQL. However, you can simulate this using UNION:
SELECT 'Red_Ball' AS Color_of_Balls, SUM(Red_Ball) as No_of_Balls
FROM color_balls
UNION ALL
SELECT 'Green_Ball', SUM(Green_Ball)
FROM color_balls
UNION ALL
SELECT 'Yellow_Ball', SUM(Yellow_Ball)
FROM color_balls
Oracle10g doesn't support this natively either, although there is this workaround.
This should work in both Oracle and MySQL:
SELECT b.Color_of_Balls
, CASE b.Color_of_Balls
WHEN 'Red_Ball' THEN d.sum_red_ball
WHEN 'Green_Ball' THEN d.sum_green_ball
WHEN 'Yellow_Ball' THEN d.sum_yellow_ball
END AS No_of_balls
FROM ( SELECT 'Red_Ball' AS Color_of_Balls FROM DUAL
UNION ALL SELECT 'Green_Ball' FROM DUAL
UNION ALL SELECT 'Yellow_Ball' FROM DUAL
) b
CROSS
JOIN ( SELECT SUM(c.Red_Ball) AS sum_red_ball
, SUM(c.Green_Ball) AS sum_green_ball
, SUM(c.Yellow_Ball) AS sum_yellow_ball
FROM color_balls c
) d
ORDER
BY CASE b.Color_of_Balls
WHEN 'Red_Ball' THEN 1
WHEN 'Green_Ball' THEN 2
WHEN 'Yellow_Ball' THEN 3
END
Note that this approach requires only one pass through the color_balls table, rather than three separate passes through the table (or more, depending on how many rows you need returned.)