472,808 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

SQL - how to - I need a query solution, no coding....

Hi. I'm looking at a problem and I can't find any solution short of
coding.

I have a pool of, say, 1000 PINS. I have 7 tables (buckets). Any of
the 1000 PINS can be in 0, 1, 2, or... or all 7 buckets. So let's say
that

-bucket A has 100 PINS
-bucket B has 300 PINS
-bucket C has 600 PINS
-bucket D has 200 PINS
-bucket E has 500 PINS
-bucket F has 350 PINS
-bucket G has 700 PINS

I need to know, for each PIN, the number of buckets (tables) it
belongs to, and which ones, i.e:
- PIN 1 belongs to A, C, D, so it belongs to 3 buckets
- PIN 2 belongs to A, C, D, F, so it belongs to 4 buckets
- PIN 3 belongs to A, so it belongs to 1 bucket
- PIN 4 belongs to A, B, C, D, G, so it belongs to 5 buckets
- PIN 5 belongs to ..., so it belongs to 0 buckets
etc, etc

What would be the simplest way to achieve that, please ?

Thank you very much
Alex.

Nov 1 '07 #1
5 1566
"Radu" <cu*************@yahoo.comwrote in message
news:11**********************@o3g2000hsb.googlegro ups.com...
Hi. I'm looking at a problem and I can't find any solution short of
coding.

I have a pool of, say, 1000 PINS. I have 7 tables (buckets). Any of
the 1000 PINS can be in 0, 1, 2, or... or all 7 buckets. So let's say
that

-bucket A has 100 PINS
-bucket B has 300 PINS
-bucket C has 600 PINS
-bucket D has 200 PINS
-bucket E has 500 PINS
-bucket F has 350 PINS
-bucket G has 700 PINS

I need to know, for each PIN, the number of buckets (tables) it
belongs to, and which ones, i.e:
- PIN 1 belongs to A, C, D, so it belongs to 3 buckets
- PIN 2 belongs to A, C, D, F, so it belongs to 4 buckets
- PIN 3 belongs to A, so it belongs to 1 bucket
- PIN 4 belongs to A, B, C, D, G, so it belongs to 5 buckets
- PIN 5 belongs to ..., so it belongs to 0 buckets
etc, etc

What would be the simplest way to achieve that, please ?

Thank you very much
Alex.
Why would you use seven tables to represent this information? There may be a
valid reason but without at least more info on keys I don't know. This looks
suspiciously like homework so rather than complete a solution for you I'll
suggest that you think about a UNION or a JOIN.

--
David Portas

Nov 1 '07 #2
SELECT PIN, COUNT(distinct Bucket)
FROM (SELECT PIN, 'A' as Bucket FROM BucketA
UNION ALL
SELECT PIN, 'B' FROM BucketB
UNION ALL
SELECT PIN, 'C' FROM BucketC
UNION ALL
SELECT PIN, 'D' FROM BucketD
UNION ALL
SELECT PIN, 'E' FROM BucketE
UNION ALL
SELECT PIN, 'F' FROM BucketF
UNION ALL
SELECT PIN, 'G' FROM BucketG)
GROUP BY PIN

If a PIN can only appear once in each bucket then you could do without
the DISTINCT in the COUNT. In fact you could do without the Bucket
column all together in that case and simply use COUNT(*). But this
should work regardless of whether PIN is unique with a bucket.

Roy Harvey
Beacon Falls, CT

On Thu, 01 Nov 2007 21:00:37 -0000, Radu <cu*************@yahoo.com>
wrote:
>Hi. I'm looking at a problem and I can't find any solution short of
coding.

I have a pool of, say, 1000 PINS. I have 7 tables (buckets). Any of
the 1000 PINS can be in 0, 1, 2, or... or all 7 buckets. So let's say
that

-bucket A has 100 PINS
-bucket B has 300 PINS
-bucket C has 600 PINS
-bucket D has 200 PINS
-bucket E has 500 PINS
-bucket F has 350 PINS
-bucket G has 700 PINS

I need to know, for each PIN, the number of buckets (tables) it
belongs to, and which ones, i.e:
- PIN 1 belongs to A, C, D, so it belongs to 3 buckets
- PIN 2 belongs to A, C, D, F, so it belongs to 4 buckets
- PIN 3 belongs to A, so it belongs to 1 bucket
- PIN 4 belongs to A, B, C, D, G, so it belongs to 5 buckets
- PIN 5 belongs to ..., so it belongs to 0 buckets
etc, etc

What would be the simplest way to achieve that, please ?

Thank you very much
Alex.
Nov 2 '07 #3
>What would be the simplest way to achieve that, please ? <<

CREATE TABLE Buckets
(bucket_name CHAR(1) NOT NULL
CHECK (bucket_name IN ('A','B','C','D','E','F','G')),
pin_nbr INTEGER NOT NULL
CHECK(pin_nbr BETWEEN 1 AND 1000),
PRIMARY KEY (bucket_name, pin_nbr));

SELECT pin_nbr, COUNT(*) AS bucket_cnt
FROM Buckets
GROUP BY pin_nbr;
Nov 2 '07 #4
Thank you all for spending the time answering. Problem solved, and I
have learned something.

Thanks again ! Alex.

Nov 2 '07 #5
As an aside, if you want to be sure that each pin is in one and only
one bucket, then add an overlapping UNIQUE constraint.

CREATE TABLE Buckets
(bucket_name CHAR(1) NOT NULL
CHECK (bucket_name IN ('A','B','C','D','E','F','G')),
pin_nbr INTEGER NOT NULL UNIQUE
CHECK(pin_nbr BETWEEN 1 AND 1000),
PRIMARY KEY (bucket_name, pin_nbr));
Nov 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: SuffrinMick | last post by:
Hello - I'm a newbie to coding! I'm working on an access 2000 database which has three tables: tblContacts - A list of customer contacts. tblOrgTypes - A list of organisational types....
4
by: Luisa Lopes | last post by:
Dear colleagues: I read somewhere that you could obtain the results of a query as a form. I have design a database for my books and waht I would like is to obtain the results of my queries...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
6
by: Andi Plotsky | last post by:
Hi - I sent this request last week, but didn't get an answer to my problem - let's try again: In my A2K database, I have a form with 2 Unbound Comboboxes, both of which open up forms to a...
1
by: Anna | last post by:
I have a simple DataGrid that displays a list of characteristics and allows you to edit a description for each characteristic. The query that feeds this involves a join, as the characteristics and...
4
by: sqlservernewbie | last post by:
Hello, I'm trying to something that just works in Oracle, but does not in Sql Server. How can I get the percentage of two counts to be returned on each row of the query? select count(sid),...
3
by: Skip | last post by:
OK, I'm a novice in JS but have lots of coding experience. I am trying to accomplish something that would seem somewhat simple - BUT IT'S NOT. I have a basic window that calls another window...
4
by: aherraiz | last post by:
Hi, I've worked with sql for some time now but i can't figure out this one. I'm pretty sure i've come across a very similar query before and i know there is a neat, good solution to this one, hope...
12
by: Yash | last post by:
My SQL query is like: SELECT ..... FROM tblCLAIM C INNER join tblXXX .... INNER join tblYYY ....(5 more joins) WHERE C.created_date between @start_date and @end_date AND...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.