Crosstab Query from a Query with a Subquery - ms-access

Can anyone help me with a problem I am having with a CrossTab Query to compare current prices from our suppliers?
The select Query that it works from has a sub query that selects on only the most resent prices for our price comparison and this works perfectly for the data we need, see below:
qryPriceComp:
SELECT tblPriceComp.SupplyerID, tblPriceComp.ProductID,
tblPriceComp.Effdt, tblPriceComp.CostPrice,
tblProduct.Product, tblSupplier.Supplier
FROM tblSupplier INNER JOIN
(tblProduct INNER JOIN tblPriceComp ON tblProduct.ProductID = tblPriceComp.ProductID)
ON tblSupplier.SupplierID = tblPriceComp.SupplyerID
WHERE (((tblPriceComp.Effdt) In
(SELECT MAX(B.EffDt) AS MaxOfDt FROM tblPriceComp AS B
WHERE tblPriceComp.ProductID=B.ProductID
AND tblPriceComp.SupplyerID=B.SupplyerID
AND B.EffDt <= Date()+1)));
This is then used for the crosstab query
qryPriceComp_Crosstab:
TRANSFORM Sum(qryPriceComp.CostPrice) AS SumOfCostPrice
SELECT qryPriceComp.Product
FROM qryPriceComp
GROUP BY qryPriceComp.Product
ORDER BY qryPriceComp.Product, qryPriceComp.Supplier
PIVOT qryPriceComp.Supplier;
But when run it gives an error that both tblPriceComp.ProductID and tblSupplier.SupplierID are invalid. I have tried adding them as perimeters but when run this gives a box to enter the ID numbers which is no good as we want to see all productIDs and SupplyerIDs. If anyone can help it would be greatly appreciated!

Not a real solution, but a usable workaround:
Change qryPriceComp to a INSERT INTO tempTable query, and then base the crosstab query on tempTable.
Before each INSERT run, a DELETE * FROM tempTable must be executed.

Related

How to Add User Input Criterion to an Access Inner Joined Crosstab Query

Prompting for user input parameters in an Access query is fairly straight forward. Go to design view and under criteria, just put [Your Text:] -- and when the query runs the user is prompted for the field criteria.
Unfortunately when I try this with my Inner Joined Crosstab query I get the error:
The Microsoft Access engine does not recognize the '[Your Text:]' as a
valid field name or expression.
I know the inner join crosstab still allows for criteria, as hardcorded criterion works.
To give you a better idea of what I'm looking at, here is the SQL code.
DOESN'T WORK:
SELECT *
FROM ([CrossTabQ1]
INNER JOIN [CrossTabQ2] ON [CrossTabQ1].[Month] = [CrossTabQ2].[Month])
INNER JOIN [Query3] ON [CrossTabQ1].[Month] = [Query3].[Month]
WHERE ((([CrossTabQ1].[Month])= [Enter Month, in YYYY-MM Format:]))
ORDER BY [CrossTabQ1].[Month];
DOES WORK:
WHERE ((([CrossTabQ1].[Month])="2015-12"))
ORDER BY [CrossTabQ1].[Month];
Any tips regarding why I'm getting this error and how I can accept user input criterion would be greatly appreciated!
Try:
PARAMETERS Enter_Month Text ( 255 );
SELECT *
FROM ([CrossTabQ1]
INNER JOIN [CrossTabQ2] ON [CrossTabQ1].[Month] = [CrossTabQ2].[Month])
INNER JOIN [Query3] ON [CrossTabQ1].[Month] = [Query3].[Month]
WHERE ((([CrossTabQ1].[Month])=[Enter_Month]))
ORDER BY [CrossTabQ1].[Month];

MySQL GROUP BY HAVING returns nothing

I have many categories with the same name and parent in my Opencart database (duplicates). Need to find all of them. That's my query:
SELECT *
FROM
(SELECT `oc_category`.category_id,
`oc_category`.parent_id,
`oc_category_description`.name
FROM `oc_category`, `oc_category_description`
WHERE `oc_category`.category_id = `oc_category_description`.category_id
) cats
GROUP BY `cats`.parent_id, `cats`.name
HAVING COUNT(*) > 1
But this query returns nothing. Please tell me if I'm wrong.
No problem with the query, it does work, check this out:
http://sqlfiddle.com/#!9/3d170/4
Please fiddle with that and populate it with the data which produces no records, and add it to your question.

Query to sum some of the values

Ive got a simple query that is used on a search. My problem is with this query is that as the records in mysql are added everytime there is a transaction, the query returns a list of data when there could only be one or a few more rows instead of a lot more.
SQLFliddle
As you can see here - the query returns a lot of rows, where I want it to return
BLSH103 A001A 31 24/01/2014
Can the qty where the product name & pallet space are the same be summed? And then show the largest date?
just use a sum function on t.Quantity (and a group by clause)
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, sum(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
group by
Pname,
pSpace,
Transac -- if you want to group by date also...
By the way, using LIKE this way (without %) doesn't make much sense...
see SqlFiddle
You just need to use GROUP BY and SUM in this way:
SELECT (t.ProductName) as Pname ,(s.PalletSpace) as PSpace, SUM(t.Quantity) as Qty,(t.TransactionDate) as Transac
FROM PalletSpaces s
JOIN ProductTrans t
ON s.PalletSpaceID = t.PalletSpace
WHERE t.ProductName LIKE 'BLSH103' OR s.PalletSpace LIKE 'BLSH103'
GROUP BY t.ProductName, s.PalletSpace;

my sub query is taking too much time

I have simple query but it is taking too much time for execution.
query:
SELECT a.primarykey,
a.SID,
a.VID,
a.topic,
a.dstart,
a.dstop,
a.vresult,
a.dstart1,
a.dstart2,
( SELECT MIN(d.vresult)
FROM _temp._pb_1_1_4_1 d
WHERE d.dstart1 > a.dstart1
) as _DOP0
FROM _temp._pb_1_1_4_1 a
column dstart1 is indexed.
dstart1 is Date type column.
Please help me to optimized above query.
if I remove d.dstart1 > a.dstart1 from the WHERE clause then query run very fast.
whenI explain the query it gives "Range checked for each record (index map: 0x1)"
I want to achieve minimum value of vresult for each row.
For each row , First filter all the records which has greater value of dstart1 for the same table and then find min(vresult) for that row.
Without knowing anything about what you're trying to do... this query should return the same results and has been removed the unnecessary calculations:
select a.primarykey, a.SID, a.VID, a.topic, a.dstart, a.dstop,
a.vresult, a.dstart1, a.dstart2, (
select MIN(d.vresult)
from _temp._pb_1_1_4_1 d
where d.dstart1 > a.dstart1) _DOP0
from _temp._pb_1_1_4_1 a
Anyway, it would help to understand what you're trying to do with that query.

MS access - count function

I am trying to count number of rows in my complex query (this query is designed to meet my requirement). The result should be, e.g. NumberOfRecord: 10. However, I am not getting the result I am expecting. How do I improve my query? Thanks
My query:
>
Result:
http://img815.imageshack.us/img815/6414/222222o.png
The MS Access SQL view:
SELECT Count(tblMeeting.Meeting_Id) AS [Number of jobs]
FROM (tblEmployee INNER JOIN tblMeetingType ON tblEmployee.Emp_Id = tblMeetingType.Chairperson_Id) INNER JOIN tblMeeting ON tblMeetingType.Type_Id = tblMeeting.Type_Id
GROUP BY tblMeeting.MeetingDate, tblMeeting.AgendaApproved, tblMeeting.AgendaTopic
HAVING (((tblMeeting.MeetingDate)>DateAdd("d",3,Now())) AND ((tblMeeting.AgendaApproved)=False) AND ((tblMeeting.AgendaTopic) Is Not Null));
Thanks guys, I finally solved my problem.
I have changed GROUP BY to WHERE.
The query should look like this:
http://img715.imageshack.us/img715/628/solutionl.png