我想要一个包含许多表的表。
在我放Count(taskid)
之前,它运作良好。
private DataTable GetData() //get any data
{
string connString = @"Data Source=aa.net;Initial Catalog=db_c;Persist Security Info=True;User ID=db_c_ExternalWriter;Password=aa";
string query = "Select a.Name, b.Restaurant, a.City, c.[Postal Code], a.[Open Date], a.POSSystem FROM Stores a, tblConcepts b, tblStates c WHERE a.ConceptID = b.Restaurantid AND a.State = c.Stateid AND a.[Open Date] >= cast(cast(getdate() as date) as datetime";
// string query2 = "SELECT Count(Taskid) FROM tblAssignedTasks WHERE Complete = 'True' GROUP BY StoreID";
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
comm.Connection = con;
sda.SelectCommand = comm;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
但是当我添加Count(taskid)
时,它不起作用。
SELECT Count(Taskid) FROM tblAssignedTasks WHERE Complete = 'True' GROUP BY StoreID;
我想添加这些查询,任何想法?
private DataTable GetData() //get any data
{
string connString = @"Data Source=aa.net;Initial Catalog=db_c;Persist Security Info=True;User ID=db_c_ExternalWriter;Password=aa";
string query = "Select a.Name, b.Restaurant, a.City, c.[Postal Code], a.[Open Date], a.POSSystem, d.Count(Taskid) FROM Stores a, tblConcepts b, tblStates c, tblAssignedTasks d WHERE a.ConceptID = b.Restaurantid AND a.State = c.Stateid AND a.[Open Date] >= cast(cast(getdate() as date) as datetime AND b.Restaurantid = d.StoreID AND d.Complete = 'True' GROUP BY d.StoreID)";
// string query2 = "SELECT Count(Taskid) FROM tblAssignedTasks WHERE Complete = 'True' GROUP BY StoreID";
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
comm.Connection = con;
sda.SelectCommand = comm;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
}
答案 0 :(得分:1)
您需要修复查询。
当您使用聚合函数(Count()
)时,必须在GROUP BY
子句中包含所有字段。
另外,将该行d.Count(Taskid) as 'taskKid'
的错误语法(并给出别名)更改为Count(d.Taskid) as 'taskKid'
另一个修复:a.[Open Date] >= cast(cast(getdate() as date) as datetime
错误更改为 a.[Open Date] >= getdate()
这应该有效:
SELECT a.Name,
b.Restaurant,
a.City,
c.[Postal Code],
a.[Open Date],
a.POSSystem,
Count(d.Taskid) as 'taskKid'
FROM Stores a,
tblConcepts b,
tblStates c,
tblAssignedTasks d
WHERE a.ConceptID = b.Restaurantid
AND a.State = c.Stateid
AND a.[Open Date] >= getdate()
AND b.Restaurantid = d.StoreID
AND d.Complete = 'True'
GROUP BY a.Name,
b.Restaurant,
a.City,
c.[Postal Code],
a.[Open Date],
a.POSSystem
答案 1 :(得分:0)
用以下查询替换你的sql Query,希望它工作正常。
javax.xml.bind.DatatypeConverter