different results for two queries - mysql

Query 1 :
SELECT
SUM(aol_int) AS AOL,
SUM(android_phone_int) AS Android_Phone,
SUM(androidTablet_int) AS Android_Tablet,
SUM(apple_mail_int) AS Apple_Mail,
SUM(blackberry_int) AS Blackberry,
SUM(Eudora_int) AS Eudora,
SUM(gmail_int) AS Gmail,
SUM(hotmail_int) AS Hotmail,
SUM(lotus_notes_int) AS Lotus_Notes,
SUM(other_int) AS Other,
SUM(other_webmail_int) as Other_Web_Mail,
SUM(Outlook_int) AS Outlook,
SUM(Postbox_int) AS Postbox,
SUM(sparrow_int) AS Sparrow,
SUM(thunderbird_int) AS Thunderbird,
SUM(windowsLiveMail_int) AS Windows_Live_Mail,
SUM(yahoo_int) AS Yahoo,
SUM(iPad_int) AS iPad,
SUM(iPhone_int) AS iPhone,
SUM(iPod_int) AS iPod
FROM mytable;
Query 2:
select sum(aol_int + android_phone_int + androidtablet_int+apple_mail_int+blackberry_int+Eudora_int+gmail_int+hotmail_int+lotus_notes_int+other_int+other_webmail_int+Outlook_int+Postbox_int+sparrow_int+thunderbird_int+windowsLiveMail_int+yahoo_int+iPad_int+iPhone_int+iPod_int)
as total_percentage
FROM mytable;
When I am summing up the results of Query 1 I am getting different sum as compared to what I am getting via Query2. The value in Query2 is less than Query 1. Why is it like that?
TROUBLESHOOTING:
I tried to write my query like this:
SELECT SUM( ISNULL(aol_int,0) +
ISNULL(android_phone_int,0) +
ISNULL(androidtablet_int,0) +
ISNULL(apple_mail_int,0) +
ISNULL(blackberry_int,0) +
ISNULL(Eudora_int,0) +
ISNULL(gmail_int,0) +
ISNULL(hotmail_int,0) +
ISNULL(lotus_notes_int,0) +
ISNULL(other_int,0) +
ISNULL(other_webmail_int,0) +
ISNULL(Outlook_int,0) +
ISNULL(Postbox_int,0) +
ISNULL(sparrow_int,0) +
ISNULL(thunderbird_int,0) +
ISNULL(windowsLiveMail_int,0)+
ISNULL(yahoo_int,0) +
ISNULL(iPad_int,0) +
ISNULL(iPhone_int,0) +
ISNULL(iPod_int,0) )AS total_percentage
FROM mytable;
However, I am getting an error after running above query in the MySQL workbench:
Error Code: 1582. Incorrect parameter count in the call to native function 'ISNULL'. What is wrong here?

This could happen if some columns in some of the rows contain nulls. When this happens, all columns in a row with even a single null column would produce null in the chain of additions, so the row will add nothing to the total.
Here is a short demo of this effect. Setup:
create table test(x int null, y int null);
insert into test(x,y) values (1,null);
insert into test(x,y) values (null,2);
insert into test(x,y) values (3,3);
Queries:
select sum(x+y) from test; -- Shows 6
select sum(x)+sum(y) from test -- Shows 9
See this demo on sqlfiddle: link.

Related

MySQL issue with NULL values

I have a table with fields: country_code, short_name, currency_unit, a2010, a2011, a2012, a2013, a2014, a2015. a2010-a2015 fields are type of double.
How do I make a query which orders the results by average of fields a2010-a2015, keeping in mind that these fields might have NULL value?
I tried this code and it did not work (returns a mistake, which tells there is something wrong in ORDER BY part. mistake was saying something about coumn names and GROUP BY). The logic is: ORDER BY ((A)/(B)) where A - sum of not NULL fields and B - count of not NULL fields.
Any ideas?
(if important, the code is going to be used in BigInsights environment)
SELECT country_code, short_name, currency_unit, a2010, a2011, a2012,
a2013, a2014, a2015
FROM my_schema.my_table
WHERE Indicator_Code = 'SE.PRM.TENR'
ORDER BY
(
(
Coalesce(a2010,0) + Coalesce(a2011,0) + Coalesce(a2012,0)
+Coalesce(a2013,0) + Coalesce(a2014,0) + Coalesce(a2015,0)
)
/
(
COUNT(Coalesce(a2010)) + COUNT(Coalesce(a2011)) + COUNT(Coalesce(a2012))
+ COUNT(Coalesce(a2013)) + COUNT(Coalesce(a2014)) +
COUNT(Coalesce(a2015))
)
) DESC;
use MySQL ifnull
IFNULL(expression_1,expression_2)
in your query :-
IFNULL(
(
COUNT(Coalesce(a2010)) + COUNT(Coalesce(a2011)) + COUNT(Coalesce(a2012))
+ COUNT(Coalesce(a2013)) + COUNT(Coalesce(a2014)) +
COUNT(Coalesce(a2015))
),
1
)

Get a value in a single cell into multiple rows using SSRS

One particular field in the SQL Table has a value in the below format.
Value11,value12,Value13
Value21,value22,value23
...
...
I need to get each of the above lines in the text into individual lines using SSRS.
for example I will get 2 rows in the report for above data.
Is there a way to do this using a reporting project in VS or Report builder?
Thanks in advance.
Update
Hi,Below is the DDL for the table
tblTest
[id] int
[Description] VARCHAR(MAX)
Lets assume there is only one record with Below
Insert Into tblTest
([id],[Description])
VALUES
(1, 'Value11,value12,Value13
Value21,value22,value23')
So there is a carriage return Caharacter in above Insert for the Description column. This will have 2 lines in the description row.
So my requirement is that when i retrieve the data, I should get into below format.
ID, Description
1, Value11,value12,Value13
1, Value21,value22,value23
You can use this SELECT for passing data to Reporting Services.
SELECT t1.id, t2.splittedDescriptions
FROM
(
SELECT tblTest.id,
CAST('<row>' + REPLACE(tblTest.[Description], CHAR(13) + CHAR(10), '</row><row>') + '</row>' AS XML) as xmlRow
FROM tblTest
) t1
CROSS APPLY
(
SELECT xmlTable.splittedRow.value('.', 'VARCHAR(MAX)') as splittedDescriptions
FROM t1.xmlRow.nodes('/row') AS xmlTable(splittedRow)
) t2
It uses XML and nodes() method to split your description when it finds a CRLF.
It work with a single CRLF, if you need to work with double CRLF you can simply modify the SELECT.
Example - input data:
INSERT INTO tblTest ([id],[Description]) VALUES
(1, 'val11, val12, val13' + CHAR(13) + CHAR(10) + 'val21, val22, val23')
INSERT INTO tblTest ([id],[Description]) VALUES
(2, 'val31, val32, val33')
INSERT INTO tblTest ([id],[Description]) VALUES
(3, 'val41, val42, val43' + CHAR(13) + CHAR(10) + 'val51, val52, val53' + CHAR(13) + CHAR(10) + 'val61, val62, val63')
Example - output:
id splittedDescriptions
----------- --------------------
1 val11, val12, val13
1 val21, val22, val23
2 val31, val32, val33
3 val41, val42, val43
3 val51, val52, val53
3 val61, val62, val63
use this,
select '1' as Id, Value11+','+value12+','+Value13 as Description into tblTest from XYZ;
Value11,value12,Value13 all should be in String

How to replace the characters by * in a specified column dynamically in ssis package?

I have an Excel source with one of the column name Emailid.
I want the output like below example:
'mahesh123#gmail.com'
The output should be
'ma*****23#gmail.com'
As a beginner i am searching for the replacement for function STUFF, in SSIS package...
select
substring(studentEMAILIDid,1,2)
+ replicate('*',len(substring(studentEMAILIDid,1,
charindex(studentEMAILIDid,'#')
-1‌​)-4)
+ substring(studentEMAILIDid,charindex(studentEMAILIDid,'#')-2,2)
+ substring(studentEMAILIDid,charindex(studentEMAILIDid,'#')+1,
len(studentEMAILIDi‌​d)
from <tablename>
I tried the above code in SSMS,i am expecting the result through SSIS package without using "Execute SQL Task".
This is the expression I tried:
emailid==
substring(
StudentEMailID,
3,
LEN(right(StudentEMailID,
findstring('#',Stud‌​entEMailID + '#')-1
)
)-4,
REPLICATE('*',LEN(LEFT(StudentEMailID,
findstring('#',StudentEMailID + '#')-1))-4))
–
Data:
select N'mahesh123#gmail.com' as email
union all
select N'EmpireAgain#gmail.com'
Derived Column Code:
SUBSTRING(email,1,2) +
REPLICATE("*",LEN(email) - FINDSTRING(email,"#",1) - 4) +
SUBSTRING(email,FINDSTRING(email,"#",1)-2,LEN(email)-(FINDSTRING(email,"#",1)-2) + 1)
Result:
email NewEmail
mahesh123#gmail.com ma*****23#gmail.com
EmpireAgain#gmail.com Em*****in#gmail.com

Mysql Error Code : 1292 Incorrect datetime value : ''

I'm trying to insert a record to a table and I get the following error.
Mysql Error Code : 1292 Incorrect datetime value : ''
Mysql code snip-let is as follows
INSERT INTO tbl_dashboard (avg_response)
SELECT cast(ifnull(floor(avg(5 * (DATEDIFF(substring(im.inq_managerreply,-10), im.inq_managerdate) DIV 7)
+ MID('0123444401233334012222340111123400001234000123440',
7 * WEEKDAY(im.inq_managerdate) + WEEKDAY(substring(im.inq_managerreply,-10)) + 1, 1))),'not_applicable')AS CHAR(45)) 'average_response_time_in_working_days'
FROM inq_manager im
There are no errors in executing the select statement which gives the average response time excluding the weekends but when I try to insert the above to my table the error is given.
The data type of the tbl_dashboard is avg_response char(45)
How can I overcome this . Please help
I am not sure this will matter, but it is worth a shot. And i'll delete this if it doesn't help but maybe
INSERT INTO tbl_dashboard (avg_response) VALUES
(SELECT cast(ifnull(floor(avg(5 * (DATEDIFF(substring(im.inq_managerreply,-10), im.inq_managerdate) DIV 7)
+ MID('0123444401233334012222340111123400001234000123440',
7 * WEEKDAY(im.inq_managerdate) + WEEKDAY(substring(im.inq_managerreply,-10)) + 1, 1))),'not_applicable')AS CHAR(45)) 'average_response_time_in_working_days'
FROM inq_manager im)

Append text to each row of the sql select query

I have a query like this
SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER
GROUP BY TYPE
The output for this is
Records Affected TYPE
---------------- ----
4 F1
3 F2
5 F3
Now I would like to change the query so that the output will be as follows
Records Affected
----------------
The number of records affected for F1 is : 4
The number of records affected for F2 is : 3
The number of records affected for F3 is : 5
"The number of records affected for " + TYPE + " is : " + COUNT.
How can I add the default text to each row of the result set instead of appending in the front end. I would like to simplify my task of just showing the records in the DataGrid as Summary.
You can easily concatenate the string using the following. You will use the + to concatenate the string to the type column and the count. Note, the count needs to be converted to a varchar for this to work:
SELECT
'The number of records affected for '+ type +
' is : '+ cast(COUNT(ID) as varchar(50)) as'Records Affected'
FROM yt
GROUP BY TYPE;
See SQL Fiddle with Demo
Just put the text in your query:
SELECT 'The number of records affected for ' + TYPE + ' is : ' + CAST(COUNT(ID) as VARCHAR(20)) AS 'Records Affected' FROM MASTER
GROUP BY TYPE
SELECT "The number of records affected for " + TYPE + " is : " + COUNT(ID) AS [Records Affected]
FROM Master
GROUP BY TYPE
Use this query:
UPDATE bookmark_linx SET link_url=(SELECT CONCAT(link_url, '?raw=true')) WHERE link_url LIKE '%dropbox%'
Try this:
SELECT 'The number of records affected for ' + TYPE + ' is : ' +
STR(X.[Records Affected]) AS [Records Affected]
FROM (SELECT COUNT(ID) 'Records Affected', TYPE FROM MASTER GROUP BY TYPE) X