Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Help with SQL query Expand / Collapse
Author
Message
Posted Saturday, July 20, 2013 12:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, July 21, 2013 8:21 PM
Points: 4, Visits: 4
Hello all, I am not sure whether this is the right thread to post this or not.

Anyways, I have one table as follows:

table mastertab(
op INT,
msisdn VARCHAR (12),
imei VARCHAR (20)
);

Sample values that I have are as follows:

aa, 0191, 111222333
aa, 0191, 111222444
aa, 0192, 111222333
aa, 0192, 111222444
aa, 0192, 111222555
aa, 0193, 111222333
bb, 0171, 222222333
bb, 0171, 222222444
bb, 0172, 222222444
cc, 0152, 333222444

Now, i want the output to show as (op, imei_count) set where imei_count displays the total number of duplicate imei and groups it by op. Expected output is as follows:

op imei_count
-- ----------
aa 2
bb 1
cc 0

Any help in this regard will highly be appreciated.

Thanks,

Mehfuz
Post #1475730
Posted Saturday, July 20, 2013 12:12 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, July 21, 2013 8:21 PM
Points: 4, Visits: 4
I forgot to mention that it has a composite primary key as follows:
(msisdn, imei)
Post #1475731
Posted Saturday, July 20, 2013 1:16 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:43 AM
Points: 1,945, Visits: 2,386
First thing is your table structure is incorrect..
You have specified op as INT but you are inserting character values in that column
aa, 0191, 111222333



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1475733
Posted Saturday, July 20, 2013 1:28 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:43 AM
Points: 1,945, Visits: 2,386
you can try this-

;WITH CTE AS
(
select op,imei, DENSE_RANK() over (order by op,imei) rankno
FROM mastertab
)
select c.op, COUNT(distinct rankno) - 1 cnt
FROM CTE c
GROUP BY c.op




_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1475734
Posted Saturday, July 20, 2013 1:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, July 21, 2013 8:21 PM
Points: 4, Visits: 4
Op is also char. Sorry about that.
Post #1475735
Posted Saturday, July 20, 2013 2:42 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:43 AM
Points: 1,945, Visits: 2,386
mehfuz.khaled (7/20/2013)
Op is also char. Sorry about that.

Change the datatype of Op to CHAR and run the query that I posted... Hope it will work for you



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1475737
Posted Saturday, July 20, 2013 5:10 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:12 AM
Points: 35,589, Visits: 32,179
kapil_kk (7/20/2013)
you can try this-

;WITH CTE AS
(
select op,imei, DENSE_RANK() over (order by op,imei) rankno
FROM mastertab
)
select c.op, COUNT(distinct rankno) - 1 cnt
FROM CTE c
GROUP BY c.op



Try with this data and see what happens..

drop table mastertab

CREATE table mastertab(
op CHAR(2),
msisdn VARCHAR (12),
imei VARCHAR (20)
);
INSERT INTO mastertab
(op,msisdn,imei)
SELECT 'aa','0191','111222333' UNION ALL
--SELECT 'aa','0191','111222444' UNION ALL
SELECT 'aa','0192','111222333' UNION ALL
--SELECT 'aa','0192','111222444' UNION ALL
SELECT 'aa','0192','111222555' UNION ALL
--SELECT 'aa','0193','111222333' UNION ALL

SELECT 'aa','0193','111222334' UNION ALL --added
SELECT 'aa','0193','111222335' UNION ALL --added
SELECT 'aa','0193','111222336' UNION ALL --added
SELECT 'aa','0193','111222337' UNION ALL --added
SELECT 'aa','0193','111222338' UNION ALL --added


SELECT 'bb','0171','222222333' UNION ALL
SELECT 'bb','0171','222222444' UNION ALL
SELECT 'bb','0172','222222444' UNION ALL
SELECT 'cc','0152','333222444'

;WITH CTE AS
(
select op,imei, DENSE_RANK() over (order by op,imei) rankno
FROM mastertab
)
select c.op, COUNT(distinct rankno) - 1 cnt
FROM CTE c
GROUP BY c.op



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1475784
Posted Saturday, July 20, 2013 5:33 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:12 AM
Points: 35,589, Visits: 32,179
mehfuz.khaled (7/20/2013)
Hello all, I am not sure whether this is the right thread to post this or not.

Anyways, I have one table as follows:

table mastertab(
op INT,
msisdn VARCHAR (12),
imei VARCHAR (20)
);

Sample values that I have are as follows:

aa, 0191, 111222333
aa, 0191, 111222444
aa, 0192, 111222333
aa, 0192, 111222444
aa, 0192, 111222555
aa, 0193, 111222333
bb, 0171, 222222333
bb, 0171, 222222444
bb, 0172, 222222444
cc, 0152, 333222444

Now, i want the output to show as (op, imei_count) set where imei_count displays the total number of duplicate imei and groups it by op. Expected output is as follows:

op imei_count
-- ----------
aa 2
bb 1
cc 0

Any help in this regard will highly be appreciated.

Thanks,

Mehfuz


First, you're new here. You'll get a whole lot more coded, tested answers if you follow the simple advice offered in the first link in my signature line below.

Second, what do you want to be returned if the data is like this?

aa, 0191, 111222333
aa, 0191, 111222444
aa, 0192, 111222333
aa, 0192, 111222444
aa, 0193, 111222555
aa, 0194, 111222555
aa, 0195, 111222556
aa, 0196, 111222556



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1475788
Posted Sunday, July 21, 2013 8:25 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, July 21, 2013 8:21 PM
Points: 4, Visits: 4
Thanks Jeff for your response.
For the inputs that you have given, my expected output would be as follow:

Op imei_count
-- -----------
aa 4

Thanks and regards,

Mehfuz
Post #1475850
Posted Monday, July 22, 2013 5:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:43 AM
Points: 6,869, Visits: 14,178
SELECT op, SUM(CASE WHEN dupes > 1 THEN 1 ELSE 0 END) 
FROM (
SELECT op, imei, dupes = COUNT(*)
FROM #mastertab
GROUP BY op, imei
) d
GROUP BY op



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1475963
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse