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

Self Join Brings Too Many Records Expand / Collapse
Author
Message
Posted Friday, March 22, 2013 10:08 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 22, 2013 9:05 AM
Points: 4, Visits: 17
I have this query to express a set of business rules.
To get the information I need, I tried joining the table on itself but that brings back many more records than are actually in the table. The table only has about 1700 records in it but I'm getting these results:

rep_id	Single Practitioner
00718264U 8836
00729486S 96
00726313T 75
01078482M 75
01005121V 71
00603023J 68


Below is the query I've tried. What am I doing wrong?

SELECT DISTINCT a.rep_id, count(*) AS 'Single Practitioner'
FROM SE_Violation_Detection a inner join SE_Violation_Detection b
ON a.rep_id = b.rep_id and a.hcp_cid = b.hcp_cid
group by a.rep_id, a.rep_name
having count(*) >= 2
ORDER BY count(*) DESC

Post #1434366
Posted Friday, March 22, 2013 10:27 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 20,857, Visits: 32,877
javib (3/22/2013)
I have this query to express a set of business rules.
To get the information I need, I tried joining the table on itself but that brings back many more records than are actually in the table. The table only has about 1700 records in it but I'm getting these results:

rep_id	            rep_name	     Single Practitioner
00718264U BONNIE COLLINS 8836
00729486S ROBERT YACHINI 96
01078482M SHABNAM ELYASZADEH 75
00726313T SHELLY PRATHER 75
01005121V STACI HARTMAN 71
00603023J NURIA SWABY 68


Below is the query I've tried. What am I doing wrong?

SELECT DISTINCT a.rep_id, a.rep_name, count(*) AS 'Single Practitioner'
FROM SE_Violation_Detection a inner join SE_Violation_Detection b
ON a.rep_id = b.rep_id and a.hcp_cid = b.hcp_cid
group by a.rep_id, a.rep_name
having count(*) >= 2
ORDER BY count(*) DESC



Based on the data displayed above, what are you expecting to be returned?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1434381
Posted Friday, March 22, 2013 10:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:28 PM
Points: 5,485, Visits: 10,323
Table DDL, sample data and expected results would help us to help you. In the absence of that, what does this return?

SELECT DISTINCT rep_id, hcp_cid
FROM SE_Violation_Detection

John
Post #1434398
Posted Friday, March 22, 2013 11:27 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 22, 2013 9:05 AM
Points: 4, Visits: 17
What I'm trying to accomplish here is to detect the instances where the same rep/hcp_cid combination appears at least twice in this violations table to flag the rep for an audit.

Table definition is as follows:
CREATE TABLE [dbo].[SE_Violation_Detection](
[Table_ID] [int] IDENTITY(1,1) NOT NULL,
[Doc_Num] [nvarchar](20) NULL,
[Call_Date] [datetime] NULL,
[Create_Date] [datetime] NULL,
[Call_Update_Date] [datetime] NULL,
[Call_Status] [nvarchar](30) NULL,
[HCP_Addr1] [nvarchar](100) NULL,
[HCP_Addr2] [nvarchar](64) NULL,
[HCP_City] [nvarchar](50) NULL,
[HCP_State] [nvarchar](2) NULL,
[HCP_Zip] [nvarchar](50) NULL,
[HCP_Country] [nvarchar](50) NULL,
[HCP_LID] [numeric](18, 0) NULL,
[Rep_Name] [nvarchar](100) NULL,
[Rep_ID] [nvarchar](9) NULL,
[Selling_Face] [nvarchar](50) NULL,
[Terr_ID] [nvarchar](50) NULL,
[Acct_CID] [numeric](18, 0) NULL,
[Acct_Name] [nvarchar](100) NULL,
[Order_Shipment_Conf_Date] [datetime] NULL,
[Ref_Num] [nvarchar](20) NULL,
[Point_Order_Number] [nvarchar](20) NULL,
[Restriction_Name] [nvarchar](100) NULL,
[Restriction_Type_Desc] [nvarchar](100) NULL,
[Violation_Dect_Date] [datetime] NULL,
[Email_Sent_Date] [datetime] NULL,
[HCP_CID] [numeric](18, 0) NULL,
[HCP_FName] [nvarchar](50) NULL,
[HCP_LName] [nvarchar](50) NULL,
[HCP_Specialty] [nvarchar](50) NULL,
[HCP_Specialty_Code] [nvarchar](10) NULL,
[HCP_Prof_Desig] [nvarchar](50) NULL,
[HCP_DEA_Number] [nvarchar](20) NULL,
[HCP_DEA_Exp_Date] [datetime] NULL,
[HCP_St_License] [nvarchar](25) NULL,
[HCP_St_License_Exp_Date] [datetime] NULL,
[S_P] [nvarchar](1) NULL,
[Prod_NDC11] [nvarchar](11) NULL,
[Prod_Description] [nvarchar](100) NULL,
[Quantity] [int] NULL,
[Quantity_Requested] [int] NULL,
[Violation_Flag] [nvarchar](1) NULL,
[Trans_DateTime] [datetime] NULL,
[Import_Datetime] [datetime] NULL CONSTRAINT [DF_SE_Violation_Detection_Import_Datetime] DEFAULT (getdate())
) ON [PRIMARY]

An extract of the table data is attached.

Based on a quick analysis of the data, Bonnie Collins has 94 instances, Robert Yachini doesn't have any, Shabnam Elyaszadeh has 9, and Shelly Prather has 3 (just to name a few).

I've also tried this other query, but all this does is bring back all reps that have more than one entry in the table. It doesn't filter the records according to the business rules.
select rep_id,rep_name,hcp_cid,hcp_fname, hcp_lname,count(*) from SE_Violation_Detection group by rep_id,rep_name,hcp_cid,hcp_fname, hcp_lname
having count(*)>1



  Post Attachments 
Book1.xls (3 views, 218.50 KB)
Post #1434436
Posted Friday, March 22, 2013 11:34 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 10:48 AM
Points: 20,857, Visits: 32,877
javib (3/22/2013)
What I'm trying to accomplish here is to detect the instances where the same rep/hcp_cid combination appears at least twice in this violations table to flag the rep for an audit.

Table definition is as follows:
CREATE TABLE [dbo].[SE_Violation_Detection](
[Table_ID] [int] IDENTITY(1,1) NOT NULL,
[Doc_Num] [nvarchar](20) NULL,
[Call_Date] [datetime] NULL,
[Create_Date] [datetime] NULL,
[Call_Update_Date] [datetime] NULL,
[Call_Status] [nvarchar](30) NULL,
[HCP_Addr1] [nvarchar](100) NULL,
[HCP_Addr2] [nvarchar](64) NULL,
[HCP_City] [nvarchar](50) NULL,
[HCP_State] [nvarchar](2) NULL,
[HCP_Zip] [nvarchar](50) NULL,
[HCP_Country] [nvarchar](50) NULL,
[HCP_LID] [numeric](18, 0) NULL,
[Rep_Name] [nvarchar](100) NULL,
[Rep_ID] [nvarchar](9) NULL,
[Selling_Face] [nvarchar](50) NULL,
[Terr_ID] [nvarchar](50) NULL,
[Acct_CID] [numeric](18, 0) NULL,
[Acct_Name] [nvarchar](100) NULL,
[Order_Shipment_Conf_Date] [datetime] NULL,
[Ref_Num] [nvarchar](20) NULL,
[Point_Order_Number] [nvarchar](20) NULL,
[Restriction_Name] [nvarchar](100) NULL,
[Restriction_Type_Desc] [nvarchar](100) NULL,
[Violation_Dect_Date] [datetime] NULL,
[Email_Sent_Date] [datetime] NULL,
[HCP_CID] [numeric](18, 0) NULL,
[HCP_FName] [nvarchar](50) NULL,
[HCP_LName] [nvarchar](50) NULL,
[HCP_Specialty] [nvarchar](50) NULL,
[HCP_Specialty_Code] [nvarchar](10) NULL,
[HCP_Prof_Desig] [nvarchar](50) NULL,
[HCP_DEA_Number] [nvarchar](20) NULL,
[HCP_DEA_Exp_Date] [datetime] NULL,
[HCP_St_License] [nvarchar](25) NULL,
[HCP_St_License_Exp_Date] [datetime] NULL,
[S_P] [nvarchar](1) NULL,
[Prod_NDC11] [nvarchar](11) NULL,
[Prod_Description] [nvarchar](100) NULL,
[Quantity] [int] NULL,
[Quantity_Requested] [int] NULL,
[Violation_Flag] [nvarchar](1) NULL,
[Trans_DateTime] [datetime] NULL,
[Import_Datetime] [datetime] NULL CONSTRAINT [DF_SE_Violation_Detection_Import_Datetime] DEFAULT (getdate())
) ON [PRIMARY]

An extract of the table data is attached.

Based on a quick analysis of the data, Bonnie Collins has 94 instances, Robert Yachini doesn't have any, Shabnam Elyaszadeh has 9, and Shelly Prather has 3 (just to name a few).


I really hope the data you just posted doesn't have any PII. Plus, having taken a quick look, not going to take the time to even use it.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1434444
Posted Friday, March 22, 2013 12:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, May 22, 2013 9:05 AM
Points: 4, Visits: 17
New extract without any PII has been posted. Sorry about that. Should have deleted that info before posting.
Post #1434458
Posted Friday, March 22, 2013 3:38 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: 2 days ago @ 5:10 PM
Points: 222, Visits: 1,027
javib (3/22/2013)
I've also tried this other query, but all this does is bring back all reps that have more than one entry in the table. It doesn't filter the records according to the business rules.
We are not psychics here, we don't know your business rules.



Alex Suprun
Post #1434534
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse