MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old December 3rd, 2003, 10:13 AM
treydaddy treydaddy is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 1 treydaddy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL help

I have a file that looks like this...

TYPE INV_DATE
____ ________
MI 11/30/03
MI 11/30/03
MI 11/30/03
NG 11/30/03
WP 11/30/03
CN 11/30/03
MI 11/30/03
WP 11/30/03
NG 11/30/03
MI 11/30/03



I want to write a query that will allow me to get a count for the total occurance of each type within a date range. I was guessing something like this...

select
count(*) as total
count(type-'MI') as moneyinvoice
count(type='WP') as wip
count(type='CN') as cancel
from filename
where inv_date>= '11/30/2003'

I would expect the query answer to look like this:

Total moneyinvoice wip cancel
10 5 2 3




can you help me with this? Thanks!

Reply With Quote
  #2  
Old December 3rd, 2003, 01:56 PM
Zeron Zeron is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: BC, Canada
Posts: 32 Zeron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 47 sec
Reputation Power: 5
The parameter for count can only be a field name.

You could do:

DELCARE @EndDate DateTime
SET @EndDate = '11/30/2003'

SELECT
COUNT(*) as total,
(SELECT count(*) FROM filename WHERE type='MI' AND inv_date >= @EndDate) as moneyinvoice,
(SELECT count(*) FROM filename WHERE type='WP' AND inv_date >= @EndDate) as wip,
(SELECT count(*) FROM filename WHERE type='CN' AND inv_date >= @EndDate) as cancel
FROM filename
WHERE inv_date >= @EndDate

or to make it look a little neater, but do the same:

DELCARE @EndDate DateTime
SET @EndDate = '11/30/2003'

SELECT
(SELECT count(*) FROM filename WHERE AND inv_date>= @EndDate) as total,
(SELECT count(*) FROM filename WHERE type='MI' AND inv_date >= @EndDate) as moneyinvoice,
(SELECT count(*) FROM filename WHERE type='WP' AND inv_date >= @EndDate) as wip,
(SELECT count(*) FROM filename WHERE type='CN' AND inv_date >= @EndDate) as cancel

Reply With Quote
  #3  
Old December 3rd, 2003, 05:29 PM
messorian messorian is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: NY
Posts: 18 messorian User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 18 sec
Reputation Power: 0
Code:
SELECT 	COUNT(*) as Totals, 
		CASE WHEN GROUPING(Type) = 1 THEN 'Total' 
			ELSE Type 
		END as Type
	FROM dbo.FileName
	GROUP BY Type WITH ROLLUP


Quick to code and will not be limitted to only values you code for. Of course if you decide to return more complete names from the database (ie. moneyinvoice) you will want to add it into the second part of the case statement as another case statement.
-- Messorian

Last edited by messorian : December 4th, 2003 at 01:16 AM.

Reply With Quote
  #4  
Old December 4th, 2003, 10:08 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,733 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 20 h 11 m 38 sec
Reputation Power: 869
another way:
Code:
select count(*) as total
     , sum(case when type = 'MI'
                then 1 else 0 end ) as moneyinvoice
     , sum(case when type = 'WP'
                then 1 else 0 end ) as wip
     , sum(case when type = 'CN'
                then 1 else 0 end ) as cancel
  from filename
 where inv_date >= '11/30/2003'


rudy
http://r937.com/

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > SQL help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway