473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with SQL script

There must be a way to do what I want to do without using a cursor, but I can't think of it. FYI, I'm a marketing analyst and not all that good with sql.

Here's the scenario:

SITE table
Expand|Select|Wrap|Line Numbers
  1. ID  NAME          AD1          CITY          STATE  MONTHLY
  2. 1   JOHN DOE      11 MAIN ST   BIGCITY       FL     30.00
  3. 2   JANE DOE      22 MAIN ST   BIGCITY       FL     25.00
  4. 3   BOB SMITH     33 MAIN ST   BIGCITY       FL     32.00
  5. 4   JOE JONES     44 MAIN ST   SMALLTOWN     FL     27.00
  6. 5   MIKE COOPER   55 MAIN ST   BIGCITY       FL     40.00
  7. 6   MIKE COOPER   55 MAIN ST   SUBURB        FL      0.00
  8. 7   JIM ANDREWS   66 MAIN ST   BIGCITY       FL     45.00
  9. 8   JIM ANDREWS   66 MAIN ST   BIGCITY       FL      0.00
  10. 9   LISA LOCKE    77 MAIN ST   SMALLTOWN     FL      0.00
  11.  
What I'm trying to do is get a list from the SITE table, but I don't want do a select *.

Do you see ID rows 5-8? The name and address duplicate, but the city name may not (but they usually match, or one is a suburb of the other.) The main thing is that the monthly amount is always zero for one of the records.

Notice in record 9 (ID value) that a person may have one "address" row, but still have zero for the monthly.

What I need is a list that pulls all the recs in SITE, EXCEPT leaving out any with zero amounts IF AND ONLY IF there are two records and one is zero monthly and the other is GT zero.

Such a select would return to me ID rows 1,2,3,4,5,7,9. ID rows 6 and 8 would be omitted because of the duplicate name and address. Row 9 would be included in the list because the monthly is zero, yes, but there is no matching record.

Is there any way to do this without a cursor?

Any help is greatly appreciated! Thanks!
Sep 28 '11 #1
4 1730
Rabbit
12,516 Expert Mod 8TB
Duplicates are always 0? There's never a case where it's not zero?
Sep 28 '11 #2
The duplicate records ALWAYS have the same name, the same street address, but different monthly amounts. The monthly amount on the dupe record is always zero, no exception.

There could be other zero monthly amounts, but there wouldn't be duplicate name/address records for those, as shown by ID record 9 in my example table. So I can't just say, "select * from site where monthly <> 0". I have to have some kind of name and/or address comparison.
Sep 28 '11 #3
Rabbit
12,516 Expert Mod 8TB
Try something like this, you might have to tweak it a little bit.
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM [SITE]
  3. WHERE [ID] IN (
  4.     SELECT T1.[ID]
  5.     FROM [SITE] T1, [SITE] T2
  6.     WHERE T1.[NAME] = T2.[NAME]
  7.         AND T1.AD1 = T2.AD1
  8.         AND T1.MONTHLY <= T2.MONTHLY
  9.     GROUP BY T1.[ID], T1.[NAME], T1.AD1
  10.     HAVING COUNT(*) = 1
  11. )
Sep 28 '11 #4
Awesome! Thanks Rabbit!
Sep 29 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: |:::. George .::::| | last post by:
Hi, When I put the first javascript, it works fine. As soon as I put the second one, the effect stops on both and you can't see any pictures. This is the script I'm using. {How can I have this...
4
by: Throw | last post by:
G'day everyone! I would like to design typing tutor exercises for Afrikaans (and other languages possibly). This is for a GPL project. For this, I need a script that can extract words from a...
6
by: D | last post by:
Hello all...I have an issue with one of my java script functions that I'm hoping someone can easily help with. I have a web based application that we use to create/sign up for overtime. When we...
7
by: imatts | last post by:
Hi can anyone help with this little problem. I have a simple script to swap between two divs on a page. It works perfectly in Firefox & Safari & Opera. It fails in IE 6 giving Object Required error...
0
by: hrakbari | last post by:
hi vijay im looking for script to Send SMS from Meteor.ie site, but the problem is that body of text has to copy content of text file and also the reciptioner mobile is saved in a text file also....
5
by: deppeler | last post by:
Can someone look at this for me: I am trying to set up a script to edit an item in a flat file DB but I don't seem to be getting the data to the Photoedit script. It seems to be reading the 1st line...
2
by: Kesavan | last post by:
I need a function such that it returns a string holding the contents of function in other php file.., ...
9
by: dbase | last post by:
Hey guys, I'm new here. Sorry if this is not the correct forum for my question. I run a blog that gets a lot of comments from people. But often I have a problem with spammers that post images...
1
by: garethharvey | last post by:
Hello, I am new here, but read many posts over the past year or so which has helped my business. We have a dedicated server running a script to call a remote XML file using the following code: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.