Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...SQL and SQL Ser...SQL and SQL Ser...SQL Query Not working SQL Query Not working
Previous
 
Next
New Post
8/4/2010 1:16 AM
 
I want to query my database and get the suburb, region, email and created date for users in a list. I've copied two queries I know work:
______________
select
us.firstname as [First Name],
us.lastname as [Last Name],
us.email as [Email],
up.createddate as [Entry Date]
from users us
inner join userportals up on us.userid=up.userid
where up.portalid=0
_____________

and:
_____________
SELECT DISTINCT
  u.LastName AS [Last Name],  u.FirstName AS [First], upd.Profession, upd.Region, u.Email
FROM dbo.Users as u INNER JOIN
  dbo.UserRoles AS ur ON u.UserID = ur.UserID
INNER JOIN Roles R on UR.Roleid = R.Roleid
INNER JOIN
  (SELECT      
    up.UserID,  
    MAX(CASE WHEN ppd.PropertyName = 'Profession' THEN up.PropertyValue ELSE '' END) AS Profession,
    MAX(CASE WHEN ppd.PropertyName = 'Region' THEN up.PropertyValue ELSE '' END) AS Region
   FROM           
    dbo.UserProfile AS up INNER JOIN 
    dbo.ProfilePropertyDefinition AS ppd ON up.PropertyDefinitionID = ppd.PropertyDefinitionID AND ppd.PortalID = 0 
   GROUP BY up.UserID) AS upd ON u.UserID = upd.UserID
_____________

and tried to merge them into:

SELECT DISTINCT
  u.LastName AS [Last Name],  u.FirstName AS [First], upd.Profession, upd.Region, u.Email, up.createddate
FROM dbo.Users as u INNER JOIN
  dbo.UserRoles AS ur ON u.UserID = ur.UserID INNER JOIN
users us
INNER JOIN userportals up on us.userid=up.userid
INNER JOIN Roles R on UR.Roleid = R.Roleid
INNER JOIN
  (SELECT      
    up.UserID,  
    MAX(CASE WHEN ppd.PropertyName = 'Profession' THEN up.PropertyValue ELSE '' END) AS Profession,
    MAX(CASE WHEN ppd.PropertyName = 'Region' THEN up.PropertyValue ELSE '' END) AS Region
   FROM           
    dbo.UserProfile AS up INNER JOIN 
    dbo.ProfilePropertyDefinition AS ppd ON up.PropertyDefinitionID = ppd.PropertyDefinitionID AND ppd.PortalID = 0 
   GROUP BY up.UserID) AS upd ON u.UserID = upd.UserID 

but it says I have an incorrect syntax on line 16 near 'UserID'. (what ever I leave as the end, it says the error is there. Ie: If I delete everything after '0', it says the syntax error is at line 0.

I'm a total noob, I'm just trying to make these queries work using my limited knowledge. Can anyone give me a fix, and even better, explain why the fix works?

Thanks!

 
New Post
8/4/2010 4:34 AM
 
do not reuse shortcuts in nested queries, in your case up. Besides, remove all joins with tables you don't need, e.g. UserPortals, userRoles, Roles. Use joins on the first level for all, including a Property-Join per Property, ideally by using propertydefitionid instead of propertyname, if you don't need the query to be portable.

SELECT u.LastName [Last Name], u.FirstName [First], P1.Profession, P2.Region, u.Email, up.createddate 
FROM Users U
INNER JOIN UserPortals UP on U.UserId = up.UserId
LEFT JOIN (SELECT UserID, PropertyValue AS Profession FROM UserProfile WHERE PropertyDefinitionID = 17) P1 ON U.UserID = P1.UserID
LEFT  JOIN (SELECT UserID, PropertyValue AS Region FROM UserProfile WHERE PropertyDefinitionID = 33) P2 ON U.UserID = p2.USERID
WHERE up.PortalID = 0

you need to lookup appropriate values for PropertyDefinitionID in Host SQL by running SELECT * FROM ProfilePropertyDefinition first and might need to adjust PortalID as well.


Cheers from Germany,
Sebastian Leupold

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
New Post
8/5/2010 12:22 AM
 
Thanks for your reply Sebastian. It still tells me 'There was an error executing the data source: There is an error in your SQL at line 6: Incorrect syntax near the keyword 'WHERE'. '. I've looked up the property definitiion ids and entered them (17 becomes 9 and 33 becomes 56) Is there anything else I can try? (I tried deleting line 6 - no joy).
 
New Post
8/5/2010 5:08 AM
 
sorry, I didn't test the query and missed two links for the joins. Fixed in previous post (and tested this time).

Cheers from Germany,
Sebastian Leupold

dnnWerk - The DotNetNuke Experts   German Spoken DotNetNuke User Group

Speed up your DNN Websites with TurboDNN
 
Previous
 
Next
HomeHomeDevelopment and...Development and...SQL and SQL Ser...SQL and SQL Ser...SQL Query Not working SQL Query Not working


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out