Solved

Get all records where there are duplicates in 2 fields in one table, returning data from multiple tables.

Posted on 2009-07-03
6
178 Views
Last Modified: 2012-05-07
Hi,

I am using MSSQL2005 and need help to write a SQL statement to achieve the following.

I have 1 tables that I want to create a recordset from.

I want return all records where there are multiple records of any given VENDOR_NO, and for each of those unique VENDOR_NO's there are multiple COST records with the same value.

I hope this is explained clearly.  



What I need is all records where the cost and vendor are the same.

I need this...

VENDOR_NO       COST

AMYS                    2.00
AMYS                    2.00
AMYS                    2.00
BRANDX                4.00
BRANDX                4.00
BRANDX                1.00
BRANDX                1.00

Not this...

VENDOR_NO       COST

AMYS                    2.00
AMYS                    2.00
AMYS                    2.00
BRANDX               2.00
BRANDX                4.00
BRANDX                4.00
BRANDX                1.00
CANDY                  1.00


This SQL statement is working well, however I ran into difficulty adding additional conditions.

Select t.Vendor_no, t.Cost
From   Table t INNER JOIN (
                            Select Vendor_no, Cost
                            From Table
                            Group by Vendor_no, Cost
                            Having COUNT(*) > 1
                          ) m
      On t.Vendor_no = m.Vendor_no and
         t.cost = m.cost


I need to get records where the conditions above are true, and also the BRAND and DEPARTMENT fields from another table are a given value to (to be determined at run time),

(Both tables have ITEM_NO field to match up items)

Thanks in advance,

Ariel
0
Comment
Question by:AD1080
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 24775332
you can create a procedure and call it like this
exec selectvendors 123, 123

create procedure selectvendors 
@yourbrand int,
@yourdepartment int
as
 
Select t.Vendor_no, t.Cost
From   Table t INNER JOIN (
                            Select Vendor_no, Cost
                            From Table
                            Group by Vendor_no, Cost
                            Having COUNT(*) > 1
                          ) m
      On t.Vendor_no = m.Vendor_no and
         t.cost = m.cost
inner join anothertable c on t.item_no = c.item_no
where t.brand = @yourbrand and
t.department = @yourdepartment

Open in new window

0
 

Author Comment

by:AD1080
ID: 24775362
Thanks for your reply,

Can you provide an example of how I would call this procedure in my client app?

I am using VB6.  VBA in Excel to be exact.  

Do I need to save this as a stored procedure in MSSQL2005, and then call it somehow in VB?

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24775579
yes, you have to create above stored procedure in SQL Server 2005 and than call it from VB
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 41

Accepted Solution

by:
ralmada earned 50 total points
ID: 24777372
Attached is an example of the VBA code. (Make sure you have Microsoft ActiveX Data Objects 2.8 Library checked in Tools->References)
This is using Windows Authentication, but I've also include the SQL authentication code commented out. Just use the one that fits your needs.
So after you create the stored procedure in SQL Server you can call it from your VBA code as indicated below.
You can take a look at this article.
http://www.sqlservercentral.com/articles/Stored+Procedures/63537/

Sub main()
 
' Create a connection object.
Dim cnn As ADODB.Connection
Dim Cmd As ADODB.Command
Dim rst As ADODB.Recordset
 
'Using Windows Authentication
 
    Set cnn = New ADODB.Connection
    cnn.Open "Provider=sqloledb;" & _
    "Data Source=yourservername;" & _
    "Initial Catalog=yourdatabasename;" & _
    "Integrated Security=SSPI;"
 
 
'Using SQL Authentication
'    Set cnn = New ADODB.Connection
'    cnn.Open "Provider=sqloledb;" & _
'    "Data Source=yourservername;" & _
'    "Initial Catalog=yourdatabasename;" & _
'    "User Id=username;" & _
'    "Password=yourpassword"
 
    'Set current sheet
    Range("A1").Activate
 
 
' Define a command object for a stored procedure
    Set Cmd = New ADODB.Command
    Set Cmd.ActiveConnection = cnn
    Set rst = New ADODB.Recordset
    
    Cmd.CommandText = "selectvendors"
 
    
    
    Cmd.Parameters.append Cmd.createparameter("yourbrand", adsmallint, adParamInput, , 123) 
    Cmd.Parameters.append Cmd.createparameter("yourdepartment", adSmallInt, adParamInput, , 123)
 
    Cmd.CommandType = adCmdStoredProc
 
    rst.Open Cmd.Execute
    
    'add headers
    For x = 0 To rst.Fields.Count - 1
        Sheet1.Cells(1, x + 1) = rst.Fields(x).Name
    Next
    
    'result of query
    Sheet1.Range("A2").CopyFromRecordset rst
    
    Set Cmd = Nothing
    cnn.Close
    Set cnn = Nothing
End Sub

Open in new window

0
 

Author Closing Comment

by:AD1080
ID: 31599700
Thanks for the detailed answer.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question