Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


how to get this year and last year totals in tow separate columns


how to get this year and last year totals in tow separate columns

Author
Message
itortu
itortu
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 185
I have two queries that give me the total sales amount for the current year, and the last year.

SELECT SUM([Sales (LCY)]) 
FROM [$Cust_ Ledger Entry] cle
LEFT OUTER JOIN dw.dim.FiscalDate fd
ON fd.CalendarDate = cle.[Posting Date]
WHERE [Customer No_] = '10135'
AND fd.CalendarYear = '2013'


SELECT SUM([Sales (LCY)])
FROM [$Cust_ Ledger Entry] cle
LEFT OUTER JOIN dw.dim.FiscalDate fd
ON fd.CalendarDate = cle.[Posting Date]
WHERE [Customer No_] = '10135'
AND fd.CalendarYear = '2014'



I would like to learn how to be able to make this a single query and end up with two columns and their summed up totals. Like it shows on the attached image.


This is my query without the columns I need:

SELECT 
c.CustomerNumber
, c.Name
, c.ChainName
, c.PaymentTermsCode
, cle.CreditLimit AS 'CreditLimit'
, SUM(cle.Amount) AS 'Amount'
, (cle.CreditLimit - SUM(cle.Amount)) AS 'Credit Limit/ Total Sales Ratio'
, cle.DBPaydexScore
, cle.DBFinancialStressScore
, cle_b.PostingDate
, cle_b.DocumentNumber
FROM Customer c
LEFT OUTER JOIN [CustomerLedgerEntry] cle
ON c.CustomerNumber = cle.CustomerNumber
LEFT OUTER JOIN
(
SELECT TOP 1 CustomerNumber, DocumentNumber, MAX(PostDate) AS PostingDate
FROM CustomerLedgerEntry
WHERE DocumentType = 2
AND CustomerNumber = '10135'
GROUP BY CustomerNumber, DocumentNumber
ORDER BY MAX(PostDate) DESC, DocumentNumber DESC
) cle_b
ON cle.CustomerNumber = cle_b.CustomerNumber
WHERE c.CustomerNumber = '10135'
GROUP BY
c.CustomerNumber
, c.Name
, c.ChainName
, c.PaymentTermsCode
, cle.CreditLimit
, cle.DBPaydexScore
, cle.DBFinancialStressScore
, cle_b.PostingDate
, cle_b.DocumentNumber



Thank you very much for your help.
Attachments
Capture.PNG (7 views, 2.00 KB)
Sachin Nandanwar
Sachin Nandanwar
Old Hand
Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)Old Hand (397 reputation)

Group: General Forum Members
Points: 397 Visits: 2633
SELECT SUM(CASE WHEN fd.CalendarYear = '2013' THEN [Sales (LCY)]) 2013,
SUM(CASE WHEN fd.CalendarYear = '2014' THEN [Sales (LCY)])2014
FROM [$Cust_ Ledger Entry] cle
LEFT OUTER JOIN dw.dim.FiscalDate fd
ON fd.CalendarDate = cle.[Posting Date]
WHERE [Customer No_] = '10135'
AND fd.CalendarYear IN( '2013','2014')



--------------------------------------------------------------------------------------------------
I am just an another naive wannabe DBA trying to learn SQL Server
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