Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple quantities per zip

Author  Topic 

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 16:00:22
I need to know if there is a way to select a random set of records by zipcode... however my need is really to chain to a file of 300 zip codes with desired quantities beside each zip....can i refer to that file during an extract and pull those desire quantities...
file would be
zip quantity
45678 345
34789 190
23433 9022

etc

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 16:28:00
I don't understand what you want. What does "chain to a file of 300 zip codes with desired quantities beside each zip" mean?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 16:34:57
inner join to a seperate file where only 2 fields exist ...zip and desired quantity for that zip...So i need to extract data from a date file and the result would be only the zip codes in the inner joined file and only the amount of records that were in the desired quanitityy column....hope that makes sense
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 16:39:17
Can you import it into a staging table so that we can use T-SQL to query it?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 16:41:10
I have never used T-SQL before.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 16:59:30
T-SQL is the SQL language for Microsoft SQL Server. Are you using SQL Server or something else?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 17:00:00
SQL SERVER 2008
Go to Top of Page

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 17:00:38
Sorry ..not up on lingo yet
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 17:06:26
Is the file formatted in such a way that it can be imported easily? Such as csv, tab delimited, ...?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 17:14:21
which file? there are 2... My database of 11 million records of which i want ot extract from. The second file is the zip code file that contains 2 fields ..Zip,desired quantity.

I need to extract data from the 11 million records by zip ..but i only need the desired quanities in the second column in the second file
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 17:25:15
I'm confused by the terminology you are using. Are you referring to a table or an actual file? We need to get the terminology right in order to help you. By extra, do you mean query it or actually extract it to somewhere?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 17:34:54
both are tables ..imported into SQL
one is the actual database containing data.. and the other is the 2 fields i have been speaking about... zip code and the desired quantity my customer would like for the zip code .. there are 400 zip codes with variable quantities listed so it has a higher level of difficulty for me.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 17:41:24
Well I'll take a stab at it, but I'm still pretty confused.

SELECT TOP 100 t.ZipCode, z.Quantity, ...
FROM Table1 t
JOIN ZIP z
ON t.ZipCode = z.ZipCode
ORDER BY NEWID()

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

erichk34
Starting Member

8 Posts

Posted - 2011-06-16 : 19:46:03
Let me try again ....
I have a table with 11 million records in it with various fields. One of those fields is zip code.

I have another table that contain 400 zip codes with a desired quantity (count)

zip desired qty
98212 3,456
76332 2,111
etc....


in my file of 11 million records i have thousands of records per zip ... but i only want to extract the desired qty from the table inner joining by zip.

So in my 11 million record table... i have a count for zip code 98212 of 78,995 ,, but i only want the qty specified in the other table ..which for that zip code is 3,456.

so instead of writing 400 different select top 'INSERT QTY HERE" queries per zip code.. i was wondering if when i am inner joining by zip code i can also tell the query to bring in the desired quantity and only pull that many records.

If you dont get it now .. then i give up .. this is my first time in a forum and i thought i explained it fairly well.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-06-16 : 22:40:57
Please show us a better data example to make your problem clear. Show us sample rows from each table and what the expected output would be using those sample rows.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-17 : 07:23:35
The query I am posting below has two problems - (but having problems with queries has never before stopped me from posting, so why stop now?)

First: I am sort of kind of guessing what you are looking for.

Second: this is not going to be very efficient with 11 million rows.

;WITH CTE AS
(
SELECT
*,ROW_NUMBER() OVER (PARTITION BY zip ORDER BY (SELECT NULL)) AS RN
FROM
YourTableWith11MillionRecords
)
SELECT
c.*
FROM
CTE c
INNER JOIN DesiredQuantityTable d ON
d.zip = c.zip
WHERE
c.RN <= d.[desired qty]
Oh, another thing - which may or may not be a problem: This will work only SQL 2005 or later.
Go to Top of Page
   

- Advertisement -