I have three tables
Table A (orders)
order_number order_id
9999 123
Table B (order_items)
order_id product_id price
123 111 10
123 112 11
123 113 12
123 114 13
and Table C (product_customfields)
product_id customfield_id customfield_value
111 10 A
112 10 A
113 10 B
113 9 xyz
As a result I would like to get the product_id the price and in case a product has the customfield_id = 10 also the customfield_value
So in this case as a result I expect:
product_id price customfield_value
111 10 A
112 11 A
113 12 B
114 13 (empty)
In general my query looks like the following:
select B.product_id, B.price, C.customfield_value from orders A
left join order_items B on A.order_id=B.order_id
left join product_customfields C on B.product_id=C.product_id where A.order_number=9999 and C.customfield_id = 10
Of course the result will not show the product_id 114 because it has no customfield_id assigned in the database table with a value of "10"
Nevertheless could someone point me in the right direction how to build the query in a way to also show all products of the orders also if they are not assigned to a condition in the table.
Thank you
Does
select B.product_id, B.price, C.customfield_value from orders A
left join order_items B on A.order_id=B.order_id
left join product_customfields C on B.product_id=C.product_id
where A.order_number=9999 and (C.customfield_id = 10 or C.customfield_id IS NULL)
solves your issue?
You need a left outer join on the product_custom_fields like this
select B.product_id, B.price, C.customfield_value
from orders A
left join order_items B
on A.order_id=B.order_id
left outer join product_customfields C
on B.product_id=C.product_id
where A.order_number=9999 and
(C.customfield_id = 10 or C.customfield_id IS NULL)
Related
i have query select of 3 tables using left joins by that i want sum of second table column value but by adding third table join we get some extra column on summing second table column value i need to avoid this
here explanation,
indents(table 1)
indent_no | indent_name | request_qty
1131 AAA 834
purchase_order(table 2)
pur_id | pur_qty | indent_no
121 34 1131
122 100 1131
123 10 1131
grn (table 3)
grn_no | grn_val | pur_id
1 34 121
2 23 121
3 21 122
my query to get sum(purchase_order.pur_qty) it should be 144 right
but i am getting 178 because the grn table contains 2 rows for pur_id(121)
pls note i can only allowed to use group by clause for indents.indent_no
only
i tried to get 144 but getting 178 ... see you can get actual result if you use group by purchase_order.pur_id and with the select of sum(purchase_order.pur_qty)/count(grn.pur_id) but i cant use group by column other than indents.indent_no
select sum(purchase_order.pur_qty) from indents
left join purchase_order on purchase_order.indent_no = indents.indent_no
left join grn on grn.pur_id = purchase_order.pur_id
group by indents.indent_no
i want the result
indent_no sum(purchase_order.pur_qty) SUM(grn_val)
1131 144 78
Try this query,
select indents.indent_no,sum(DISTINCT(purchase_order.pur_qty)),sum(grn.grn_val) from indents
left join purchase_order on purchase_order.indent_no = indents.indent_no
left join grn on grn.pur_id = purchase_order.pur_id
group by indents.indent_no
You can try below - you don't need to join with grn table as you only want to sum of purchage qty
select indents.indent_no,sum(purchase_order.pur_qty) from indents
left join purchase_order on purchase_order.indent_no = indents.indent_no
group by indents.indent_no
In case you need to join with grn table then you can do this using subquery of grn table -
You can see that grn table has multiple value of one purchase id and that's reason of duplication and you get wrong result from your current query
select indents.indent_no,sum(purchase_order.pur_qty) from indents
left join purchase_order on purchase_order.indent_no = indents.indent_no
left join (select pur_id, sum(grn_va) as gval from grn group by pur_id) as grn
on grn.pur_id = purchase_order.pur_id
group by indents.indent_no
mysql SELECT query with left join is not producing the result I am expecting.
I hope someone can show me or point me to the right direction,
I am trying to build a query where I get all the users name from the "users" table and
fetch the sum of all the time they spent for a particular date from the master table. I've used the left join but I am not getting the result as expected.
SUM(m.time_spent) as sum_total_time
FROM master as m
LEFT OUTER JOIN users as u ON u.user_id = m.user_id
WHERE m.date_created >= '2016-05-09'
AND m.date_created <= '2016-05-13'
GROUP BY name
ORDER BY name
master table
master_id user_id time_spent date_created
1 1 40 2016-05-01
2 2 36 2016-05-02
3 3 56 2016-05-03
4 2 33 2016-05-03
5 1 32 2016-05-05
nth nth nth number nth date
users table
user_id first_name last_name
1 James Green
2 Robert Cox
3 Andy Roger
etc etc etc
I want the output result should look like this:
user_id Name sum_total_time
1 James Green 62
2 Robert Cox 69
3 Andy Roger 56
4 Brian Harper 0
5 Angel Lee 0
6 Andrew Martin 55
.....
.....
Nth Name Nth value
You have to select data directly from the master table, group by user and calculate the sum. Then you can join this result with the user table to get all the information about the user.
Could be date conversione issue ..
SUM(m.time_spent) as sum_total_time
FROM master as m
LEFT OUTER JOIN users as u ON u.user_id = m.user_id
WHERE m.date_created >=STR_TO_DATE( '2016-05-09', '%Y-%m-%d)
and/or you have also incomplete sql
SUM(m.time_spent) as sum_total_time
FROM master as m
LEFT OUTER JOIN users as u ON u.user_id = m.user_id
WHERE m.date_created >= '2016-05-09'
AND m.date_created // this condition don't match with nothing..
// could be you forgot a part
Update 1
If you want user totale then
select u.id, u.name, SUM(m.time_spent) as sum_total_time
FROM master as m
Inner JOIN users as u ON u.user_id = m.user_id
WHERE m.date_created >=STR_TO_DATE( '2016-05-09', '%Y-%m-%d)
AND m.date_created <= =STR_TO_DATE('2016-05-13'', '%Y-%m-%d)
Group by u.id
I've got the following tables:
Articles:
Nr Name Price
1011 DU 10
1012 DA 5
1013 DO 20
Clients
Nr Name Street Zip
123 John ... ...
234 Will ... ...
Orders
Nr Client_Nr Art_Nr Quantity
1 123 1011 1
2 123 1012 2
3 234 1012 2
4 234 1013 5
To know the total sum of orders per customer,
I use the following statement:
SELECT Clients.Name,
SUM(Orders.Quantity * Article.Price) AS "Total"
FROM Orders
LEFT OUTER JOIN Articles
ON Orders.Art_Nr = Articles.Nr
LEFT OUTER JOIN Clients
ON Orders.Client_Nr = Clients.Nr
GROUP BY Clients.Name;
Is left outer joins used when there are rows in one table that may not be referenced in the second table.
What is the better way of writing the above query?
Can I do this:
SELECT clients.name, SUM(orders.quantity * articles.price)
FROM orders
INNER JOIN articles ON orders.art_nr = articles.nr
INNER JOIN clients ON orders.client_nr = clients.nr
GROUP BY clients.name;
I know there are many posts in similar issues to mine, but I do not seem to be able to find an applicable solution to my case, as below.
I have added null columns so that the number of columns match and I know that I need to have all columns stated in the first select. Therein lies the problem. I do not know how to include a COUNT column in the first select that would correspond to the COUNT in the second select.
Thanks for the assistance.
First select:
SELECT brands.brand_id,
brands.brand,
models.model_id,
models.model,
segments.segment_id,
segments.segment,
versions.version_id,
versions.version,
versions.places,
versions.gearbox,
versions.doors,
prices.price
FROM versions
INNER JOIN models USING (model_id)
INNER JOIN segments USING (segment_id)
INNER JOIN brands USING (brand_id)
INNER JOIN prices USING(version_id)
WHERE price BETWEEN 200001 AND 225000
AND brands.active = 'Y'
AND models.active = 'Y'
AND versions.active = 'Y'
Second select:
SELECT Count(*) AS SafetyItems,
version_id,
NULL AS COL3,
NULL AS ....,
NULL AS COL12
FROM versiontrim
INNER JOIN trims USING(trim_id)
INNER JOIN versions USING(version_id)
INNER JOIN prices USING(version_id)
INNER JOIN models USING (model_id)
INNER JOIN brands USING (brand_id)
WHERE trimtype IN( 'sec', 'help' )
AND price BETWEEN 200001 AND 225000
AND brands.active = 'Y'
AND models.active = 'Y'
AND versions.active = 'Y'
GROUP BY version_id
Sample result of first select:
brand_id brand model_id model segment_id version_id price
58 Renault 11 Megane 4 44 209900
58 Renault 14 Scenic 5 54 209900
58 Renault 11 Megane 4 69 200900
71 Toyota 29 Yaris 2 214 200900
71 Toyota 30 Auri 4 216 207900
52 Nissan 58 Pick-up 14 282 209000
24 Ford 21 Focus 4 290 209000
Sample result of second select that I want have appended to above ( after the price column):
SafetyItems version_id
9 44
7 54
9 69
10 214
6 216
1 282
10 290
I guess you also want to put a NULL column in your first SELECT statement that would correspond for the COUNT column in the second SELECT statement.
Just put a NULL column in your first SELECT just like in your second SELECT statement, that should be fine. And do not forget the ALIAS of the column, since UNION uses the column name of the first SELECT.
SELECT
NULL AS SafetyItems,
brands.brand_id,
brands.brand,
models.model_id,
models.model,
segments.segment_id,
segments.segment,
versions.version_id,
versions.version,
versions.places,
versions.gearbox,
versions.doors,
prices.price
FROM versions
INNER JOIN models
USING (model_id)
INNER JOIN segments
USING (segment_id)
INNER JOIN brands
USING (brand_id)
INNER JOIN prices
USING(version_id)
WHERE price BETWEEN 200001 AND 225000
AND brands.active='Y'
AND models.active='Y'
AND versions.active='Y'
But then, you need to add another NULL column in the second SELECT to match with column count in the first SELECT. Hope this will help you.
I realized I was going about this with the wrong approach. Initially I thought I could not do select 2 with COUNT as part of select 1. It turns out it is possible and I managed to have it done after some struggling.
Thanks for the input. Please see solution below:
SELECT brands.brand_id,
brands.brand,
models.model_id,
models.model,
segments.segment_id,
segments.segment,
versions.version_id,
versions.version,
versions.places,
versions.gearbox,
versions.doors,
prices.price,
COUNT(trimtype)
FROM versions
INNER JOIN models USING (model_id)
INNER JOIN segments USING (segment_id)
INNER JOIN brands USING (brand_id)
INNER JOIN prices USING(version_id)
INNER JOIN versiontrim USING(version_id)
INNER JOIN trims USING(trim_id)
WHERE price BETWEEN 200001 AND 210000
AND trimtype IN('sec', 'help')
AND brands.active = 'Y'
AND models.active = 'Y'
AND versions.active = 'Y'
GROUP BY version_id
I am trying to make one query, to get some statistic data from database.
My tables structure described here:
PRODUCTS
id | price | buy_price | vendor_code | ...
ORDERS
id | shipping_method_id | ...
ORDER_ITEMS
id | order_id | product_id | quantity | ...
SHIPPING_METHODS
id | cost | ...
SUPPLIERS
id | code | ...
I want to get data like this. In words, I am making reports on Product total income, expenses and quantity of buys in my ecommerce, and want them to group by supplier. I wrote this sql:
SELECT orders.id,
suppliers.code,
COUNT(products.id)*items.quantity buys,
SUM(products.price*items.quantity + shipping_methods.cost) sales,
SUM(products.buy_price*items.quantity) expenses
FROM `orders` orders
INNER JOIN `order_items` items ON items.order_id = orders.id
INNER JOIN `products` products ON items.product_id = products.id
INNER JOIN (SELECT DISTINCT suppliers.code FROM `suppliers`) suppliers
ON products.vendor_code LIKE CONCAT(suppliers.code, '%%')
INNER JOIN `shipping_methods` shipping_methods ON orders.shipping_method_id = shipping_methods.id
WHERE (
orders.delivery_date_to BETWEEN '2011-11-18' AND '2011-11-19'
)
GROUP BY suppliers.code, orders.id
ORDER BY buys DESC
this returns to me this data:
order_id code buys sales expenses
85 SB 4 1504 1111.32
84 VD 2 496 350.82
60 lg 2 1418 1052.31
88 SB 1 376 277.83
When I change GROUP BY suppliers.code, orders.id to GROUP BY suppliers.code, it returns almost correct data, I mean data is grouped by code, but counting is wrong. Admit that sales and expenses are correct
order_id code buys sales expenses
85 SB 8 1880 1389.15
60 lg 2 1418 1052.31
84 VD 2 496 350.82
If u see SB counted total 8 sales, but really there are only 5, as u can see in previous table. I'm sure I missed something in my query, but cant understand how to correct this.
PS field order_id are unused in my further scripts, I use it because django's Model.objects.raw() query does need to have primary key in result, don't really understand why
try this query.
SELECT t1.code, SUM(t1.buys), SUM(t1.sales) FROM (
SELECT orders.id,
suppliers.code,
COUNT(products.id)*items.quantity buys,
SUM(products.price*items.quantity + shipping_methods.cost) sales,
SUM(products.buy_price*items.quantity) expenses
FROM `orders` orders
INNER JOIN `order_items` items ON items.order_id = orders.id
INNER JOIN `products` products ON items.product_id = products.id
INNER JOIN (SELECT DISTINCT suppliers.code FROM `suppliers`) suppliers
ON products.vendor_code LIKE CONCAT(suppliers.code, '%%')
INNER JOIN `shipping_methods` shipping_methods ON orders.shipping_method_id = shipping_methods.id
WHERE (
orders.delivery_date_to BETWEEN '2011-11-18' AND '2011-11-19'
)
GROUP BY suppliers.code, orders.id
ORDER BY buys DESC
) AS t1
GROUP BY t1.code
Edit: I've forgotten SUM() parts. Just added, please retry if you've already tried.