Showing posts with label totally. Show all posts
Showing posts with label totally. Show all posts

Monday, March 26, 2012

Help finding the top 3 zipcodes within the top 5 counties

I need to show data for the top 3 zipcodes for EACH of the top 5
counties. I feel totally blocked on how to make this work properly.
Here is my code - anything you can suggest will be greatly appreciated:

SET ROWCOUNT 5
DECLARE @.tblTopMarkets TABLE (StateCD CHAR(2), CountyCD CHAR(3))
INSERT INTO @.tblTopMarkets
select
S.StateCD,
S.CountyCD
FROM DAPSummary_By_County S
WHERE S.SaleMnYear > '01/01/2004'
GROUP BY S.StateCD, S.CountyCD Order By Sum(S.Nbr_MTG) DESC
-- the above works fine but next select produces only 3 rows;
-- I need 3 times 5 rows (how to effect a "loop")
SELECT TOP 3-- zips in a county
D.StateCD,
D.CountyCD,
D.Zip,
"Nbr_Mtg"= Sum(Nbr_MTG)
FROM @.tblTopMarkets T
LEFT JOIN GovtFHADetails D
ON T.StateCD = D.StateCD AND T.CountyCD = D.CountyCD

WHERE D.SaleMnYear > '01/01/2004'
GROUP BY D.StateCD, D.CountyCD, D.Zip
Order By Sum(Nbr_MTG) DESCJJA (johna@.cbmiweb.com) writes:
> I need to show data for the top 3 zipcodes for EACH of the top 5
> counties. I feel totally blocked on how to make this work properly.
> Here is my code - anything you can suggest will be greatly appreciated:
> SET ROWCOUNT 5
> DECLARE @.tblTopMarkets TABLE (StateCD CHAR(2), CountyCD CHAR(3))
> INSERT INTO @.tblTopMarkets
> select
> S.StateCD,
> S.CountyCD
> FROM DAPSummary_By_County S
> WHERE S.SaleMnYear > '01/01/2004'
> GROUP BY S.StateCD, S.CountyCD Order By Sum(S.Nbr_MTG) DESC
> -- the above works fine but next select produces only 3 rows;
> -- I need 3 times 5 rows (how to effect a "loop")
> SELECT TOP 3 -- zips in a county
> D.StateCD,
> D.CountyCD,
> D.Zip,
> "Nbr_Mtg" = Sum(Nbr_MTG)
> FROM @.tblTopMarkets T
> LEFT JOIN GovtFHADetails D
> ON T.StateCD = D.StateCD AND T.CountyCD = D.CountyCD
> WHERE D.SaleMnYear > '01/01/2004'
> GROUP BY D.StateCD, D.CountyCD, D.Zip
> Order By Sum(Nbr_MTG) DESC

This is a whole nicer to do in SQL 2005, where you have ranking functions,
so you can rank the rows in the query.

But now we are on SQL 2000. Being a bit tired tonight, I didn't come up
with anything better than:

SET ROWCOUNT 0 -- don't forget to reset!

CREATE TABLE #temp (ident int IDENTITY,
stateCD ...
countyCD ...
zip ...
Nbr_mtg ...)
INSERT #temp (stateCD, coutnyCD, zip, nbr_mtg)
SELECT D.StateCD, D.CountyCD, D.Zip, Sum(Nbr_MTG)
FROM @.tblTopMarkets T
LEFT JOIN GovtFHADetails D
ON T.StateCD = D.StateCD AND T.CountyCD = D.CountyCD

WHERE D.SaleMnYear > '01/01/2004'
GROUP BY D.StateCD, D.CountyCD, D.Zip
GROUP BY D.StateCD, D.CountyCD, D.Zip, Sum(Nbr_MTG) DESC

SELECT a.stateCD, a.countyCD, a.zip, a.nbr_mtg
FROM #temp a
JOIN (SELECT stateCD, countyCD, zip, ident = min(ident)
FROM #temp
GROUP BY stateCD, countyCD, zip= AS b
ON a.stateCD = b.stateCD
AND a.countyCD = b.countyCD
AND a.zip = b.zip
AND a.ident < b.ident +3
ORDER BY a.stateCD, a.countyCD, a.zip, a.nbr_mtg

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks very much for your suggestion. I couldn't get it to work as it
was but I tried to take the idea and apply it. Came up with a new
version (same objective) but I am still stuck. I cannot get it to peel
off the top 3 rows in each county. I get 393 rows in the final
resultset where I really want only 15 rows (5 counties times top 3
zipcodes in each county). I am beginning to think I need a cursor.
Here's my SQL:

SET ROWCOUNT 5
DECLARE @.tblTemp TABLE (
ident int IDENTITY,
StateCD CHAR(2),
CountyCD CHAR(3),
ZipCHAR(5),
Nbr_MtgINT)
DECLARE @.tblTopMarkets TABLE (StateCD CHAR(2), CountyCD CHAR(3))
INSERT INTO @.tblTopMarkets
SELECT S.StateCD,
S.CountyCD
FROM DAPSummary_By_County S
WHERE S.SaleMnYear > '01/01/2004'
GROUP BY S.StateCD, S.CountyCD Order By Sum(S.Nbr_MTG) DESC

SET ROWCOUNT 0
INSERT INTO @.tblTemp (StateCD, CountyCD, Zip, Nbr_Mtg)
SELECT D.StateCD,
D.CountyCD,
D.Zip,
"Nbr_Mtg"= Sum(Nbr_MTG)
FROM @.tblTopMarkets T
LEFT JOIN GovtFHADetails D
ON T.StateCD = D.StateCD AND T.CountyCD = D.CountyCD
WHERE D.SaleMnYear > '01/01/2004' AND D.NonPro IS NOT NULL
GROUP BY D.StateCD, D.CountyCD, D.Zip
Order By Sum(Nbr_MTG) DESC
DECLARE @.tblByCounty TABLE (
ident int IDENTITY,
StateCD CHAR(2),
CountyCD CHAR(3),
ZipCHAR(5),
Nbr_MtgINT,
NationalRank INT)
INSERT INTO @.tblByCounty (StateCD, CountyCD, Zip, Nbr_Mtg,
NationalRank)
SELECT A.StateCD, A.CountyCD, A.Zip, A.Nbr_Mtg, A.ident AS NationalRank
FROM @.tblTemp A -- this set ranks by biggest zipcodes WITHIN
each county
ORDER BY A.StateCD, A.CountyCD, A.Nbr_MTG DESC, A.Zip
SELECT A.StateCD, A.CountyCD, A.Zip, A.Nbr_Mtg, A.ident, A.NationalRank
FROM @.tblByCounty A -- this set ranks by biggest zipcodes WITHIN
each county
ORDER BY A.StateCD, A.CountyCD, A.Nbr_MTG DESC, A.Zip

SELECT A.ident, B.ident, A.StateCD, A.CountyCD, A.Zip, A.Nbr_Mtg,
A.NationalRank
FROM @.tblByCounty A
JOIN
(SELECT Min(X.ident) AS ident, X.StateCD, X.CountyCD, X.Zip,
X.Nbr_Mtg, X.NationalRank
FROM @.tblByCounty X
GROUP BY X.StateCD, X.CountyCD, X.Zip, X.Nbr_Mtg, X.NationalRank
) AS B
ON A.StateCD = B.StateCD
AND A.CountyCD = B.CountyCD
AND A.Zip = B.Zip
AND A.ident = B.ident
WHERE A.ident < B.ident + 3
ORDER BY A.StateCD, A.CountyCD, A.Nbr_Mtg DESC|||it would be a snap in 2005, yet it's quite doable in 2000

create table #zips(id int, region char(2), zip int, sum_sales int)
insert into #zips values(1, 'IL', 60563, 12)
insert into #zips values(2, 'IL', 60564, 13)
-- a tie deliberately
insert into #zips values(3, 'IL', 60565, 14)
insert into #zips values(4, 'IL', 60566, 14)
insert into #zips values(5, 'IL', 60567, 14)
insert into #zips values(6, 'IL', 60569, 14)

insert into #zips values(7, 'WI', 53718, 12)
insert into #zips values(8, 'WI', 53711, 1)
insert into #zips values(9, 'WI', 53712, 4)
insert into #zips values(10, 'WI', 53715, 7)
insert into #zips values(11, 'WI', 53714, 5)
insert into #zips values(12, 'WI', 53712, 3)

select * from #zips z
where (select count(*) from #zips z1 where z.region=z1.region
and ((z.sum_sales<z1.sum_sales)or(z.sum_sales=z1.sum_sales and
z.id<=z1.id))) <= 3

id region zip sum_sales
---- -- ---- ----
4 IL 60566 14
5 IL 60567 14
6 IL 60569 14
7 WI 53718 12
10 WI 53715 7
11 WI 53714 5

(6 row(s) affected)

drop table #zips|||You are brilliant! Thank you so much for your help! I adapted your
approach and example to my data and it works beautifully. Here is my
final SQL:

SET ROWCOUNT 5
DECLARE @.tblTemp TABLE (
ident int IDENTITY,
StateCD CHAR(2),
CountyCD CHAR(3),
ZipCHAR(5),
Nbr_MtgINT)
DECLARE @.tblTopMarkets TABLE (StateCD CHAR(2), CountyCD CHAR(3))
INSERT INTO @.tblTopMarkets
SELECT S.StateCD,
S.CountyCD
FROM DAPSummary_By_County S
WHERE S.SaleMnYear > '01/01/2004'
GROUP BY S.StateCD, S.CountyCD Order By Sum(S.Nbr_MTG) DESC

SET ROWCOUNT 0
INSERT INTO @.tblTemp (StateCD, CountyCD, Zip, Nbr_Mtg)
SELECT D.StateCD,
D.CountyCD,
D.Zip,
"Nbr_Mtg"= Sum(Nbr_MTG)
FROM @.tblTopMarkets T
LEFT JOIN GovtFHADetails D
ON T.StateCD = D.StateCD AND T.CountyCD = D.CountyCD
WHERE D.SaleMnYear > '01/01/2004' AND D.NonPro IS NOT NULL
GROUP BY D.StateCD, D.CountyCD, D.Zip
Order By Sum(Nbr_MTG) DESC
DECLARE @.tblByCounty TABLE (
ident int IDENTITY,
StateCD CHAR(2),
CountyCD CHAR(3),
ZipCHAR(5),
Nbr_MtgINT,
NationalRank INT)
INSERT INTO @.tblByCounty (StateCD, CountyCD, Zip, Nbr_Mtg,
NationalRank)
SELECT A.StateCD, A.CountyCD, A.Zip, A.Nbr_Mtg, A.ident AS NationalRank
FROM @.tblTemp A
ORDER BY A.StateCD, A.CountyCD, A.Nbr_MTG DESC, A.Zip

SELECT A.ident, A.StateCD, A.CountyCD, A.Zip, A.Nbr_Mtg, A.NationalRank
FROM @.tblByCounty A
WHERE
(SELECT COUNT(*)
FROM @.tblByCounty X
WHERE X.StateCD = A.StateCD AND X.CountyCD = A.CountyCD
AND
(
(A.Nbr_Mtg < X.Nbr_Mtg)
OR
( A.Nbr_Mtg = X.Nbr_Mtg AND A.ident <= X.ident)
)
) <= 3
ORDER BY A.StateCD, A.CountyCD, A.Nbr_Mtg DESC

Wednesday, March 21, 2012

Help connecting to SQL Database

Hi. I'm totally new here, and I'm not an expert when it comes to SQL, so bear with me. This is my dilemma:

I use Access 2000 to connect to my Microsoft SQL Server 2000 database. Recently, our provider switched us from a shared server to a dedicated server. The site is up and running perfectly, but I can't connect to my database via Access anymore. Once I received the new location of the database, I ran odbcad32.exe and configured the database's DSN info where appropriate. I run the "Test Data Source..." function and the tests are seemingly successful. When I open my database using Access, I'm able to log in successfully, but when I click on any one of my linked tables, I get the following error:

Could not execute query; could not find linked table.

[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid object name
'NAMEOFDATABASE.NAMEOFTABLE'. (#208)

The support team at my provider are not exactly geniuses, so who knows if and when they'll be getting back to me. In the meantime, does anyone have a clue what might be causing this? Since the site is working perfectly, I assume the connection strings are okay and the database is perfectly intact (I even checked to see that the most recent entries were live), so what gives? If there's any more info I can provide, let me know. Thanks in advance.This is a pain. You have to view the linked tables in design view. Right-click on the title bar and select properties. You can then change the connection string to what it needs to be. The connection string gets hard-coded. Isn't that nice. :)

It's usually easier to just drop the linked tables and recreate all of them.|||Thank you. I was able to open each table in design view, but when I right clicked on the title bar and selected propeterties, I wasn't able to change the connection string under Description. This is so weird to me, since I'm using the same version of Access I used to access the SQL database when it was on a different server. I'm also able to make the connection and open the database using SQL Query Tool, but that obviously doesn't help me when it comes to inputing data. How do I exactly drop the tables and recreate them? :(|||delete the linked tables in access (only the linked tables!!). this does NOT delete the real tables on your server, only the links to them.

...then relink to them in their new location.

izy

LATER: i mean delete the links to the linked tables... ie in A's database window / tables, use the DEL button on all the linked tables|||Thanks izyrider. Repulsion you are just deleting the links like he said.|||Thanks derrickleggett and izyrider for bearing with me. That was a big help and with a little experimenting I was able to delete the linked talbes in Access, relinked to them in their new location, and lo and behold: I don't get the error message anymore. I can see all my tables, but now I can't input, change or delete data. The User and Group Permissions function under Tools-->Secruity seems to be locked. Is there something I can do on my end to actually use my database now, or do I have to contact my provider to deal with permission settings?|||If it's a third-party database contact your provider.sql

Monday, March 12, 2012

Help ! Date problem !

Hi,
I am experiencing a totally unexplainable date behavior, coming out of
the blue, without warning, I don't understand anything any more.
Let me explain: I am using Dreamwaever MX 2004, ASP, VBScript, and
SQLServer 2000 as DB. I am developing since a while an application, and
I am nearly finished with the most complicated bit of it. Until this
afternoon, everything worked fine, and I had no particular problems,
neither with data nor with dates.
Today, I was adding some harmless code to retrieve some records from the
database, and suddenly, my app, and SQLServer Enterprise Manager, began
to behave VERY WEIRDLY on D A T E S.
a) in Enterprise Manager, without me doing anything, the date format
changes from Swiss (dd.mm.yyyy) to US (m/d/yyyy) !
b) Not only that, but the regional date settings is also changed to US
date !
c) a SQL Statement that was finding a row in the DB does NOT find it
anymore, because on of the attributes it uses is a date, and the format
stuff above screws up the access
d) a central simple routine of mine that converts dates from the Swiss
format dd.mm.yyyy to the SQLServer format yyyymmdd STOPS working and
gives me an error. If I then change my date regional settings back to
Swiss, the routine starts working again ! But the code in it is like this:
DToutSQL = Year(DTIn) & leadZeros(Month(DTin)) & leadZeros(Day(DTin))
where DTIn is the date in Swiss format, and leadZeros just adds a
leading zero to 1-digit month or day numbers. The input is a string
"01.03.2005", and the test IsDate(DTIn) fails !
e) no matter what I do, my SQLStatement cannot find the row that I see
in the DB, but when I copy the statement to Enterprise Manager and run
it, the DB, after a long thinking pause, finds it, but switches again
the format to US !!! Then my central routine conks out again, i.e. my
app fails !
Has ANYBODY any idea what's going on ? I'm currently scanning my machine
for viruses, but it seems clean. What can I do to sort out once and for
all all these stupid date problems, and to avoid the current situation ?
Thanks
Bernard Thouin
Zurich, Switzerland
bthouin wrote:
> Hi,
> I am experiencing a totally unexplainable date behavior, coming out of
> the blue, without warning, I don't understand anything any more.
> Let me explain: I am using Dreamwaever MX 2004, ASP, VBScript, and
> SQLServer 2000 as DB. I am developing since a while an application,
> and I am nearly finished with the most complicated bit of it. Until
> this
> afternoon, everything worked fine, and I had no particular problems,
> neither with data nor with dates.
> Today, I was adding some harmless code to retrieve some records from
> the database, and suddenly, my app, and SQLServer Enterprise Manager,
> began to behave VERY WEIRDLY on D A T E S.
> a) in Enterprise Manager, without me doing anything, the date format
> changes from Swiss (dd.mm.yyyy) to US (m/d/yyyy) !
> b) Not only that, but the regional date settings is also changed to US
> date !
> c) a SQL Statement that was finding a row in the DB does NOT find it
> anymore, because on of the attributes it uses is a date, and the
> format stuff above screws up the access
> d) a central simple routine of mine that converts dates from the Swiss
> format dd.mm.yyyy to the SQLServer format yyyymmdd STOPS working and
> gives me an error. If I then change my date regional settings back to
> Swiss, the routine starts working again ! But the code in it is like
> this: DToutSQL = Year(DTIn) & leadZeros(Month(DTin)) &
> leadZeros(Day(DTin)) where DTIn is the date in Swiss format, and
> leadZeros just adds a leading zero to 1-digit month or day numbers.
> The input is a string
> "01.03.2005", and the test IsDate(DTIn) fails !
> e) no matter what I do, my SQLStatement cannot find the row that I see
> in the DB, but when I copy the statement to Enterprise Manager and run
> it, the DB, after a long thinking pause, finds it, but switches again
> the format to US !!! Then my central routine conks out again, i.e. my
> app fails !
>
> Has ANYBODY any idea what's going on ? I'm currently scanning my
> machine for viruses, but it seems clean. What can I do to sort out
> once and for all all these stupid date problems, and to avoid the
> current situation ?
> Thanks
> Bernard Thouin
> Zurich, Switzerland
There are only two supported formats for date comparisons in SQL Server.
Either will prevent the locale from interfering with date comparisons:
yyyy-mm-ddThh:mm:ss.mmm
yyyymmdd
David Gugick
Quest Software
www.imceda.com
www.quest.com
|||Hi David,
Well, I'm using ONLY the 2nd one (yyyymmdd), and religiously througout
the app, that's what my central routine is all about, and that's why I
don't understand why suddenly everything fails.
Bernard
David Gugick wrote:
> bthouin wrote:
>
> There are only two supported formats for date comparisons in SQL Server.
> Either will prevent the locale from interfering with date comparisons:
> yyyy-mm-ddThh:mm:ss.mmm
> yyyymmdd
>
|||bthouin wrote:
> Hi David,
> Well, I'm using ONLY the 2nd one (yyyymmdd), and religiously througout
> the app, that's what my central routine is all about, and that's why I
> don't understand why suddenly everything fails.
Interpretation of character string dates is affected by the SET
DATEFORMAT setting for the connection. But this does not affect the
display format. The display format comes from your local Windows
settings (see Regional and Language Settings in Control Panel).
Regading your other issues"
c) a SQL Statement that was finding a row in the DB does NOT find it
anymore, because on of the attributes it uses is a date, and the format
stuff above screws up the access
If you are using the correct format, it will work. My guess is the
problem stems from item (d) below.
d) a central simple routine of mine that converts dates from the Swiss
format dd.mm.yyyy to the SQLServer format yyyymmdd STOPS working and
gives me an error. If I then change my date regional settings back to
Swiss, the routine starts working again ! But the code in it is like
this:
DToutSQL = Year(DTIn) & leadZeros(Month(DTin)) & leadZeros(Day(DTin))
where DTIn is the date in Swiss format, and leadZeros just adds a
leading zero to 1-digit month or day numbers. The input is a string
"01.03.2005", and the test IsDate(DTIn) fails !
The problem is that the incoming format "01.03.2005" is not in a
universal format and depending on what DATEFORMAT settings are on the
client, the date interpretation can fail (Jan 3, 2005 or March 1, 2005).
You should always use YYYYMMDD to start with or stay away from the DAY,
MONTH, YEAR functions and parse manually using SUBSTRING.
David Gugick
Quest Software
www.imceda.com
www.quest.com
|||OK, David, 3 more questions/statements:
1) Can you tell me how do I find out about the DATEFORMAT setting ?
2) >>You should always use YYYYMMDD to start with or stay away from the
DAY, MONTH, YEAR functions and parse manually using SUBSTRING<<
That sounds like a good idea, I'll do that. The problem is that I am
using a language code page id (Session.LCID = 2055) in my code, and that
shows me the dates directly in Swiss format, when I pull them out of the
DB, so I thought the Day, Month, and Year functions would work fine on
these dates, and they did until yesterday !
3) Is it "normal", that, after qerying the DB in Enterprise Manager
using "... where mydate = '20050301' ...", the Windows regional date
settings are changed (from Swiss) to US date format (m/d/yyyy), and the
Enterprise Manager displays dates also in US format ? Or is it a bug
of Entreprise Manager (Version 8.0) ?
Regards
Bernard
David Gugick wrote:
> bthouin wrote:
>
> Interpretation of character string dates is affected by the SET
> DATEFORMAT setting for the connection. But this does not affect the
> display format. The display format comes from your local Windows
> settings (see Regional and Language Settings in Control Panel).
> Regading your other issues"
> c) a SQL Statement that was finding a row in the DB does NOT find it
> anymore, because on of the attributes it uses is a date, and the format
> stuff above screws up the access
> If you are using the correct format, it will work. My guess is the
> problem stems from item (d) below.
>
> d) a central simple routine of mine that converts dates from the Swiss
> format dd.mm.yyyy to the SQLServer format yyyymmdd STOPS working and
> gives me an error. If I then change my date regional settings back to
> Swiss, the routine starts working again ! But the code in it is like this:
> DToutSQL = Year(DTIn) & leadZeros(Month(DTin)) & leadZeros(Day(DTin))
> where DTIn is the date in Swiss format, and leadZeros just adds a
> leading zero to 1-digit month or day numbers. The input is a string
> "01.03.2005", and the test IsDate(DTIn) fails !
> The problem is that the incoming format "01.03.2005" is not in a
> universal format and depending on what DATEFORMAT settings are on the
> client, the date interpretation can fail (Jan 3, 2005 or March 1, 2005).
> You should always use YYYYMMDD to start with or stay away from the DAY,
> MONTH, YEAR functions and parse manually using SUBSTRING.
>
|||bthouin wrote:[vbcol=seagreen]
> OK, David, 3 more questions/statements:
> 1) Can you tell me how do I find out about the DATEFORMAT setting ?
> 2) >>You should always use YYYYMMDD to start with or stay away from
> the DAY, MONTH, YEAR functions and parse manually using SUBSTRING<<
> That sounds like a good idea, I'll do that. The problem is that I am
> using a language code page id (Session.LCID = 2055) in my code, and
> that shows me the dates directly in Swiss format, when I pull them
> out of the DB, so I thought the Day, Month, and Year functions would
> work fine on these dates, and they did until yesterday !
> 3) Is it "normal", that, after qerying the DB in Enterprise Manager
> using "... where mydate = '20050301' ...", the Windows regional date
> settings are changed (from Swiss) to US date format (m/d/yyyy), and
> the Enterprise Manager displays dates also in US format ? Or is it
> a bug of Entreprise Manager (Version 8.0) ?
> Regards
> Bernard
> David Gugick wrote:
You can find the current value using DBCC USEROPTIONS. However, I would
not rely on the setting and would just avoid the DAY/MONTH/YEAR
functions since you are using a non-standard date format. Just parse the
string manually instead if you know the incoming format. For example,
use SUBSTRING(@.datevar, 1, 2) + SUBSTRING(@.datevar, 4, 2) + ... to
create the date.
David Gugick
Quest Software
www.imceda.com
www.quest.com
|||Hi David,
Well, I found the problem... entirely my fault. My SQL statement was
allright, but NOT the way I was executing it... A hasty copy+paste left
me with a messy query routine that had some rests of a ADODB.Command
logic in it ! So I thought I wasn't finding the rows in the DB, but that
was not really the case. So much for that.
[vbcol=seagreen]
What ? I'm just using a typical European format (just like the UK
format, but with dots instead of slashes), which in any case makes much
more sense than the insane US format, which defies any rule of the most
basic logic. At least, SQLServer has the most logical format. If only
Americans could accept that...
Anyway, thanks for your help, and sorry for the time spent.
Regards
Bernard
David Gugick wrote:
> bthouin wrote:
>
> You can find the current value using DBCC USEROPTIONS. However, I would
> not rely on the setting and would just avoid the DAY/MONTH/YEAR
> functions since you are using a non-standard date format. Just parse the
> string manually instead if you know the incoming format. For example,
> use SUBSTRING(@.datevar, 1, 2) + SUBSTRING(@.datevar, 4, 2) + ... to
> create the date.
>