最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ms access - How to add a parameter to a union query - Stack Overflow

programmeradmin1浏览0评论

I found the following query on here and found it useful. Having made the query work for my database I would now like to add a parameter to it to filter out records that are not required - the table required for the parameter is called Season with a join from the members table on seasons.idseasons = members.members id no and the parameter would be WHERE (((seasons.Season)=[enter season])).

Select  (tMin.Initial1 + ' and ' + tMax.Initial1) As initial, tMax.surname1 As surname, tMax.[first line address], tMax.[second line address], tMax.town, tMax.postcode, tMax.GroupCount From (Select Distinct Max(initial) As initial1, Max(surname) As surname1,[first line address], [second line address], town, postcode, Count() As  GroupCount From members Group By [first line address], [second line address], town, postcode Having Count() = 2) As tMax Inner Join

   (Select Distinct Min(initial) As initial1, Min(surname) As surname1, [first line address],[second line address], town, postcode, Count(*) As GroupCount
   From members
   Group By [first line address], [second line address], town, postcode
   Having Count(*) = 2) As tMin

           On (tMax.[first line address] = tMin.[first line address]) And 
              (IIf(IsNull(tMax.[second line address]), '', tMax.[second line address]) = IIf(IsNull(tMin.[second line address]), '', tMin.[second line address])) And 
              (tMax.town = tMin.town) And  
               (tMax.postcode = tMin.postcode)

UNION ALL Select Max(initial) As initial1, Max(surname) As surname1, [first line address], [second line address], town, postcode, Count() As GroupCount From members Group By [first line address], [second line address], town, postcode Having Count() = 1 Or Count(*) > 2

ORDER BY surname, initial, town, postcode;

Not sure where the parameter should go within the code. Can you help with this?

I found the following query on here and found it useful. Having made the query work for my database I would now like to add a parameter to it to filter out records that are not required - the table required for the parameter is called Season with a join from the members table on seasons.idseasons = members.members id no and the parameter would be WHERE (((seasons.Season)=[enter season])).

Select  (tMin.Initial1 + ' and ' + tMax.Initial1) As initial, tMax.surname1 As surname, tMax.[first line address], tMax.[second line address], tMax.town, tMax.postcode, tMax.GroupCount From (Select Distinct Max(initial) As initial1, Max(surname) As surname1,[first line address], [second line address], town, postcode, Count() As  GroupCount From members Group By [first line address], [second line address], town, postcode Having Count() = 2) As tMax Inner Join

   (Select Distinct Min(initial) As initial1, Min(surname) As surname1, [first line address],[second line address], town, postcode, Count(*) As GroupCount
   From members
   Group By [first line address], [second line address], town, postcode
   Having Count(*) = 2) As tMin

           On (tMax.[first line address] = tMin.[first line address]) And 
              (IIf(IsNull(tMax.[second line address]), '', tMax.[second line address]) = IIf(IsNull(tMin.[second line address]), '', tMin.[second line address])) And 
              (tMax.town = tMin.town) And  
               (tMax.postcode = tMin.postcode)

UNION ALL Select Max(initial) As initial1, Max(surname) As surname1, [first line address], [second line address], town, postcode, Count() As GroupCount From members Group By [first line address], [second line address], town, postcode Having Count() = 1 Or Count(*) > 2

ORDER BY surname, initial, town, postcode;

Not sure where the parameter should go within the code. Can you help with this?

Share Improve this question edited Feb 8 at 10:11 Mark Rotteveel 109k226 gold badges155 silver badges219 bronze badges asked Feb 8 at 5:57 Eliz A PurvesEliz A Purves 131 silver badge3 bronze badges New contributor Eliz A Purves is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

Try this where both the two subqueries and the second query have been expanded with an Inner Join and a Where clause:

    Inner Join
        Seasons
        On members.memberid = Seasons.idseasons
    Where
        Seasons.season = [Enter Season]
Select  
    (tMin.Initial1 + ' and ' + tMax.Initial1) As initial, 
    tMax.surname1 As surname, 
    tMax.[first line address], 
    tMax.[second line address], 
    tMax.town, tMax.postcode, 
    tMax.GroupCount 
From 
    (Select Distinct 
        Max(initial) As initial1, 
        Max(surname) As surname1,
        [first line address], 
        [second line address], 
        town, postcode, 
        Count() As  GroupCount 
    From 
        members
    Inner Join
        Seasons
        On members.memberid = Seasons.idseasons
    Where
        Seasons.season = [Enter Season]
    Group By 
        [first line address], 
        [second line address], 
        town, 
        postcode 
    Having Count() = 2) As tMax 

Inner Join

    (Select Distinct 
        Min(initial) As initial1, 
        Min(surname) As surname1, 
        [first line address],
        [second line address], 
        town, 
        postcode, 
        Count(*) As GroupCount
    From 
        members
    Inner Join
        Seasons
        On members.memberid = Seasons.idseasons
    Where
        Seasons.season = [Enter Season]
    Group By 
        [first line address], 
        [second line address], 
        town, 
        postcode
    Having Count(*) = 2) As tMin

    On  (tMax.[first line address] = tMin.[first line address]) And 
        (IIf(IsNull(tMax.[second line address]), '', tMax.[second line address]) = IIf(IsNull(tMin.[second line address]), '', tMin.[second line address])) And 
        (tMax.town = tMin.town) And  
        (tMax.postcode = tMin.postcode)

UNION ALL 

Select 
    Max(initial) As initial1, 
    Max(surname) As surname1, 
    [first line address], 
    [second line address], 
    town, 
    postcode, 
    Count() As GroupCount 
From 
    members 
Inner Join
    Seasons
    On members.memberid = Seasons.idseasons
Where
    Seasons.season = [Enter Season]
Group By 
    [first line address], 
    [second line address], 
    town, 
    postcode 
Having 
    Count() = 1 Or Count(*) > 2
ORDER BY 
    surname, 
    initial, 
    town, 
    postcode;
发布评论

评论列表(0)

  1. 暂无评论