SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Filtering multiple alias for single column in sql


Filtering multiple alias for single column in sql

Author
Message
srikarkalle
srikarkalle
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 2
My query is as below which works perfectly fine:

SELECT
[le].[id] AS [eventId]
,MAX(Convert (varchar(10), [le].[eventCreationTime], 104)) AS [Swipe Date]
,MAX(Convert (varchar(10), [le].[eventCreationTime], 108)) AS [Swipe Time]
,MAX(Replace(Replace([la].[addressTag], 'AccessEngine.Devices.',''),'.Event','')) AS [Reader Name]
,MAX([ls].[stateNumber]) AS [stateNumber]
,MAX([ls].[stateName]) AS [state Name]
,[First Name] = MAX(CASE WHEN [levt].[eventValueName] = 'FIRSTNAME' THEN [lev].[stringValue] END)
,Name = MAX(CASE WHEN [levt].[eventValueName] = 'NAME' THEN [lev].[stringValue] END)
,[Card NO] = MAX(CASE WHEN [levt].[eventValueName] = 'CARDNO' THEN [lev].[stringValue] END)
,[Person NO] = MAX(CASE WHEN [levt].[eventValueName] = 'PERSNO' THEN [lev].[stringValue] END)
,Department = MAX(CASE WHEN [levt].[eventValueName] = 'DEPARTMENT' THEN [lev].[stringValue] END)
FROM LogDetectorType AS ldt INNER JOIN
LogEvent AS le LEFT OUTER JOIN
LogState AS ls ON le.stateId = ls.Id INNER JOIN
LogAddress AS la ON le.addressId = la.ID ON ldt.ID = la.detectorTypeId LEFT OUTER JOIN
LogEvent2Value AS le2v ON le.ID = le2v.eventId LEFT OUTER JOIN
LogEventValueType AS levt INNER JOIN
LogEventValue AS lev ON levt.ID = lev.eventTypeId ON le2v.valueId = lev.Id
WHERE (stateName IN(@StateName))
AND ([le].[eventCreationTime] BETWEEN (@From) AND (@To))
AND (Replace(Replace([la].[addressTag], 'AccessEngine.Devices.',''),'.Event','') IN (@Reader))
GROUP BY le.ID, [le].[eventCreationTime]
ORDER BY [le].[eventCreationTime] DESC, MAX([le].[id])

I need to add a new AND condition for filter anAlias column [First Name]. When i do this, other alias columns like [Card NO]; [Name]; [Pers NO] are shown with blank results.

Kindly help me in filtering this.

Thanks in advance.
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66933 Visits: 18570
If you want to filter for First Name, then you need to use [lev].[stringValue] in your where clause.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

srikarkalle
srikarkalle
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 2
If i use stringvalue, other alias columns will be NULL or will not show any results.
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66933 Visits: 18570
What is your goal in trying to filter by somebody's First name?

Do you have sample data and table definitions to help solve this problem you are running into?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search