We help IT Professionals succeed at work.

SQL Group By Question

sbornstein2 asked
Last Modified: 2017-03-17
Details below and test setup.

Declare @Customer Table
	CustomerCode varchar(50),
	CustomerDate date,
	CurrencyType varchar(50),
	Identifier int
Insert into @Customer values 
('ABC', '3/15/2017', 'CAD', 1234),
('ABC', '3/15/2017', 'USD', 5678);

Declare @CustomerDetail Table 
	CustomerCode varchar(50),
	CustomerDate date,
	CurrencyType varchar(50),
	CustomerAmount int
Insert into @CustomerDetail values
('ABC', '3/15/2017', 'CAD', -100),
('ABC', '3/15/2017', 'USD', 300)

SELECT * FROM @Customer
SELECT * FROM @CustomerDetail

Open in new window

--I need to return all the columns for detail and apply the Identifier from  @Customer where the @CustomerDetail record with the > 0 or positive CurrencyType
--equals the @Customer CurrencyType all grouped by those fields across the rows so the result set would be

ABC      2017-03-15      CAD      -100 5678
ABC      2017-03-15      USD      300 5678

So the 5678 is getting applied across the group CustomerCode, CustomerDate, CurrencyType  which is the tough part.  Essentially the positive value identifier needs to get set to the other row.
Watch Question

PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

An "apply operator" could be used
FROM @CustomerDetail cd
cross apply (
    select [Identifier]
    from @Customer c
    where c.CustomerCode = cd.CustomerCode
    and  c.CurrencyType = 'USD'
    ) ca

Open in new window

|    | CustomerCode |    CustomerDate     | CurrencyType | CustomerAmount | Identifier |
|  1 | ABC          | 15.03.2017 00:00:00 | CAD          |           -100 |       5678 |
|  2 | ABC          | 15.03.2017 00:00:00 | USD          |            300 |       5678 |

Open in new window

alternatively just join to @Customer using the following conditions

inner join @Customer on c.CustomerCode = cd.CustomerCode AND c.CurrencyType = 'USD'
SELECT cd.CustomerCode, cd.CustomerDate, cd.CurrencyType, cd.CustomerAmount, c.Identifier
FROM @CustomerDetail cd
INNER JOIN @Customer c
    ON c.CustomerCode = cd.CustomerCode
    AND  c.CurrencyType = 'USD'

Open in new window


ya but how would that work where I don't know which side is positive until after I sum the amount
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
Unlock this solution and get a sample of our free trial.
(No credit card required)


Huge Help I have been struggling all day with this insanity.   Thanks so much..

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.