Error in WITH RECURSIVE while troubleshooting hacker rank - mysql

Summary HackerRank Problem
(1) Draw Triangle 1
P(5) :
* * * * *
* * * *
* * *
* *
*
This is example and i have to make this P(20)
WITH RECURSIVE stars (n,star) AS(
SELECT 39,LEFT('* * * * * * * * * * * * * * * * * * * *',39)
UNION ALL
SELECT n-2,LEFT('* * * * * * * * * * * * * * * * * * * *',n-2)
FROM stars
WHERE n>1
)
SELECT star
FROM stars
I solved question of Draw the Triangle 1 in this way.
(2) Draw Triangle 2
P(5):
*
* *
* * *
* * * *
* * * * *
This is example and i have to make this P(20)
However, this does not work in Draw THE Triangle 2
WITH RECURSIVE stars (n,star) AS(
SELECT 1, LEFT('* * * * * * * * * * * * * * * * * * * *',1)
UNION ALL
SELECT n+2, LEFT('* * * * * * * * * * * * * * * * * * * *',n+2)
FROM stars
WHERE n<38
)
SELECT star
FROM stars
WITH RECURSIVE stars (n,star) AS(
SELECT 1,'*'
UNION ALL
SELECT n+1, CONCAT(star,' *')
FROM stars
WHERE n<20
)
SELECT star
FROM stars
I tried using LEFT() by changing the number just like Triange 1, but the HackerRank site showed a message like 'ERROR 1406 (22001) at line 1: Data too long for column 'star' at row 1' In my personal mysql, I only got OK, but I couldn't see the result table.
If the data is too long and it is an error, shouldn't I meet the same error in the first problem?
Is there something I don't know ?

Take a good look at outputs of queries for "triangle 1" and "triangle 2".
What is the difference? That's right, rows for the "triangle 2" query are in reverse order.
Which value in the query for "triangle 1" defines the order of rows? That's right, it's n.
How can I modify the query for "triangle 1" so that it outputs rows in reverse order? That's right, since the value of n in the first row is 39 and the last one is 1 (the rows are ordered by n in descending order), just reverse rows order by adding ORDER BY n.
So the query for "triangle2" should look like
WITH RECURSIVE stars(n, star) AS(
SELECT 39, LEFT('* * * * * * * * * * * * * * * * * * * *', 39)
UNION ALL
SELECT n-2, LEFT('* * * * * * * * * * * * * * * * * * * *', n-2)
FROM stars
WHERE n > 1
)
SELECT star
FROM stars
ORDER BY n
Just check it

Related

MySQL using `IN` while having multiple values

I am trying to count of coupons sold by each store from the list of stores within 20 miles range. I know the following syntax will work if there is only 1 store.
SELECT sum(couponscount) as count where restaurant IN (SELECT storename where bhal bhal bhal and output is one value)
What is I the IN (SELECTstorenamewhere bhal bhal bhal and output is multiple values) will return multiple values?
Like in my case the complete SQL is like and its not working
SELECT sum(couponscount) as count FROM `coupons` having `restaurant` IN (SELECT `storename`, ((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI( )) *60 * 1.1515) AS `distance` FROM `stores` WHERE `status`=’active’ HAVING `distance` <=20)
Is there anyway to make it working?
SELECT sum(couponscount) AS COUNT,restaurant
FROM `coupons`
WHERE `restaurant` IN
(SELECT `storename`
FROM `stores`
WHERE `status`='active'
AND
((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI()) *60 * 1.1515) <=20)
GROUP BY restaurant
Also use proper quotes for active.
Presumably, you want to get the count of coupons from stores within a distance of 20. Moving the having condition to a where clause should do what you want:
SELECT sum(couponscount) as count
FROM `coupons`
WHERE `restaurant` IN (SELECT `storename`
FROM `stores`
WHERE `status` = 'active' AND
((ACOS(SIN(-27.561264299999998 * PI()/180) * SIN(latitude * PI()/180) + COS(-27.561264299999998 * PI()/180) * COS(latitude * PI()/180) * COS((153.07304890000003 – longitude) * PI()/180)) *180 / PI( )) *60 * 1.1515) <= 20
);
You had a major syntax problem because your subquery returned two columns. When you use a subquery with in, you can only return one column, in this case, storename. I moved the code for the distance calculation to the where clause. No having clause is needed either in the subquery or the outer query.

send dot clicks to server using html5

I am trying to echo user input. Idea is user clicks the dots to draw a shape and then clicks submit. I receive the 'dots' he clicked, and then I reply back the dot numbers. example --
A B C D E F G H I J
1 * * * * * * * * * *
2 * * * * * * * * * *
3 * * * * * * * * * *
4 * * * * * * * * * *
5 * * * * * * * * * *
If a user clicks the '+' ones --
A B C D E F G H I J
1 * * * * * * * * * *
2 * * * * * + * + * *
3 * * * * * + * + * *
4 * * * * * * + * * *
5 * * * * * * * * * *
and then submits the page, I then give him - F2F3G4H3H2 (in sequence of how he clicked).
My issue is how to send the data to server? And, I can manage to do on click color change to make sure that the user clicked it. Or, even when he clicks, there is a line connecting the dots (like a stick shape).
How to do the same in HTML5, wherein every click he does on dots, is registered and then sent to the server.
One way would be to encode the state and then send it using an XMLHTTPRequest to the server.

MYSQL - Select most recent of column type entry

I have a table with columns:
s_Id (primary int), sup_type (int), sup_date (datetime), sup_req (int)
Values in sup_type so far range from 0-5.
How can I extract the most recent entry (by sup_date) of each sup_type (0-5).
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* s_Id * sup_type * sup_date * sup_req *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 1 * 0 * 2012-06-15 10:13:21 * 4 *
* 2 * 0 * 2012-06-15 11:22:01 * 4 *
* 3 * 1 * 2012-06-15 13:23:32 * 4 *
* 4 * 2 * 2012-06-16 08:04:29 * 4 *
* 5 * 1 * 2012-06-16 16:23:24 * 4 *
* 6 * 1 * 2012-06-17 13:14:05 * 4 *
* 7 * 3 * 2012-06-18 13:37:55 * 4 *
* 8 * 4 * 2012-06-19 13:21:52 * 4 *
* 9 * 4 * 2012-06-20 16:15:19 * 4 *
* 10 * 5 * 2012-06-20 16:17:37 * 4 *
* 11 * 0 * 2012-06-20 16:21:53 * 4 *
* 12 * 1 * 2012-06-20 16:28:13 * 4 *
* 13 * 3 * 2012-06-21 12:23:29 * 4 *
* 14 * 3 * 2012-06-22 07:26:41 * 4 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
I want to extract
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* s_Id * sup_type * sup_date * sup_req *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 4 * 2 * 2012-06-16 08:04:29 * 4 *
* 9 * 4 * 2012-06-20 16:15:19 * 4 *
* 10 * 5 * 2012-06-20 16:17:37 * 4 *
* 11 * 0 * 2012-06-20 16:21:53 * 4 *
* 12 * 1 * 2012-06-20 16:28:13 * 4 *
* 14 * 3 * 2012-06-22 07:26:41 * 4 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Thanks.
SELECT
b.*
FROM
(
SELECT sup_type, MAX(sup_date) AS maxsupdate
FROM tbl
GROUP BY sup_type
) a
INNER JOIN
tbl b ON
a.sup_type = b.sup_type AND
a.maxsupdate = b.sup_date
ORDER BY
b.s_Id

MySQL, select rows where a parameter value depends on the value that it has in a different row

I have a table where I can find the same parameter in subsequent rows (See Example A). I need a query to select only the rows where the value is different from the previous row (See Example B), something like
SELECT * FROM tableName WHERE Par(id)!=Par(id-1)
It shouldn't be difficult but I'm new to MySQL (and databases in general) and I haven't found an command or an example for this.
Example A Example B
********* *********
*ID *Par* **ID*Par*
********* *********
*1 * a * *5 * a *
*2 * a * *6 * g *
*3 * a * *7 * f *
*4 * a * *8 * d *
*5 * a * *9 * f *
*6 * g * *10 * h *
*7 * f * *11 * j *
*8 * d * *12 * f *
*9 * f * *17 * f *
*10 * h * *18 * d *
*11 * j * *19 * s *
*12 * f * *20 * g *
*13 * f * *21 * t *
*14 * f * *22 * g *
*15 * f *
*16 * f *
*17 * f *
*18 * d *
*19 * s *
*20 * g *
*21 * t *
*22 * g *
Try this:
SELECT t.id,t.par FROM your_table t
WHERE t.par <>
(SELECT par FROM your_table
WHERE id = t.id + 1)
SELECT DISTINCT Par
FROM table_name
http://www.w3schools.com/sql/sql_distinct.asp

COUNT(*) and Having

The following query gets me a column of distances.
But what i need is only the count of results with matching distances, not the distances themselves. A Subselect cannot be used.
SELECT
( 6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) * (sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) * cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869))) ) AS Distance
FROM ...
WHERE ...
HAVING Distance > 0 AND Distance <= 25
You just need to move the distance calculation to the where clause:
SELECT COUNT(*) FROM ...
WHERE ( 6368 * SQRT(2*(1-...) BETWEEN 0 AND 25
If you don't need the distances, only the count, maybe this will work:
SELECT Count(*)
FROM ...
WHERE ... AND
(6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) *
(sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) *
cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869)))
) BETWEEN 0 AND 25
This will give the totalResults, and you can discard the other column.
SELECT COUNT(*) totalResults,
( 6368 * SQRT(2*(1-cos(RADIANS(loc_lat)) * cos(0.899945742869) * (sin(RADIANS(`loc_lon`)) * sin(0.14286767838) + cos(RADIANS(`loc_lon`)) * cos(0.14286767838)) - sin(RADIANS(loc_lat)) * sin(0.899945742869))) ) AS Distance
FROM ...
WHERE ...
HAVING Distance > 0 AND Distance <= 25