SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query Help


Query Help

Author
Message
mbrady5
mbrady5
SSChasing Mays
SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)

Group: General Forum Members
Points: 611 Visits: 792
Hi, I am in need of some Sum and Grouping help. The below query is returning multiple rows and I only want one returned With the open amounts Summed and 1 voucher amount representing a summary of the three below rows.

Thanks for any help you can provide.



SELECT     'SourceName' AS SourceDB
, 'NAME' AS 'SiteName'
, RTRIM(APIBH.IDVEND) AS VendorNumber
, RTRIM(APIBH.IDINVC) AS VoucherNumber
, APIBH.AMTGROSDST AS VoucherTotalDomestic
, (APIBH.AMTGROSDST * APIBH.EXCHRATEHC) AS VoucherTotalUSD
, (APIBH.AMTGROSDST - ISNULL(APTCP.AMTPAYM, 0)- APTCP.AMTERNDISC) AS OpenAmountDomestic
, ((APIBH.AMTGROSDST * APIBH.EXCHRATEHC) - ISNULL(APTCP.AMTPAYM, 0)* APIBH.EXCHRATEHC)- (APTCP.AMTERNDISC * APIBH.EXCHRATEHC) AS OpenAmountUSD
, dbo.udf_convert_int_date(APIBH.DATEBUS) AS PostedDate
, dbo.udf_convert_int_date(APTCR.DATEBUS) AS AppliedDate
, dbo.udf_convert_int_date(APIBH.DATEINVC) AS AgingDate
, dbo.udf_convert_int_date(APIBH.DATEDUE) AS DueDate
, dbo.udf_convert_int_date(APIBH.DATEINVC) AS DocumentDate
, NULL AS ReceivedDate
, CASE WHEN (APTCR.DATERMIT) = 0 THEN NULL ELSE dbo.udf_convert_int_date(APTCR.DATERMIT) END AS PaidDate
, CASE WHEN (APIBH.DATEDISC) = 0 THEN NULL ELSE dbo.udf_convert_int_date(APIBH.DATEDISC) END AS DiscountDate
, CONVERT(bigint, CASE WHEN APIBH.AMTGROSDST - ISNULL(APTCP.AMTPAYM, 0) = 0 THEN dbo.udf_convert_int_date(APTCR.DATERMIT) ELSE GETDATE() END - dbo.udf_convert_int_date(APIBH.DATEINVC)) AS AgeDays
, CONVERT(bigint, dbo.udf_convert_int_date(APIBH.DATEDUE) - CASE WHEN APIBH.AMTGROSDST - ISNULL(APTCP.AMTPAYM, 0) = 0 THEN dbo.udf_convert_int_date(APTCR.DATERMIT)
ELSE GETDATE() END) AS DueDays
, APIBH.CODECURN AS CurrencyCode
, APIBH.IDTRX, ISNULL(APTCP.AMTPAYM, 0) AS PayAmt
,APIBH.EXCHRATEHC AS EffectiveExchangeRate
FROM MyTable.APIBH AS APIBH LEFT OUTER JOIN
MyTable.APTCP AS APTCP ON APIBH.IDVEND = APTCP.IDVEND AND APIBH.IDINVC = APTCP.IDINVC INNER JOIN
MyTable.APTCR AS APTCR ON APTCP.BATCHTYPE = APTCR.BTCHTYPE AND APTCP.CNTBTCH = APTCR.CNTBTCH AND
APTCP.CNTRMIT = APTCR.CNTENTR
WHERE (1 = 1)
AND (APIBH.ERRBATCH = 0)
--AND (APIBH.FISCYR >= '2010')
AND (APIBH.IDTRX <> 32)
AND APIBH.IDINVC = '010106'


Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2193 Visits: 1119
What about the rest of the columns? Do you still want all of those? You're grouping by all the columns and some of those columns have different values so you'll always get multiple rows until you narrow it down.
mbrady5
mbrady5
SSChasing Mays
SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)SSChasing Mays (611 reputation)

Group: General Forum Members
Points: 611 Visits: 792
Yes, I do want all of those columns. I have decided that I'm going to try and handle for this in SSIS. I thought I was going crazy that I couldn't make this happen.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search