1

I have two tables, arrc_PurchActivity and arrc_Voucher. The purchase activity table contains multiple records per purchase, tied together by the credit card authorization. I need to return a single row per purchase, plus pull in a field from the voucher table. If I just grab a few fields from the purchase activity table, like this:

SELECT group_concat( VoucherID ) , CcAuthCode FROM arrc_PurchaseActivity GROUP BY CcAuthCode

it works fine, returning something like this:

group_concat( VoucherID )  | CcAuthCode
=========================================
610643,611139,610642       | 8LUPDN

What I need to do is pull in another contatenated field (VoucherNbr), this time from the arrc_Voucher table, where the voucher table's VoucherID is equal to the purchase table's VoucherID. In this case, because VoucherID is a concatenation, I need to return a concatenated column of VoucherNbr for each VoucherID in the concatenated column. Clear as mud, right? What I need would look like this:

group_concat( VoucherID )  | group_concat( VoucherNbr)  |  CcAuthCode
===========================|============================|=============
610643,611139,610642       | 123,456,789                |  8LUPDN 

In other words, the VoucherNbr for VoucherID 610643 is 123, VoucherNbr for 611139 is 456, etc.

Can anyone help me out? This is way over my head...

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
EmmyS
  • 11,892
  • 48
  • 101
  • 156

1 Answers1

2

Use:

   SELECT pa.ccauthcode,
          GROUP_CONCAT(DISTINCT pa.voucherid) AS voucherids,
          GROUP_CONCAT(v.vouchernbr) AS vouchernbrs
     FROM ARRC_PURCHASEACTIVITY pa 
LEFT JOIN ARRC_VOUCHER v ON v.voucherid = pa.voucherid
 GROUP BY pa.ccauthcode

I specified the DISTINCT in the GROUP_CONCAT for the voucherid's because it's possible you'd have multiple vouchernbr's to a voucherid. If not, remove the DISTINCT.

The LEFT JOIN ensures you'll get ARRC_PURCHASEACTIVITY records that do not have supporting records in ARRC_VOUCHER. Change "LEFT JOIN" to "JOIN" if you don't want this behavior.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
  • Thank you! Up until these past few weeks, my need for sql has been fairly limited, and my experience is also fairly limited - to basic select from where-type statements. – EmmyS Sep 27 '10 at 19:19
  • can you recommend a good book or website tutorial that would help me get past the basics? I'm pretty sure you've answered a few of my other questions this last month. I've done the www.w3schools.com tutorial, but it's pretty simple. I know there are a lot of reference sites, but you kind of have to know what you're looking for to make use of a reference. – EmmyS Sep 27 '10 at 19:23
  • @EmmyS: You're welcome. Sorry, I'm not familiar with more advanced tutorials. The problem is that answers depend on the database, table design (incl. data types), data and the expected result. I'm still tweaking [my list of categorizing SQL functionality](http://stackoverflow.com/questions/2119859/questions-every-good-database-sql-developer-should-be-able-to-answer/2120339#2120339), but I'd start with focusing on problems dealing with the basics. – OMG Ponies Sep 27 '10 at 19:30
  • Thanks. That's a great thread; although I have no desire to be an actual database developer, as a web developer I do need to be able to write queries on databases provided by clients. Your list (and a few of the other posts) give me some specific things to look at. – EmmyS Sep 27 '10 at 20:35
  • @EmmyS: Come to the Dark.. er, Database Side. We have cookies =) – OMG Ponies Sep 27 '10 at 22:27