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"])+"
Related
I am trying to create a query that brings me data according to user level and user id.
In my query, Level 1 is Administrator and can see everything, but other users can only see their records.
As i wrote this query works only for other users and not for Administrators.
If my LEVEL_TYPE = 1 then get all rows,
elsif my LEVEL_TYPE 1 then get all rows for this USER_ID.
SELECT cl.ID as ID,
cl.FIRST_NAME || ' ' || cl.LAST_NAME as PATIENT,
cl.AGE as AGE,
CASE
when cl.SEX ='M' then 'Άντρας' when cl.SEX ='F' then 'Γυναίκα'
when cl.SEX ='O' then 'Άλλο' when cl.SEX ='C' then 'Ζευγάρι' END as SEX,
sa.SESSION_AREA as SESSION_AREA,
ms.MARITAL_KIND as MARITAL_KIND,
wt.WORK_KIND as WORK_KIND,
ph.PATIENT_PHOTO as PATIENT_PHOTO,
case when cl.ACTIVE = 1 then 'ΕΝΕΡΓΟΣ'
when cl.ACTIVE = 2 then 'ΑΝΕΝΕΡΓΟΣ' end as PATIENT_ACTIVE,
pt.PAYMENT_KIND as PAYMENT_KIND
FROM CLIENTS cl
left join PAYMENT_TYPE pt on pt.ID = cl.PATIENT_TYPE
left join WORK_TYPE wt on wt.ID = cl.WORKING_CONDITION
left join MARITAL_STATUS ms on ms.ID = cl.MARITAL_STATUS
left join SESSIONS_AREA sa on sa.ID = cl.SESSION_AREA
left outer join PHOTOS ph on ph.CLIENTS_ID = cl.ID
inner join USER_PATIENTS up ON up.PATIENT_ID = cl.ID
inner join APP_USERS au on au.ID = up.USER_ID
WHERE (au.LEVEL_TYPE = 1) or
(au.LEVEL_TYPE <> 1 and up.USER_ID = (SELECT au.id
FROM APP_USERS au
WHERE au.APEX_ID = APEX_UTIL.GET_CURRENT_USER_ID))
ORDER BY cl.ACTIVE, cl.FIRST_NAME || ' ' || cl.LAST_NAME
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 have the this SQL query in a typed dataset in asp.net 2005 and SQL Server 2008 R2.
i.e I have used in Typed Dataset -- "Add Query" -- "Use Sql Statements" and used the below script, then I returned a Datatable. ("Fill a Datatable" checkbox was unchecked")
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as
CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P on P.ProjectManagerId = U.UserID inner join
Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users U inner join Messages M on M.CreatedBy = U.UserID Where M.MessageID =
Messages.MessageID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
**(stuff((select ','+U.EMAIL
FROM USERS U INNER JOIN Recipients R ON U.USERID = R.USERID INNER JOIN MESSAGES M
ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=Messages.MessageID AND R.RecipientTypeID=1
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '')) as [TO],**
**(stuff((select ','+U.EMAIL
FROM USERS U INNER JOIN Recipients R ON U.USERID = R.USERID INNER JOIN
MESSAGES M ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=Messages.MessageID AND R.RecipientTypeID=2
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '')) as CC**
FROM Messages INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId =
dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID =
dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
But on clicking next , it is causing an error like this:
Error in WHERE clause near '('.
Unable to parse query text.
However If I run the same above query in SQL Server 2008 R2 Management Studio, it runs fine and returns the results.
Please suggest what am I doing wrong.
Thanks
Hi, I have used one way out for this..ie used this Query(after slight modification) as inline query in .cs code behind file.. but now another problem is arising....ie.
Now it is taking too much of time to return results.. is it because of the functions I have used in the query ? please suggest some workaround for this..
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P on P.ProjectManagerId = U.UserID inner join Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users U inner join Messages M on M.CreatedBy = U.UserID Where M.MessageID = Messages.MessageID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,1)) as [TO],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,2)) as [CC]
FROM Messages INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID = dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
-- FUNCTION "fn_ForEmailReport" used in above query.
ALTER FUNCTION [dbo].[fn_ForEmailReport]
(
#MessageID int,
#RecipientTypeID int
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE #List VARCHAR(8000)
set #List = (stuff((select ','+U.EMAIL
FROM USERS U inner JOIN Recipients R ON R.USERID = U.USERID inner JOIN
MESSAGES M ON R.MESSAGEID = M.MESSAGEID
WHERE m.MessageID=#MessageID AND R.RecipientTypeID=#RecipientTypeID
for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, ''))
RETURN #List;
END
There are too many joins I believe. For example, the query below
(select U.firstName + ' ' + U.LastName
from Users U inner join Projects P
on P.ProjectManagerId = U.UserID
inner join Messages M on M.ProjectId = P.ProjectID
where M.MessageID = Messages.MessageID) as ProjectManagerName,
gets only a firstname and lastname, a cell, it doesn't need to be a join query. Plus, you have already joined Projects table at the end. You can give an alias to outer query and make inner queries simpler:
SELECT DISTINCT
ISNULL(Projects.ProjectID,'0') as
ProjectID,Messages.MessageID, Projects.ProjectName, Customers.CustomerName as CustomerName, Regions.RegionName,
(select U.firstName + ' ' + U.LastName
from Users WHERE P.ProjectManagerId = UserID) as ProjectManagerName,
dbo.phases.TagName as Phase,
(select U.firstName + ' ' + U.LastName
from Users WHERE M.CreatedBy = UserID) as CreatedBy,
Messages.DateCreated as EmailCreatedDate,
Messages.MessageSubject as MessageSubject,
Users.Email as [From],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,1)) as [TO],
(select [dbo].[fn_ForEmailReport](Messages.MessageID,2)) as [CC]
FROM Messages M INNER JOIN Users
ON Messages.Sender = Users.UserID
INNER JOIN Recipients
ON Messages.MessageID = Recipients.MessageID
LEFT OUTER JOIN dbo.Projects P
ON dbo.Messages.ProjectID = dbo.Projects.ProjectID
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
Left outer join dbo.regions on dbo.Customers.RegionID = dbo.Regions.RegionID
left outer join dbo.Phases on messages.phaseid = dbo.phases.phaseid
where (Messages.MessageTypeID=1)
and Projects.ProjectID <> '0'
order by Projects.ProjectName
Finally, if you don't retrieve any information from Customers table, you can omit the following line:
Left OUTER JOIN dbo.Customers on dbo.Projects.CustomerId = dbo.Customers.CustomerID
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
I am trying to write sql query to show search throw resource that user's bookmarked. my problem is when user type keyword the search will search all resource on the website and including his bookmark I want to be able to search user bookmark
SELECT
a.Id ,
a.summary ,
a.pageId ,
a.name ,
a.createdOn ,
a.userID ,
Total
FROM Resources AS a
LEFT JOIN Topics_Resources AS b ON a.Id = b.ResourceID
LEFT JOIN Skills_Resources AS c ON a.Id = c.ResourceID
INNER JOIN Modules AS m ON a.ModuleId = m.ModuleID
INNER JOIN ContentItems AS ci ON m.ModuleID = ci.ModuleID
INNER JOIN Tabs AS t ON t.TabID = ci.TabID
INNER JOIN TabModules AS tb ON t.TabID = tb.TabID
INNER JOIN Bookmarks ON a.Id = Bookmarks.resourceID
INNER JOIN Bookmarks AS Bookmarks_1 ON a.Id = Bookmarks_1.resourceID
INNER JOIN
(
SELECT
r.Id , COUNT(l.resourceId) Total
FROM resources r
LEFT JOIN likes l ON r.Id = l.resourceid
GROUP BY r.Id
)
l ON a.Id = l.Id
LEFT OUTER JOIN HtmlText AS h ON tb.ModuleID = h.ModuleID
WHERE (h.content LIKE '%winter%')
OR (t.description LIKE '%winter%')
OR (t.keywords LIKE '%winter%')
OR (a.summary LIKE '%winter%')
AND Bookmarks.userID = '695A8626-A28B-4308-AD9B-D5AC576B4E5A'
GROUP BY a.Id ,
a.summary ,
a.pageId ,
a.name ,
a.createdOn ,
a.userID ,
Total
ORDER BY a.createdOn DESC
try enclosing every OR in brackets
SELECT a.Id, a.summary, a.pageId, a.name, a.createdOn,a.userID,Total
FROM Resources AS a
LEFT JOIN Topics_Resources AS b ON a.Id = b.ResourceID
LEFT JOIN Skills_Resources AS c ON a.Id = c.ResourceID
INNER JOIN Modules AS m ON a.ModuleId = m.ModuleID
INNER JOIN ContentItems AS ci ON m.ModuleID = ci.ModuleID
INNER JOIN Tabs AS t ON t.TabID = ci.TabID
INNER JOIN TabModules AS tb ON t.TabID = tb.TabID
INNER JOIN Bookmarks ON a.Id = Bookmarks.resourceID
INNER JOIN Bookmarks AS Bookmarks_1 ON a.Id = Bookmarks_1.resourceID
INNER JOIN (select r.Id, count(l.resourceId) Total
from resources r
left join likes l on r.Id = l.resourceid
group by r.Id) l
on a.Id = l.Id
LEFT OUTER JOIN HtmlText AS h ON tb.ModuleID = h.ModuleID
where ((h.content like '%winter%') or ( t.description like '%winter%') or ( t.keywords like '%winter%') or ( a.summary like '%winter%'))
and Bookmarks.userID='695A8626-A28B-4308-AD9B-D5AC576B4E5A'
GROUP BY a.Id, a.summary, a.pageId, a.name, a.createdOn,a.userID,Total
ORDER BY a.createdOn DESC
Note: if you expect big quantity of data, this kind of query will last hours. you should add FULLTEXT