Solved

How to search the greatest count among four temporary table.

Posted on 2011-02-16
2
286 Views
Last Modified: 2012-05-11
I have four temporary tables, they are billingaddress, postaladdress, shippingaddress, officeaddress now there are different number of rows in each table. I want to find out which of them has highest number of rows and assign the variable @x that value. how to get the highest count(*) among 4 #temporary tables?
0
Comment
Question by:ExpertKapur
2 Comments
 
LVL 8

Accepted Solution

by:
rushShah earned 125 total points
ID: 34909091
you can try something like this,


DECLARE @x INT
select @x = Count(*) from billingaddress
select @x = CASE WHEN @x > Count(*) then @x else Count(*) end from postaladdress
select @x = CASE WHEN @x > Count(*) then @x else Count(*) end from shippingaddress
select @x = CASE WHEN @x > Count(*) then @x else Count(*) end  from officeaddress

select @x

Open in new window

0
 
LVL 13

Assisted Solution

by:devlab2012
devlab2012 earned 125 total points
ID: 34909097

select max(cnt) from
(
select count(*) AS cnt from tb1
UNION ALL
select count(*) AS cnt from tb2
UNION ALL
select count(*) AS cnt from tb3
UNION ALL
select count(*) AS cnt from tb4
) #temp
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Detach & Attach 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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

815 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now