MS Access query, IIF function is returning error as "No value given for one or more required parameters" - ms-access-2010

I have Access query which does Union, group, get Maximum, Left Join and IIF(isNull) functions.
I have to union three subTables and group by column "mainid", get max-value from column "time" as "MaxSubTime" and mainid-column is filterd by mainTable.id.
Then I am applying LEFT Join MainTable to Union-Group.
If MaxSubTime is Null then i am applying the MainTable.endtime to MaxSubTime and getting it as "MaxMainTableEndTime".
till here everything works fine.
string myQry = "SELECT MainTable.id, MainTable.sttime, IIF(IsNULL(a.MaxSubTime), MainTable.endtime, a.MaxSubTime) AS MaxMainTableEndTime
FROM (MainTable LEFT JOIN (SELECT mainid, MAX(SubTime) AS MaxSubTime
FROM (SELECT Sub_table_1.mainid, Sub_table_1.time AS SubTime FROM Sub_table_1
UNION SELECT Sub_table_2.mainid, Sub_table_2.time AS SubTime Sub_table_2
UNION SELECT Sub_table_3.mainid, Sub_table_3.time AS SubTime Sub_table_3 GROUP BY mainid) AS a ON a.mainid = MainTable.id)
WHERE MaxMainTableEndTime >= #" + DateTime.Now + "# ";
But when i filter the final query by "MaxMainTableEndTime", i am getting this error "No value given for one or more required parameters".
Where i am doing mystake?

Me solved this issue by using "Having" clause and editing my code a little
string myQry = "SELECT MainTable.id, MainTable.sttime, a.MaxSubTime
FROM MainTable LEFT JOIN (SELECT mainid, MAX(SubTime) AS MaxSubTime
FROM(SELECT Sub_table_1.mainid, Sub_table_1.time AS SubTime FROM Sub_table_1
UNION SELECT Sub_table_2.mainid, Sub_table_2.time AS SubTime Sub_table_2
UNION SELECT Sub_table_3.mainid, Sub_table_3.time AS SubTime Sub_table_3
GROUP BY mainid HAVING MaxSubTime >= #" + DateTime.Now + "#)
AS a ON a.mainid = MainTable.id) WHERE MainTable.endtime >= #" + DateTime.Now + "# "

Related

SQL query to update value in table to sum of attribute in another table, where the ID's match

I have tried several different queries and cannot seem to find one that works without causing an error
Incorrect syntax near keyword ' '
I have one table called Scores that saves the userID, week number, and that weeks score from a quiz.
Schema:
Scores (Id, userName, weekNumber, currentScore)
There are 12 weeks total, so in the end each user will have 12 entries in this table
I have another table Leaderboard that has schema :
Leaderboard (Id, userName, week1, week2, week3 ..... week12, totalScore)
Each user will only have one entry in this table.
I have been trying to save the sum of the currentScore from Scores into the totalScore attribute of Leaderboard and cannot seem to figure out the correct syntax.
My query is as follows:
UPDATE t1
SET t1.totalScore = t2.completeScore
FROM dbo.Leaderboard AS t1,
((SELECT Id, SUM(weeklyScore) AS completeScore FROM dbo.Scores) as F
INNER JOIN
(SELECT Id FROM dbo.Scores GROUP BY Id) AS S ON F.Id = S.Id) AS t2
WHERE t1.Id = t2.Id
Try this and let us know how it goes:
var total1 = ("UPDATE t1 " +
"SET t1.totalScore = t2.completeScore " +
"FROM dbo.Leaderboard AS t1, (SELECT F.Id, F.completeScore FROM " +
"(SELECT Id, SUM(weeklyScore) AS completeScore FROM dbo.Scores GROUP BY ID) as F " +
"INNER JOIN (SELECT Id FROM dbo.Scores/* GROUP BY Id*/) AS S "+
"ON F.Id = S.Id) AS t2 "+
"WHERE t1.Id = t2.Id");
By the way I think you were missing a GROUP BY clause in the definition of the F derived table and that you don't need the GROUP BY in the definition of the S derived table.

Conversion failed when converting character string to smalldatetime data type

SELECT B.Value
FROM table1 A WITH (NOLOCK)
INNER JOIN table2 B WITH (NOLOCK) ON A.id = B.id
WHERE
A.Name = 'COMPLETED_AT'
AND CONVERT(smalldatetime, A.Value) < GETDATE() - 30
AND B.Name = 'RESULT'
Getting error message
Conversion failed when converting character string to smalldatetime data type
when executing the above query
Example table structure
ID Name Value
1 Result R12344
1 Completed_At 2015-03-20T06:06:46
2 Result R23445
2 Completed_At 2014-03-20T06:06:46
Column value is of nvarchar(400) datatype
The query result should display the values of result name type which have been made an entry of more than 30 days.
Looking forward in hearing from you.
Avoid Null In Datetime Coloumn field & fix this error (Conversion failed when converting character string to smalldatetime data type.):
First, I give this type get this error:
select * from Tabel1 where ISNULL(Datecoloumn1,**'0'**)!='1900-01-01 00:00:00'
The single quotation Zero is the Problem
I fix like this method :
select * from Tabel1 where ISNULL(Datecoloumn1,**0**) != '1900-01-01 00:00:00'
It works just fine, just changed Table2 to Table1, because you said is a self join.
CREATE TABLE Table1
([ID] int, [Name] varchar(12), [Value] varchar(19))
;
INSERT INTO Table1
([ID], [Name], [Value])
VALUES
(1, 'Result', 'R12344'),
(1, 'Completed_At', '2015-03-20T06:06:46'),
(2, 'Result', 'R23445'),
(2, 'Completed_At', '2014-03-20T06:06:46')
;
SELECT B.Value
FROM table1 A WITH (NOLOCK)
INNER JOIN table1 B WITH (NOLOCK) ON A.id = B.id
WHERE
A.Name = 'COMPLETED_AT'
AND CONVERT(smalldatetime, A.Value) < GETDATE() - 30
AND B.Name = 'RESULT'
Result
Value
R23445

Dynamic order by in SqlDataSource by a dropdownlist and grid view

I have a grid view that connect to the DataSqlSource. I want to sort my queries dynamically by a dropdownlist. for example by date,name,family etc.
I also join some tables in my queries.
I use this code in my DataSqlSource:
SELECT AddTitle.Title, SubmitManuscript.Status, AddArticleType.Type, AddArticleType.UserName, AddArticleType.ArticleType, SubmitManuscript.date, SubmitManuscript.ArticleNum, AddArticleType.ArticleID, CONVERT (VARCHAR(10), SubmitManuscript.date, 103) AS date1, OtherWritter.ArticleID AS Expr1, OtherWritter.name, OtherWritter.family, AddArticleType.CheckFinish FROM AddArticleType INNER JOIN AddTitle ON AddArticleType.ArticleID = AddTitle.ArticleID INNER JOIN SubmitManuscript ON AddArticleType.ArticleID = SubmitManuscript.ArticleID INNER JOIN OtherWritter ON AddTitle.ArticleID = OtherWritter.ArticleID WHERE (AddArticleType.ArticleID IN (SELECT ArticleID FROM AddUpload_4 AS AddUpload_4_1 WHERE (AddArticleType.CheckFinish = '0'))) AND (AddArticleType.Type = #Type) AND (SubmitManuscript.Status = 'Accept') AND
(OtherWritter.MainAuthor = 'Yes') ORDER BY '[' + #SortOrder + ']' DESC
but it doesn't work for me and no sorting happen!
And I also try this code, this time it gave me an error:
here is the code:
SELECT ...
FROM ...
ORDER BY
CASE WHEN #order=Country THEN Country END DESC,
CASE WHEN #order= City THEN City END ASC,
CASE WHEN #order= name THEN name END ASC
Can any body help me?
I guess it should be:
SELECT ...
FROM ...
ORDER BY
CASE WHEN #order='Country' THEN Country END DESC,
CASE WHEN #order='City' THEN City END ASC,
CASE WHEN #order='name' THEN name END ASC

Inner Join & Count, Microsoft SQL

I am trying to do a count inside a nested statement with inner join
select a.app_id, a.first_name, a.last_name, d.svd_id
from wwhs_app a inner join
wwhs_svc d on a.app_id = d.app_id
where a.app_id in(
select top 50 app_id
from wwhs_app
Where app_create_dt > '2012-07-23 00:00:00')
I need a count of svd_id as well, but I keep getting errors every way I try. Suggestions?
You need to count for svd_id but it's not in the query.
Did you mean 'app_id'?
Try this...
SELECT a.app_id, a.first_name, a.last_name, d.svd_id
FROM wwhs_app a
INNER JOIN wwhs_svc d on a.app_id = d.app_id
WHERE a.app_id in (
SELECT TOP 50 app_id, COUNT(*) as id_count
FROM wwhs_app
WHERE app_create_dt > '2012-07-23 00:00:00'
GROUP BY app_id ORDER BY id_count)

Run time Exception with SQL Statement in VB (ASP.NET)

I have this SQL Statement:
de.SqlStatement = "SELECT A.Name, A.Catagory , COUNT(Patient_ID) AS PNO FROM PatientApplyToAssociation P INNER JOIN Association A on A.Association_ID = P.Association_ID WHERE A.Catagory='" & "health" & " '" & "' GROUP BY A.Name '"
with this Exception:
System.Data.SqlClient.SqlException was unhandled by user code
Class=16
ErrorCode=-2146232060
LineNumber=1
Message=Column
'Association.Name' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
I had this exception when I added the where clause, It seems that the compiler cant see the Group By Section , because of the quotation ending before it, I tried to write it like this:
de.SqlStatement = "SELECT A.Name, A.Catagory , COUNT(Patient_ID) AS PNO FROM PatientApplyToAssociation P INNER JOIN Association A on A.Association_ID = P.Association_ID WHERE A.Catagory='" & "health" & "' GROUP BY A.Name '" & " '"
But it was an Incorrect syntax near ' '.
Any ideas?
I'm pretty sure you don't want the ' around the GROUP BY clause:
SELECT A.Name, A.Catagory
, COUNT(Patient_ID) AS PNO
FROM PatientApplyToAssociation P
INNER JOIN Association A
ON A.Association_ID = P.Association_ID
WHERE A.Catagory='" & "health" & " '" & " GROUP BY A.Name
In order to use the statement in your original question, you will need to revise it to be:
de.SqlStatement = "SELECT A.Name, A.Catagory, COUNT(1) AS PNO FROM PatientApplyToAssociation P INNER JOIN Association A on A.Association_ID = P.Association_ID WHERE A.Catagory='health' GROUP BY A.Name, A.Catagory"
There are 3 changes in this statement:
1) The extraneous string joins in the where and group by have been removed.
2) Group by A.Catagory has been added so that you will not receive another error similar to error in the question for this field.
3) COUNT(Patient_ID) has been changed to COUNT(1) as a possibly better practice. If the field you are counting contains nulls, it will not be included in the count. If this is the desired behavior, then your previous code was correct; otherwise you should use the revision.

Resources