Sum() + DCount() combination in SQL not producing correct results - ms-access

How can I get the total number of Granny Smith apples? I have tried everything and nothing seems to work. This is a cross tab query and I'm trying to count the number of Granny_Smith apples in each shipping container.
MS Access 2007 Expression
Total_Green_Apples: Sum(DCount("[Apple_Type]","[Apples]","[Apple_Type]"='Granny_Smith'))
SQL
TRANSFORM Count(DCount("[Apple_Type]","[Apples]")) AS Apple_Type
SELECT Shipping.Container_number, Sum(DCount("[Apple_Type]","[Apples]","[Apple_Type]"='Granny_Smith')) AS Total_Green_Apples
FROM Shipping INNER JOIN Apples ON Shipping.ID = Apples.ID
GROUP BY Shipping.Container_number
PIVOT Apples.Apple_Type;
Please help.

If I'm reading your question correctly, you just need a count of all Granny Smith apples in all shipping containers?
I'm sort of guessing at your DB structure and here's what I come up with:
SELECT COUNT(Apples.ID) as Total_Green_Apples
FROM Shipping
JOIN Apples ON Apples.ID = Shipping.ID
WHERE Apples.Apple_Type = 'Granny Smith'
If you want the count of all Granny Smith apples in each container it'd be:
SELECT Shipping.ID, COUNT(Apples.ID) as Total_Green_Apples
FROM Shipping
JOIN Apples ON Apples.ID = Shipping.ID
WHERE Apples.Apple_Type = 'Granny Smith'
GROUP BY Shipping.ID
If you provide your table definitions I can tune the query.

Related

All records not showing when using a WHERE statement with CONCAT

I have a list of plant, which can be filtered with a CONCAT, originally it was just text, but I have converted it to ID's instead. It was showing all records and could be filtered before I converted to ID's.
This involves 4 tables. (with example data) "" are not used in the fields, they are just to show you that it is a word.
plant
idplant example 1
plantname example "001 Forklift"
idplanttype1 example 1
idlocation1 example 1
iddepartment1 example 1
planttypes
idplanttype example 1
planttype example "Forklift Truck"
locations
idlocation example 1
location example "Preston"
departments
iddepartment example 1
department example "Waste Disposal"
Without the WHERE statement, it shows all records, including nulls. (but the filter doesn't work)
But With the WHERE statement, it is only showing complete records (all of which have no Null fields and the filter works) records with nulls do not show
The issue seems to be the CONCAT. (i've cleaned up the parentheses, but had to add a 1 to make the id's different)
if(isset($_POST['search'])) {$valueToSearch = $_POST['valueToSearch'];}
$sql = "
SELECT idplant, plantname, planttype, location, department
FROM plant
LEFT JOIN planttypes ON idplanttype1 = idplanttype
LEFT JOIN locations ON idlocation1 = idlocation
LEFT JOIN departments ON iddepartment1 = iddepartment
WHERE CONCAT(plantname, planttype, location, department) LIKE
'%".$valueToSearch."%'
ORDER BY plantname";
SOLUTION
The above code works, it was just missing.
WHERE CONCAT_WS
I'm new to Joins, so any help would be greatly appreciated.
Edit: Using Linux Server - Apache Version 2.4.46
Thanks in advance!
Your problem is probably blanks.
WHERE CONCAT(plantname, planttype, location, department)
LIKE '%001 Forklift Forklift Truck Preston Waste Disposal%'
won't find anything for example, as the concated strings result in '001 ForkliftForklift TruckPrestonWaste Disposal', not '001 Forklift Forklift Truck Preston Waste Disposal'.
You want blanks between the substrings, which is easiest to achieve with CONCAT_WS:
SELECT p.idplant, p.plantname, pt.planttype, l.location, d.department
FROM plant p
INNER JOIN planttypes pt ON pt.idplanttype = p.idplanttype1
INNER JOIN locations l ON l.idlocation = p.idlocation1
INNER JOIN departments d ON d.iddepartment = p.iddepartment1
WHERE CONCAT_WS(' ', p.plantname, pt.planttype, l.location, d.department)
LIKE '%001 Forklift Forklift Truck Preston Waste Disposal%'

Retrieve data in a single row using group keyword in sql

Let's say I have a table tbl_marks with columns and data as below.
name id section1 section2 section3 section4 year
cherry 1 100 101 102 103 2016
cherry 1 200 201 202 203 2015
cherry 1 300 301 302 303 2014
Expected Output Format :
cherry 1 100101102103 200201202203 300301302303
I would like to have scores of all sections of one year to be concatenated and then followed by scores of another year separated by space.
So I need 5 columns in single row. (name, id, scores of year1, scores of year2, scores of year3)
Please let me know how should i update the query below. Thank you.
Query : select name, id, ??? from tbl_marks group by id;
Use GROUP_CONCAT:
SELECT name,
id,
GROUP_CONCAT(CONCAT(section1, section2, section3, section4)
ORDER BY section1 SEPARATOR ' ')
FROM yourTable
GROUP BY name, id
This answer assumes that you want three columns in your result set, the three columns being the name, id, and a CSV list of marks for that name/id group (123,456,789 in this case).
SQLFiddle (courtesy of #Luke)
The process you are looking for is called 'pivoting' and is not dynamically possible in basic SQL as it is a set-based language.
Just retrieve the rows in your business logic code and turn it into columns there.
All you need to use is group_concat() and concat() functions of MySQL, e.g.
select name, group_concat(concat(value1, value2))
from table
group by name
Here is the sample SQL Fiddle.
One way to solve this is self joins.
SELECT a.id,a.name,CONCAT(a.section1, a.section2, a.section3, a.section4) as val2015
,CONCAT(b.section1, b.section2, b.section3, b.section4) as val2016
,CONCAT(c.section1, c.section2, c.section3, c.section4) as val2017
FROM test a
LEFT JOIN test b
ON b.id=a.id
LEFT JOIN test c
ON c.id=a.id
WHERE a.year=2015 AND b.year=2016 AND c.year=2017
SQL Fiddle
Edit: Changed the query according to comments.

exact search and similar search combining two tables with multiple keywords mysql

Say I've two tables products and brands having below data.
tbl_products
ID Name BrandID Price
1 Keyboard 1 100
2 Keyboard 2 120
3 Keyboard wireless 1 130
4 Keyboard wireless 2 150
tbl_brands
ID Name
1 Microsoft
2 Dell
3 HP
What I want is when I type 'Microsoft Keyboard' or 'Keyboard Microsoft' then it should list me product ID 1 and 3 not 2 or 4 even 2 or 4 has keyboard. I may search for more keywords but it should give me only the items matching itself.
SELECT p.*, b.Name BrandName FROM tbl_products p INNER JOIN tbl_brands b ON b.ID = p.BrandID WHERE p.Name LIKE '%Microsoft%' OR b.Name LIKE '%Microsoft%' OR p.Name LIKE '%Keyboard%' OR b.Name LIKE '%Keyboard%'
Please help me to write proper MySQL query or any schema change with query..
Appreciate the question. Though I don't have exact answer but surely can discuss one approach to solve the problem.
STEP 1 Get BRAND NAME+NAME as single string.
STEP 2 Tokenise the entered STRING. Example Microsoft Keyboard = Mincrosoft,Keyboard Following Link for spliting the entered data. How do I split a string so I can access item x?
STEP 3 DO a like query on the string obtained in step 1.

MySQL summing totals by state by year

I have been playing around with this for what seems like hours and I can't get the results I want. Here is the query I am having trouble with:
SELECT year.year, dstate,
(SELECT sum(amount) FROM gift
WHERE year.year = gift.year
AND gift.donorno = donor.donorno)
FROM donor, gift, year
WHERE year.year = gift.year
AND gift.donorno = donor.donorno;
This seems redundant. Anyway, I am trying display the total donations (gift.amount) for each state by year.
ex.
1999 GA 500 (donorno 1 from GA donated 200 and donorno 2 from GA donated 300)
1999 FL 400
2000 GA 600
2000 FL 500
...
To clarify donors can be from the same state but I am trying to total the gift amounts for that state for the year it is donated.
Any advice is appreciated. I feel like the answer is right in front of me.
Here is a picture of tables for reference:
This is a very simple join & aggregation problem.
SELECT y.year, d.state, SUM(g.amount) AS total
FROM gift AS g
INNER JOIN year AS y ON y.year=g.year
INNER JOIN donor AS d ON d.donorno=g.donorno
GROUP BY y.year, d.state
You don't need the sub-query in your SELECT clause in order to get the total amount. You can sum it by grouping. (I think the GROUP BY clause is what you're missing. I recommend reading up on it.) What you've done is called a correlated sub-query and it is going to be very slow over large data sets because it has to be calculated row-by-row instead of as a set operation.
Also, please don't use the old style comma join syntax. Instead use the explicit join syntax as shown above. It is much clearer and will help avoid accidental Cartesian products.

SQL GROUP BY - Multiple results in one column?

I am trying to perform a SELECT query using a GROUP BY clause, however I also need to access data from multiple rows and somehow concatenate it into a single column.
Here's what I have so far:
SELECT
COUNT(v.id) AS quantity,
vt.name AS name,
vt.cost AS cost,
vt.postage_cost AS postage_cost
FROM vouchers v
INNER JOIN voucher_types vt
ON v.type_id = vt.id
WHERE
v.order_id = 1 AND
v.sold = 1
GROUP BY vt.id
Which gives me the first four columns I need in the following format.
quantity | name | cost | postage_cost
2 X 5 1
2 Y 6 1
However, I would also like a fifth column to be displayed, showing all of the codes associated with each line of the order like this:
code
ABCD, EFGH
IJKL, MNOP
Where the comma separated values are pulled from the voucher table.
Is this possible?
Any advice would be appreciated.
Thanks
This is what GROUP_CONCAT does.
Assuming the column is called code you would just add ,GROUP_CONCAT(v.code) As Codes to your select list.