I've got a problem with an view on my database. All other views are working correctly, but when I'm trying to add a GROUP BY statement behind my query,
then I'll get the next Error:
1214 - The used table type doesn't support FULLTEXT indexes
I've set all my tables to ISAM, and i noticed the whole problem is in the GROUP BY statement.
My query looks like this:
CREATE VIEW `id_winkels`
AS
SELECT
`w`.`w_id` AS `w_id`,
`w`.`k_id` AS `k_id`,
`w`.`w_naam` AS `w_naam`,
`w`.`w_logo` AS `w_logo`,
`w`.`w_homepage` AS `w_homepage`,
`w`.`w_straat` AS `w_straat`,
`w`.`w_huisnummer` AS `w_huisnummer`,
`w`.`w_postcode` AS `w_postcode`,
`w`.`w_woonplaats` AS `w_woonplaats`,
`w`.`w_land` AS `w_land`,
`w`.`w_actief` AS `w_actief`,
`w`.`w_datum` AS `w_datum`,
COUNT(`p`.`p_id`) AS `totaal`
FROM `Winkels` `w`
LEFT JOIN `Producten` `p`
ON `w`.`w_id` = `p`.`w_id`
GROUP BY `w`.`w_naam`
Is there another option to count the amount of products by my shop without useing a group by?
When I'm running the query without the group by statement, it will return my total amount of product in one row.
As you only select fields from Winkels, you should get the product count in a subquery rather than joining the tables:
CREATE VIEW id_winkels AS
SELECT
w.w_id AS w_id,
w.k_id AS k_id,
w.w_naam AS w_naam,
w.w_logo AS w_logo,
w.w_homepage AS w_homepage,
w.w_straat AS w_straat,
w.w_huisnummer AS w_huisnummer,
w.w_postcode AS w_postcode,
w.w_woonplaats AS w_woonplaats,
w.w_land AS w_land,
w.w_actief AS w_actief,
w.w_datum AS w_datum,
(
select count(*)
from Producten p
where p.w_id = w.w_id
) AS totaal
FROM Winkels w;
Related
I am working with three tables, basically, one is a bill of materials, one contains part inventory, and the last one contains work orders or jobs. I am trying to find out if it is possible to have a correlated subquery that can perform a math operation using a value from the outer query. Here's an example of what I'm trying to do:
SELECT A.work_order,A.assembly,A.job_quantity,
(SELECT COUNT(X.part_number)
FROM bom X
WHERE X.assembly = A.assembly
AND (X.quantity_required * A.job_quantity) >= (SELECT Y.quantity_available FROM inventory Y WHERE
Y.part_number = X.part_number)) AS negatives
FROM work_orders A
ORDER BY A.assembly ASC
I am attempting to find out, for a given work order, if there are parts that we do not have enough of to build the assembly. I'm currently getting an "Error correlating fields" error. Is it possible to do this kind of operation in a single query?
Try moving the subquery to a join, something like this:
SELECT a.work_order, a.assembly, a.job_quantity, n.negatives
FROM work_orders a JOIN (SELECT x.part_number, COUNT(x.part_number) as negatives
FROM bom x JOIN work_orders b
ON x.assembly = b.assembly
WHERE (x.quantity_required * b.job_quantity) >= (SELECT y.quantity_available
FROM inventory y WHERE
y.part_number = x.part_number)
GROUP BY x.part_number) n
ON a.part_number = n.part_number
ORDER BY a.assembly ASC
Or create a temporary cursor with the subquery and then use it to join the main table.
Hope this helps.
Luis
I have the following tables below and their schema:
INV
id, product code, name, ucost, tcost, desc, type, qoh
1,123,CPASS 700,1.00,5.00,CPASS 700 Lorem, COM,5
2,456,Shelf 5,2.00,6.00,Shelf 5 KJ, BR,3
GRP
id,type,desc
1,COM,COMPASS
2,BR,SHELF
Currently I have a query like this:
SELECT INV.*,GRP.DESCR AS CATEGORY
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
There is no problems with that query.
Right now,I want to know the SUM of the TCOST of every INV record where their QOH is 0.
In this situation, does that I mean all I have to do is to write a separate query like the one below:
SELECT SUM(TCOST)
FROM INV
WHERE QOH = 0
Does it make any sense for me to try and combine those two queries as one ?
First understand that SUM is the aggregate function hence either you can run the Query like
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as total
This will return Sum of TCOST in INV Table for mentioned condition.
Another approach is finding the Sum based on the some column (e.g. Type)
you could write query like
SELECT Type , SUM(TCOST) FROM INV WHERE QOH=0 GROUP BY type ;
Its not clear on what criteria you want to sum . But I think above two approaches would provide you fare idea .
Mmm, you could maybe use a correlated query, though i'm not sure it's the best approach since I'm not sure I understand what your attempting to do:
SELECT INV.*,
GRP.DESCR AS CATEGORY ,
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as your_sum
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
If you want only one value for the sum(), then your query is fine. If you want a new column with the sum, then use window functions:
SELECT INV.*, GRP.DESCR AS CATEGORY,
SUM(INV.TCOST) OVER () as sum_at_zero
FROM INV LEFT JOIN
GRP
ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0;
It does not make sense to combine the queries by adding a row to the first one, because the columns are very different. A SQL result set requires that all rows have the same columns.
I created a query which selects sum of columns from a table grouped by a field in Crystal reports, now I want the result to be filtered by a date range from another table which I am unable to do. Please Help...
Here is the query
SELECT
BDETAIL.HSN,
SUM(BDETAIL.TAXABLE),
SUM(BDETAIL.SGST_V),
SUM(BDETAIL.CGST_V),
SUM(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM
BDETAIL
JOIN
BILL ON BDETAIL.BILL_ID = BILL.BILL_ID
WHERE
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY
BDETAIL.HSN, BDETAIL.SGST_P
I finally figured it out ..here is the working query
SELECT BDETAIL.HSN,
sum(BDETAIL.TAXABLE),
Sum(BDETAIL.SGST_V),
Sum(BDETAIL.CGST_V),
Sum(BDETAIL.TOTAL),
BDETAIL.SGST_P
FROM BDETAIL INNER JOIN BILL ON
( BILL.BILL_ID = BDETAIL.BILL_ID AND
BILL.BDATE BETWEEN {?FROM_DATE} AND {?TO_DATE} )
GROUP BY BDETAIL.HSN, BDETAIL.SGST_P;
thank you all for showing interest in my issue.
I have tried endless things to get this to work and it seems to break over and over again and not work. I'm trying to GROUP BY product after I have calculated the field quantity returned/quantity ordered, but I get the error
your query does not include the specified expression 'quantity_returned/quantity_ordered' as part of an aggregate function.
I do not want to GROUP BY quantity_returned, quantity_ordered, and product, I only want to GROUP BY product.
Here's what my SQL looks like currently...
SELECT
quantity_returned/quantity_ordered AS percentage_returned,
quantity_returned,
quantity_ordered,
returns_fact.product
FROM
Customer_dimension
INNER JOIN
(
Product_dimension
INNER JOIN
(
Day_dimension
INNER JOIN
returns_fact
ON Day_dimension.day_key = returns_fact.day_key
)
ON Product_dimension.product_key = returns_fact.product_key
)
ON Customer_dimension.customer_key = returns_fact.customer_key
GROUP BY returns_fact.product;
When you use a group by you need to actually include everything in your select that isn't a aggregate function.
I have no idea how your tables are set up, but I am throwing a blind dart. If you provide fields in each of the 4 tables someone will be better able to help.
SELECT returns_fact.product, count(quantity_returned), count(quantity_ordered), count(quantity_returned)/count(quantity_ordered) as percentage returned
I have a PHP page running in postgres. I have 3 tables - workorders, wo_parts and part2vendor. I am trying to multiply 2 table column row datas together, ie wo_parts has a field called qty and part2vendor has a field called cost. These 2 are joined by wo_parts.pn and part2vendor.pn. I have created a query like this:
$scoreCostQuery = "SELECT SUM(part2vendor.cost*wo_parts.qty) as total_score
FROM part2vendor
INNER JOIN wo_parts
ON (wo_parts.pn=part2vendor.pn)
WHERE workorder=$workorder";
But if I add the costs of the parts multiplied by the qauntities supplied, it adds to a different number than what the script is doing. Help....I am new to this but if someone can show me in SQL I can modify it for postgres. Thanks
Without seeing example data, there's no way for us to know why you're query totals are coming out differently that when you do the math by hand. It could be a bad join, so you are getting more/less records than you expected. It's also possible that your calculations are off. Pick an example with the smallest number of associated records & compare.
My suggestion is to add a GROUP BY to the query:
SELECT SUM(p.cost * wp.qty) as total_score
FROM part2vendor p
JOIN wo_parts wp ON wp.pn = p.pn
WHERE workorder = $workorder
GROUP BY workorder
FYI: MySQL was designed to allow flexibility in the GROUP BY, while no other db I've used does - it's a source of numerous questions on SO "why does this work in MySQL when it doesn't work on db x...".
To Check that your Quantities are correct:
SELECT wp.qty,
p.cost
FROM WO_PARTS wp
JOIN PART2VENDOR p ON p.pn = wp.pn
WHERE p.workorder = $workorder
Check that the numbers are correct for a given order.
You could try a sub-query instead.
(Note, I don't have a Postgres installation to test this on so consider this more like pseudo code than a working example... It does work in MySQL tho)
SELECT
SUM(p.`score`) AS 'total_score'
FROM part2vendor AS p2v
INNER JOIN (
SELECT pn, cost * qty AS `score`
FROM wo_parts
) AS p
ON p.pn = p2v.pn
WHERE p2n.workorder=$workorder"
In the question, you say the cost column is in part2vendor, but in the query you reference wo_parts.cost. If the wo_parts table has its own cost column, that's the source of the problem.