all groups > sql server programming > may 2006 >
You're in the

sql server programming

group:

Count of rows in conditional basis


Count of rows in conditional basis mukesh
5/17/2006 9:57:25 PM
sql server programming:
Hi,

I have a table with Seq_No, City, and Customers' Disposition.

CREATE TABLE CUSTOMER_DATA(
Seq_No bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,
City varchar(10),
Disposition varchar(10)
)


INSERT INTO CUSTOMER_DATA VALUES('DEL','BUSY')
INSERT INTO CUSTOMER_DATA VALUES('MUM','FREE')
INSERT INTO CUSTOMER_DATA VALUES('MUM','BUSY')
INSERT INTO CUSTOMER_DATA VALUES('CHE','FREE')
INSERT INTO CUSTOMER_DATA VALUES('MUM','FREE')
INSERT INTO CUSTOMER_DATA VALUES('CAL','BUSY')
INSERT INTO CUSTOMER_DATA VALUES('DEL','OTHER')
INSERT INTO CUSTOMER_DATA VALUES('CAL','OTHER')
INSERT INTO CUSTOMER_DATA VALUES('CHE','BUSY')
INSERT INTO CUSTOMER_DATA VALUES('CHE','FREE')

I want citywise analysis of datapoints in the following format.

City, Count_of_Total_Data_Points_City_Wise,
Count_of_Free_Disposition_Count_City_Wise

How would I get third column count?

Thanks
Mukesh
Re: Count of rows in conditional basis Omnibuzz
5/17/2006 10:35:01 PM
Thanks again for the DLL. Next time post the desired result too.
And Uri I think he needed this.. not sure though


SELECT City,
COUNT(City) as city_wise_something ,sum(case disposition when 'free' then 1
else 0 end) as count_free_blah_blah
FROM CUSTOMER_DATA
Re: Count of rows in conditional basis Omnibuzz
5/17/2006 11:10:01 PM
forgive my ignorance.. but what does OP mean..
I can make out that its something to do with the one who asks the question..
Re: Count of rows in conditional basis mukesh
5/17/2006 11:29:06 PM
Thanks,
It is working Fine.
Re: Count of rows in conditional basis Baileys
5/18/2006 12:00:00 AM
OP is short for "original poster" :)

[quoted text, click to view]
Re: Count of rows in conditional basis Uri Dimant
5/18/2006 12:00:00 AM
Yep, I think you are both right



[quoted text, click to view]

Re: Count of rows in conditional basis Uri Dimant
5/18/2006 12:00:00 AM
Thank for posting DDL
SELECT City,
COUNT(City),COUNT(Disposition)
FROM CUSTOMER_DATA
GROUP BY City

If it does not help ,please provide a desired result




[quoted text, click to view]

Re: Count of rows in conditional basis Baileys
5/18/2006 12:00:00 AM
I think the OP only wants to count the "Free"'s in the third column,
something like:

SELECT
City,
COUNT(City) AS [Total],
SUM(CASE DISPOSITION WHEN 'FREE' THEN 1 ELSE 0 END) AS [Free]
FROM
CUSTOMER_DATA
GROUP BY
City


[quoted text, click to view]
Re: Count of rows in conditional basis Mukesh
5/21/2006 7:10:05 AM
Hi,

Suppose I am adding a new table named Disposition Master.

CREATE TABLE DISPOSITION_MASTER(
Seq_No bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,
Disposition varchar(10) UNIQUE,
Status int
)

INSERT INTO DISPOSITION_MASTER VALUES('BUSY',1)
INSERT INTO DISPOSITION_MASTER VALUES('FREE',0)
INSERT INTO DISPOSITION_MASTER VALUES('OTHER',0)
INSERT INTO DISPOSITION_MASTER VALUES('CALLBACK',1)
INSERT INTO DISPOSITION_MASTER VALUES('INTERESTED',1)


Now in the third column I want count of dispositions from customer_data
where disposition name is having status 1 in Disposition_Master table.

Thanks
Mukesh


[quoted text, click to view]
Re: Count of rows in conditional basis Erland Sommarskog
5/21/2006 2:27:59 PM
Mukesh (cmukesh19@gmail.com) writes:
[quoted text, click to view]

What purpose would the Seq_No column serve?

[quoted text, click to view]

If I understand this correctly:

SELECT c.City, d.Dispotision, COUNT(DISTINCT City), COUNT(*)
FROM Customer_Data c
JOIN Disposiiotn_master d ON c.Disposition = d.Disposition
WHERE d.Status = 1
GROUP BY c.City, d.disposition


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
AddThis Social Bookmark Button