MySQL multiplication - mysql

I am trying to do a multiplication operation in MySQL.
I have the following table called vehicul:
id car_type car_model number price car_id
1 audi <model1> 2 320 1
2 audi <model2> 4 100 1
3 bmw <model1> 3 240 2
4 bmw <model2> 6 500 2
5 bmw <model3> 1 400 2
I have this command:
SELECT car_type, sum(price) FROM vehicul as v, GROUP BY marca
Which displays the following:
car_type price
audi 420
bmw 1140
But what I want is to display the number * price and the result to be:
car_type price
audi 1040
bmw 4120
Basically.. sum(number*price).. how can I do that? I have tried doing:
SELECT car_type, sum(price*number) FROM vehicul as v, GROUP BY marca
But I am getting:
#1052 - Column 'number' in field list is ambiguous

select car_type, sum(price*number) as price from vehicul group by car_type..
http://sqlfiddle.com/#!2/3d881/1
you should use sum(field_1*field_2) instead of sum()*sum()

Your query is totally correct. You may have more than 1 table which contain 2 or more number column. Try:
SELECT car_type, sum(price)*sum(number)
FROM vehicul
GROUP BY marca;
But with the logic you will need this to get total price:
SELECT car_type, sum(price*number)
FROM vehicul
GROUP BY marca;

Related

MYSQL for selecting values from table and show with comma separator

I have two tables:
sales Table
===============================
id cust_id total_price
===============================
1 1 1000
2 2 1500
sales_item Table
======================================================
id sales_id cust_id product quantity
======================================================
1 2 2 pen 2
2 2 2 pencil 3
3 1 1 book 2
4 1 1 pencil 2
I need to query these two tables inorder to get the following result:
=========================================
sales_id cust_id product
=========================================
2 2 pen,pencil
1 1 book,pencil
Can anyone help me to query these 2 tables inorder to get the above result??
I tried using GROUP_CONCAT. Hers is what I have tried:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
And the result I got is:
======================================================
id cust_id total_price product
======================================================
2 2 1500 pen,pencil,book
This is the not the result am expecting..
Try something like this, consider that this is not tested, it may help you.
select sales.id,sales.cust_id, concat(sales_item.product)
from sales LEFT JOIN sales_item ON sales_item.sales_id = sales.id
group by sales.id
I got the answer by using GROUP_CONCAT itself. Here is my Query:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
GROUP BY s.id

mysql get the columns sum and also get the distinct values at a time

I have my data base like this
id project_id client_id price
1 1 1 200
2 2 1 123
3 2 1 100
4 1 1 87
5 1 1 143
6 1 1 100
7 3 3 123
8 3 3 99
9 4 3 86
10 4 3 43
11 4 3 145
12 4 3 155
Now here I want that it will sum the price columns with the same client_id.
For that I just made my query like this
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`
This one is doing sum the price but I am getting only two project_id in the result. I want the result should be all the distinct project for the client id and the price will be summed for the group clients.
So can someone tell me how to do this? Any help and suggestions will be really appreciable. Thanks
You should not have "bare" column in a group by query that are not in the group by statement.
If you want the list of projects, you can get them in a list like this:
SELECT client_id, GROUP_CONCAT(project_id), SUM(price)
FROM table-name
GROUP BY client_id;
you only have two client that why you are getting only two record , you can group by two column,
Select `project_id`, SUM(`price`) FROM `table-name` GROUP BY `client_id`, `project_id`

MySQL, two tables displaying information from both tables

I am learning SQL currently and I have a question. I have two tables called "vehicul" and proprietate.
The first table (vehicul) looks like this:
id nr_vehicul marca id_marca tip culoare capacitate_cilindrica
1 DJ-01-AAA Mercedes 1 CLK 350 negru 3500
2 DJ-01-BBB Mercedes 1 S 500 silver 5000
3 DJ-01-CCC Mercedes 1 ML 550 alb 5500
4 DJ-01-DDD BMW 2 325 galben 2500
5 DJ-01-EEE BMW 2 X5 negru 350
And the second table (proprietate) looks like this:
id serie_buletin cnp nr_vehicul data_cumpararii pret
1 AK162332 2006036035087 DJ03AAA 2014-05-01 35000
2 AK162332 2006036035087 DJ03BBB 2014-05-02 90000
3 AK176233 6548751520125 DJ03CCC 2014-05-03 55000
4 BZ257743 6548751520125 DJ03DDD 2014-05-04 25000
5 BZ257743 2006036035087 DJ03EEE 2014-05-05 63000
I want to display the column "marca" from the first table and the column "price" from the second, but like this.
marca | pret
Mercedes | 180000
BMW | 88000
Basically I have three Mercedes cars and two BMW cars, how can I display Mercedes one time and the prices of those three summed up?
You need to join two tables and GROUP them based on marca field and sum pret
select marca, sum(pret)
from table1 as t1, table2 as t2
where t1.id=t2.id
group by marca
Here I'm assuming that id field is joining two tables, (but as I can see from your sampel data it isn't relating to each-other actually)
EDIT
I think you are missing id_marca field in table2. If its there then it would join to that column as below example:
select marca, sum(pret)
from table1 as t1, table2 as t2
where t1.id_marca=t2.id_marca
group by id_marca;
use this requeste
select marca, sum(pret)
from vehicul as Ve, proprietate as Pr
where Ve.nr_vehicul=PR.nr_vehicul
group by marca
or
select marca, sum(pret)
from vehicul as Ve
where Ve.id in(select nr_vehicul from proprietate)
group by marca
Your tables are not related and hence its impossible to get the desired output as you are looking for.
For the 2nd table I would suggest to add a column called id_marca and store the value of each id_marca from first table to the 2nd table. So for all Mercedes its 1 and in the 2nd table also store it as 1 and for BMW its 2 and so on.
Now your tables are related and you can join as
select
v.marca
sum(p.pret) as pret
from vehicul v
join proprietate p on p.id_marca = v.id_marca
group by marca

Group by only for "limit and offset" in MySQL

Whilst trying to do pagination I've run into this problem.
My table-
ID CarBrand Car Model
---------------------------
1 Alfa Romeo Guilietta
2 Alfa Romeo Mito
3 Audi A3
4 Audi R8
5 Audi TT
6 Fiat Punto
7 Fiat Panda
8 Ford Mondeo
9 Ford Mustang
10 Nissan Almera
11 Nissan Note
12 Nissan Qashqai
13 Toyota Aygo
14 Toyota Prius
15 Volkswagen Beetle
16 Volkswagen Golf
17 Volkswagen Polo
18 Volkswagen Up
I have the data displayed like so, in groups of two:
-Fiat - Punto
Panda
-Ford - Mondeo
Mustang
So there are 2 brands but 4 database results.
Is it possible to have a query limit and offset my results to two brands while showing all the models for the brand?
Sorry if I'm not clear!
It is clear. Try this:
select * from t t1
join (
select distinct carBrand from t
limit 2
) s on t1.carBrand = s.carBrand
Before the limit 2 apply the ordering you want.
To get a limit, without using the limit keyword, you can impose a count.
For example, given the table definition
create table cars (id int,
carBrand char(10),
carModel char(10));
this will give you all the Car Models for the top 2 Car Brands
select cars.carBrand, cars.carModel
from cars
where ((select count(*) from
(select distinct carBrand from cars) as carBrands
where carBrands.carBrand < cars.carBrand) < 2)
order by cars.carBrand, cars.carModel;
This creates an inline table just listing the carBrands and then joins this back to cars to get the list of all cars that are in the top 2 brands. The count(*) .... < 2 enforces the limit. Consider 'Ford', for example, in your above data. In 'Ford''s case, there are 3 brands that are < 'Ford' alphabetically, so the count(*) above = 3. Since 3 is not less than 2, no 'Ford' cars appear in the output.
The output on your test data would be:
CARBRAND CARMODEL
Alfa Romeo Guilietta
Alfa Romeo Mito
Audi A3
Audi R8
Audi TT
Now, you didn't say how you wanted to pick the 2 brands -- you just listed Ford and Fiat in your example -- I don't know how you happened to pick those. If you want something other than alphabetical criteria for ordering, that's doable, but harder.
SQL Fiddle and results for all this: http://sqlfiddle.com/#!2/33a8f/3
It's a matter of database design. Maybe you should split your data into two tables model (model names) and brand (brand names). Then you can write a query like this:
SELECT m.name, b.name
FROM model m
INNER JOIN brand b
WHERE b.id IN (
SELECT id
FROM brand
ORDER BY name ASC
LIMIT 2 OFFSET 0
)
I did not test the code. No need for GROUP BY in my opinion.

Tricky "grouped" ordering in SQL

Been having trouble with this for some time.
I have a database sort of like this:
Car_ID Car_Brand Car_Model Car_Price
1 Ford Fiesta 4000
2 Ford Mustang 29000
3 Ford Focus 12000
4 Honda Civic 15000
6 Honda Jazz 5000
7 Toyota Prius 14000
I want to perform a search that finds the cheapest car then orders the rest of the cars of the same brand by price ascending.
I want my output to be this:
Car_ID Car_Brand Car_Model Car_Price
1 Ford Fiesta 4000
3 Ford Focus 12000
2 Ford Mustang 29000
6 Honda Jazz 5000
4 Honda Civic 15000
7 Toyota Prius 14000
The cheapest car is the Ford Fiesta so that and the rest of the Ford models follow it directly ordered by price. Honda then has the second cheapest model so the Jazz and the rest of the Hondas follow and so on.
Is this possible?
What you need to do is create a transient data set that contains car_brand and the lowest price for that brand (which I'll call brand_price), then JOIN that data back to your original cars table. This will give you the additional piece of information (brand_price) that you need to sort the data:
SELECT car_id, car_brand, car_model, price FROM cars C1
JOIN (select car_brand, MIN(price) AS brand_price FROM cars GROUP BY car_brand) C2
ON C1.car_brand = C2.car_brand
ORDER BY C2.brand_price, C1.car_brand, C1.price
Something like this should work:
SELECT a.*
FROM Cars a
LEFT JOIN (
SELECT Car_Brand, MIN(Car_Price) AS MinPrice
FROM Cars
GROUP BY Car_Brand
) b ON a.Car_Brand = b.Car_Brand
ORDER BY b.MinPrice, a.Car_Price
I would do a min grouped by Car_Brand order by the min price and then do a join ordered by the sorted car_brand and price. I will see if I can get the query done for this.
I think you will be able to do that with the following query
SELECT Car_ID, Car_Brand, Car_Model, Car_Price
FROM tblcars
ORDER BY Car_Price, Car_Brand
unless I missed something
select * from cars order by Car_Price, Car_Brand ASC