query ms sql

SELECT        COUNT(Receipt_Detail.Receipt_Number) AS totalcountreceipt, SUM(Receipt_Detail.Amount_Applied) AS receiptamount, Invoice_Header.Invoice_Number,?
FROM            Invoice_Detail INNER JOIN
                         Invoice_Header ON Invoice_Detail.Invoice_Number = Invoice_Header.Invoice_Number INNER JOIN
                         Member_Association ON Invoice_Header.Member_Number = Member_Association.Member_Number INNER JOIN
                         Member ON Invoice_Header.Member_Number = Member.Member_Number INNER JOIN
                         Receipt_Detail ON Invoice_Detail.Invoice_Number = Receipt_Detail.Invoice_Number INNER JOIN
                         Receipt_Header ON Member_Association.Member_Number = Receipt_Header.Member_Number AND Receipt_Detail.Receipt_Number = Receipt_Header.Receipt_Number
WHERE        (Invoice_Detail.Charge_Code = 'D' OR
                         Invoice_Detail.Charge_Code = 'T' OR
                         Invoice_Detail.Charge_Code = 'N')  (Invoice_Header.Invoice_Date BETWEEN '20181201' AND '20190131') AND (Receipt_Header.Deposit_Date BETWEEN 
                         '20181201' AND '20190131')  and (Invoice_Header.Invoice_Amount > 0)
GROUP BY Invoice_Header.Invoice_Number
ORDER BY totalcountreceipt DESC

Open in new window

Inside of "?" if I want to add like below, is it possible? Please show me how.

select top 1 (Receipt_Detail.Amount_Applied) where Receipt_Header.Deposit_Date BETWEEN '20181201' AND '20190131' order by Receipt_Header.Deposit_Date asc
LVL 1
ITsolutionWizardAsked:
Who is Participating?
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.

Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
Few suggestions below and try the modified query..
1. Always use Table alias for better readability of query..
2. Formatting makes the query more readable..
SELECT Invoice_Number, totalcountreceipt, receiptamount, Amount_Applied
FROM (
SELECT IH.Invoice_Number, COUNT(RD.Receipt_Number) OVER (PARTITION BY IH.Invoice_Number) AS totalcountreceipt, SUM(RD.Amount_Applied) OVER (PARTITION BY IH.Invoice_Number) AS receiptamount
, RD.Amount_Applied, ROW_NUMBER() OVER(PARTITION BY IH.Invoice_Number order by RH.Deposit_Date asc) rnum
FROM Invoice_Detail ID
JOIN Invoice_Header IH ON ID.Invoice_Number = IH.Invoice_Number
JOIN Member_Association MA ON IH.Member_Number = MA.Member_Number
JOIN Member M ON IH.Member_Number = M.Member_Number
JOIN Receipt_Detail RD ON ID.Invoice_Number = RD.Invoice_Number
JOIN Receipt_Header RH ON MA.Member_Number = RH.Member_Number AND RD.Receipt_Number = RH.Receipt_Number
WHERE ID.Charge_Code IN ('D', 'T', 'N') 
AND IH.Invoice_Date BETWEEN '20181201' AND '20190131'
AND RH.Deposit_Date BETWEEN '20181201' AND '20190131'
and IH.Invoice_Amount > 0
) temp
WHERE rnum = 1
ORDER BY totalcountreceipt DESC

Open in new window

ITsolutionWizardAuthor Commented:
i do not understand. where is value for select top 1 (Receipt_Detail.Amount_Applied)?
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
TOP 1 was calculated in this piece of code..
ROW_NUMBER() OVER(PARTITION BY IH.Invoice_Number order by RH.Deposit_Date asc)
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Scott PletcherSenior DBACommented:
DECLARE @start_date date
DECLARE @end_date date
SET @start_date= '20181201'
SET @end_date = '20190131'

SELECT        COUNT(Receipt_Detail.Receipt_Number) AS totalcountreceipt, SUM(Receipt_Detail.Amount_Applied) AS receiptamount, Invoice_Header.Invoice_Number,
  oa1.First_Amount_Applied
FROM            Invoice_Detail INNER JOIN
                         Invoice_Header ON Invoice_Detail.Invoice_Number = Invoice_Header.Invoice_Number INNER JOIN
                         Member_Association ON Invoice_Header.Member_Number = Member_Association.Member_Number INNER JOIN
                         Member ON Invoice_Header.Member_Number = Member.Member_Number INNER JOIN
                         Receipt_Detail ON Invoice_Detail.Invoice_Number = Receipt_Detail.Invoice_Number INNER JOIN
                         Receipt_Header ON Member_Association.Member_Number = Receipt_Header.Member_Number AND Receipt_Detail.Receipt_Number = Receipt_Header.Receipt_Number
                          OUTER APPLY (
                               SELECT TOP (1) (Receipt_Detail.Amount_Applied) AS First_Amount_Applied
                               from Receipt_Header
                               where Receipt_Header.Deposit_Date BETWEEN @start_date AND @end_date
                               order by Receipt_Header.Deposit_Date asc
                           ) AS oa1
WHERE        (Invoice_Detail.Charge_Code = 'D' OR
                         Invoice_Detail.Charge_Code = 'T' OR
                         Invoice_Detail.Charge_Code = 'N')  (Invoice_Header.Invoice_Date BETWEEN @start_date AND @end_date) AND (Receipt_Header.Deposit_Date BETWEEN
                         @start_date AND @end_date)  and (Invoice_Header.Invoice_Amount > 0)
GROUP BY Invoice_Header.Invoice_Number
ORDER BY totalcountreceipt DESC

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
ITsolutionWizardAuthor Commented:
Scott: yours does not work : Msg 8120, Level 16, State 1, Line 5
Column 'oa1.First_Amount_Applied' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Scott PletcherSenior DBACommented:
Use:
MAX(oa1.First_Amount_Applied) AS First_Amount_Applied
ITsolutionWizardAuthor Commented:
THANK. but the data result has duplicated invoice number
ITsolutionWizardAuthor Commented:
Raja Jegan R: ROW_NUMBER() OVER(PARTITION BY
what is that mean? Thanks
Raja Jegan RSQL Server DBA & Architect, EE Solution GuideCommented:
ROW_NUMBER() OVER(PARTITION BY IH.Invoice_Number order by RH.Deposit_Date asc) rnum
ROW_NUMBER() function will create a sequence of number based upon the partitioned value(in your case it is partitioned based upon Invoice Number ordered by Deposit date ascending...
Once we get the sequence no. we can filter or extract the required values in the outer SELECT statement like the below to get the TOP 1 value..
WHERE rnum = 1
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
Query Syntax

From novice to tech pro — start learning today.