1

Hi everyone after some research i don't really succeed to find an answer that fits my problem

Here is my problem, I have an SQL Query and i want to iterate on this one and the iteration concerns the change of one parameter in the query. Here is the basic query

select count(*) 
from car_rent 
inner join car on car_rent.car_id = car.id 
where car_rent.created_at BETWEEN 2019-01-01 and 2019-01-31 
and car.zipcode like '75%'

Now what i want is to iterate over this query, to get all the results, and replacing at each time the 75% parameter by an other one. I have to go from 01 to 95.

I looked on stack and found some stuff that could be interesting but i don't really find how to put my query on this on to obtain the result i'm expecting

Dept | Count(*)
01   | 9
02   | 14
03   | 2
04   | 18
....

This is the result i'm looking for =)

The other query i found it this one =>

SELECT
    SEQ.SeqValue
FROM
(
SELECT
    (HUNDREDS.SeqValue + TENS.SeqValue + ONES.SeqValue) SeqValue
FROM
    (
    SELECT 0  SeqValue
    UNION ALL
    SELECT 1 SeqValue
    UNION ALL
    SELECT 2 SeqValue
    UNION ALL
    SELECT 3 SeqValue
    UNION ALL
    SELECT 4 SeqValue
    UNION ALL
    SELECT 5 SeqValue
    UNION ALL
    SELECT 6 SeqValue
    UNION ALL
    SELECT 7 SeqValue
    UNION ALL
    SELECT 8 SeqValue
    UNION ALL
    SELECT 9 SeqValue
    ) ONES
CROSS JOIN
    (
    SELECT 0 SeqValue
    UNION ALL
    SELECT 10 SeqValue
    UNION ALL
    SELECT 20 SeqValue
    UNION ALL
    SELECT 30 SeqValue
    UNION ALL
    SELECT 40 SeqValue
    UNION ALL
    SELECT 50 SeqValue
    UNION ALL
    SELECT 60 SeqValue
    UNION ALL
    SELECT 70 SeqValue
    UNION ALL
    SELECT 80 SeqValue
    UNION ALL
    SELECT 90 SeqValue
    ) TENS
) SEQ

it provides me a result of 99 rows, but i don't really know how to combine my sql request with this one to obtain the result i want

If anyone could give me some hints, that would be really appreciate

2
  • What is the source of the Dept column, and what is its relationship to the count? (there doesn't appear to be any relationship). Commented Oct 10, 2019 at 9:27
  • I would like the source of the dept variable to be the iteration of 1 to 99, and to use this dept information as parameter in my query Commented Oct 10, 2019 at 9:30

3 Answers 3

2

I believe following query is what you need

SELECT S.SeqValue, COUNT(C.zipcode)
FROM
(
    SELECT
        SEQ.SeqValue
    FROM
    (
    SELECT
        (TENS.SeqValue + ONES.SeqValue) SeqValue
    FROM
        (
        SELECT 0  SeqValue
        UNION ALL
        SELECT 1 SeqValue
        UNION ALL
        SELECT 2 SeqValue
        UNION ALL
        SELECT 3 SeqValue
        UNION ALL
        SELECT 4 SeqValue
        UNION ALL
        SELECT 5 SeqValue
        UNION ALL
        SELECT 6 SeqValue
        UNION ALL
        SELECT 7 SeqValue
        UNION ALL
        SELECT 8 SeqValue
        UNION ALL
        SELECT 9 SeqValue
        ) ONES
    CROSS JOIN
        (
        SELECT 0 SeqValue
        UNION ALL
        SELECT 10 SeqValue
        UNION ALL
        SELECT 20 SeqValue
        UNION ALL
        SELECT 30 SeqValue
        UNION ALL
        SELECT 40 SeqValue
        UNION ALL
        SELECT 50 SeqValue
        UNION ALL
        SELECT 60 SeqValue
        UNION ALL
        SELECT 70 SeqValue
        UNION ALL
        SELECT 80 SeqValue
        UNION ALL
        SELECT 90 SeqValue
        ) TENS
    ) SEQ
) AS S
LEFT JOIN
( 
    SELECT car.zipcode
    FROM car_rent 
    INNER JOIN car ON car_rent.car_id = car.id 
    WHERE car_rent.created_at BETWEEN 2019-01-01 AND 2019-01-31 
) AS C ON C.zipcode LIKE CONCAT(CONVERT(VARCHAR, S.SeqValue), '%')
GROUP BY S.SeqValue

UPDATE

ON clause updated for MYSQL (thanks @Raymond for pointed out)

Sign up to request clarification or add additional context in comments.

5 Comments

Can you tell me why? I didn't run in MYSQL but the syntax is same as SQL Server. So I still believe it's correct.
"So I still believe it's correct" Almost it is as + '%' is not possible in MySQL, MySQL uses CONCAT() to string concat...
@Raymond, there is no need to be aggresive here. I honestly asked your opionion. I'm not a mysql expert. If you have an update which will make this answer correct happy to hear it.
hi! First of all thanks for you answer, i tried what you provided unfortunatelly it has some issue with the count i think, because i get only 0 for every value.... But the basic request works and gives me the amount for one DPT. So i think there might be something to re-work on your request, i will have a look on why it's not working properly EDIT: Might be my database that doesn't contains data for the date provided previously i will l check
Yes @T3ddy. If there is no data it will be just 0 because of LEFT JOIN. You can change it to INNER JOIN. That way, if there is no data it will not be appear
1

Something like this:

select left(car.zipcode, 2) Zip, count(*) 
from car_rent 
inner join car on car_rent.car_id = car.id 
where car_rent.created_at BETWEEN 2019-01-01 and 2019-01-31 
**and car.zipcode like '75%'** EDIT Apologies: this line needs to be removed.
group by left(car.zipcode, 2)

..will do it without iterating.

However, I'm not sure that this is valid SQL for MySQL as I don't have it. You may need to change it to

  • get the 2 left-most characters of the zipcode
  • name the selected column (ISTR MySql does something with ` marks and naming?)
  • MySQL GROUP BY has some interesting quirks.

4 Comments

Hi simon ! THanks for the solution it's working properly and in reality really simple to use !
@T3ddy No problem - glad it helped! There's a convention on this site that if an answer helps you, you give it a vote (tick the up-arrow to the left of the answer). This gives the answerer reputation points as a nice way of saying thank-you. It's not required, but it is appreciated. Cheers!
i will just give the upvote to serdar, your solution works but it excludes the dept with a count of 0, whereas his solution provide them ! However if you can update your solution to avoid this issue i will upvote on yours !
No problem. (I've used a variation on Serdar's technique before and it's great tool to have in your armoury!)
0

You have multiple issues in the query. A very close approximation to what you want is:

select left(c.zipcode, 2) as zip2, count(*) 
from car c join
     car_rent cr
     on cr.car_id = c.id 
where cr.created_at >= '2019-01-01' and
      cr.created_at < '2019-02-01'
group by left(c.zipcode, 2)
order by zip2;

Notes:

  • Date constants need single quotes.
  • Table aliases make the query easier to write and to read.
  • It is unclear if created_at has a time component. If so, your version misses rows created on 2019-01-31, unless created at exactly midnight. Replacing the between with the above logic works both for dates and datetimes.
  • The basic solution to your question is group by.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.