Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
hayashiryo
Starting Member
46 Posts |
Posted - 2011-10-26 : 23:51:36
|
Hi all,I need to generate a report that displays a sequence of running numbers.T_Invoice1.ID2.InvoiceNumber3.Date4.CustomerNameSample DataID InvoiceNumber Date CustomerName1 1001 15-Oct-11 CompanyA2 1004 26-Oct-11 CompanyB3 1006 13-Oct-11 CompanyC4 1002 22-Oct-11 CompanyB I need to generate a report as followsSearch Criteria:a.Start Invoice Numberb.End Invoice NumberE.g. user key in 1001 to 1010Note: There is no record for 1003,1005 and 1007 to 1010Generate report should look like thisInv# Date Name1001 15-Oct-11 CompanyA1002 22-Oct-11 CompanyB1003 1004 26-Oct-11 CompanyB10051006 13-Oct-11 CompanyC1007100810091010 For those invoices that have not been keyed into the system, the report should just show the invoice number and the other fields need to be blank. (e.g. 1003, 1005, 1007-1010)I think I should use view to generate this report? Or a sql statement?Thanks in advance.Hayashi Ryo |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-10-27 : 00:19:54
|
use a number table or this to generate the series of invoice number and then LEFT JOIN to your T_Invoice table. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-27 : 02:53:22
|
here's one way of generating number table on the flyDECLARE @Start int,@End intSELECT @Start=1001,@End=1010;With Numbers (N)AS(SELECT @StartUNION ALLSELECT N+1FROM NumbersWHERE N + 1 <=@End)SELECT n.N,i.[Date],i.CustomerName AS [Name]FROM Numbers nLEFT JOIN Invoice i ON i.InvoiceNumber = n.N ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
hayashiryo
Starting Member
46 Posts |
Posted - 2011-10-27 : 02:55:29
|
| Hi khtan, do you have a sample code to do the inner join? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-27 : 02:57:37
|
quote: Originally posted by hayashiryo Hi khtan, do you have a sample code to do the inner join?
i've already given you a suggestion. try it and see if it fits your requirements------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
hayashiryo
Starting Member
46 Posts |
Posted - 2011-10-28 : 22:02:45
|
| Hi all. I got it working. Thanks for the great suggestions! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-29 : 00:58:31
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|