I want to select data using EntityDataSource control on ASP.NET page. Parameters given to data source are set by this code:
dsPlastics.Select = "top(10) it.PlasticId, it.Name, it.DateModified, it.Producer.Name as ProducerName, it.PlasticType.PlasticFamily.Name as PlasticFamilyName, it.PlasticType.Name as PlasticTypeName";
dsPlastics.Where = "it.Active == true";
dsPlastics.OrderBy = "it.DateModified DESC";
Im am expecting query similar to this (selects data as I need - last 10 records joined with related data):
select top(10) PlasticId, pl.Name, DateModified, pr.Name as ProducerName, pf.Name as PlasticFamilyName, pt.Name as PlasticTypeName
from Plastics pl
left join Producers pr ON pl.ProducerId = pr.ProducerId
left join PlasticTypes pt ON pl.PlasticTypeId = pt.PlasticTypeId
left join PlasticFamilies pf ON pt.PlasticFamilyId = pf.PlasticFamilyId
where pl.Active = 1
order by pl.DateModified DESC
But entity framework generates this (does not select data as I need):
SELECT
[Limit1].[PlasticId] AS [PlasticId],
[Limit1].[Name] AS [Name],
[Limit1].[DateModified] AS [DateModified],
[Extent2].[Name] AS [Name1],
[Extent4].[Name] AS [Name2],
[Extent5].[Name] AS [Name3]
FROM (SELECT TOP (10) [Extent1].[PlasticId] AS [PlasticId], [Extent1].[ProducerId] AS [ProducerId], [Extent1].[PlasticTypeId] AS [PlasticTypeId], [Extent1].[Name] AS [Name], [Extent1].[DateModified] AS [DateModified]
FROM [dbo].[Plastics] AS [Extent1]
WHERE [Extent1].[Active] = 1 ) AS [Limit1]
LEFT OUTER JOIN [dbo].[Producers] AS [Extent2] ON [Limit1].[ProducerId] = [Extent2].[ProducerId]
LEFT OUTER JOIN [dbo].[PlasticTypes] AS [Extent3] ON [Limit1].[PlasticTypeId] = [Extent3].[PlasticTypeId]
LEFT OUTER JOIN [dbo].[PlasticFamilies] AS [Extent4] ON [Extent3].[PlasticFamilyId] = [Extent4].[PlasticFamilyId]
LEFT OUTER JOIN [dbo].[PlasticTypes] AS [Extent5] ON [Limit1].[PlasticTypeId] = [Extent5].[PlasticTypeId]
ORDER BY [Limit1].[DateModified] DESC
Please, how should I build the query, to get query which I need?
You can try like this.
<asp:EntityDataSource ID="ProductDataSource" runat="server"
CommandText="select top(10) PlasticId, pl.Name, DateModified, pr.Name as ProducerName, pf.Name as PlasticFamilyName, pt.Name as PlasticTypeName
from Plastics pl
left join Producers pr ON pl.ProducerId = pr.ProducerId
left join PlasticTypes pt ON pl.PlasticTypeId = pt.PlasticTypeId
left join PlasticFamilies pf ON pt.PlasticFamilyId = pf.PlasticFamilyId
where pl.Active = 1
order by pl.DateModified DESC"
ConnectionString="name=AdventureWorksEntities"
DefaultContainerName="AdventureWorksEntities" >
</asp:EntityDataSource>
Or try setting your query in the Command Text property
Related
I'm trying to get a list of ALL user roles for each user through the nopcommerce database. i just need the Customer.Id, Customer.Username, Customer.Email and Customer.Role (some have multiple roles)
This is all jacked up.
SELECT Customer.Id, Customer.Username, Customer.Email, CustomerRole.Name
FROM ((Customer
INNER JOIN CustomerRole ON CustomerRole.Id = Customer.Id)
INNER JOIN Customer_CustomerRole_Mapping ON Customer_CustomerRole_Mapping.CustomerRole_Id = CustomerRole.Id)
Any help is greatly appreciated!
I think you need a LEFT OTUER JOIN because in your DB can be user without role; in this case you can use this:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email, CR.Id, CR.Name,
CCRM.CustomerRole_Id ,
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
Also, you can have only one row per user with a column with his roles, like this:
SELECT DISTINCT CUST.Id, CUST.Username, CUST.Email,
Roles = STUFF((
SELECT ',' + CAST(CR1.Name as varchar(4000))
FROM CustomerRole CR1
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM1
ON CCRM1.CustomerRole_Id = CR1.Id
LEFT OUTER JOIN CUSTOMER CUST1
ON CUST1.ID = CCRM1.Customer_Id
WHERE CUST1.Id = CUST.Id
FOR XML PATH('')
), 1, 1, '')
FROM CUSTOMER CUST
LEFT OUTER JOIN Customer_CustomerRole_Mapping CCRM
ON CUST.ID = CCRM.Customer_Id
LEFT OUTER JOIN CustomerRole CR
ON CCRM.CustomerRole_Id = CR.Id
Depending of your using, last query can be a little bit slow if you have a lot of customers.
I am querying the static backend db for a game I play (trying to keep up on my coding), and I am having an issue getting the full results that I want.
So the query that I have so far is:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID == mc.constellationID
join mapRegions as mr on ms.regionID == mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
join invTypes as it on it.typeID == ii.typeID
where it.groupID = 9
group by solarSystemName
the problem comes when there are no rows where it.groupID == 9. What I need is for the count to return 0 and I can't for the life of me figure out how to get this to work.
I tried doing left outer join on the final join statement, but no joy.
Change the last join to a left join and set the condition in the on clause istead of the where clause:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID = mc.constellationID
join mapRegions as mr on ms.regionID = mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
left join invTypes as it on it.typeID = ii.typeID and it.groupID = 9
group by solarSystemName
can someone help me.
I need to change my sql query to a querybuilder. I don't know how to use select inside a innerJoin.
Here my sql query:
SELECT
user.id,
analytic_youtube_demographic.age_group,
analytic_youtube_demographic.percentage
FROM
user
INNER JOIN user_youtube ON user_youtube.id = user.id
INNER JOIN analytic ON analytic.id = user_youtube.analytic_id
INNER JOIN analytic_youtube_demographic ON analytic_youtube_demographic.analytic_id = analytic.id
INNER JOIN
(
SELECT analytic_youtube_demographic.id as id, MAX(analytic_youtube_demographic.percentage) max
from user
INNER JOIN user_youtube ON user_youtube.id = user.id
INNER JOIN analytic ON analytic.id = user_youtube.analytic_id
INNER JOIN analytic_youtube_demographic ON analytic_youtube_demographic.analytic_id = analytic.id
GROUP BY user.id
) AS T ON analytic_youtube_demographic.percentage = T.max
WHERE analytic_youtube_demographic.age_group IN ('18-24')
Thanks a lot for your help.
In Visual Studio 2010 with ASP.NET 4, I am trying to join several tables together to put the results in a gridview and details view with sqldatasource. In the sqldatasource wizard I have chosen to specify a custom SQL statement or stored procedure and then in the Query Builder to define complex queries such as JOINs, I have tried to generate a SQL statement to join the problem table with speficific columns from other tables. But when I try to test the query I get an error message which says "Cannot call methods on varchar". I am new to sql statements so please can you advise on what is wrong with the statement.
Here is the generated sql statement below
SELECT Problem.ProblemID, Problem.CustomerID, Problem.Summary,
Problem.DateLogged, Problem.DateUpdated, Status.Status, Priority.Priority,
Technician.Name, Technician.Surname, [Skill Group].[Skill Group],
HelpdeskOperator.Name AS Expr1,
HelpdeskOperator.Surname AS Expr2, Problem.NoteID, Problem.ResolutionID
FROM Problem
INNER JOIN Status ON Problem.StatusID = Status.Status.StatusID
INNER JOIN HelpdeskOperator ON
Problem.HelpdeskID = HelpdeskOperator.HelpdeskID AND Status.StatusID = HelpdeskOperator.StatusID
INNER JOIN Priority ON Problem.PriorityID = Priority.PriorityID
INNER JOIN [Skill Group] ON Problem.SkillGroupID = [Skill Group].SkillGroupID
INNER JOIN Technician ON Problem.ProblemID = Technician.ProblemID
AND Status.StatusID = Technician.StatusID AND
Priority.PriorityID = Technician.PriorityID
AND [Skill Group].SkillGroupID = Technician.SkillGroupID
Thank you in advance
Fixed your query:
SELECT p.ProblemID, p.CustomerID, p.Summary, p.DateLogged, p.DateUpdated, s.Status, pr.Priority, t.Name, t.Surname,
sg.* , ho.Name AS Expr1, ho.Surname AS Expr2, p.NoteID, p.ResolutionID
FROM Problem p
INNER JOIN Status s ON p.StatusID = s.StatusID
INNER JOIN HelpdeskOperator ho ON p.HelpdeskID = ho.HelpdeskID AND s.StatusID = ho.StatusID
INNER JOIN Priority pr ON p.PriorityID = pr.PriorityID
INNER JOIN [Skill Group] sg ON p.SkillGroupID = sg.SkillGroupID
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
AND sg.SkillGroupID = t.SkillGroupID
You had duplicate table identifier in your join clause Status.Status.StatusID
I doubt that your Skill Group table contains column [Skill Group] so changed it to return all values from Skill Group
I just think those were the errors, if not I will need more info about your query and table structure.
EDIT:
First it did not return anything for HelpdeskOperator, look at our query:
INNER JOIN HelpdeskOperator ho ON p.HelpdeskID = ho.HelpdeskID AND s.StatusID = ho.StatusID
that meanse that here is no such HelpdeskOperator record that is assigned to our problem AND statusid, so either problem id points to noexisting helpdeskoperator or statusid of
this operator is different that problem status id.
next is Skill Group
INNER JOIN [Skill Group] sg ON p.SkillGroupID = sg.SkillGroupID
again our problem point to no existing skill group
then Technican
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
AND sg.SkillGroupID = t.SkillGroupID
here is more work as more checks, technicas must be assigned to our problem with given status and priorityt and be in skill group, BUT our skill group is null? so to check if
there is technican for our problem remove AND sg.SkillGroupID = t.SkillGroupID so you get
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
and see if now we get any technican.
I hope this points you into right direction. You must be sure that there are matching record in every joining table.
I am using sql server 2005. In this query i want the log-in user detail should also display but it is not displaying .
So please modify the query so that log-in user detail should also display with the help of session[userId].tostring();
Query written by me is:
SELECT DISTINCT MUDMEMBER.PK_ID, MUDMEMBER.EMPLOYEE_ID, LKB.BANK_NAME, MUHD.SALARY_ACCOUNT_NO, MUHD.PF_NO,
MUHD.PAN_NO, MUHD.GENDER, LKD.DESIGNATION_NAME FROM M_LEADERLED MLL INNER JOIN M_USER_DETAILS MUDMEMBER ON
MLL.LED_ID = MUDMEMBER.PK_ID AND MLL.START_DATE <= Getdate() AND MLL.END_DATE > Getdate() AND MLL.LEADER_ID = '1' LEFT OUTER JOIN
M_USER_HR_DETAILS MUHD ON MUHD.FK_USER_ID = MUDMEMBER.PK_ID AND MUHD.IS_ACTIVE =1 LEFT OUTER JOIN
LK_BANKS LKB ON LKB.PK_ID = MUHD.FK_BANK_ID LEFT OUTER JOIN LK_DESIGNATION LKD ON
LKD.DESIGNATION_VALUE = MUHD.FK_DESIGNATION_VALUE AND LKD.FK_ORGANIZATION_ID = 1 AND LKD.IS_ACTIVE = 1 WHERE MUDMEMBER.ACTIVE = 1
ASP.Net Page you can fetch the loggedin user's detail as follows:
SELECT DISTINCT MUDMEMBER.PK_ID, MUDMEMBER.EMPLOYEE_ID, LKB.BANK_NAME, MUHD.SALARY_ACCOUNT_NO, MUHD.PF_NO,
MUHD.PAN_NO, MUHD.GENDER, LKD.DESIGNATION_NAME FROM M_LEADERLED MLL INNER JOIN M_USER_DETAILS MUDMEMBER ON
MLL.LED_ID = MUDMEMBER.PK_ID AND MLL.START_DATE <= Getdate() AND MLL.END_DATE > Getdate()
AND MLL.LEADER_ID = '1' LEFT OUTER JOIN
M_USER_HR_DETAILS MUHD ON MUHD.FK_USER_ID = MUDMEMBER.PK_ID AND MUHD.IS_ACTIVE =1
LEFT OUTER JOIN
LK_BANKS LKB ON LKB.PK_ID = MUHD.FK_BANK_ID LEFT OUTER JOIN LK_DESIGNATION LKD ON
LKD.DESIGNATION_VALUE = MUHD.FK_DESIGNATION_VALUE AND LKD.FK_ORGANIZATION_ID = 1
AND LKD.IS_ACTIVE = 1
WHERE MUDMEMBER.ACTIVE = 1 AND MUDMEMBER.PK_ID ="+Convert.ToInt32(Session["UserId"])+"