Why does SQL operation shows the same output? [duplicate] - mysql

This question already has answers here:
Compare dates in MySQL
(5 answers)
Closed last year.
when I output these two SQL operations, the columns for the two months (01 = January) and (02 = February) are summed together instead of individually. So they show the same output. But why?
If if want to output (02 February) it should not show the sum of January but it does. Where is my problem in my code?
"SELECT SUM(" + dataBaseHelper.GET_SUM + ") FROM " + dataBaseHelper.TABLE_NAME + " WHERE " + dataBaseHelper.DATE + " BETWEEN '01/01/2022' AND " + "'07/01/2022'"
and
"SELECT SUM(" + dataBaseHelper.GET_SUM + ") FROM " + dataBaseHelper.TABLE_NAME + " WHERE " + dataBaseHelper.DATE + " BETWEEN '01/02/2022' AND " + "'07/02/2022'"

The standard syntax for date in MySQL is yyyy-mm-dd. I think you could replace the dates in your where between statement using this consideration. Otherwise read string to date cast specifications.

Related

SSRS - trying to get total rows by using CountRows function

I am trying to get the total rows returned by each tablix. Each row returned is an error and I need to get the total errors. Each tablix has it own data set name so I am trying to use CountRows(data set) and add them together to get the total rows returned or total errors.
Code:
="Number of Errors: " & CountRows("Reader_Check")'' + '' & CountRows("Access_Panel_Check")'' + '' & CountRows("Reader_Check")'' + '' & CountRows("Alarm_Panel_Check")'' + '' & CountRows("Alarm_Input_Check")'' + '' & CountRows("Alarm_Output_Check")'' + '' & CountRows("Segment_Check")'' + '' & CountRows("Access_Level_Check")'' + '' & CountRows("Area_Check")'' + '' & CountRows("Timezone_Check")'' + '' & CountRows("Alarm_Check")
Any help would be great. Thanks
It looks like you are appending the + as a string?
I think you just need something like
="Number of Errors: " &
(CountRows("Reader_Check")
+ CountRows("Access_Panel_Check")
+ CountRows("Reader_Check")
+ CountRows("Alarm_Panel_Check")
+ CountRows("Alarm_Input_Check")
+ CountRows("Alarm_Output_Check")
+ CountRows("Segment_Check")
+ CountRows("Access_Level_Check")
+ CountRows("Area_Check")
+ CountRows("Timezone_Check")
+ CountRows("Alarm_Check")
)
Note: you have Reader_Check specified twice which I assume is incorrect

[21000][1242] Subquery returns more than 1 row

My Query:
entityManager.createQuery("SELECT " +
"q.id, " +
"q.title, " +
"q.user.fullName, " +
"q.user.reputationCount, " +
"q.viewCount, " +
"q.countValuable, " +
"q.persistDateTime, " +
"t.id, " +
"t.name, " +
"t.description, " +
"(SELECT COUNT (a) FROM Answer a WHERE a.question.id = q.id), " +
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) " +
"FROM Question q JOIN q.tags t")
Here I get the error - [21000][1242] Subquery returns more than 1 row
By the method of exceptions, I determined that the error in this query string:
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) "
How to make the correct request so that there is no this error? Thank!
Two common ways are aggregation and limiting:
(SELECT MAX(a.isHelpful) FROM Answer a WHERE a.question.id = q.id)
(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id LIMIT 1)
However, those are really just hacks to get around an "issue" with the data. I put issue in quotes, but the real issue is probably your understanding of data and not the data itself.
You should understand why there are duplicates. Then decide which value you want. And implement the correct logic for what you want.
Subquery returns more than 1 row, this simply means that your query is not returning a single row for the outer select statement to work.
"(SELECT a.isHelpful FROM Answer a WHERE a.question.id = q.id) "
you have to apply a set of conditions to filter out your data uniquely or use joins to combine your table Answer and Question and then filter data accordingly.
you can also group each row data in one column by GROUP_CONCAT Mysql function like this :
"(SELECT GROUP_CONCAT(a.isHelpful) FROM Answer a WHERE a.question.id = q.id) "
Although GROUP_CONCAT is not available in Mysql, for that you can also bind SQL function in hibernate as described in this post.
After a day of various trial and error, I found the following solution, I hope someone will broaden their horizons and help in solving their problem:
entityManager.createQuery("SELECT " +
"q.id, " +
"q.title, " +
"q.user.fullName, " +
"q.user.reputationCount, " +
"q.viewCount, " +
"q.countValuable, " +
"q.persistDateTime, " +
"t.id, " +
"t.name, " +
"t.description, " +
"(SELECT COUNT (a) FROM Answer a WHERE a.question.id = q.id), " +
"(SELECT CASE WHEN MAX (a.isHelpful) > 0 THEN true ELSE false END FROM Answer a WHERE a.question.id = q.id) " +
"FROM Question q JOIN q.tags t")

Optimizing hive queries

I am trying to optimize hive query. I have partitioned and stored my base table as ORC file as shown below.
create table if not exists processed (
plc string,
direction string,
table int,
speed float,
time string
) PARTITIONED BY (time_id bigint) STORED AS ORC;
I am firing the below query on the above table (contains 500.000 records). The final result I get is stored as a json. The whole transaction takes about 35 secs. Is there a way wherein I can reduce this time. Or may be, someone could suggest me using a different framework instead of Hive. This is the query :
String finalQuery = "select plc,direction,AVG(speed) as speed ,COUNT(plc) as count,time_id from processed WHERE plc IN "
+ " "
+ "("
+ plcCSV
+ ")"
+ " " + " " + "AND" + " " + "time_id =" + " " + time_id + " "
+ "group by plc,direction,time_id";
First of all create an index on plc column and then try.

Why adding SUM changes the number of retrieved rows?

I have this sql which gives me 6 rows without SUM clause (sur can give more if condition is true)
SELECT id, prodname, prodid, st_date, montant, tvaval, quantite, status, factureno
FROM StockData WHERE " + VenteWhere + " ORDER BY " + Order_by + " " + SortDir + "
PS: VenteWhere, Order_by abd SortDir are the variables.
BUt when I add SUM(quantite) I get only one row. Is there a way to have 6 rows data and the sum of 6 rows or I have to do another query for getting it
I think you can use
SELECT id, prodname, prodid, st_date, montant, tvaval, SUM(quantite), status, factureno
FROM StockData
WHERE " + VenteWhere + "
ORDER BY " + Order_by + " " + SortDir + "
GROUP BY xxxxx
Where xxxxx represents the variable you want to group by per row.
Summing will remove the rows its sums over and only display the sum itself. So you have to group by some variable to make it split the results over that variables 'occurence'

SSIS Convert string to DateTime dd/mm/yyy hh:mm:ss

I want to convert a string like "2152012 101946" using a derived column in SSIS.
The output should be like "21/05/2012 10:19:46" to fit into a [DateTime] SQL Server 2008 field
Thanks!
You should think about enriching your date time data . You need to have a proper format like
YYYYMMDD HH:MM::SS
or something similar to it . You just can't have
YYYYMDD HH:MM:SS
If you have your data in the correct format DDMMYYY HH:MM:SS then you can use the below expression in derived column
LEN(column) == 0 ? NULL(DT_DBTIMESTAMP) :
(DT_DBTIMESTAMP)(substring(column,1,2) + "-" + substring(column,3,2) + "-" +
substring(column,5,4) + " " + substring(column,10,2) + ":" substring(column,12,2)+ ":"
+ substring(column,14,2))
DT_DBTIMESTAMP data types must be in the following format for proper conversion:
YYYY-MM-DD HH:MM:SS
Using the date & time "2012-07-30 02:03:10" as an example, your derived column would appear as:
(DT_DBTIMESTAMP)(SUBSTRING([column],1,4) + "-" + SUBSTRING([column],5,2) + "-" + SUBSTRING([column],7,2) + " " + SUBSTRING([column],9,2) + ":" + SUBSTRING([column],11,2) + ":" + SUBSTRING([column],13,2))
The resulting output column would appear as:
2012-07-30 02:03:10.000
Recall that any missing values within a given date can be concatenated with the date values within your column. For example, using the string value "2152012 101946", your derived column expression would be written as:
(DT_DBTIMESTAMP)(SUBSTRING([column],4,4) + "- 0" + SUBSTRING([column],3,1) + "-" + SUBSTRING([column],1,2) + " " + SUBSTRING([column],9,2) + ":" + SUBSTRING([column],11,2) + ":" + SUBSTRING([column],13,2) + ".000")
Taking this one step further, since dates in string format often are not clean, you might consider writing a conditional statement which checks the length of a value prior to concatenating & converting the values. For example, if 2012/09/30 11:59pm and 2012/10/01 11:59pm are represented as strings in a column where leading zeros are not present, the strings may appear as:
"2012930 115959"
"20121001 115959"
To account for leading zeros in the month, an if-then-else expression could be incorporated such that:
LEN(column) = 14 ? (DT_DBTIMESTAMP)(SUBSTRING([column],1,4) + "- 0" + SUBSTRING([column],5,1) + "-" + SUBSTRING([column],6,2) + " " + SUBSTRING([column],9,2) + ":" + SUBSTRING([column],11,2) + ":" + SUBSTRING([column],13,2)) : (DT_DBTIMESTAMP)(SUBSTRING([column],1,4) + "-" + SUBSTRING([column],5,2) + "-" + SUBSTRING([column],7,2) + " " + SUBSTRING([column],10,2) + ":" + SUBSTRING([column],12,2) + ":" + SUBSTRING([column],14,2))
Note that the above expression does not take values that have a length less than 14 characters or greater than 15 characters into account. In this case, you could expand the above if-then-else expression to nest additional expressions for varying lengths.