How to show multiple condition with mysql - mysql

I have a problem with with my query. I have this data : Check this out here is full data and my table. http://sqlfiddle.com/#!9/f440d7/1
Please i hope you can help me.
My expected result should be like this :
|PartID | IdMesin |ket1 |ket2 |Ket3|
|---------- |-------- |---------------- |---------| |
|AELE-00071-XX-KGX|BS 150U |- |1.00 Tes1|- |
|GCXX-MX070-XX-KGX|BS 320F |0.50 Tes3, 2.00 Tes4|0.50 Tes2|- |
I hope you can help me to fix this problem. Anyhelp can be appreciated.

case when should be used in group_concat:
SELECT partid,
idmesin,
COALESCE(GROUP_CONCAT(CASE WHEN idshift = '1' THEN CONCAT(ROUND(
TIME_TO_SEC(
MAKETIME(
durasi + 0,
SUBSTRING_INDEX(durasi, 'Jam ', - 1) + 0, 0)) / 3600, 2), ' ',
keterangandowntime) end), '-') ket1,
COALESCE(GROUP_CONCAT(CASE WHEN idshift = '2' THEN CONCAT(ROUND(
TIME_TO_SEC(
MAKETIME(
durasi + 0,
SUBSTRING_INDEX(durasi, 'Jam ', - 1) + 0, 0)) / 3600, 2), ' ',
keterangandowntime) end), '-') ket2,
COALESCE(GROUP_CONCAT(CASE WHEN idshift = '3' THEN CONCAT(ROUND(
TIME_TO_SEC(
MAKETIME(
durasi + 0,
SUBSTRING_INDEX(durasi, 'Jam ', - 1) + 0, 0)) / 3600, 2), ' ',
keterangandowntime) end), '-') ket3
FROM trans_lhpdtdw
WHERE divisiid = 'IJ001'
AND tanggal = '2017-11-20'
AND idlocation LIKE '%3%'
GROUP BY partid,
idmesin
See demo in SQLFiddle.

Related

Pivoting and getting #1111 - Invalid use of group function for this query

I'm try to Pivoting and getting #1111 - Invalid use of group function for this query. I really need help here.
SELECT pack.ActionName, up.Name
SUM(IF(SUBSTRING_INDEX(SUBSTRING_INDEX(SchoolYear, ' ', 1), ' ', -1) = 'X', COUNT(tph.UniqueActionPackageId)*pack.TotalPrice, 0)) AS Total_Revenue_X,
AVERAGE(IF(SUBSTRING_INDEX(SUBSTRING_INDEX(SchoolYear, ' ', 1), ' ', -1) = 'X', Total_Revenue_X/COUNT(tph.UniqueActionPackageId),0)) AS Rataan_Kelas_X,
SUM(IF(SUBSTRING_INDEX(SUBSTRING_INDEX(SchoolYear, ' ', 1), ' ', -1) = 'XI', COUNT(tph.UniqueActionPackageId)*pack.TotalPrice, 0)) AS Total_Revenue_XI,
AVERAGE(IF(SUBSTRING_INDEX(SUBSTRING_INDEX(SchoolYear, ' ', 1), ' ', -1) = 'XI', Total_Revenue_XI/COUNT(tph.UniqueActionPackageId),0)) AS Rataan_Kelas_XI,
SUM(IF(SUBSTRING_INDEX(SUBSTRING_INDEX(SchoolYear, ' ', 1), ' ', -1) = 'XII', COUNT(tph.UniqueActionPackageId)*pack.TotalPrice, 0)) AS Total_Revenue_XII,
AVERAGE(IF(SUBSTRING_INDEX(SUBSTRING_INDEX(SchoolYear, ' ', 1), ' ', -1) = 'XII', Total_Revenue_XII/COUNT(tph.UniqueActionPackageId),0)) AS Rataan_Kelas_XII
FROM packages AS pack
JOIN transaction_package_history AS tph ON pack.ActionPackagesId = tph.UniqueActionPackageId
JOIN user_profile AS up ON up.UniqueId = tph.UserId
GROUP BY pack.ActionName;
Can you guys help me
I suspect you are trying to write something like this:
SELECT p.ActionName, up.Name
SUM(CASE WHEN SchoolYear LIKE '%X %' THEN p.TotalPrice END) as Total_Revenue_X,
AVG(CASE WHEN SchoolYear LIKE '%X %' THEN p.TotalPrice END) as Rataan_Kelas_X,
SUM(CASE WHEN SchoolYear LIKE '%XI %' THEN p.TotalPrice END) as Total_Revenue_XI,
AVG(CASE WHEN SchoolYear LIKE '%XI %' THEN p.TotalPrice END) as Rataan_Kelas_XI,
SUM(CASE WHEN SchoolYear LIKE '%XII %' THEN p.TotalPrice END) as Total_Revenue_XI,
AVG(CASE WHEN SchoolYear LIKE '%XII %' THEN p.TotalPrice END) as Rataan_Kelas_XI,
FROM packages p JOIN
transaction_package_history tph
ON p.ActionPackagesId = tph.UniqueActionPackageId JOIN
user_profile up
ON up.UniqueId = tph.UserId
GROUP BY p.ActionName;

Is there a way to sum only integer from rows and then sum strings without integer

I have this table
------------------------------------------
id | Machines
------------------------------------------
1. | 1 Truck
------------------------------------------
2. | 1 Bobcat
------------------------------------------
3. | 2 Platform
------------------------------------------
4. | Telehender
------------------------------------------
5. | Teodolit, 3 Platform
------------------------------------------
6. | 2 Tractor
------------------------------------------
7. | NULL
------------------------------------------
Result: | 11
------------------------------------------
I want to sum firstly integers (1+1+2+3+2), then SUM Count of values without Integer (Telehender and Teodolit = 2) and skip NULL values..
The result of this table need to be 11.
I am using this query
SELECT Sum((Char_length(machines) - Char_length(Replace(machines, ',','')) + 1)) AS ukupno
FROM izvestaji
WHERE projekatid='8'
AND datum='2019-10-03'
But I get the Result = 9.
Is there a way to make that query?
Bad bad database design... :/
However , here it it a solution in which i suppose you have no more than 3 elements
into machines field
SELECT
sum(case
when length(machines)-length(replace(machines,',','')) = 0
then if( CAST(machines AS UNSIGNED) = 0, 1, cast(machines as unsigned))
when length(machines)-length(replace(machines,',','')) = 1
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned)))
when length(machines)-length(replace(machines,',','')) = 2
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') as unsigned)))
end) AS ukupno
FROM izvestaji
#WHERE projekatid='8'
#AND datum='2019-10-03'
you can obtain other cases from 2 to M including cases like this
when length(machines)-length(replace(machines,',','')) = M
then ( if( CAST(SUBSTRING_INDEX(machines, ',', 1) AS UNSIGNED) = 0, 1, cast(SUBSTRING_INDEX(machines, ',', 1) as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 1),','),'') as unsigned))) +
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', 2),','),'') as unsigned))) +
...
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', j),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', j),','),'') as unsigned))) +
...
(if( CAST(replace(machines , concat(SUBSTRING_INDEX(machines, ',', M),','),'') AS UNSIGNED) = 0, 1, cast(replace(machines , concat(SUBSTRING_INDEX(machines, ',', M),','),'') as unsigned)))
if you see case 1 or case 2 i think you can understand it.
You can use combinations of sign(), abs(), substr(), instr() and length() functions, and can filter the integer values by string + 0 formula :
select sum( int_value_1 + int_value_2 ) +
sum( case when int_value_1 = 0 or int_value_2 = 0 then
sign( length(before_comma) ) + abs( sign( after_comma+0 ) - 1 )
end) as total
from
(
select substr( Machines, 1, instr(Machines,',') - 1 ) + 0 as int_value_1,
substr( Machines, instr(Machines,',')+1, length(Machines) ) as int_value_2,
substr( Machines, 1, instr(Machines,',') - 1 ) as before_comma,
substr( Machines, instr(Machines,',')+1, length(Machines) ) as after_comma
from izvestaji
) i;
Demo

MySQL calculating only first row

I have table that have stored date time...i need using mysql to calculate duration in 0d 0h 0m 0s format to show it in datatables:
+----+---------------------+
| id | playtime |
+----+---------------------+
| 2 | 08:58:24 20/06/2017 |
| 1 | 08:57:33 20/06/2017 |
+----+---------------------+
This is my query:
SELECT (SELECT GROUP_CONCAT(
TIMESTAMPDIFF(day, live.playtime, NOW()) , 'd ',
MOD(TIMESTAMPDIFF(hour, live.playtime, NOW()), 24), 'h ',
MOD(TIMESTAMPDIFF(minute, live.playtime, NOW()), 60), 'm ',
MOD(TIMESTAMPDIFF(second, live.playtime, NOW()), 60), 's'
) FROM live) AS duration;
The problem is that only first id is calculated and others ids are ignored....how can i return multiple result for each id calulated time?
I need to return this:
+-----------------------------+
| duration |
+-----------------------------+
| 0d 0h 13m 50s,0d 0h 12m 59s |
+-----------------------------+
| 0d 0h 11m 12s,0d 0h 6m 9s |
+-----------------------------+
UPDATED FULL QUERY:
SELECT SQL_CALC_FOUND_ROWS live.id, live.user, live.player,
SUBSTRING_INDEX(streams.channel, '_', -1) AS channel, bouquets.bouquet,
DATE_FORMAT(live.playtime, '%H:%i:%s %d/%m/%Y') AS playtime,
(SELECT CONCAT(
TIMESTAMPDIFF(day, live.playtime, NOW()) , 'd ',
MOD(TIMESTAMPDIFF(hour, live.playtime, NOW()), 24), 'h ',
MOD(TIMESTAMPDIFF(minute, live.playtime, NOW()), 60), 'm ',
MOD(TIMESTAMPDIFF(second, live.playtime, NOW()), 60), 's'
) FROM live GROUP BY live.id LIMIT 1) AS duration,
resellers.nick, live.ip, servers.server, live.id, live.id
FROM live LEFT JOIN streams ON live.stream=streams.id LEFT JOIN bouquets ON
live.bouquet=bouquets.id LEFT JOIN resellers ON live.reseller=resellers.id
LEFT JOIN servers ON live.server=servers.id
ORDER BY live.id desc LIMIT 0, 10;
Assuming there is only one record per id, could you try adding a WHERE clause in nested SELECT query, e.g.:
SELECT SQL_CALC_FOUND_ROWS live.id, live.user, live.player,
SUBSTRING_INDEX(streams.channel, '_', -1) AS channel, bouquets.bouquet,
DATE_FORMAT(live.playtime, '%H:%i:%s %d/%m/%Y') AS playtime,
(SELECT CONCAT(
TIMESTAMPDIFF(day, l.playtime, NOW()) , 'd ',
MOD(TIMESTAMPDIFF(hour, l.playtime, NOW()), 24), 'h ',
MOD(TIMESTAMPDIFF(minute, l.playtime, NOW()), 60), 'm ',
MOD(TIMESTAMPDIFF(second, l.playtime, NOW()), 60), 's'
) FROM live l WHERE l.id = live.id) AS duration,
resellers.nick, live.ip, servers.server, live.id, live.id
FROM live LEFT JOIN streams ON live.stream=streams.id LEFT JOIN bouquets ON
live.bouquet=bouquets.id LEFT JOIN resellers ON live.reseller=resellers.id
LEFT JOIN servers ON live.server=servers.id
ORDER BY live.id desc LIMIT 0, 10;

Select string till first or second space in string

I have table column that contains in each row data like this:
| Simbols |
|--------------------------------------|
|H412 Text text |
|H413 Text text text text |
|EUH 001 Text text text text text text |
|EUH 006 text text |
|EUH 201/201A Text text. Text text |
And I need from that data get data like this:
|Simbols |
|------------|
|H412 |
|H413 |
|EUH 001 |
|EUH 006 |
|EUH 201/201A|
I tried with SUBSTRING and CHARINDEX but it till the end don't work... It takes only first space or something like that...
QUERY:
SELECT
CASE
WHEN SUBSTRING(Simbols, 1, CHARINDEX(' ', Simbols)) = ''
THEN Simbols + ' '
ELSE SUBSTRING(Simbols, 1, CHARINDEX(' ', Simbols))
END 'Simbols'
FROM dbo.table
RESULT:
| Simbols |
|------------|
|H412 |
|H413 |
|EUH |
|EUH |
|EUH |
How can I make this work, and where is the problem?
Maybe there is different way to get these Simbols?
P.S. "Text text text" is a example, there comes a explanations of "Simbols"
The CharIndex() function has an optional 3rd parameter - start_location - that will be key here.
SELECT your_column
, CharIndex(' ', your_column) As first_space
, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1) As second_space
, SubString(your_column, 1, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1)) As first_two_words
FROM your_table
Unfortunately when the CharIndex() function can't find the specified string (in this case a single space ' ') it will return 0 (zero).
This means that if there isn't a first or second space the result of first_two_words in my above example will return an empty string as SubString(your_column, 1, 0) = ''.
To get around this you need to be a little clever.
Essentially, if second_space = 0 then we need to return the full string. We have a few options for this:
SELECT your_column
, CharIndex(' ', your_column) As first_space
, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1) As second_space
, SubString(your_column, 1, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1)) As first_two_words
, SubString(your_column, 1, Coalesce(NullIf(CharIndex(' ', your_column, CharIndex(' ', your_column) + 1), 0), Len(your_column))) As first_two_words_option1
, CASE WHEN CharIndex(' ', your_column, CharIndex(' ', your_column) + 1) = 0 THEN your_column ELSE SubString(your_column, 1, CharIndex(' ', your_column, CharIndex(' ', your_column) + 1)) END As first_two_words_option2
FROM (
SELECT 'one' As your_column
UNION ALL SELECT 'one two'
UNION ALL SELECT 'one two three'
UNION ALL SELECT 'one two three four'
) As x
Try this: It works
SELECT CASE WHEN charindex(' ', Simbols, charindex(' ', Simbols) + 1) = 0
THEN Simbols
ELSE LEFT(Simbols, charindex(' ', Simbols, charindex(' ', Simbols) + 1))
END
FROM dbo.table
Here is screenshot what I tried.
Here is new EDIT
SELECT REPLACE(Simbols, 'text', '') FROM dbo.table
Here is screen shot
Try something like this:
select TRIM(REPLACE(lower(type),"text" ,"")) as T, type from supportContacts
Sql Fiddle: http://sqlfiddle.com/#!2/d5cf8/4
for more info :http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

Show modified strings that appear more than once

I got a Query which cuts off domain suffixes for every row, e.g. google.com -> google or google.co.uk -> google
The query is as follows
SELECT id,domain,
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
Now I want to display all modified domains that occur more than once. How can I do that? Thanks for helping me out ;)
Just add
GROUP BY Keydomain
HAVING COUNT(*) > 1
to your query.
EDIT:
Could you tell me if there is a way to list the complete domains one by one with your addition?
SELECT * FROM
(
SELECT
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
GROUP BY Keydomain
HAVING COUNT(*) > 1
) d1
INNER JOIN
(
SELECT id, domain,
CASE
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 1 THEN REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))
WHEN LENGTH(domain) - LENGTH(REPLACE(domain, '.', '')) = 2 THEN REVERSE(SUBSTRING(REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000))), LOCATE('.', REVERSE(REVERSE(SUBSTRING(REVERSE(domain), LOCATE('.', REVERSE(domain)) + 1, 1000)))) + 1, 1000))
END as Keydomain
FROM sites
) d2
ON d1.Keydomain = d2.Keydomain