Query keeps showing all records instead of a criteria - sql

I have a very simple Select query in access. I have created a report from the query, where there are two date fields on the form for the user to fill and then a button to preview that report. But it keeps showing all of the records instead of showing only the ones within the range. What might be wrong with the query?
SELECT Marketics.Agent, Marketics.[Policy Number], Marketics.[Policy Status], Marketics.Decision, Marketics.[Old Premium], Marketics.[New Premium], Marketics.Difference, Marketics.Case
FROM Marketics
WHERE [Marketics].[Date Completed]>=[forms]![Policies Worked]![from]<=[forms]![Policies Worked]![to];

Consider:
SELECT * FROM Marketics
WHERE [Date Completed] >= [forms]![Policies Worked]![from] AND [Date Completed] <= [forms]![Policies Worked]![to]
Or
SELECT * FROM Marketics
WHERE [Date Completed] BETWEEN [forms]![Policies Worked]![from] AND [forms]![Policies Worked]![to]

Related

Order by Clause conflicts with distinct in Excel VBA?

I can't figure out how to get past this error. I've seen how to do it when selecting one field, but not more than one. Can any SQL guru's out there give advice?
SELECT [Tool Name], MAX([Tool Start Time]) AS [Last Run]
FROM [Sheet1$]
GROUP BY [Tool Name]
ORDER BY [Tool Start Time] DESC
The first three lines work, but when trying to order the dates, I get the error.

Using IIF Statement with DateDiff, True Is Not Working

(SELECT IIF([Date Retired/Left] = NULL, DateDiff("yyyy", [Date Employed],Now()),
DateDiff("yyyy",[Date Employed],[Date Retired/Left]))
FROM [Employment History]
WHERE [Employment History].EmployeeID = [Training History].EmployeeID)
AS [Time Employed]
This is a subquery that returns the correct value if [Date Retired/Left] holds a date however when it does not hold a date i.e. has not left or retired I want to collect the length of employment to now. I've attempted to use the Now () command but cannot get it to work, receiving blanks in these fields.
Apologies if it is a simple error, I am a fresh novice trying SQL for the first time this week.
Dates
It's been forever since the last time I've worked with ms-access, but I think the problem is this part: [Date Retired/Left] = NULL.
In every database I know, you can't compare values to NULL - the result will always be either false or unknown. You need to change that to [Date Retired/Left] IS NULL.
So your sub query should be this:
(SELECT IIF([Date Retired/Left] IS NULL,
DateDiff("yyyy", [Date Employed], Now()),
DateDiff("yyyy", [Date Employed], [Date Retired/Left])
)
FROM [Employment History]
WHERE [Employment History].EmployeeID = [Training History].EmployeeID)
AS [Time Employed]

How to group totals

I am trying to group by category - But the SQL I am using is grouping them all by date.
Example:
If a user selects a date range of 01/04/17 - 20/04/17 it will show them total number found under category A
Whats actually happening:
The results are showing Category A 10 times and showing me the total number of each date rather than a complete grouped total
Hope this makes sense
SELECT tbl_ComplaintsCoded.CauseManager, Count(tbl_ComplaintsCoded.CauseManager) AS CountOfCauseManager, tbl_ComplaintsCoded.[Account Number], tbl_ComplaintsCoded.TouchCSM, tbl_ComplaintsCoded.[Mail Date]
FROM tbl_ComplaintsCoded
GROUP BY tbl_ComplaintsCoded.CauseManager, tbl_ComplaintsCoded.[Account Number], tbl_ComplaintsCoded.TouchCSM, tbl_ComplaintsCoded.[Mail Date]
HAVING (((tbl_ComplaintsCoded.TouchCSM)=[Forms]![frm_Central_Reporting]![Combo209]) AND ((tbl_ComplaintsCoded.[Mail Date]) Between [Forms]![frm_Central_Reporting]![Text204] And [Forms]![frm_Central_Reporting]![Text206]));
this should get what you want:
SELECT tbl_ComplaintsCoded.CauseManager, Count(tbl_ComplaintsCoded.CauseManager) AS CountOfCauseManager, tbl_ComplaintsCoded.[Account Number], tbl_ComplaintsCoded.TouchCSM, tbl_ComplaintsCoded.[Mail Date]
FROM tbl_ComplaintsCoded
WHERE tbl_ComplaintsCoded.TouchCSM = [Forms]![frm_Central_Reporting]![Combo209]
AND (tbl_ComplaintsCoded.[Mail Date] BETWEEN [Forms]![frm_Central_Reporting]![Text204] AND [Forms]![frm_Central_Reporting]![Text206])
GROUP BY tbl_ComplaintsCoded.CauseManager
HAVING CountOfCauseManager > 0;
Also, please read: WHERE vs HAVING

Date Range in an Access Crosstab

Trying to get a Crosstab to bring up a prompt when a query is opened to allow a date range (start and end) to be input (dd-mm-yyyy) so that only this data comes back when the query is ran.
Currently sitting on the following code;
TRANSFORM Count(AlphaData.[Invoice]) AS CountOfInvoice
SELECT AlphaData.[Reason], Count(AlphaData.[Invoice]) AS [Total Of Invoice]
FROM AlphaData
WHERE ((AlphaData.[DateRaised]) Between AlphaData.[DateRaised] And AlphaData.[DateRaised])
GROUP BY AlphaData.[Reason]
PIVOT Format([DateRaised],"Short Date");
But cannot for the life of me get around the "MS Access DB engine does not recognise 'Alphadata.[DateRaised:]' as a valid field name or expression" issue.
The "WHERE" portion of the query does work in other queries, but it just goes to pot when it's applied in a crosstab.
Any suggestions?
It seems a bit mixed up. How about:
PARAMETERS
[From Date:] DateTime,
[To Date:] DateTime;
TRANSFORM
Count(*) AS CountOfInvoice
SELECT
AlphaData.[Reason],
Sum(AlphaData.[Invoice]) AS [Total Of Invoice]
FROM
AlphaData
WHERE
AlphaData.[DateRaised] Between [From Date:] And [To Date:]
GROUP BY
AlphaData.[Reason]
PIVOT
Format([DateRaised],"Short Date");
You need to add the parameters to the query:
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
TRANSFORM Count(AlphaData.Invoice) AS CountOfInvoice
SELECT AlphaData.Reason, Count(AlphaData.Invoice) AS [Total Of Invoice]
FROM AlphaData
WHERE (((AlphaData.DateRaised) Between [Start Date] And [End Date]))
GROUP BY AlphaData.Reason
PIVOT Format([DateRaised],"Short Date");
(note: parameter added as the first line, and then used in the Between statement).
If you're using the graphical interface you need to look for the Parameter option:
and enter your parameters in the dialog box:
I didn't realise this would happen with a cross-tab as you can just type the parameter in for a select query:
SELECT Invoice, Reason, DateRaised
FROM AlphaData
WHERE DateRaised=[Start Date]

Query to find late parts shipped and list them by quarter

I am having the toughest time getting a query created for this scenario:
I need to list all parts that were late and sum them by quarter.
Here is the query that I have so far:
SELECT DISTINCTROW
Format$([InvoiceSub].[Date_Shipped],'\Qq yyyy') AS [Date_Shipped By Quarter]
, Sum(InvoiceSub.Quantity) AS [Sum Of Quantity]
FROM
InvoiceSub
INNER JOIN
Job_Book_Sub
ON (InvoiceSub.[Job #] = Job_Book_Sub.[Job #])
AND (InvoiceSub.[LineItem#] = Job_Book_Sub.[LineItem#])
GROUP BY
Format$([InvoiceSub].[Date_Shipped],'\Qq yyyy')
, Year([InvoiceSub].[Date_Shipped]) * 4 + DatePart('q', [InvoiceSub].[Date_Shipped]) -1;
This query works fine to display all parts that were shipped by quarter. However, I want to see all parts that were shipped LATE by quarter.
I have a field in a table named: Job_Book_Sub, and that field is [LineItem_DueDate]. I want to use that in the query so that it will display all parts that were late ([date_shipped] > [lineitem_duedate]). The [date_shipped] is the actual date the parts were shipped, [lineitem_duedate] is the date the parts were due. I know that I need to incorporate these two fields into the query, I just don't know how.
Can someone please show me how to do this? If I add the [LineItem_DueDate] to the query, then it no longer sums the dates by quarter but instead lists every record (so instead of having 45 records with different quarters over the past 10+ years, I all of a sudden have 13000+ records because it is listing each record with date_shipped in the query.
Can someone help please?
Try adding your filter in a WHERE clause:
SELECT DISTINCTROW
Format$([InvoiceSub].[Date_Shipped],'\Qq yyyy') AS [Date_Shipped By Quarter]
, Sum(InvoiceSub.Quantity) AS [Sum Of Quantity]
FROM
InvoiceSub
INNER JOIN
Job_Book_Sub
ON (InvoiceSub.[Job #] = Job_Book_Sub.[Job #])
AND (InvoiceSub.[LineItem#] = Job_Book_Sub.[LineItem#])
WHERE
Job_Book_Sub.[date_shipped] > Job_Book_Sub.[lineitem_duedate]
GROUP BY
Format$([InvoiceSub].[Date_Shipped],'\Qq yyyy')
, Year([InvoiceSub].[Date_Shipped]) * 4 + DatePart('q', [InvoiceSub].[Date_Shipped]) -1;