[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL 2008 MAX RECORD

Posted on 2012-09-06
19
Medium Priority
?
393 Views
Last Modified: 2012-09-06
Hello,
I'm trying to add in the where clause to get the last record (invoiceNumber).

Select  foi.invoicenumber, fo.accountnumber, fo.orderNo, fo.orderdate, fo.caller, fo.reference, fo.department, fo.pod, fo.weight, fo.baseamount, fo.weightamount, fo.waitingtimeamount, fo.packageamount, fo.vehicleamount, fo.afterhoursamount, fo.insuranceamount,fo.fuelamount, fo.DiscountAmount,fo.tax1amount, fo.subtotalamount, fo.totalamount, (Select Description from Servicetypes where Servicetypes.ServiceTypeId = fo.ServiceTypeId) AS [Service Type],
fo.ServiceTypeID
From finalizedorders fo, finalizedordersinvoices foi
where fo.orderno  = foi.orderno and Accountnumber = 9600
AND INVOICE NUMBER IS THE LAST INVOICE NUMBER
order by fo.orderNo desc

Any help is appreciated
0
Comment
Question by:W.E.B
  • 10
  • 8
19 Comments
 
LVL 10

Expert Comment

by:plummet
ID: 38371713
If there is one invoice number for each order no:

Select  top 1 foi.invoicenumber, fo.accountnumber, fo.orderNo, fo.orderdate, fo.caller, fo.reference, fo.department, fo.pod, fo.weight, fo.baseamount, fo.weightamount, fo.waitingtimeamount, fo.packageamount, fo.vehicleamount, fo.afterhoursamount, fo.insuranceamount,fo.fuelamount, fo.DiscountAmount,fo.tax1amount, fo.subtotalamount, fo.totalamount, (Select Description from Servicetypes where Servicetypes.ServiceTypeId = fo.ServiceTypeId) AS [Service Type],
fo.ServiceTypeID
From finalizedorders fo, finalizedordersinvoices foi
where fo.orderno  = foi.orderno and Accountnumber = 9600 
order by foi.invoicenumber desc

Open in new window

0
 

Author Comment

by:W.E.B
ID: 38371738
Hello plummet,
Your code will only get the last ordernumber inside that Invoice.
I have about 2000 orders under the same Invoice number.

I have few Invoice numbers for the client,
the orders I'm trying to find are the orders inside the last Invoice number (highest) only.

thanks,
0
 
LVL 10

Expert Comment

by:plummet
ID: 38371756
Aha - try this then
Select  foi.invoicenumber, fo.accountnumber, fo.orderNo, fo.orderdate, fo.caller, fo.reference, fo.department, fo.pod, fo.weight, fo.baseamount, fo.weightamount, fo.waitingtimeamount, fo.packageamount, fo.vehicleamount, fo.afterhoursamount, fo.insuranceamount,fo.fuelamount, fo.DiscountAmount,fo.tax1amount, fo.subtotalamount, fo.totalamount, (Select Description from Servicetypes where Servicetypes.ServiceTypeId = fo.ServiceTypeId) AS [Service Type],
fo.ServiceTypeID
From (select top 1 invoicenumber, orderno  from finalizedordersinvoices order by invoicenumber desc) foi 
left join finalizedorders fo
on fo.orderno  = foi.orderno 
where Accountnumber = 9600 

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:W.E.B
ID: 38371766
Hello
I get error
Msg 207, Level 16, State 1, Line 9
Invalid column name 'OrderNo'.

left join finalizedorders fo
on fo.OrderNo  = foi.OrderNo
0
 

Author Comment

by:W.E.B
ID: 38371778
TABLE FinalizedOrdersInvoices has 2 columns
OrderNo, InvoiceNumber
0
 
LVL 10

Expert Comment

by:plummet
ID: 38371799
Hello - I made a mistake and then edited the SQL, so did you get the current version?

Here it is again just in case:

Select  foi.invoicenumber, fo.accountnumber, fo.orderNo, fo.orderdate, fo.caller, fo.reference, fo.department, fo.pod, fo.weight, fo.baseamount, fo.weightamount, fo.waitingtimeamount, fo.packageamount, fo.vehicleamount, fo.afterhoursamount, fo.insuranceamount,fo.fuelamount, fo.DiscountAmount,fo.tax1amount, fo.subtotalamount, fo.totalamount, (Select Description from Servicetypes where Servicetypes.ServiceTypeId = fo.ServiceTypeId) AS [Service Type],
fo.ServiceTypeID
From (select top 1 invoicenumber, orderno  from finalizedordersinvoices order by invoicenumber desc) foi 
left join finalizedorders fo
on fo.orderno  = foi.orderno 
where Accountnumber = 9600 

Open in new window

0
 

Author Comment

by:W.E.B
ID: 38371823
Hello,
No errors,
but no data is displayed. (0 row(s) affected)

I should have 2087 records.

thanks
0
 
LVL 10

Expert Comment

by:plummet
ID: 38372047
Are you sure the last invoice number is for account 9600? Maybe remove that where clause and see if you get some results?
0
 

Author Comment

by:W.E.B
ID: 38372090
Yes I do get records if I remove the
where fo.Accountnumber = 9600

I get one record, with the highest Invoice number I have in the system (for another client).

thanks,
0
 
LVL 10

Expert Comment

by:plummet
ID: 38372133
OK, I understand now, so you need to find the greatest invoice number for that one client. You have to join to finalizedorders to get it so this should hopefully work:
Select  foi.invoicenumber, fo.accountnumber, fo.orderNo, fo.orderdate, fo.caller, fo.reference, fo.department, fo.pod, fo.weight, fo.baseamount, fo.weightamount, fo.waitingtimeamount, fo.packageamount, fo.vehicleamount, fo.afterhoursamount, fo.insuranceamount,fo.fuelamount, fo.DiscountAmount,fo.tax1amount, fo.subtotalamount, fo.totalamount, (Select Description from Servicetypes where Servicetypes.ServiceTypeId = fo.ServiceTypeId) AS [Service Type],
fo.ServiceTypeID
From (select top 1 invoicenumber, orderno  
	from finalizedordersinvoices i 
	inner join finalizedorders o 
	on o.orderno= i.orderno
	where Accountnumber = 9600 
	order by invoicenumber desc) foi 
left join finalizedorders fo
on fo.orderno  = foi.orderno 
where Accountnumber = 9600 

Open in new window

0
 

Author Comment

by:W.E.B
ID: 38372155
Error,
Msg 209, Level 16, State 1, Line 7
Ambiguous column name 'OrderNo'.


left join finalizedorders fo
on fo.orderno  = foi.orderno
0
 

Author Comment

by:W.E.B
ID: 38372203
I changed orderno to I.orderno in the from..

From (select top 1 invoicenumber,i.OrderNo  from finalizedordersinvoices i inner join finalizedorders o
      on o.orderno= i.orderno
      where Accountnumber = 9600
      order by invoicenumber desc) foi

this still gives me only one orderno.

it should be 2087 orders, under the same Invoice number

.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38372222
0
 
LVL 10

Accepted Solution

by:
plummet earned 600 total points
ID: 38372296
It's difficult without data to try things against, so sorry if it's a bit of an iterative process. Here is some SQL that should extract all order numbers for the top invoice number. Hopefully

Select  foi.invoicenumber, fo.accountnumber, fo.orderNo, fo.orderdate, fo.caller, fo.reference, fo.department, fo.pod, fo.weight, fo.baseamount, fo.weightamount, fo.waitingtimeamount, fo.packageamount, fo.vehicleamount, fo.afterhoursamount, fo.insuranceamount,fo.fuelamount, fo.DiscountAmount,fo.tax1amount, fo.subtotalamount, fo.totalamount, (Select Description from Servicetypes where Servicetypes.ServiceTypeId = fo.ServiceTypeId) AS [Service Type],
fo.ServiceTypeID
From 
select orderno from finalizedordersinvoices where invoicenumber = 
	(select top 1 invoicenumber
	from finalizedordersinvoices i 
	inner join finalizedorders o 
	on o.orderno= i.orderno
	where Accountnumber = 9600 
	order by invoicenumber desc) sub1) foi 
left join finalizedorders fo
on fo.orderno  = foi.orderno 
where Accountnumber = 9600 

Open in new window

0
 

Author Comment

by:W.E.B
ID: 38372380
Hello,
I appreciate your time helping.

Msg 156, Level 15, State 1, Line 7
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near 'sub1'.

select orderno from finalizedordersinvoices where invoicenumber =
      (select top 1 invoicenumber
      from finalizedordersinvoices i
      inner join finalizedorders o
      on o.orderno= i.orderno
      where Accountnumber = 9600
      order by invoicenumber desc) sub1) foi

I added (
(select  .....

But I get error
Msg 102, Level 15, State 1, Line 13
Incorrect syntax near 'sub1'.
0
 
LVL 10

Expert Comment

by:plummet
ID: 38372454
Damn it! Sorry about that, try getting rid of the "sub1)"
0
 

Author Comment

by:W.E.B
ID: 38372583
thank you very much for your time and help,
this worked.

I just had to add the Field InvoiceNumber into the
(select InvoiceNumber, orderno from finalizedordersinvoices where ........)

thanks again.
0
 

Author Closing Comment

by:W.E.B
ID: 38372585
Excellent,
thank you.
0
 
LVL 10

Expert Comment

by:plummet
ID: 38372626
You're welcome, I was doing about 5 things at the same time and also without your data it's difficult to get it right straight away, but I'm glad it is now working!

John
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question