Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Click here to copy the following block
--Create Table to hold sample data
Create table #Pricing
(
Product varchar(50),
Amount smallmoney
)
go
--Populate Table
insert into #Pricing values('Television', 117.00)
insert into #Pricing values('Television', 118.00)
insert into #Pricing values('Television', 117.00)
insert into #Pricing values('Television', 118.00)
insert into #Pricing values('Television', 110.00)
insert into #Pricing values('Television', 119.99)
insert into #Pricing values('Phone', 27.99)
insert into #Pricing values('Phone', 29.99)
insert into #Pricing values('Phone', 29.99)
insert into #Pricing values('Stereo', 119.99)
insert into #Pricing values('Stereo', 129.99)
go

-- Calculation of Statistical Mode
-- Most common amount for each product &
-- ties should be resolved by taking the lowest amount

-- Method #1: Standard SQL & will work in any database with ANSI join
-- support. Can be used from SQL6x onwards
SELECT p.Product , MIN( p.Amount ) AS CommonPrice
FROM (
SELECT p1.Product, p1.Amount
FROM #Pricing p1
GROUP BY p1.Product, p1.Amount
HAVING COUNT( * ) = (SELECT MAX( Cnt )
           FROM (SELECT COUNT( * ) AS Cnt
              FROM #Pricing p2
              WHERE p2.Product = p1.Product
              GROUP BY p2.Amount
           ) AS p3
           )
) AS p
GROUP BY p.Product

-- Method #2: Using TOP clause available in SQL70.
SELECT p.Product , MIN( p.Amount ) AS CommonPrice
FROM (
SELECT p1.Product, p1.Amount
FROM #Pricing p1
GROUP BY p1.Product, p1.Amount
HAVING COUNT( * ) = (SELECT TOP 1 COUNT( * )
           FROM #Pricing p2
           WHERE p2.Product = p1.Product
           GROUP BY p2.Amount
           ORDER BY COUNT( * ) DESC
           )
) AS p
GROUP BY p.Product


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.