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 2000 Forums
 SQL Server Development (2000)
 Display count of all records from 3 tablesin 1row

Author  Topic 

rush2hotguy
Starting Member

3 Posts

Posted - 2005-09-07 : 00:07:11
I have Table1, Table2,Table3
[ Each of the 3 tables have the same structure : col1,col2,col3,creationdate ]

I need to write a SQL-Query or Procedure which takes a daterange(ie date1, date2) and display

---Table1---Table2--Table3
----10-------12------14

[ where 10 is total count of records in table1 where creationdate lies between date1 and date2 ][ assume both dates inclusive]
[ where 12 is total count of records in table2 where creationdate lies between date1 and date2 ]
[ where 13 is total count of records in table3 where creationdate lies between date1 and date2 ]

Do i need to write a stored procedure or SQL query to implement this.
I would appreciate if someone could help me in sovling this.
{ it might sound strange, but i need this for my report generation using SQL Server Report Services}

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2005-09-07 : 00:16:32
Use subqueries to do the job.

SELECT (SELECT COUNT(*) FROM dbo.Table1 WHERE dbo.Table1.CreationDate BETWEEN @StartDate AND @EndDate) AS Table1Count,
(SELECT COUNT(*) FROM dbo.Table2 WHERE dbo.Table2.CreationDate BETWEEN @StartDate AND @EndDate) AS Table2Count,
(SELECT COUNT(*) FROM dbo.Table3 WHERE dbo.Table3.CreationDate BETWEEN @StartDate AND @EndDate) AS Table3Count


Dustin Michaels
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-09-07 : 00:17:15
I'm stupid and reading this wrong aren't I. :)


SELECT
(SELECT COUNT(1) FROM sysobjects) AS table1,
(SELECT COUNT(1) FROM syscolumns) AS table2,
(SELECT COUNT(1) FROM syscomments) AS table3


MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -