now i have a table test_table with some data like this:
(id-type is a unique index)
id
type
value
1
x
a
1
y
b
2
x
aa
and with the sql:
SELECT id,
group_concat(IF(type = 'x', value, NULL) SEPARATOR '') AS 'x',
group_concat(IF(type = 'y', value, NULL) SEPARATOR '') AS 'y'
FROM test_table
GROUP BY id;
i get the result like this:
id
x
y
1
a
b
2
aa
null
it makes the 'type' to be a colume.
so this is a better way to get the same result?
a better sql to implements my question~
Use conditional aggregation:
SELECT
id,
MAX(CASE WHEN type = 'x' THEN value END) AS x,
MAX(CASE WHEN type = 'y' THEN value END) AS y
FROM test_table
GROUP BY id
ORDER BY id;
Related
I have below table and SQL query written, this query should not return any result but its returning ID = 1 , what is wrong with the SQL query? Can anyone please help?
** Note balance data type is decimal rest are varchar
ID code balance level
1 C 150.00
1 P 40027.42 F
1 P 40027.42 F
select distinct ID from table
(
(code = 'P' and balance = 40027.42 and level = 'F') or
(code = 'C' and balance = 151.00 )
)
group by ID
having count(ID) >=2
If you do not want to count the same code twice, you can use count(distinct code):
select ID
from t
where (code = 'P' and balance = 40027.42 and level = 'F')
or (code = 'C' and balance = 151.00 )
group by ID
having count(distinct code) >=2
If you want to only count a distinct set of values once, you can use a derived table/subquery to select distinct rows:
select ID
from (
select distinct id, code, balance, level
from t
) as s
where (code = 'P' and balance = 40027.42 and level = 'F')
or (code = 'C' and balance = 151.00 )
group by ID
having count(ID) >=2
rextester demo for both: http://rextester.com/LBKO57534
SELECT *, REPLACE(number_option, 'Issue ', '') AS new_number_option FROM
jgl_article WHERE status = 1 AND topic = 'Current Issues' ORDER BY id ASC
new_number_option Column returns :
1
1
1
2
2
3
3
I want to get the highest value from new_number_option column. Here 3 is the highest value. So, I want to get the records which contains highest value. I tried HAVING MAX(new_number_option) in above query. But, it won't works.
Try CAST because your column type is string
CAST(new_number_option as SIGNED)
use
MAX( CAST(REPLACE(number_option, 'Issue ', '') AS UNSIGNED) )
SELECT JA.*
FROM
jgl_article JA INNER JOIN
(SELECT MAX( CAST(REPLACE(number_option, 'Issue ', '') AS UNSIGNED) ) AS MAX_number_option FROM
jgl_article) T
ON CAST(TRIM(JA.number_option) as UNSIGNED) = T.MAX_number_option
WHERE JA.status = 1 AND JA.topic = 'Current Issues'
This will work
Hope this helps..
Data
name 'chan' value 'a'
name 'chan' value 'b'
name 'max' value 'a'
name 'max' value 'b'
name 'tony' value 'a'
name 'tony' value 'c'
I need to find out user who both have value a and b, this is my solution:
SELECT * FROM `table`
GROUP BY `name`
HAVING SUM(IF(`value` = 'a', 1, 0)) >= 1 AND SUM(IF(`value` = 'b', 1, 0)) >= 1
Any better way?
Your solution is ok, but it would be better written as:
SELECT name
FROM `table`
GROUP BY `name`
HAVING SUM(`value` = 'a') >= 1 AND SUM(`value` = 'b') >= 1;
A possibly more efficient form is:
SELECT name
FROM `table`
WHERE value in ('a', 'b')
GROUP BY `name`
HAVING COUNT(DISTINCT value) = 2;
And, depending on your data structure and indexes and size, this could also be efficient:
select ta.name
from table ta join
table tb
on ta.name = tb.name and ta.value = 'a' and tb.value = 'b';
I prefer the methods using group by and having because they generalize to a more diverse set of conditions.
Try this out. a DISTINCT is required if name per value in not unique.
eg (COUNT(DISTINCT value) = 2)
SELECT name
FROM tableName
WHERE value IN ('a', 'b')
GROUP BY name
HAVING COUNT(*) = 2
How to check whether all rows in a table have unique values for a column
having char datatype in a table in MySQL and return the value as yes or no ?
You can try something like this, where ? is the column you want to check :
SELECT IF(t.total = t.total_distinct, 'YES', 'NO') AS result
FROM ( SELECT COUNT(*) AS total
, COUNT(DISTINCT ?) AS total_distinct
FROM tbl
) t
If you ignore NULL values, you can just compare count() and count(distinct):
select (case when count(col) = count(distinct col) then 'All Unique' else 'Duplicates' end)
from table t;
If NULL values are a concern (so NULL would be allowed at most one time), then you can aggregate and look at the maximum count:
select (case when max(cnt) = 1 then 'All Unique' else 'Duplicates' end)
from (select col, count(*) as cnt
from table t
group by col
) col
I would go with the first version of Gordon's answer, but grouped by the Primary key of the table. In other words :
select primary_key_field, (case when count(col) = count(distinct col) then 'All Unique' else 'Duplicates' end)
from table t
group by primary_key_field.
I have a statistical query that would return three rows (as I have 3 types by which I group by), I also know the order of the rows as I do explicit ORDER BY FIELD:
SELECT COUNT(id) AS c FROM Vehicles GROUP BY VehicleTypeID ORDER BY FIELD(VehicleTypeID, 1,2,3)
Is there a simple way to transpose the rows into columns? Something like (PSEUDO SQL):
SELECT c[0] AS CarsCount, c[1] AS MotorcyclesCount, c[2] AS TrucksCount FROM (
SELECT COUNT(id) AS c
FROM Vehicles
GROUP BY VehicleTypeID
ORDER BY FIELD(VehicleTypeID, 1,2,3)
)
Yes, it is a case statement with aggregation:
SELECT max(case when fieldnum = 1 then c end) AS CarsCount,
max(case when fieldnum = 2 then c end) AS MotorcyclesCount,
max(case when fieldnum = 3 then c end) AS TrucksCount
FROM (SELECT COUNT(id) AS c , FIELD(VehicleTypeID, 1,2,3) as fieldnum
FROM Vehicles
GROUP BY VehicleTypeID
) t;