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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 expecting two counts in single query

Author  Topic 

sateesh.sqldba
Starting Member

25 Posts

Posted - 2010-07-22 : 00:15:57
HI experts
ALTER Proc [dbo ].[ic_Proc_IBA]
(@yr int, @qtr int, @mn int, @wk int, @Zone varchar(50), @Area varchar(50), @LCT varchar(50),
@business varchar(50), @Modality varchar(50), @subMod varchar(50), @report_type varchar(50)) as
Begin
SET NOCOUNT ON



if @report_type = 'Management'

BEGIN
Create table #temp_1 (yr numeric, mn numeric, zone varchar(100), cnt numeric)
insert into #temp_1
SELECT Yr Yr2, Mn Mn2, Zone COL1,SUM(sid) from(
select Tm.Yr,Tm.Mn,RM.Zone,
count(*) sid from ic IBA Inner Join
ic_LCTMapping RM on IBA.LCT = RM.Center_Description
Inner Join
(Select distinct m.Mn_Id, m.mn, q.yr from C_Time_Wk w, C_Time_Mn m, C_Time_Qtr q


Where q.qtr_id = m.qtr_id and q.yr_id = m.yr_id and q.qtr_id = w.qtr_id and q.yr_id = w.yr_id and


w.mn_id = m.mn_id and w.yr_id = m.yr_id) Tm on IBA.Mn_Id = Tm.Mn_Id
where Tm.Mn_Id between @mn_id - 11 and @mn_id
and RM.Zone = isnull(@Zone, RM.Zone) and
RM.LCT = isnull(@LCT, RM.LCT) Group by Tm.Yr,Tm.Mn,RM.Zone )y
Group by Yr,Mn,Zone order by 3,1,2

Create table #temp_2 (yr numeric, mn numeric, zone varchar(100), cnt numeric)
insert into #temp_2
SELECT Yr Yr2, Mn Mn2, Zone COL1,SUM(sid) from(
select Tm.Yr,Tm.Mn,RM.Zone,
count(*) sid from ic_Defects IBA Inner Join
ic_LCTMapping RM on IBA.LCT = RM.Center_Description
Inner Join
(Select distinct m.Mn_Id, m.mn, q.yr from C_Time_Wk w, C_Time_Mn m, C_Time_Qtr q


Where q.qtr_id = m.qtr_id and q.yr_id = m.yr_id and q.qtr_id = w.qtr_id and q.yr_id = w.yr_id and


w.mn_id = m.mn_id and w.yr_id = m.yr_id) Tm on IBA.Mn_Id = Tm.Mn_Id
where Tm.Mn_Id between @mn_id - 11 and @mn_id
and RM.Zone = isnull(@Zone, RM.Zone) and
RM.LCT = isnull(@LCT, RM.LCT) Group by Tm.Yr,Tm.Mn,RM.Zone )y
Group by Yr,Mn,Zone order by 3,1,2

Select a.Yr, a.Mn, a.Zone, cast(((1-(b.Cnt/ a.Cnt))*100) as numeric) from #temp_1 a
left outer join #temp_2 b on a.yr = b.yr and a.mn = b.mn and a.zone = b.zone


END

SET NOCOUNT off


end




this query is working fine but we r using temp tables here with out that con't we make this

main thing we could need to group yr,mn,Zone there is only on change in two temp tables

con't we?


sateesh

Sachin.Nand

2937 Posts

Posted - 2010-07-22 : 00:21:46
Please post some sample data & expected o/p.
No on here has time to get into the logic of your code & then try to figure it out what you need.


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

sateesh.sqldba
Starting Member

25 Posts

Posted - 2010-07-22 : 00:29:02
Hi experts


i have two tables one is IC and another one is IC_defects i need count of both tables two find out count(IC)/COUNT(IC_DEFECTS) there are some other columns like mn,yr,zone to display it



quote:
Originally posted by sateesh.sqldba


ALTER Proc [dbo ].[ic_Proc_IBA]
(@yr int, @qtr int, @mn int, @wk int, @Zone varchar(50), @Area varchar(50), @LCT varchar(50),
@business varchar(50), @Modality varchar(50), @subMod varchar(50), @report_type varchar(50)) as
Begin
SET NOCOUNT ON



if @report_type = 'Management'

BEGIN
Create table #temp_1 (yr numeric, mn numeric, zone varchar(100), cnt numeric)
insert into #temp_1
SELECT Yr Yr2, Mn Mn2, Zone COL1,SUM(sid) from(
select Tm.Yr,Tm.Mn,RM.Zone,
count(*) sid from ic IBA Inner Join
ic_LCTMapping RM on IBA.LCT = RM.Center_Description
Inner Join
(Select distinct m.Mn_Id, m.mn, q.yr from C_Time_Wk w, C_Time_Mn m, C_Time_Qtr q


Where q.qtr_id = m.qtr_id and q.yr_id = m.yr_id and q.qtr_id = w.qtr_id and q.yr_id = w.yr_id and


w.mn_id = m.mn_id and w.yr_id = m.yr_id) Tm on IBA.Mn_Id = Tm.Mn_Id
where Tm.Mn_Id between @mn_id - 11 and @mn_id
and RM.Zone = isnull(@Zone, RM.Zone) and
RM.LCT = isnull(@LCT, RM.LCT) Group by Tm.Yr,Tm.Mn,RM.Zone )y
Group by Yr,Mn,Zone order by 3,1,2

Create table #temp_2 (yr numeric, mn numeric, zone varchar(100), cnt numeric)
insert into #temp_2
SELECT Yr Yr2, Mn Mn2, Zone COL1,SUM(sid) from(
select Tm.Yr,Tm.Mn,RM.Zone,
count(*) sid from ic_Defects IBA Inner Join
ic_LCTMapping RM on IBA.LCT = RM.Center_Description
Inner Join
(Select distinct m.Mn_Id, m.mn, q.yr from C_Time_Wk w, C_Time_Mn m, C_Time_Qtr q


Where q.qtr_id = m.qtr_id and q.yr_id = m.yr_id and q.qtr_id = w.qtr_id and q.yr_id = w.yr_id and


w.mn_id = m.mn_id and w.yr_id = m.yr_id) Tm on IBA.Mn_Id = Tm.Mn_Id
where Tm.Mn_Id between @mn_id - 11 and @mn_id
and RM.Zone = isnull(@Zone, RM.Zone) and
RM.LCT = isnull(@LCT, RM.LCT) Group by Tm.Yr,Tm.Mn,RM.Zone )y
Group by Yr,Mn,Zone order by 3,1,2

Select a.Yr, a.Mn, a.Zone, cast(((1-(b.Cnt/ a.Cnt))*100) as numeric) from #temp_1 a
left outer join #temp_2 b on a.yr = b.yr and a.mn = b.mn and a.zone = b.zone


END

SET NOCOUNT off


end




this query is working fine but we r using temp tables here with out that con't we make this

main thing we could need to group yr,mn,Zone there is only on change in two temp tables

con't we?


sateesh



sateesh
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-07-22 : 00:35:07
Is there any relationship between the 2 tables?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page

sateesh.sqldba
Starting Member

25 Posts

Posted - 2010-07-22 : 01:12:47
yes i have two columns zone,lct
quote:
Originally posted by Idera

Is there any relationship between the 2 tables?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH



sateesh
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-07-22 : 01:19:36
quote:
Originally posted by sateesh.sqldba

yes i have two columns zone,lct
quote:
Originally posted by Idera

Is there any relationship between the 2 tables?


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH



sateesh



select count(column from IC)/COUNT(column from IC_DEFECTS),
other columns from IC
inner join IC_defects D on D.zone=IC.zone ,D.lct=IC.lct
group by other columns in the select list


Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.

PBUH
Go to Top of Page
   

- Advertisement -