Access 2007 Error 3071 -report using parameters - sql

I have a query in MS Access 2007 that pulls data from two different tables and displays in a report. This occurs after a user clicks a button on the main form, which opens a date field parameter form where the user can select two dates. From there the query runs using the two dates the user provided, or the dates are 'faked' if none are selected to fill in the dates. I'm getting a Run-time error 3071, this expression is typed incorrectly, or it is too complex to be evaluated and i'm not sure why.
I run a similar query in another database and it executes perfectly so i'm at a loss.
Query is below,
(SELECT
[Group Name],
tbGroups AS [Group Number],
Analyst,
[Account Manager],
NULL AS [SER Number],
[Received Date] AS [Corporate Recevied],
DateValue(Created) AS [Sales Submitted],
tbBAAcceptedDate AS [BA Accepted],
NULL AS [Submitted to MDSS],
NULL AS [Completed],
NULL AS [Cancelled],
DateDiff("d",[Received Date], IIf([Forms]![frmReportDateFilter].[tbToDate] = '01/01/2116', Date(), CDate([Forms]![frmReportDateFilter].[tbToDate]))) AS [Aging Days Count],
LocalID AS [ID Number]
FROM ChangeRequest
WHERE DateValue([Created]) BETWEEN CDate([Forms]![frmReportDateFilter].[tbFromDate]) AND CDate([Forms]![frmReportDateFilter].[tbToDate]))
UNION ALL (SELECT
tbGroupProgramProductName AS [Group Name],
tbGroups AS [Group Number],
cboAnalyst AS Analyst,
tbAccountManager AS [Account Manager],
tbSERNumber AS [SER Number],
tbCorpReceivedDate AS [Corporate Recevied],
tbBAReceivedDate AS [Sales Submitted],
IIf([tbBAAcceptedDate] > CDate([Forms]![frmReportDateFilter].[tbToDate]), NULL, [tbBAAcceptedDate]) AS [BA Accepted],
IIf([tbSubmittedToMDSS] > CDate([Forms]![frmReportDateFilter].[tbToDate]), NULL, [tbSubmittedToMDSS]) AS [Submitted to MDSS],
DateValue(tbCompleteDate) AS [Completed],
DateValue(tbCancelDate) AS [Cancelled],
DateDiff("d",tbCorpReceivedDate, IIf([Forms]![frmReportDateFilter].[tbToDate] = '01/01/2116', Date(), CDate([Forms]![frmReportDateFilter].[tbToDate]))) AS [Aging Days Count],
LocalID AS [ID Number]
FROM tblDD
WHERE DateValue([tbBAReceivedDate]) BETWEEN CDate([Forms]![frmReportDateFilter].[tbFromDate]) AND CDate([Forms]![frmReportDateFilter].[tbToDate]))
ORDER BY [Group Name];
Any help is greatly appreciated.

Specify the control as Date. Something like this with no CDate - and no outer parenthesis:
PARAMETERS
[Forms]![frmReportDateFilter].[tbFromDate] Date,
[Forms]![frmReportDateFilter].[tbToDate] Date;
SELECT
[Group Name],
tbGroups AS [Group Number],
Analyst,
[Account Manager],
NULL AS [SER Number],
[Received Date] AS [Corporate Recevied],
DateValue(Created) AS [Sales Submitted],
tbBAAcceptedDate AS [BA Accepted],
NULL AS [Submitted to MDSS],
NULL AS [Completed],
NULL AS [Cancelled],
DateDiff("d",[Received Date], IIf([Forms]![frmReportDateFilter].[tbToDate] = #01/01/2116#, Date(), [Forms]![frmReportDateFilter].[tbToDate])) AS [Aging Days Count],
LocalID AS [ID Number]
FROM ChangeRequest
WHERE DateValue([Created]) BETWEEN [Forms]![frmReportDateFilter].[tbFromDate] AND [Forms]![frmReportDateFilter].[tbToDate]
UNION ALL
SELECT
tbGroupProgramProductName AS [Group Name],
tbGroups AS [Group Number],
cboAnalyst AS Analyst,
tbAccountManager AS [Account Manager],
tbSERNumber AS [SER Number],
tbCorpReceivedDate AS [Corporate Recevied],
tbBAReceivedDate AS [Sales Submitted],
IIf([tbBAAcceptedDate] > [Forms]![frmReportDateFilter].[tbToDate], NULL, [tbBAAcceptedDate]) AS [BA Accepted],
IIf([tbSubmittedToMDSS] > [Forms]![frmReportDateFilter].[tbToDate], NULL, [tbSubmittedToMDSS]) AS [Submitted to MDSS],
DateValue(tbCompleteDate) AS [Completed],
DateValue(tbCancelDate) AS [Cancelled],
DateDiff("d",tbCorpReceivedDate, IIf([Forms]![frmReportDateFilter].[tbToDate] = #01/01/2116#, Date(), [Forms]![frmReportDateFilter].[tbToDate])) AS [Aging Days Count],
LocalID AS [ID Number]
FROM tblDD
WHERE DateValue([tbBAReceivedDate]) BETWEEN [Forms]![frmReportDateFilter].[tbFromDate] AND [Forms]![frmReportDateFilter].[tbToDate]
ORDER BY [Group Name];

Related

How can I summarize a set of records into one line where only one record in a column has text and all others are null?

I have invoice data stored in a SQL database and I need a summary query to bring up the Invoice Number, PO number, Date, and Invoice Amount in a single line using an MS Access Query. Unfortunately, the customer PO number is only on one line of the invoice data and pops up on the query result like this.
Invoice Date
Invoice #
PO Number
Amount
8/11/22
12345
NULL
$23.00
8/11/22
12345
456
$00.00
I need the output to look like this instead:
Invoice Date
Invoice #
PO Number
Amount
8/11/22
12345
456
$23.00
My query looks like this:
SELECT
[Invoice Date],
[Invoice #],
[PO Number],
FORMAT$(Sum([Amount])),'$#,##0.00') AS [Amount]
FROM [Invoice Details]
GROUP BY
[Invoice Date],
[Invoice #],
[PO Number]
HAVING
[INVOICE DATE] BETWEEN [8/11/2022] AND [8/11/2022]
ORDER BY
[INVOICE #]
I am still a novice when it comes to SQL queries and am not sure what I am missing here. Any help is appreciated.
Then you can exclude [PO Number] column from your GROUP BY and just take the greater value in each group. I think you can use :
SELECT
[Invoice Date],
[Invoice #],
MAX(Nz([PO Number], 0)) AS [PO Number],
FORMAT$(Sum([Amount])),'$#,##0.00') AS [Amount]
FROM [Invoice Details]
GROUP BY
[Invoice Date],
[Invoice #],
HAVING
[INVOICE DATE] BETWEEN [8/11/2022] AND [8/11/2022]
ORDER BY
[INVOICE #]
Nz is replacing Null by 0 here.
You have very limited example input, but assuming TABLE1
This will work
SELECT Amounts.[Invoice Date], Amounts.[Invoice #], [PO Numbers].[PO Number], Amounts.Amount
FROM Table1 AS Amounts
INNER JOIN Table1 AS [PO Numbers]
ON (Amounts.[Invoice Date] = [PO Numbers].[Invoice Date]) AND (Amounts.[Invoice #] = [PO Numbers].[Invoice #])
WHERE ((([PO Numbers].[PO Number]) Is Not Null) AND ((Amounts.[PO Number]) Is Null));
Looks like this in the query window

Your Query does not include the expression 'Open Amount' as part of an aggerate function - MS Access

SELECT [doc type], [Open Amount]
, [customer number]
, COUNT([customer number]) As CountCustomerNumber
, SUM(IIF([Open Amount]>'0', [Open Amount], 0)) AS sum_open_amount_pos
, SUM(IIF([Open Amount]<'0', [Open Amount], 0)) As sum_open_amount_neg
FROM
(SELECT d.[customer number] & d.[membership number] AS CustMemb
, d.[customer number]
, agg.[Open Amount]
, agg.[doc type]
, SUM(agg.[Open Amount]) AS SumOpenAmount
FROM (SELECT [doc type]
, [customer number]
, SUM([Open Amount]) AS TotalSubOpenAmount
FROM data
WHERE [doc type] = 'RU'
GROUP BY [doc type]
, [customer number]
) agg
INNER JOIN [data] d
ON d.[customer number] = agg.[customer number]
GROUP BY d.[customer number] & d.[membership number]
, d.[customer number]
, agg.[doc type]
, agg.[Open Amount]
) AS sub
GROUP BY [doc type]
, [customer number]
, [Open Amount]
HAVING COUNT([customer number]) = 1
Added Open Amount to Group BY Clause - Looking for parameter value for agg.Open Amount -----------------------------------------
See the following stackoverflow post: “You tried to execute a query that does not include the specified aggregate function”. I think you just need to add [Open Amount] to the final GROUP BY
.
.
.
GROUP BY [doc type]
, [customer number]
, [Open Amount]
HAVING COUNT([customer number]) = 1
???
You have three elements in you select that are not part of any aggregation but only two elements in your group by, that is why you get the error message.
When you use aggregation function, all the non aggregated elements of the select need to be in the group by hence your query become like this :
EDIT : You are selecting [Open Amount] and aggregating SUM(agg.[Open Amount]) AS SumOpenAmount in your sub query.
You are also doing something similar in your external query
SELECT [doc type], [Open Amount]
, [customer number]
, COUNT([customer number]) As CountCustomerNumber
, SUM(IIF([Open Amount]>'0', [Open Amount], 0)) AS sum_open_amount_pos
, SUM(IIF([Open Amount]<'0', [Open Amount], 0)) As sum_open_amount_neg
FROM SubQueries
GROUP BY [doc type]
[Open Amount]
, [customer number]
HAVING COUNT([customer number]) = 1
Should be something like that :
SELECT [doc type]
, [customer number]
, COUNT([customer number]) As CountCustomerNumber
, SUM(IIF([Open Amount]>'0', [Open Amount], 0)) AS sum_open_amount_pos
, SUM(IIF([Open Amount]<'0', [Open Amount], 0)) As sum_open_amount_neg
FROM SubQueries
GROUP BY [doc type]
, [customer number]
HAVING COUNT([customer number]) = 1
When you sum over [Open Amount], you cannot also select each separate "Open Amount". So remove it from both "SELECT" and "GROUP BY":
SELECT [doc type] --, [Open Amount]
, [customer number]
--, COUNT([customer number]) As CountCustomerNumber
, SUM(IIF([Open Amount]>'0', [Open Amount], 0)) AS sum_open_amount_pos
, SUM(IIF([Open Amount]<'0', [Open Amount], 0)) As sum_open_amount_neg
FROM
-- snip
GROUP BY [doc type]
, [customer number]
-- , [Open Amount]
-- snip
And similarly, if you group by "Customer Number", a Count([Customer Number]) would return 1 (if it worked).

Failed to convert NVARCHAR to INT, but I'm not trying to

I get this error:
Msg 245, Level 16, State 1, Line 5
Conversion failed when converting the nvarchar value 'L NOVAK ENTERPRISES, INC' to data type int.
I've been wrestling with this query for quite a while and just can't figure out in what place that the conversion is being attempted. Using SQL Server 2017.
DECLARE #StartDate AS DateTime
DECLARE #MfgGroupCode AS Varchar(20)
SET #StartDate='2/27/2020'
SET #MfgGroupCode = 'VOLVO_NLA'
SELECT DISTCINT
CT.No_ AS [Contact Number],
CT.Name AS [Contact Name],
UAL.Time AS [Search Date],
UAL.Param1 AS [Search Part],
CT.[E-Mail] AS [Contact Email],
CT.[Phone No_] AS [Contact Phone],
CT.[Company Name] AS [Search By Customer],
(SELECT C.Name
FROM dbo.[Customer] C
WHERE C.No_ = SL.[Sell-to Customer No_]
AND C.Name <> '') AS [Sold To Customer],
SL.[Posting Date] AS [Invoice Date],
SL.[Document No_] AS [Invoice],
SL.Quantity AS [Quantity],
SL.[Unit Price] AS [Unit Price],
SL.Amount AS [Amount],
DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) AS [Interval]
FROM
dbo.[User Action Log] UAL
JOIN
dbo.[User Action Types] UAT ON UAL.[User Action ID] = UAT.ID
JOIN
dbo.[Item] I ON UAL.Param1 = I.[OEM Part Number]
JOIN
dbo.[Contact] CT ON UAL.[Contact No_] = CT.No_
LEFT OUTER JOIN
dbo.[Sales Invoice Line] SL ON UAL.Param1 = SL.[OEM Part Number]
AND SL.[Posting Date] >= #StartDate
WHERE
UAT.Name IN ('SinglePartSearch', 'MultiPartSearch')
AND UAL.[MFG Group Code] = #MfgGroupCode
AND UAL.Time >= #StartDate
AND UAL.Param3 > 0
-- AND DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) < 0 -- Uncomment to see Current Searches with Past Orders
-- AND DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) > -1 -- Uncomment to see Searches resulting in Future Order
AND DATEDIFF(DAY, UAL.Time, SL.[Posting Date]) IS NULL -- Uncomment to See Searches with no Order
ORDER BY
Interval DESC
Thanks to all of your help and questioning, I was able to identify that the culprit is UAL.Param3.
The User Action Log table stores a variety of different "Actions" and the parameters that are affiliated with each type of action (param1, param2, param3). For one action "RequestForAccess", the "L NOVAK" value is perfectly acceptable. For this query, we're looking at the "SinglePartSearch" and "MultiPartSearch" actions, which will only contain numeric values in UAL.Param3
I replaced this line (AND UAL.Param3 > 0) with (AND ISNUMERIC(UAL.Param3) = 1 AND UAL.Param3 <> 0) in the Where clause and it is now returning the results I hoped for.
Let me know if there is a more correct way of doing this and thank you to all who contributed!

How can I know the exact column name and value on which the conversion failed while running an SQL query?

I got below error while running a SQL query:
Msg 241, Level 16, State 1, Line 16
Conversion failed when converting date and/or time from character string.
I tried putting it Line by Line so that the error would point out the exact line and it did.
But to my surprise a date / time conversion error was given and pointed to a field which is varchar. In this example it pointed out to 'F1' as Facility field which is the first line of the query.
SELECT 'F1' AS FACILITY, NULL AS NSH, NULL AS EMC, NULL AS OD, NULL AS OA,
URN AS PA,
Title, [First Name], [Second Name], [Third Name], [Family Name], Sex, [Date Of Birth],
[Estimated Date Of Birth ], [Marital Status],
Religion, [Nationality Code],
Nationality, [Passport Number], [Country Of Birth], [Preferred Language],
Address, [PO Box], CONVERT(VARCHAR(15), [Home Phone]) AS [Home Phone], [Office Phone], [Mobile Phone], [Blood Type], VIP, Notes, [National ID], NULL AS [Deceased Date], NULL AS [Deceased Time],
NULL AS [Deceased Indicator (Y/N)], NULL AS [Location Of Death], NULL AS [Death Notified By]
FROM PD_PA
UNION ALL
SELECT 'F2' AS FACILITY, URN AS NSH, NULL AS EMC, NULL AS OD, NULL AS OA, NULL AS PA, Title, [First Name], [Second Name], [Third Name], [Family Name], Sex, BDATE AS [Date Of Birth], [Estimated Date Of Birth], [Marital Status], Religion, [NationalityCode],
Nationality, [Passport Number], [Country], [Preferred Language],
Address, [PO Box], CONVERT(VARCHAR(15), [Home Phone]) AS [Home Phone], [Office Phone], [Mobile Phone] AS [MobilePhone], [Blood Type], VIP, Notes, [National ID], NULL AS [Deceased Date], NULL AS [Deceased Time],
NULL AS [Deceased Indicator (Y/N)], NULL AS [Location Of Death], NULL AS [Death Notified By]
FROM PD_LKIL
Is there a way to trace or know the details of which is the exact field and on which value exactly is the error thrown?
Since my data has over 100k entries, its difficult to trace out and proceed.
If you are on a version greater than 2008 you can replace your CONVERT with TRY_CONVERT.
This returns null if the conversion fails so you can simply check for NULLs in the output.

Workaround for PIVOT statement

I have this query, is taking like 2 minutes to resolve, I need to find a workaround, I know that UNPIVOT has a better solution using CROSS APPLY, is there anything similar for PIVOT?
SELECT [RowId], [invoice date], [GL], [Entité], [001], [Loc], [Centre Cout], [Compte_1], [Interco_1], [Futur_1], [Department], [Division], [Compagnie], [Localisation], [Centre/Cout], [Compte], [Interco], [Futur], [Account], [Mobile], [Last Name], [First Name], [license fee], [GST], [HST], [PST], [Foreign Tax], [Sales Tax License], [Net Total], [Total], [ServiceType], [Oracle Cost Center], [CTRL], [EXPENSE], [Province]
FROM
(SELECT fd.[RowId], fc.[ColumnName], fd.[Value]
FROM dbo.FileData fd
INNER JOIN dbo.[FileColumn] fc
ON fc.[FileColumnId] = fd.[FileColumnId]
WHERE FileId = 1
AND TenantId = 1) x
PIVOT
(
MAX(Value)
FOR [ColumnName] IN ( [invoice date], [GL], [Entité], [001], [Loc], [Centre Cout], [Compte_1], [Interco_1], [Futur_1], [Department], [Division], [Compagnie], [Localisation], [Centre/Cout], [Compte], [Interco], [Futur], [Account], [Mobile], [Last Name], [First Name], [license fee], [GST], [HST], [PST], [Foreign Tax], [Sales Tax License], [Net Total], [Total], [ServiceType], [Oracle Cost Center], [CTRL], [EXPENSE], [Province])
) AS p
Pivots are great, but so are Conditional Aggregations. Also, there would be no datatype conficts or conversions necessary
SELECT [RowId]
,[invoice date] = max(case when [FileColumnId] = ??? then Value end)
,[GL] = max(case when [FileColumnId] = ??? then Value end)
,... more fields
FROM dbo.FileData fd
WHERE FileId = 1
AND TenantId = 1
Group By [RowId]
EDIT
You could add back the join to make it more readable.