1

I have trouble finding the correct SQL Query. As you can see in the picture, I have two Tables ('Table_1' with column 'VEHICULES') and ('Table_2' with column 'IMMATRICULATION' and 'NOMBRE'). In the Table_2 I want to insert in the column ('Nombre') the counted values from the Table_1 of VEHICULE (CarA, CarB and CarC) in the same row of these values as in the picture.

I tried the below query but it is added a value instead of placing it in the same row of CarA, CarB....

INSERT INTO Table_2 (NOMBRE)
    VALUES ((SELECT COUNT(*) FROM Table_1 WHERE VEHICULES = 'CarA'),
            (SELECT COUNT(*) FROM Table_1 WHERE VEHICULES = 'CarB'),
            (SELECT COUNT(*) FROM Table_1 WHERE VEHICULES = 'CarC')
           );

Picture with tables and required result

1

3 Answers 3

1

First generate the select query that returns the values you want inserted into the new table, then add INSERT INTO in the beginning. The group by clause is also what you're missing.

Here is an example

INSERT INTO Table_2 (Name, Cnt)
select VEHICULES, count(*)
from Table_1
group by VEHICULES
Sign up to request clarification or add additional context in comments.

6 Comments

Like that ? : INSERT INTO Table_2 (NOMBRE) VALUES ((SELECT COUNT () FROM Table_1 WHERE VEHICULES = 'CarA')), ((SELECT COUNT() FROM Table_1 WHERE VEHICULES = 'CarB')), ((SELECT COUNT(*) FROM Table_1 WHERE VEHICULES = 'CarC')); GROUP BY VEHICULES;
No, you don't need to specify a where clause at all. You just need to group by statement and it will count all of the values in that column "VEHICULES". All you need is the SQL example I've listed above and it will automatically insert the correct values for vehicules and count.
Okay I see ! But is it possible to update it whithout adding each time new values ?
I mean by keeping CarA, CarB and CarC in the table_2 and changing just the value of column NOMBRE
In that case, use UPDATE INTO instead of INSERT INTO. That will then update those records. Gordons answer below is more complete!
|
0

You seem to want update, not insert:

update table_2 t2 join
       (select vehicules, count(*) as cnt
        from table_1
        group by vehicules
       ) t1
       on t2.immatriculation = t1.vehicules
    set nombre = t1.cnt;

2 Comments

I have this ; Msg 102, Niveau 15, État 1, Ligne 1 Incorrect syntax near 't2'. Msg 102, Niveau 15, État 1, Ligne 5 Incorrect syntax near 't1'.
Your question is tagged MySQL, but that error looks like SQL Server. What database are you really using?
0

You need update obviously, rather than insert, and if you're using MSSQL Server (which's suggested by the error text you got), then use such a statement :

WITH t AS
(
 SELECT vehicules, COUNT(*) AS cnt
   FROM table_1
  GROUP BY vehicules 
)
UPDATE table_2
   SET nombre = cnt
  FROM t
 WHERE immatriculation = vehicules

Demo

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.