Query that provides one line.

I have written Access SQL that creates a query.  The issue is that I get the data in three rows and I would like them to be grouped in one row.  Can this query be combined to one line instead of three.  
SELECT OPM_Table.LOCNAME, LocationID.LocationDescription, '5/7/9' AS GRADE, 'GS-5/7/9' AS GRADE2, IIf(OPM_Table.GRADE2='GS-5',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2)) AS FY2019, IIf(OPM_Table.GRADE2='GS-7',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2020],2)) AS FY2020, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2021],2)) AS FY2021, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL2*[PayInflation]![2022],2)) AS FY2022, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2023],2)) AS FY2023, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2024],2)) AS FY2024
FROM PayInflation, LocationID INNER JOIN OPM_Table ON LocationID.LOCNAME = OPM_Table.LOCNAME
WHERE (((LocationID.LocationDescription)=[Forms]![Fee Review Summary]![Combo117]) AND (([Forms]![Fee Review Summary]![Combo137]='GS-5/7/9' And [GRADE2]='GS-5' Or [GRADE2]='GS-7' Or [GRADE2]='GS-9')<>False));

Open in new window

Thanks,
Terry
Terry MooreAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertConsultingCommented:
Hi,

I think your query perform a cartesian product with the PayInflation table.
Can you do a join on it ?

Else, either add a GROUP BY clause, or do a SELECT TOP 1
SELECT TOP 1 OPM_Table.LOCNAME, LocationID.LocationDescription, '5/7/9' AS GRADE, 'GS-5/7/9' AS GRADE2, IIf(OPM_Table.GRADE2='GS-5',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2)) AS FY2019, IIf(OPM_Table.GRADE2='GS-7',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2020],2)) AS FY2020, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2021],2)) AS FY2021, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL2*[PayInflation]![2022],2)) AS FY2022, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2023],2)) AS FY2023, IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2024],2)) AS FY2024
FROM PayInflation, LocationID INNER JOIN OPM_Table ON LocationID.LOCNAME = OPM_Table.LOCNAME
WHERE (((LocationID.LocationDescription)=[Forms]![Fee Review Summary]![Combo117]) AND (([Forms]![Fee Review Summary]![Combo137]='GS-5/7/9' And [GRADE2]='GS-5' Or [GRADE2]='GS-7' Or [GRADE2]='GS-9')<>False));

Open in new window

Terry MooreAuthor Commented:
I tried your SQL and I now only get data for FY2019.  From FY2020 on it is blank.
Gustav BrockCIOCommented:
Try to sum:

SELECT 
    OPM_Table.LOCNAME, 
    LocationID.LocationDescription, 
    '5/7/9' AS GRADE, 
    'GS-5/7/9' AS GRADE2,
    Sum(IIf(OPM_Table.GRADE2='GS-5',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2))) AS FY2019, 
    Sum(IIf(OPM_Table.GRADE2='GS-7',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2020],2))) AS FY2020, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2021],2))) AS FY2021, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL2*[PayInflation]![2022],2))) AS FY2022, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2023],2))) AS FY2023, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2024],2))) AS FY2024
FROM 
    PayInflation, 
    LocationID 
INNER JOIN 
    OPM_Table 
    ON LocationID.LOCNAME = OPM_Table.LOCNAME
WHERE 
    ([Forms]![Fee Review Summary]![Combo137]='GS-5/7/9' And [GRADE2]='GS-5' Or [GRADE2]='GS-7' Or [GRADE2]='GS-9')<>False
GROUP BY
    OPM_Table.LOCNAME, 
    LocationID.LocationDescription
HAVING
    LocationID.LocationDescription=[Forms]![Fee Review Summary]![Combo117]

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Terry MooreAuthor Commented:
Thank you so much ,it works!  This works.  Can I loop another SQL with the same SQL with different criteria?
Gustav BrockCIOCommented:
Yes, just remember to put filtered fields, that also are selected, under HAVING.
Terry MooreAuthor Commented:
So If I have two scripts I want to combine, how could I do it.  I am new at SQL and still learning.  Attached are the two SQL Scripts that work.  Can you assist me with it or should I post the question again and you can get points helping me?
5_7_9.txt
PayRoll.txt
Gustav BrockCIOCommented:
What does "combine" mean?
Terry MooreAuthor Commented:
It means that if it is not GS-5/7/9 then run the other script.
Gustav BrockCIOCommented:
It might be doable with a union  query:

SELECT 
    OPM_Table.LOCNAME, 
    LocationID.LocationDescription, 
    '5/7/9' AS GRADE, 
    'GS-5/7/9' AS GRADE2,
    Sum(IIf(OPM_Table.GRADE2='GS-5',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2))) AS FY2019, 
    Sum(IIf(OPM_Table.GRADE2='GS-7',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2020],2))) AS FY2020, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2021],2))) AS FY2021, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL2*[PayInflation]![2022],2))) AS FY2022, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2023],2))) AS FY2023, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2024],2))) AS FY2024
FROM 
    PayInflation, 
    LocationID 
INNER JOIN 
    OPM_Table 
    ON LocationID.LOCNAME = OPM_Table.LOCNAME
WHERE 
    ([Forms]![Fee Review Summary]![Combo137]='GS-5/7/9' And [GRADE2]='GS-5' Or [GRADE2]='GS-7' Or [GRADE2]='GS-9')<>False
GROUP BY
    OPM_Table.LOCNAME, 
    LocationID.LocationDescription
HAVING
    LocationID.LocationDescription=[Forms]![Fee Review Summary]![Combo117]

UNION ALL

SELECT 
    OPM_Table.LOCNAME, 
    LocationID.LocationDescription, 
    OPM_Table.GRADE, 
    OPM_Table.GRADE2, 
    Sum(FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2)) AS FY2019, 
    Sum(FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2020],2)) AS FY2020, 
    Sum(FormatCurrency([FY2020]*[PayInflation]![2021],2)) AS FY2021, 
    Sum(FormatCurrency(OPM_Table.ANNUAL5*[PayInflation]![2022],2)) AS FY2022, 
    Sum(FormatCurrency([FY2022]*[PayInflation]![2023],2)) AS FY2023, 
    Sum(FormatCurrency(OPM_Table.ANNUAL6*[PayInflation]![2024],2)) AS FY2024
FROM 
    PayInflation, 
    LocationID 
INNER JOIN 
    OPM_Table 
    ON LocationID.LOCNAME = OPM_Table.LOCNAME
GROUP BY
    OPM_Table.LOCNAME, 
    LocationID.LocationDescription,
    OPM_Table.GRADE, 
    OPM_Table.GRADE2
HAVING
    LocationID.LocationDescription=Forms![Fee Review Summary]!Combo117 And 
    OPM_Table.GRADE2=Forms![Fee Review Summary]!Combo137;

Open in new window

Terry MooreAuthor Commented:
I get an error message.  It says that subqueries cannot be used in the expression (FormatCurrency(FY2020]*[PayInflation]![2021],2)).
Gustav BrockCIOCommented:
That's right. Shouldn't it read:

    Sum(FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2)) AS FY2019, 
    Sum(FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2020],2)) AS FY2020, 
    Sum(FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2021],2)) AS FY2021, 
    Sum(FormatCurrency(OPM_Table.ANNUAL5*[PayInflation]![2022],2)) AS FY2022, 
    Sum(FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2023],2)) AS FY2023, 
    Sum(FormatCurrency(OPM_Table.ANNUAL6*[PayInflation]![2024],2)) AS FY2024

Open in new window

or similar?
Terry MooreAuthor Commented:
It should read this for a GS5/7/9.
Sum(IIf(OPM_Table.GRADE2='GS-5',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2))) AS FY2019, 
    Sum(IIf(OPM_Table.GRADE2='GS-7',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2020],2))) AS FY2020, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL1*[PayInflation]![2021],2))) AS FY2021, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL2*[PayInflation]![2022],2))) AS FY2022, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2023],2))) AS FY2023, 
    Sum(IIf(OPM_Table.GRADE2='GS-9',FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2024],2))) AS FY2024

Open in new window


And this for single grades.
FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2) AS FY2019, FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2020],2) AS FY2020, FormatCurrency([FY2020]*[PayInflation]![2021],2) AS FY2021, FormatCurrency(OPM_Table.ANNUAL5*[PayInflation]![2022],2) AS FY2022, FormatCurrency([FY2022]*[PayInflation]![2023],2) AS FY2023, FormatCurrency(OPM_Table.ANNUAL6*[PayInflation]![2024],2) AS FY2024

Open in new window

Gustav BrockCIOCommented:
But you can use the summed alias FY2020 and FY2024 in expressions.
So, try with:

    Sum(FormatCurrency(OPM_Table.ANNUAL3*[PayInflation]![2019],2)) AS FY2019, 
    Sum(FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2020],2)) AS FY2020, 
    Sum(FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2020]*[PayInflation]![2021],2)) AS FY2021, 
    Sum(FormatCurrency(OPM_Table.ANNUAL5*[PayInflation]![2022],2)) AS FY2022, 
    Sum(FormatCurrency(OPM_Table.ANNUAL4*[PayInflation]![2022]*[PayInflation]![2023],2)) AS FY2023, 
    Sum(FormatCurrency(OPM_Table.ANNUAL6*[PayInflation]![2024],2)) AS FY2024

Open in new window

Terry MooreAuthor Commented:
It works, but when I run the report for a GS-5, the GS-5/7/9 also shows up in the query.
Gustav BrockCIOCommented:
Then you may to include the "reverse" WHERE statement in the last query:

WHERE 
    ([Forms]![Fee Review Summary]![Combo137]='GS-5/7/9' And [GRADE2]='GS-5' Or [GRADE2]='GS-7' Or [GRADE2]='GS-9') = False

Open in new window


In any case, the WHERE and HAVING clauses are what control what is included. So, tweak these until success.
Terry MooreAuthor Commented:
I am going to work on tweaking it.  It still does not work.  GS-9 does not work at all.  It only shows GS 5/7/9.
Gustav BrockCIOCommented:
OK. It's you that have the data.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.