SQL query modification

Hello All,

Recently I posted a question about how to create a SQL query that will concatenate and will SUM some values of some columns of those concatenated field records. That question has been resolved but now I need to add something else to that resultant SQL query.

Below is the URL for the original post:
http://www.experts-exchange.com/questions/28697487/SQL-queries-for-Concatenate-and-SUM.html

The resultant query was the following:
SELECT
      Period
    , [Period ID]
    , ConcatField
    , SUM(Balance) AS Balance
    , MAX(TotalBal) as TotalBal
FROM (
      SELECT
            Period
          , [Period ID]
          , Balance
          , CA.ConcatField
          , SUM(Balance) OVER() as TotalBal
      FROM Table1
      CROSS APPLY (
           SELECT Account + [Account suffix]
           ) as CA (ConcatField)
      WHERE [Period ID] = 1
      AND ConcatField IN ('551', '572')
) AS D
GROUP BY Period
       , [Period ID]
       , ConcatField



Which generated the following table:

Period | Period ID | ConcatField | Balance | TotalBal |
|--------|----------------|-------------------|------------|-------------|
|   2015 |              1 |               551 |          20 |           80 |
|   2015 |              1 |               572 |          60 |           80 |

The whole reason for the query was just to get the calculation of the "TotalBal" column, but as you can see the value is repeated along all records.

Is it possible to modify the query to only select one of these records? for instance only the record WHERE ConcatField = 551
LuiLui77Asked:
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.

Mike EghtebasDatabase and Application DeveloperCommented:
you can change

WHERE [Period ID] = 1
      AND ConcatField IN ('551', '572')

to

WHERE [Period ID] = 1
      AND ConcatField IN ('551')

or

WHERE [Period ID] = 1
      AND ConcatField  = '551'
F IgorDeveloperCommented:
If you plan to select any of the records , use an aggregate function on ConcatField , eg: MIN  (or MAX)
and drop it from the GROUP BY clause


SELECT
      Period
    , [Period ID]
    , MAX(ConcatField) as ConcatField
    , SUM(Balance) AS Balance
    , MAX(TotalBal) as TotalBal
FROM (
      SELECT
            Period
          , [Period ID]
          , Balance
          , CA.ConcatField
          , SUM(Balance) OVER() as TotalBal
      FROM Table1
      CROSS APPLY (
           SELECT Account + [Account suffix]
           ) as CA (ConcatField)
      WHERE [Period ID] = 1
      AND ConcatField IN ('551', '572')
) AS D
GROUP BY Period
       , [Period ID]

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
Mike EghtebasDatabase and Application DeveloperCommented:
oh , i see you want running sum like

Period | Period ID | ConcatField | Balance | TotalBal |
|--------|----------------|-------------------|------------|-------------|
|   2015 |              1 |               551 |          20 |          20 |
|   2015 |              1 |               572 |          60 |           80 |

please verify.
LuiLui77Author Commented:
Hello Experts, Thank you for your posts!

eghtebas, I was trying to change the original query in a way that it will only show me one record. I was able to accomplish this with fraigor recommendations. Thank again for helping out.
PortletPaulEE Topic AdvisorCommented:
LuiLui77

this is a very strange new item given the history of this query so far....

further unnecessary complication has been to the query
and now BOTH columns containing balance WILL BE THE SAME VALUE....
| Period | Period ID | ConcatField | Balance | TotalBal |
|--------|-----------|-------------|---------|----------|
|   2015 |         1 |         572 |      80 |       80 |

Open in new window


see: http://sqlfiddle.com/#!3/d745a/8
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
Microsoft SQL Server

From novice to tech pro — start learning today.