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 |
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-07-01 : 13:35:10
|
| My query is in belowSelect INC.RefCode, INC.Month, INC.Year From HSSPMS_Tbl_IncomeDetails AS INC , HSSPMS_TblTenantMasterBankDetails AS TBD Where IncomeType = 'Rent'And Year <= Year(getDate()) And INC.RefCode = TBD.TenantCodeMy Result is RefCode Month Year ----------------------------------280ca0 5 2008 85270b 5 2008 280ca0 7 2008 a05830 10 2007 a05830 11 2007 280ca0 7 2007 280ca0 8 2007 I need to display all the remaining months Except the above month and year, this upto display the Current Month And YearGive me the solution any one knowsRegards,Prabu R |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-01 : 13:44:25
|
Make a month table variable declare @months table(Month int)INSERT INTO @MonthsSELECT 1UNION ALLSELECT 2...UNION ALLSELECT 12Select COALESCE(INC.RefCode,'Unknown'), mnth.Month, mnth.Year From (SELECT y.Year,m.MonthFROM(SELECT DISTINCT Year FROM HSSPMS_Tbl_IncomeDetails)yCROSS JOIN @months m)mnthLEFT JOIN HSSPMS_Tbl_IncomeDetails AS INC ON INC.Year= mnth.YearAND INC.Month=mnth.MonthLEFT JOIN HSSPMS_TblTenantMasterBankDetails AS TBD ON INC.RefCode = TBD.TenantCodeWhere (IncomeType = 'Rent' OR IncomeType IS NULL)And mnth.Year <= Year(getDate()) |
 |
|
|
R.Prabu
Starting Member
33 Posts |
Posted - 2008-07-01 : 14:15:30
|
| i am asking Now the Result isMonth Year----- ----3 20074 20075 20088 200810 2008I need Month Year----- ----1 20072 20075 20076 20077 20078 20079 200710 200711 200712 20071 20082 20083 20084 20086 20087 20089 200811 200812 2008I also having the Month Table. The DDL isSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[HSSPMS_Tbl_Month]( [Id] [int] IDENTITY(1,1) NOT NULL, [Month] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_HSSPMS_Tbl_Month] PRIMARY KEY CLUSTERED ( [Id] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFRegards,Prabu R |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-01 : 14:26:03
|
quote: Originally posted by R.Prabu i am asking Now the Result isMonth Year----- ----3 20074 20075 20088 200810 2008I need Month Year----- ----1 20072 20075 20076 20077 20078 20079 200710 200711 200712 20071 20082 20083 20084 20086 20087 20089 200811 200812 2008I also having the Month Table. The DDL isSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[HSSPMS_Tbl_Month]( [Id] [int] IDENTITY(1,1) NOT NULL, [Month] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_HSSPMS_Tbl_Month] PRIMARY KEY CLUSTERED ( [Id] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFRegards,Prabu R
Do you think we can see or guess your tables? why dont you specify this in beginning? if you have a month table then use it instead of table variableSelect COALESCE(INC.RefCode,'Unknown'), mnth.Month, mnth.Year From (SELECT y.Year,m.MonthFROM(SELECT DISTINCT Year FROM HSSPMS_Tbl_IncomeDetails)yCROSS JOIN HSSPMS_Tbl_Month m)mnthLEFT JOIN HSSPMS_Tbl_IncomeDetails AS INC ON INC.Year= mnth.YearAND INC.Month=mnth.MonthLEFT JOIN HSSPMS_TblTenantMasterBankDetails AS TBD ON INC.RefCode = TBD.TenantCodeWhere (IncomeType = 'Rent' OR IncomeType IS NULL)And mnth.Year <= Year(getDate()) |
 |
|
|
|
|
|
|
|