im having issues getting a query to output how i want,
SELECT
`orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`,
`orders`.`quantity`
FROM
`orders`
JOIN `products` ON `orders`.`item_id` = `products`.`id`
JOIN `suppliers` ON `products`.`supplier_ref` = `suppliers`.`supplier_ref`
WHERE
`suppliers`.`id` = 159
AND `orders`.`order_status` = 'NOTED'
which is returning the results:
item_id item_code item_name quantity
1271 RA001G Green Mop Bucket 12L 2
1270 RA001 Blue Mop Bucket 12L 1
1270 RA001 Blue Mop Bucket 12L 1
but i would like it to bring back distinct item_id with the quantity added together how ever when i've tried to add distinct and count i end up only have one line returned.
If you want to sum up the quantities for same items, try grouping over item_id. Like this:
SELECT
`orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`,
sum(`orders`.`quantity`) as quantity,
FROM
`orders`
JOIN `products` ON `orders`.`item_id` = `products`.`id`
JOIN `suppliers` ON `products`.`supplier_ref` = `suppliers`.`supplier_ref`
WHERE
`suppliers`.`id` = 159
AND `orders`.`order_status` = 'NOTED'
GROUP BY `orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`
Please use group by clause
SELECT
`orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`,
sum(`orders`.`quantity`) as quantity
FROM
`orders`
JOIN `products` ON `orders`.`item_id` = `products`.`id`
JOIN `suppliers` ON `products`.`supplier_ref` = `suppliers`.`supplier_ref`
WHERE
`suppliers`.`id` = 159 AND `orders`.`order_status` = 'NOTED'
group by
`orders`.`item_id`;
Related
I have two tables products and user_sales. I need to get the products which have 0 sales for the last 7 days.
I tried some code which I found in stack overflow with the "HAVING" statement but I found out if I put a limit of 12 products than it doesnt works.
Can anoyone please help ?
table products
id | title | price | images | description
table user_sales
id | product_id | sale_date | user_owner
Well that was the simplified version of the tables. The actual query looks somehow like this.
SELECT `product_store`.`friendly_name` AS `store_friendly_name`,
`api_keys`.`key_data` AS `api_data`,
`ep`.*,
`supplier_store`.`icon` AS `supplier_store_icon`,
`supplier_store`.`internal_name` AS `supplier_store_internal_name`,
`supplier_store`.`friendly_name` AS `supplier_store_friendly_name`,
`supplier_store`.`amazon_type_product` AS
`supplier_store_amazon_type_product`,
`supplier_store`.`handler` AS `supplier_store_handler`,
`sp`.`extra_data` AS `sp_extra_data`,
`sp`.`id` AS `sp_id`,
`sp`.`remote_id` AS `sp_remote_id`,
`sp`.`url` AS `sp_url`,
`sp`.`price` AS `sp_price`,
`sp`.`stock` AS `sp_stock`,
`sp`.`picture` AS `sp_picture`,
`sp`.`store_id` AS `sp_store_id`,
Count(us.date) AS sale_date
FROM `products` `ep`
LEFT JOIN `stores` `product_store`
ON `ep`.`store_id` = `product_store`.`id`
LEFT JOIN `api_keys`
ON `api_keys`.`id` = `ep`.`link_key`
LEFT JOIN `products` `sp`
ON `ep`.`linked_to` = `sp`.`id`
LEFT JOIN `stores` `supplier_store`
ON `supplier_store`.`id` = `sp`.`store_id`
RIGHT JOIN `user_sales` `us`
ON `ep`.`remote_id` = `us`.`remote_id`
WHERE `ep`.`user_owner` = '3992'
AND `ep`.`expired` = 0
AND `us`.`user_id` = '3992'
AND us.date > "2019-02-09 14:21:34"
AND us.date < "2019-05-10 14:21:34"
AND `ep`.`store_id` = 3
GROUP BY `ep`.`id`
HAVING `sale_date` < 1
ORDER BY `ep`.`id` DESC
LIMIT 15
You want to ensure that no sales exist for the product in the last seven days. Use NOT EXISTS for that.
From your query I take it that you want to restrict this to products belonging to user_owner 3992 and to sales of the same user.
WHERE ep.user_owner = 3992
AND ep.expired = 0
AND ep.store_id = 3
AND NOT EXISTS
(
SELECT *
FROM user_sales us
WHERE us.user_id = ep.user_owner
AND us.date > current_date - interval 7 day
)
The complete query:
SELECT product_store.friendly_name AS store_friendly_name,
api_keys.key_data AS api_data,
ep.*,
supplier_store.icon AS supplier_store_icon,
supplier_store.internal_name AS supplier_store_internal_name,
supplier_store.friendly_name AS supplier_store_friendly_name,
supplier_store.amazon_type_product AS
supplier_store_amazon_type_product,
supplier_store.handler AS supplier_store_handler,
sp.extra_data AS sp_extra_data,
sp.id AS sp_id,
sp.remote_id AS sp_remote_id,
sp.url AS sp_url,
sp.price AS sp_price,
sp.stock AS sp_stock,
sp.picture AS sp_picture,
sp.store_id AS sp_store_id
FROM products ep
LEFT JOIN stores product_store ON ep.store_id = product_store.id
LEFT JOIN api_keys ON api_keys.id = ep.link_key
LEFT JOIN products sp ON ep.linked_to = sp.id
LEFT JOIN stores supplier_store ON supplier_store.id = sp.store_id
WHERE ep.user_owner = 3992
AND ep.expired = 0
AND ep.store_id = 3
AND NOT EXISTS
(
SELECT *
FROM user_sales us
WHERE us.user_id = ep.user_owner
AND us.date > current_date - interval 7 day
)
ORDER BY ep.id DESC;
I have 3 tables
table_supplier_bills - bill_id, supplier_id, date
table_supplier_bill_details - bill_id, product_id, quantity, rate
table_supplier_bill_payment_details - id, bill_id, payment_date, amount
I want to get all the bills with their bill_amount and paid_amount.
This is my query.
select
SB.bill_id,
SB.date, SB.supplier_id,
SUM(SBD.quantity * SBD.rate) as bill_amount,
COALESCE(SUM(SBPD.payment_amount), 0.00) as paid_amount
from table_supplier_bills SB
INNER JOIN
table_supplier_bill_details SBD
ON SB.bill_id = SBD.bill_id
LEFT JOIN table_supplier_bill_payment_details SBPD
ON SBD.bill_id = SBPD.bill_id
group by SBD.bill_id;
But this query doesn't give correct paid_amount if there are multiple rows in table_supplier_bill_details for a bill. in case of multiple rows query gives the paid_amount multiplied by as many rows are in table_supplier_bill_details for that table.
Can anyone help me what is wrong here?
Use a correlated query instead :
SELECT SB.bill_id,
SB.date,
SB.supplier_id,
SUM(SBD.quantity * SBD.rate) as bill_amount,
COALESCE((SELECT SUM(SBPD.payment_amount)
FROM table_supplier_bill_payment_details SBPD
WHERE SBD.bill_id = SBPD.bill_id ),0.00) as paid_amount
FROM table_supplier_bills SB
INNER JOIN table_supplier_bill_details SBD
ON SB.bill_id = SBD.bill_id
GROUP BY SBD.bill_id;
Can anyone see what is wrong with the below query?
The Value I have is the products_model number 000011195001
What I need is the sum attributes_stock of all corresponding products_id in Table products_attribute
The sum of all attributes_stock of products_id 1726 in Table products_attribute is 500
These 500 need I in Table products in Field products_quantity
When I run it I get:
UPDATE products As C INNER JOIN (
SELECT SUM( attributes_stock ) AS products_quantiry
FROM products_attributes
WHERE products_id IN(
SELECT products.products_id
FROM products
WHERE products_model LIKE '000011195001'
)
)
AS A ON products.products_id = products_attributes.products_id
SET C.products_quantity = A.products_quantiry
Table products
products_model, products_id,products_quantity
values
000011195001, 1726
Table products_attribute
products_id,attributes_stock
1726, 300
1726, 150
1726, 50
Thanks in advance
here (i have done a sql fiddle but the website isn't working right now :'():
UPDATE products
INNER JOIN
(SELECT products_id, SUM(attributes_stock) as qty
FROM products_attribute
GROUP BY products_id) T ON T.products_id = products.products_id
SET products.products_quantity = T.qty
WHERE products_model = '000011195001'
;
I have those queries
SELECT CA_id, item_id, item_Cant, item_desc FROM items WHERE CA_id = 135
SELECT CA_id, prov_name, unitval, totval FROM provprices WHERE CA_id = 135 AND prov_name = 'SITECH'
and I want to join this two results, just like:
item_id - item_Cant - item_desc - unitval - totval
I tried diferent forms but the max that i can get is:
(If you notice the result from second table is dublicated, should be like the second image )
SELECT items.item_id,items.item_Cant,items.item_desc,provprices.unitval,provprices.totval
FROM items,provprices
WHERE items.CA_id = provprices.CA_id
AND provprices.prov_name = 'SITECH'
AND items.CA_id = '135'
GROUP BY items.item_id
If i change to GROUP BY provprices.unitval the duplicated result now is the first one
I hope you can help me. Thanks
I don't understand so much, but try this,
How do you wanna calculate the unitval and the totval
select item_id, item_Cant, item_desc, AVG(unitval), sum(totval)
from (
SELECT CA_id, item_id, item_Cant, item_desc
FROM items
WHERE CA_id = 135
) A
inner join (
SELECT CA_id, prov_name, unitval, totval
FROM provprices
WHERE CA_id = 135 AND prov_name = 'SITECH'
) B ON A.CA_id=B.CA_id
GROUP BY item_id, item_Cant, item_desc
With this query, I am getting result with null values and duplicate Ids...
SELECT QuesId,QuesName,[Ketan Mevada],[Parvej],[Parvez Vahora]
FROM (
SELECT tbl_EvolutionAnswer.QuesId,tbl_QuestionMaster.Name as QuesName,
dbo.Evaluation_Calculation_CourseWise(tbl_EvolutionAnswer.QuesId,34,'Course-Green Course-2045',1065, tbl_EvolutionAnswer.TrainerId ) as Average,
tbl_EvolutionAnswer.TrainerId,
tbl_TrainerMaster.Name as TrName
from tbl_EvolutionAnswer
inner join tbl_TrainerMaster
on tbl_EvolutionAnswer.TrainerId = tbl_TrainerMaster.Id
inner join tbl_QuestionMaster
on tbl_EvolutionAnswer.QuesId = tbl_QuestionMaster.QuestionId
where tbl_EvolutionAnswer.EvolId =34
and tbl_EvolutionAnswer.TrainerId <> 0
and tbl_EvolutionAnswer.CourseId = 'Course-Green Course-2045'
and tbl_EvolutionAnswer.SchID = 1065
) as Books
PIVOT (
MAX(Average) FOR TrName IN ([Ketan Mevada],[Parvej],[Parvez Vahora])
) as Result
I need Following Output
QuesId QuesName Ketan Mevada Parvej Parvez Vohra
122 Did your trainer answer... 2 3 2
123 was your trainer activ.. 1 4 3
It appears that you have a column inside your subquery that is unique and it is causing the grouping of the aggregate function to be skewed. When you are using the PIVOT function, you should only include the columns needed for the PIVOT and the final select list, otherwise you run the risk of the end result being spilt over multiple rows.
It looks like the column you need to remove is tbl_EvolutionAnswer.TrainerId. Making your actual query:
SELECT QuesId,QuesName,[Ketan Mevada],[Parvej],[Parvez Vahora]
FROM
(
SELECT tbl_EvolutionAnswer.QuesId,
tbl_QuestionMaster.Name as QuesName,
dbo.Evaluation_Calculation_CourseWise(tbl_EvolutionAnswer.QuesId,34,'Course-Green Course-2045',1065, tbl_EvolutionAnswer.TrainerId ) as Average,
tbl_TrainerMaster.Name as TrName
from tbl_EvolutionAnswer
inner join tbl_TrainerMaster
on tbl_EvolutionAnswer.TrainerId = tbl_TrainerMaster.Id
inner join tbl_QuestionMaster
on tbl_EvolutionAnswer.QuesId = tbl_QuestionMaster.QuestionId
where tbl_EvolutionAnswer.EvolId =34
and tbl_EvolutionAnswer.TrainerId <> 0
and tbl_EvolutionAnswer.CourseId = 'Course-Green Course-2045'
and tbl_EvolutionAnswer.SchID = 1065
) as Books
PIVOT (
MAX(Average) FOR TrName IN ([Ketan Mevada],[Parvej],[Parvez Vahora])
) as Result