Re: Need help to modify my query

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Steve Kass (skass_at_drew.edu)
Date: 12/07/04


Date: Mon, 06 Dec 2004 22:41:04 -0500

Mitra,

  This is not very well-suited to SQL, because the rows in
the result set don't represent any real-world entity. The
e-mail address and the group name that appear in a particular
row both belong to the same person, but they are not
otherwise related to each other. A report writer might
do a much better job here.

Here's a query that will produce this report - it works
by creating an artificial relationship between email and
grou names by considering them related if the ordinal
position of the email address among all email addresses
for the given person equals the ordinal position of the
group id among all the groups for the given person, and
then uses an outer join, since there may be different numbers
of each for a given person. The query then only prints
the user name if the ordinal position of the email or group
name is 1.

select
  case when coalesce(emailRank,gnameRank) = 1
       then coalesce(euname, guname)
  else '' end as name,
  coalesce(email,'') as email,
  coalesce(groupname,'') as groupName
from (
  select
    E.euname, E.email, E.emailRank,
    G.guname, G.groupname, G.gnameRank
  from (
    select
      su.id_user,
      su.name as euname,
      sue1.email,
      (select count(*) from S_useremail sue2
       where sue2.id_user = su.id_user
       and sue2.email >= sue1.email
      ) as emailRank
    from S_user as su
    left outer join S_useremail as sue1
    on su.id_user = sue1.id_user
  ) E full outer join (
    select
      su.id_user,
      su.name as guname,
      sg1.name as groupname,
      (select count(*) from S_group sg2
       where sg2.id_group in (
         select sug.id_group from S_usergroup sug
         where sug.id_user = su.id_user
       ) and sg2.id_group <= sg1.id_group
      ) as gnameRank
    from S_user as su
    left outer join S_usergroup as sug
    on sug.id_user = su.id_user
    left outer join S_group as sg1
    on sg1.id_group = sug.id_group
  ) G
  on E.id_user = G.id_user
  and emailRank = gnameRank
) T
order by
  coalesce(euname, guname),
  coalesce(emailRank,gnameRank)

-- Steve Kass
-- Drew University
-- Ref: 66C57956-71B7-432F-AD7C-11568CB8747F

mitra wrote:

>Hugo,
>
>Sorry , I was not clear with how I need the query result.
>Below is the ddl and the data your rquested and with
>more explanation of how I need the query to return the result.
>============================================
>CREATE TALBE S_User (
> id_user int not null,
> name varchar (30)
>)
>
>INSERT INTO S_user ( id_user, name)
>VALUES (1, 'mitra')
>INSERT INTO S_user ( id_user, name)
>VALUES (2, 'john')
>INSERT INTO S_user ( id_user, name)
>VALUES (3, 'patrick')
>
>
>CREATE TABLE S_Group (
> id_group int not null,
> name varchar(30)
>)
>
>INSERT INTO S_group ( id_group, name)
>VALUES (1, 'Sales')
>INSERT INTO S_group ( id_group, name)
>VALUES (2, 'Operation')
>INSERT INTO S_group ( id_group, name)
>VALUES (3, 'Human Resources')
>INSERT INTO S_group ( id_group, name)
>VALUES (4, 'Technical Support)
>
>CREATE TABLE S_useremail (
> id_user int not null,
> email Varchar(30)
>)
>
>INSERT INTO S_useremail (id_user, email)
>Values(1, 'mitra@abc.com')
>INSERT INTO S_useremail (id_user, email)
>Values(1, 'mitra@att.com')
>INSERT INTO S_useremail (id_user, email)
>Values(2,'john@abc.com')
>INSERT INTO S_useremail (id_user, email)
>Values(2, 'john@att.com')
>INSERT INTO S_useremail (id_user, email)
>Values(3,'patrick@abc.com')
>INSERT INTO S_useremail (id_user, email)
>Values(3, 'patrick@att.com')
>
>CREATE TABLE S_userGroup (
> id_user int,
> id_group int
>)
>
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (1,1)
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (1,2)
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (1,3)
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (2,1)
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (2,2)
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (2,3)
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (3,2)
>INSERT INTO S_userGroup (id_user, id_group)
>VALUES (3,3)
>
>Please note users can belong to more than one group and users
>can have more than one email address.
>
>I like to avoid repeating the name and user's email
>addresses for every gorup to which they belong.
>
>I like to change the query to return a result like:
>===========================================
>NAME EMAIL_ADDRESS Group_NAME
>
>mitra mitra@abc.com, Sales
> mitra@att.com Human Resource
> Operation
>
>john john@abc.com, Sales
> john@att.com Human Resources
> Operation
>
>Patrick patrick@abc.com, Human Resources
> patrick@att.com Operation
>============================================
>
>Below is the query statement that i need help to modify
>to make it return results like above:
>
>==============================================
>SELECT [User Name],
> [Email Address],
> [Group Name]
>FROM (SELECT
>su.fullname as RealUserName,
> sue.email as RealEmailAdd,
>'' as [User Name],
>'' as [Email Address],
>sg.name as [Group Name]
>FROM S_userGroup sug
>LEFT JOIN S_user su ON sug.id_sysUser = su.id
>LEFT JOIN S_group sg ON sug.id_group = sg.id
>LEFT JOIN S_useremail sue ON su.id = sue.id_user
>WHERE sg.name <> (
> SELECT MIN (sg2.name)
> FROM S_userGroup sug2
> LEFT JOIN S_group sg2
> ON sug2.id_user = sug.id_user
> AND sug2.id_group = sg2.id
> )
>
>UNION ALL
>SELECT
> su.fullname,
>sue.email,
>su.fullname,
>sue.email,
> min(sg.name)
>FROM S_userGroup sug
>LEFT JOIN S_user su ON sug.id_user = su.id
>LEFT JOIN S_group sg ON sug.id_group = sg.id
>LEFT JOIN S_useremail sue ON su.id = sue.id_user
>GROUP BY su.fullname,sue.email )
> Report
>ORDER BY RealUserName, RealEmailAdd, [Group Name]
>
>==================================================
>
>Thank you so much!
>
>--Mitra
>
>
>
>"Hugo Kornelis" wrote:
>
>
>
>>On Fri, 3 Dec 2004 12:23:01 -0800, mitra wrote:
>>
>>
>>
>>>Hi,
>>>
>>>After learning how to do the query statement shown below, of course with
>>>help from this newsgroup, I now need to modify the statement to return all
>>>the email addresses once as well. I tried but I could not get it to work.
>>>
>>>Query below returns results like:
>>>===============================================
>>>Mitra Brown mbn@abc.com Human Resources
>>> Operations
>>> Technical Support
>>>Mitra Brown mbn@att.com Human Resources
>>> Operations
>>> Technical Support
>>>
>>>
>>>================================================
>>>
>>>I like to modify the query to return a result like:
>>>
>>>Mitra Brown mbn@abc.com Human Resources
>>> mbn@att.com Operations
>>> Technical Support
>>>
>>>
>>(snip)
>>
>>Hi mitra,
>>
>>I assume that you have already been advised to do the presentation logic
>>in the presentation tier?
>>
>>Like any other presentation stuff, it CAN be done in SQL but it will be a
>>bitch to write and an even nastier bitch to maintain. Plus, it will
>>probably perform like a snail in fresh tar.
>>
>>Before I can get to the nitty gritty of the query, I need to know *how*
>>SQL Server should decide to pair the abc-address with Human Resources and
>>the att-address with Operations. What is the logical relation that causes
>>these pairs (and not other possible pairs) to be formed?
>>
>>Finally: please post CREATE TABLE and INSERT statements that allow me to
>>quickly reproduce your tables and sample data, so that I can use that for
>>testing. If you already posted this before, provide a message-ID or a
>>Google link (with several hundred messages daily in this group, it's hard
>>to find anything if oyu don't know exactly where to look).
>>
>>Best, Hugo
>>--
>>
>>(Remove _NO_ and _SPAM_ to get my e-mail address)
>>
>>
>>



Relevant Pages

  • Re: Query with left outer join all of a sudden wont work?
    ... just doesn't work if it is a left or right outer join. ... rather than on the top most query calling it). ... SELECT * FROM [qryFarmer Contracts] ... can you post the whole SQL statement? ...
    (microsoft.public.access.queries)
  • Re: Using q() to define a query
    ... I beleive most SQL parsers will ignore white space like that in an SQL query. ... FULL OUTER JOIN jsequipment AS eq ON jsjourneys.equipment = eq.id ... If those were really in the query, I can't imagine the database would run it, so I suppose they're an artifact of the combination of using qto quote my query and using Apache's logger to output it. ...
    (perl.dbi.users)
  • Re: Cartesian product & outer join
    ... An Ambiguous Outer Join basically means that your arrows are pointing the ... My suggestion to most query problems is to not try to do it in just one ... >> Post your SQL. ... >>> time with overtime time categories and multiplies by civilian overtime ...
    (microsoft.public.access.queries)
  • again and again... query analyzer and wrong SQL statement plan... + outer join?
    ... again we have some SQL statement performance problems... ... we have aproblem with a query which use 1 inner join and 3 left outer ... Each outer join is a sub-query which contain a group by: ... This query takes more then 10 minutes, but if I precalculate each sub-query ...
    (microsoft.public.sqlserver.datawarehouse)
  • Re: DBMS and lisp, etc.
    ... Naively implemented with SQL, again for 10 ... (1 query for the initial orders, 1 query for each order for its ... soon as you upgrade to the SQL database. ... (eq (order-customer orderA) ...
    (comp.lang.lisp)