mysql error: FUNCTION asterisk.SUM does not exist. (Apache superset) - mysql

I connected the mysql database and ran a direct sql query and I got the correct table, I wanted to visualize it and clicked the "Explore" button, but I got the error: "mysql error: FUNCTION asterisk.SUM does not exist. Check the ' Function Name Parsing and Resolution 'section in the Reference Manual "Please tell me where I made a mistake, thanks.
working sql request
error
My sql request:
SELECT DATE_FORMAT(start, '%Y-%m-%d') AS Date,
disposition AS Type,
DATE_FORMAT(SEC_TO_TIME(SUM(end-start)),'%H:%i:%S') AS Call_duration,
case
when substring(dstchannel,5,4) = '1000' then 'Name1'
when substring(dstchannel,5,4) = '2000' then 'Name2'
when substring(dstchannel,5,4) = '3000' then 'Name3'
when substring(dstchannel,5,4) = '4000' then 'Name4'
when substring(dstchannel,5,3) = '100' then 'Other'
end AS Operator,
count(*) AS Number_of_calls
FROM cdr
WHERE dcontext = 'call-in'
AND disposition = 'ANSWERED'
AND end-start > 0
group by DATE_FORMAT(start, '%Y-%m-%d'), 4
order by 1 desc
My request works correctly, but when I try to create a chart, superset changes the request and it has a line break after SUM. Сan I fix this without changing the database configuration?
Superset out query:
SELECT `Date` AS `Date`,
`Type` AS `Type`,
`Call_duration` AS `Call_duration`,
`Operator` AS `Operator`,
`Number_of_calls` AS `Number_of_calls`
FROM
(SELECT DATE_FORMAT
(start, '%Y-%m-%d') AS Date,
disposition AS Type,
DATE_FORMAT(SEC_TO_TIME(SUM
(end-start)), '%H:%i:%S') AS Call_duration,
case
when substring(dstchannel, 5, 4) = '1000' then 'Name1'
when substring(dstchannel, 5, 4) = '2000' then 'Name2'
when substring(dstchannel, 5, 4) = '3000' then 'Name3'
when substring(dstchannel, 5, 4) = '4000' then 'Name4'
when substring(dstchannel, 5, 3) = '100' then 'Other'
end AS
Operator,
count(*) AS Number_of_calls
FROM cdr
WHERE dcontext = 'call-in'
AND disposition = 'ANSWERED'
AND end-
start > 0
group by DATE_FORMAT
(start, '%Y-%m-%d'),
4
order by 1 desc) AS expr_qry
LIMIT 1000;

Your group by doesn't match the select columns. Try:
SELECT DATE_FORMAT(start, '%Y-%m-%d') AS Date,
disposition AS Type,
DATE_FORMAT(SEC_TO_TIME(SUM(end-start)), '%H:%i:%S') AS Call_duration,
(case when substring(dstchannel,5,4) = '1000' then 'Name1'
when substring(dstchannel,5,4) = '2000' then 'Name2'
when substring(dstchannel,5,4) = '3000' then 'Name3'
when substring(dstchannel,5,4) = '4000' then 'Name4'
when substring(dstchannel,5,3) = '100' then 'Other'
end) AS Operator,
count(*) AS Number_of_calls
FROM cdr
WHERE dcontext = 'call-in' AND
disposition = 'ANSWERED'
end > start
group by DATE_FORMAT(start, '%Y-%m-%d'), type, call_duration, operator
order by 1 desc

Related

Mysql multiple SELECT and Count query

Can you advice in whats the best way to combine multiple select and count into the one query
For example here are some queries that are from a test_table and work perfect on their own
SELECT name, count(*) AS 'Overall' FROM test_test WHERE country_prefix = '44' AND area_code = '203' GROUP BY `city_name`;
SELECT name, count(*) AS 'unallocated' FROM test_tab le WHERE country_prefix = '44' AND area_code = '203' AND removed != '1' AND destination-value = '1234' GROUP BY `city_name`;
There's a few more queries i'll be running but I think if I get two working together ill be able to do the rest
I was thinking something like this:
SELECT name, (SELECT count(*) FROM test_table WHERE country_prefix = '44' AND area_code = '203' GROUP BY `city_name') AS "Overall", (SELECT count(*) AS 'unallocated' FROM test_table WHERE country_prefix = '44' AND city_code = '203' AND removed != '1' AND destination-value = '1234' GROUP BY 'city_name')
But unfortunately doesn't work
Would the best way to do this be using sum case when, something like this:
sum(case when destination-value = '1234' then 1 else 0 end) AS unallocated
You can do it with conditional aggregation:
SELECT city_name,
COUNT(CASE WHEN country_prefix = '44' AND area_code = '203' THEN 1 END) AS Overall,
COUNT(CASE WHEN country_prefix = '44' AND area_code = '203' AND removed != '1' AND destination-value = '1234' THEN 1 END) AS unallocated
FROM tablename
GROUP BY city_name;
or since the 2 cases contain common conditions, these conditions can be moved to a WHERE clause:
SELECT city_name,
COUNT(*) AS Overall,
COUNT(CASE WHEN removed != '1' AND destination-value = '1234' THEN 1 END) AS unallocated
FROM tablename
WHERE country_prefix = '44' AND area_code = '203'
GROUP BY city_name;
In Mysql it's possible to simplify the code with the function SUM() instead of COUNT():
SELECT city_name,
COUNT(*) AS Overall,
SUM(removed != '1' AND destination-value = '1234') AS unallocated
FROM tablename
WHERE country_prefix = '44' AND area_code = '203'
GROUP BY city_name;

Case statement not working with or condition

Please help me with this code.
SELECT (CASE LEFT(BRANCH_CODE, 1)
WHEN '1' THEN 'NL'
WHEN '2' THEN 'MM'
WHEN '3' THEN 'SL'
WHEN '4' THEN 'VIS'
WHEN '5' THEN 'MIN'
WHEN ('7' OR '8') THEN 'SA'
END) `REGION`,
This is the result:
Region
null
MM
The result should be like this:
Region
SA
MM
The following syntax is not valid:
WHEN ('7' OR '8') THEN 'SA'
What follows WHEN or ELSE using this style of CASE expression can only be a literal value, not a logical expression. So, you could refactor to:
SELECT
CASE LEFT(BRANCH_CODE, 1)
WHEN '1' THEN 'NL'
WHEN '2' THEN 'MM'
WHEN '3' THEN 'SL'
WHEN '4' THEN 'VIS'
WHEN '5' THEN 'MIN'
WHEN '7' THEN 'SA' -- repeat the 'SA' predicate twice
WHEN '8' THEN 'SA' END AS REGION
FROM yourTable;
If you wanted to combine the 7 and 8 cases in a single WHEN, then you would have to use the longer form of CASE:
SELECT
CASE WHEN LEFT(BRANCH_CODE, 1) = '1' THEN 'NL'
WHEN LEFT(BRANCH_CODE, 1) = '2' THEN 'MM'
WHEN LEFT(BRANCH_CODE, 1) = '3' THEN 'SL'
WHEN LEFT(BRANCH_CODE, 1) = '4' THEN 'VIS'
WHEN LEFT(BRANCH_CODE, 1) = '5' THEN 'MIN'
WHEN LEFT(BRANCH_CODE, 1) = '7' OR
LEFT(BRANCH_CODE, 1) = '8' THEN 'SA' END AS REGION
FROM yourTable;
This second version is fairly ugly IMHO, and I would probably stick with the first version, which just repeats the SA predicate in two places.
I had similar issues when trying to apply multiple condition statements in "CASE" statement.
I solved it by moving the test after the "WHEN" statement as following :
SELECT (CASE
WHEN LEFT(BRANCH_CODE, 1) = '1' THEN 'NL'
WHEN LEFT(BRANCH_CODE, 1) = '2' THEN 'MM'
WHEN LEFT(BRANCH_CODE, 1) = '3' THEN 'SL'
WHEN LEFT(BRANCH_CODE, 1) = '4' THEN 'VIS'
WHEN LEFT(BRANCH_CODE, 1) = '5' THEN 'MIN'
WHEN LEFT(BRANCH_CODE, 1) IN ('7' OR '8') THEN 'SA'
END) `REGION`,

Case in where condition to calculate individual totals

select
Count(*)
from gl
join TRVMAINDATA msit
on gl.TRANSACTIONID = msit.CHARGETRANSACTIONID
where gl.CREATEDDATETIME >= DATEADD(MONTH,-2,getdate()) AND
datediff(DAY,gl.BUSINESSPROCESSDATE,gl.CREATEDDATETIME)>=4 AND
gl.MARKETCODE In('535','532','056','050','039','036','034','033','030','029','027','023','022','021','018','015','012','011','010','009','007','006','005','002','001' ) and DATEADD(MONTH,DATEDIFF(MONTH,0,gl.CREATEDDATETIME),0) = '2/1/2017 12:00:00 AM'
The above code gives the total for market code mentioned.
But i also want the market code for other code which doesn't belog here. for example
select
Count(*)
from gl
join TRVMAINDATA msit
on gl.TRANSACTIONID = msit.CHARGETRANSACTIONID
where gl.CREATEDDATETIME >= DATEADD(MONTH,-6,getdate()) AND
datediff(DAY,gl.BUSINESSPROCESSDATE,gl.CREATEDDATETIME)>=0
and
DATEADD(MONTH,DATEDIFF(MONTH,0,gl.CREATEDDATETIME),0) = '2/1/2017 12:00:00 AM'
AND
(case
when gl.MARKETCODE In('535','532','056','050','039','036','034','033','030','029','027','023','022','021','018','015','012','011','010','009','007','006','005','002','001') then 'Proprietary'
when gl.MARKETCODE='037' then 'US'
else 'partner'
end )
Is it possible to calculate totals of other market codes in one sql query or do i have to calculate individually?
I think you would like to do count with case when in select statement like this:
SELECT COUNT(CASE
WHEN gl.marketcode IN ( '535', '532', '056', '050',
'039', '036', '034', '033',
'030', '029', '027', '023',
'022', '021', '018', '015',
'012', '011', '010', '009',
'007', '006', '005', '002', '001' ) THEN 1
ELSE NULL
END) AS `Proprietary`,
COUNT(CASE
WHEN gl.marketcode = '037' THEN 1
ELSE NULL
END) AS `US`,
COUNT(CASE
WHEN gl.marketcode NOT IN ( '535', '532', '056', '050',
'039', '036', '034', '033',
'030', '029', '027', '023',
'022', '021', '018', '015',
'012', '011', '010', '009',
'007', '006', '005', '002',
'001', '037' ) THEN 1
ELSE NULL
END) AS `partner`
FROM gl
JOIN trvmaindata msit
ON gl.transactionid = msit.chargetransactionid
WHERE gl.createddatetime >= DATEADD(month, -2, GETDATE())
AND DATEDIFF(day, gl.businessprocessdate, gl.createddatetime) >= 4
AND DATEADD(month, DATEDIFF(month, 0, gl.createddatetime), 0) = '2/1/2017 12:00:00 AM'

my mysql SELECT case can't return 3 columns

I'm trying to return 3 columns with the query below. the current query works fine.
SELECT cat, COUNT(*) as count FROM
(SELECT case WHEN `cat_type` = 'PREMIUM' then '1'
WHEN `cat_type` = 'NOT PREMIUM' then '2'
WHEN `cat_type` = 'GOLD' then '3'
WHEN `cat_type` = 'EXECUTIVE' then '4'
WHEN `cat_type` = 'NOT PROVIDED' then '-1'
else '-2'
end AS cat FROM `ab` AS s
JOIN `make` AS m
WHERE s.make_code = m.make_code
) AS someRandomAliasHere
GROUP BY cat
ORDER BY CAST(cat AS UNSIGNED) ASC
when i try and return another column cat_type i get an error
SELECT cat, cat_type, COUNT(*) as count FROM ....
The error i get is
#1054 - Unknown column 'cat_type' in 'field list'
but cat_type does exist in my ab table. any idea what i'm missing? Thanks
Your cat_type exists in ab table.
But your ab table does not exists in your main select.
It only exists within the sub query which returns someRandomAliasHere to you.
So you should likely select cat_type from the same select as you select your cases.
Then you should have access to it, but it would then likely mess with your group by / count.
You'll properly need a more advanced query for what you're after.
You need to include cat_type in the subquery. I would recommend:
SELECT cat, cat_type, COUNT(*) as count
FROM (SELECT (case WHEN `cat_type` = 'PREMIUM' then 1
WHEN `cat_type` = 'NOT PREMIUM' then 2
WHEN `cat_type` = 'GOLD' then 3
WHEN `cat_type` = 'EXECUTIVE' then 4
WHEN `cat_type` = 'NOT PROVIDED' then -1
else -2
end) AS cat, *
FROM `ab` AS s JOIN
`make` AS m
USING (make_code)
) AS someRandomAliasHere
GROUP BY cat, cat_type
ORDER BY abs(cat) asc
Note the following:
Added * to the subquery to capture all the columns.
Changed the join syntax. Not only are explicit joins better but the using clause allows the use of * without having to worry about duplicate names among the joined column.
Removed the single quotes from the constant values for cat. You are treating these as numbers in the order by, so use numbers for the values.
Changed the order by to abs() rather than casting to unsigned. However, do you really want unsigned here?
You're not selecting cat_type into someRandomAliasHere, hence it's not available to your main select. This should fix it:
SELECT cat, cat_type, COUNT(*) as count FROM
(SELECT case WHEN `cat_type` = 'PREMIUM' then '1'
WHEN `cat_type` = 'NOT PREMIUM' then '2'
WHEN `cat_type` = 'GOLD' then '3'
WHEN `cat_type` = 'EXECUTIVE' then '4'
WHEN `cat_type` = 'NOT PROVIDED' then '-1'
else '-2'
end AS cat, cat_type FROM `ab` AS s
JOIN `make` AS m
WHERE s.make_code = m.make_code
) AS someRandomAliasHere
GROUP BY cat, cat_type
ORDER BY CAST(cat AS UNSIGNED) ASC
You needed to include the column in subquery first and then reference that using the table.col
Try below
SELECT someRandomAliasHere.cat_type, cat, COUNT(*) as count FROM
(SELECT cat_type, case WHEN `cat_type` = 'PREMIUM' then '1'
WHEN `cat_type` = 'NOT PREMIUM' then '2'
WHEN `cat_type` = 'GOLD' then '3'
WHEN `cat_type` = 'EXECUTIVE' then '4'
WHEN `cat_type` = 'NOT PROVIDED' then '-1'
else '-2'
end AS cat FROM `ab` AS s
JOIN `make` AS m
WHERE s.make_code = m.make_code
) AS someRandomAliasHere
GROUP BY cat
ORDER BY CAST(cat AS UNSIGNED) ASC
You need to also select the column cat_type, so that someRandomAliasHere owns the column.
SELECT cat, cat_type, COUNT(*) as count FROM
(SELECT case WHEN `cat_type` = 'PREMIUM' then '1'
WHEN `cat_type` = 'NOT PREMIUM' then '2'
WHEN `cat_type` = 'GOLD' then '3'
WHEN `cat_type` = 'EXECUTIVE' then '4'
WHEN `cat_type` = 'NOT PROVIDED' then '-1'
else '-2'
end AS cat, cat_type FROM `ab` AS s
JOIN `make` AS m
WHERE s.make_code = m.make_code
) AS someRandomAliasHere
GROUP BY cat, cat_type
ORDER BY CAST(cat AS UNSIGNED) ASC
Dont forget to add cat_type in GROUP BY too.

CASE Statement in SQL WHERE clause

I'm trying to fetch data from table where I'm using a CASE condition in the WHERE clause and currently I'm using following query:-
SELECT count(enq_id) AS total, sum(purchase_amount) AS purchase
FROM temp_stock
WHERE purchase_date <> '0000-00-00'
AND purchase_date < '2012-08-01'
AND (
STATUS = 'Sold'
OR STATUS = 'In Stock'
OR STATUS = 'Ref'
)
AND CASE WHEN (
STATUS = 'Sold'
)
THEN delivery_date >= '2012-08-01'
END
But it returns 0 for total and NULL for purchase.
From your comment.
I want to use Case Statement, could u pls clarify me about case statament in where clause
You can use CASE statement in WHERE like this:
SELECT count(enq_id) AS total, sum(purchase_amount) AS purchase
FROM temp_stock
WHERE purchase_date <> '0000-00-00'
AND purchase_date < '2012-08-01'
AND ( STATUS = 'Sold'
OR STATUS = 'In Stock'
OR STATUS = 'Ref')
AND CASE STATUS
WHEN 'Sold'
THEN delivery_date >= '2012-08-01'
ELSE 1=1
END
Here you need to use ELSE 1=1. otherwise you will not get desired result. For more explanation see this SQLFiddle
I don't think that CASE can work that way. What you want is a slightly more complex expression as your WHERE clause. Probably something like this:
SELECT count(enq_id) AS total, sum(purchase_amount) AS purchase
FROM temp_stock
WHERE purchase_date <> '0000-00-00'
AND purchase_date < '2012-08-01'
AND (
(STATUS = 'Sold' AND delivery_date >= '2012-08-01')
OR STATUS = 'In Stock'
OR STATUS = 'Ref'
)