Showing posts with label learning. Show all posts
Showing posts with label learning. Show all posts

Sunday, March 25, 2012

Do I need multiple versions of Northwind?

I am learning SQL Server 2005 Express, and want to use the Northwind data base as used in many code examples. I wanted to attach the Northwind data base via SQL Management Studio, but cannot find a Northwind.mdf file.

When I do a search using 'Northwind', I see there are already a couple of different versions of Northwind already loaded. One appears to be associated with SQL Server Mobile Edition (Samples folder) and another seems to be associated with Visual Studio 8\SDK\v2.0\Quickstart.

Can I use one of these existing Northwind databases (none have .mdf extention) for SQL Server 2005 or do I need to download yet another version?

You can either download a copy of the Northwind database here:

http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034&displaylang=en

It says it's for 2000, but it works fine for express. I use pubs a lot to this day, myself.

Buck Woody

|||

Here are links to the Northwind, Pubs, and AdventureWorks sample databases. It is worth having all three of them since many books, magazine articles, and web code is based upon them.

Databases -AdventureWorks
http://msdn2.microsoft.com/en-us/library/ms124659.aspx

Databases -Northwind and Pubs
http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46A0-8DA2-EEBC53A68034

Thursday, March 22, 2012

Do I need a Primary Key?

I am programming a site in ASP. I am used to using Access which forces you
to have a Primary Key. However, I am learning to use SQL which does not
seem to force you to have a Primary Key. Do I really need one? Please let
me know and why. Thanks!!Techniclly speaking you don't need one, but I would consider any table
without a primary key poor design.
The main reason you need one is that the primary key guarantees you'll have
a column in your table that you can use to uniquely identify each record.
Without a primary key you could potentialy end up with multiple identical
records in your table which you're not able to identify individualy using a
select, update or delete statement.
HTH
Karl Gram
http://www.gramonline.com
"michaaal" <res0gyio@.verizon.net> wrote in message
news:O6$6#0iDEHA.3784@.TK2MSFTNGP10.phx.gbl...
> I am programming a site in ASP. I am used to using Access which forces
you
> to have a Primary Key. However, I am learning to use SQL which does not
> seem to force you to have a Primary Key. Do I really need one? Please
let
> me know and why. Thanks!!
>|||Hi,
To add on to old post,
1. Enforce uniqueness for values entered in specified columns
2. Will not allow nulls.
3. If you define a primary key for a table in your database, you can relate
that table to other tables, thus reducing the need for redundant data.
This will allow you to have Parent child relation ship with out writing
code.
4. This will allow you to do Cascading (Refer boks inline)
Always for a better database modelling we should enforce Primary key /
Foregn key concept.
Thanks
Hari
MCDBA
"Karl Gram" <NOSPAMkarl@.gramonline.nl> wrote in message
news:#FrwxMkDEHA.2600@.TK2MSFTNGP09.phx.gbl...
> Techniclly speaking you don't need one, but I would consider any table
> without a primary key poor design.
> The main reason you need one is that the primary key guarantees you'll
have
> a column in your table that you can use to uniquely identify each record.
> Without a primary key you could potentialy end up with multiple identical
> records in your table which you're not able to identify individualy using
a
> select, update or delete statement.
> --
> HTH
> Karl Gram
> http://www.gramonline.com
> "michaaal" <res0gyio@.verizon.net> wrote in message
> news:O6$6#0iDEHA.3784@.TK2MSFTNGP10.phx.gbl...
> you
> let
>|||> 3. If you define a primary key for a table in your database, you can
relate
> that table to other tables, thus reducing the need for redundant data.
> This will allow you to have Parent child relation ship with out
writing
> code.
The above statement brings on another question I had... Do I really
WANT to do this type of thing on the SQL server level? Or do I
want to do this type of thing in my code. My first inclination is to do
it in the code, however, I have not really sat down and researched the
possible speed differences. Any comments on this? Thank you!|||"michaaal" <res0gyio@.verizon.net> wrote in message
news:eRB$WFlDEHA.3980@.TK2MSFTNGP09.phx.gbl...
> relate
> writing
> The above statement brings on another question I had... Do I really
> WANT to do this type of thing on the SQL server level? Or do I
> want to do this type of thing in my code. My first inclination is to do
> it in the code, however, I have not really sat down and researched the
> possible speed differences. Any comments on this? Thank you!
Enforcing constraints in the code means that they will only be enforced in
your code. If someone uses Access or similar to access your database
directly they can by-pass all your constraints and wreak havoc.
It is also (IMHO) easier to document and troubleshoot. The constraints are
there as part of your table definition. All your data-centric info is in
one place.
As for speed, if you have the contsraints in SQL Server the optimizer and
can make informed decisions on how best to optimize the queries. Otherwise
in the code you will have to decide how to join the data, which is either
going to be very complicated, or not the best method in every circumstance.
Finally, and this is more a judgement on my programming skills than yours,
constraints work pretty much the way it says on the box. If you are
hand-coding all this, then bugs can creep in, you may not foresee every
eventuality, etc.
So my vote is for data-centric rules to be in the data tier.
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.614 / Virus Database: 393 - Release Date: 05/03/2004|||No, you should always enforce constraints at the DATA level. Enforcing them
in the code means that your data can become corrupt by someone simply
bypassing your application (e.g. running an insert/update/delete from Query
Analyzer).
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"michaaal" <res0gyio@.verizon.net> wrote in message
news:eRB$WFlDEHA.3980@.TK2MSFTNGP09.phx.gbl...
> relate
> writing
> The above statement brings on another question I had... Do I really
> WANT to do this type of thing on the SQL server level? Or do I
> want to do this type of thing in my code. My first inclination is to do
> it in the code, however, I have not really sat down and researched the
> possible speed differences. Any comments on this? Thank you!
>
>|||RE/
>No, you should always enforce constraints at the DATA level. Enforcing the
m
>in the code means that your data can become corrupt by someone simply
>bypassing your application (e.g. running an insert/update/delete from Query
>Analyzer).
Do you prefer to enforce RI via triggers or the other way?
"Other way" because I don't know enough go spell it out...Converted a few MS
Access DBs and wound up with triggers - so that's all I know. MSDN Univers
al
coming soon - so I guess I'll get the option to go either way via MS Visio's
DB
design tool...
--
PeteCresswell|||No, triggers can be pretty poor for performance, depending on other
circumstances. I prefer traditional primary/foreign key relationships, then
violations are stopped in their tracks rather than after the fact.
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"(Pete Cresswell)" <x@.y.z> wrote in message
news:k2op50heohj4o6j7tkgp05o7gk61hf1eue@.
4ax.com...
> RE/
> Do you prefer to enforce RI via triggers or the other way?
> "Other way" because I don't know enough go spell it out...Converted a few
> MS
> Access DBs and wound up with triggers - so that's all I know. MSDN
> Universal
> coming soon - so I guess I'll get the option to go either way via MS
> Visio's DB
> design tool...
> --
> PeteCresswell

do i link or use different query?

first post, forum looks like it might help me keep from pulling my hair out :)

noob here to sql and crystal reports, but learning as I go. I hope I can get a bit of guidance here.

I am designing a report to show productivity of employees across our system. I have a query that I am using to extract data for each column and the query will always return 'employee' as the first column and then the data I want as the second column. (in the basic query results)

So, the first 2 columns on the report are both columns from the first query. Employee and hours worked. For the remainder of the columns, I am just insterting the 2nd column from the next queries. Sales, Trades, etc.

The problem I just ran into is that I want to display something which not every employee has a record for. For instance, I want to track how many memberships each employee sold. The way my query is written, when I add it to the report, it supressed the data for the employees that have not sold any memberships. Since it supressed the whole row, I cannot view the results from the other data. My thought is that it should keep the employee row, but list a '0' for that particular column.

Is this going to be a query re-write, or can crystal do what I need?

thanks in advance.Change your link from your employee query to your new query to a left outer join. That way all employee's will show up even if no memberships were sold.
GJ|||thanks. took me a minute to find out where to do that, but i found it.

how about getting those null values to display as a 0 (zero)

and if you can tell me where to find it this time :)

thanks again!|||With Crystal open go to file options and click the reporting tab. Check the convert database null values to default. If greyed out go to database tab and uncheck grouping on server, then you can check the null values check box.

GJ.|||awesome. you know, i try to dig around before i ask, because i just love learning new stuff. i actually got it to work a different way too by using a if else function with some searching around. im gonna try it your way too.

mind if i ask a couple more questions? hehe

i have basically created one report with 23 sub reports in the report footer.

1) is there a way to uniformly space out the subreports (vertically) if more employees show up so i dont have to edit the layout? maybe a percentage of distance away from the bottom of the report above?

2) I figured out how to alternate row color, but if my subreports have an uneven amount of rows, i get 2 rows with the same color

IF (RecordNumber MOD 2 = 0) THEN
crSilver
ELSE
DefaultAttribute

maybe the formula is wrong if i want to have subreports?

3) When using the summary feature, I cannot seem to get it accurate. I have tested a couple sum summarys and avg summaries. If I double check it against an exported excel file, the numbers don't match. Out of the 23 stores, we have several 'regions'. I was initially going to create region reports to be summarized, so that when I add them to the main report, they can see the sum or average of certain data for other regions.

well, that is pretty much all i got left to figure out on this report, so thanks so much for the help!

Monday, March 19, 2012

DMO Programming

Hello All,
I'm learning SQL-DMO programming via VBScript and I'm new to VBScript
programming as well. Would anyone know
of a News Group exclusively for DMO programming ?
Thanks,
GopiAFAIK, there are no newsgroups. However, you might find some resources here
at: http://www.sqldev.net/sqldmo.htm
Anith|||You might want to know that DMO is "dead" as of SQL Server 2005. It will be
there, but at the 2000
level (new functionality for 2005 will not be exposed in DMO). The successor
of DMO is called SMO.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"rgn" <gopinathr@.healthasyst.com> wrote in message news:uOqqXX4vFHA.2132@.TK2MSFTNGP15.phx.g
bl...
> Hello All,
> I'm learning SQL-DMO programming via VBScript and I'm new to VBScript prog
ramming as well. Would
> anyone know
> of a News Group exclusively for DMO programming ?
> Thanks,
> Gopi
>|||Hello Tibor,
I saw the SQLServer 2005 CTP CD (DVD ?) in the office today and I will look
at it tomorrow in the office (I'm at home now).
I was going through the following link
"http://www.microsoft.com/sql/2005/productinfo/sql2005features.mspx" and I
was wondering :
[1] If the CTP CD contains Express Edition also.
[2] And which of these editions will I be able to install (I mean the
Database engine and not just the Client Side Tools)
in my XP Professional Workstation at the office.
Thanks for your suggestion :)
Gopi
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:uwIZLt4vFHA.2556@.TK2MSFTNGP15.phx.gbl...
> You might want to know that DMO is "dead" as of SQL Server 2005. It will
> be there, but at the 2000 level (new functionality for 2005 will not be
> exposed in DMO). The successor of DMO is called SMO.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "rgn" <gopinathr@.healthasyst.com> wrote in message
> news:uOqqXX4vFHA.2132@.TK2MSFTNGP15.phx.gbl...
>|||Tibor,
I think I found what I was looking for with respect to the "System
Requirements" from the following link.
http://download.microsoft.com/downl...sSQLEXP2005.htm
However, I'm not sure if the CD contains the Express Edition as well
Gopi
"gopi" <rgopinath@.hotmail.com> wrote in message
news:%23XJG2f6vFHA.724@.TK2MSFTNGP14.phx.gbl...
> Hello Tibor,
> I saw the SQLServer 2005 CTP CD (DVD ?) in the office today and I will
> look at it tomorrow in the office (I'm at home now).
> I was going through the following link
> "http://www.microsoft.com/sql/2005/productinfo/sql2005features.mspx" and I
> was wondering :
> [1] If the CTP CD contains Express Edition also.
> [2] And which of these editions will I be able to install (I mean the
> Database engine and not just the Client Side Tools)
> in my XP Professional Workstation at the office.
> Thanks for your suggestion :)
> Gopi
> "Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote
> in message news:uwIZLt4vFHA.2556@.TK2MSFTNGP15.phx.gbl...
>|||> However, I'm not sure if the CD contains the Express Edition as well
I'm sorry, I don't know that, since I don't have that CD. You might want to
post this on the beta
newsgroups...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"gopi" <rgopinath@.hotmail.com> wrote in message news:eYeK4l6vFHA.3688@.tk2msftngp13.phx.gbl.
.
> Tibor,
> I think I found what I was looking for with respect to the "System Require
ments" from the
> following link.
> http://download.microsoft.com/downl...sSQLEXP2005.htm
> However, I'm not sure if the CD contains the Express Edition as well
> Gopi
> "gopi" <rgopinath@.hotmail.com> wrote in message news:%23XJG2f6vFHA.724@.TK2
MSFTNGP14.phx.gbl...
>

Sunday, March 11, 2012

Division in Query - Simple, but Lost...

Hey everyone,
I am rather new to this, but I'm making way and learning a great deal.
However, after much research I've become lost on a topic and I'm hoping
you can help me out.
I have the following query:
SELECT Title, [Date], Number
FROM (
SELECT
'OptOuts' [Title], CONVERT(varchar, DateOptedOut, 101) [Date] ,
count(contacts.id) [Number]
FROM contacts
WHERE DateOptedOut > '10/06/2005'
GROUP BY CONVERT(varchar, DateOptedOut, 101)
UNION
SELECT
'Signups' [Title], CONVERT(varchar, dateEntered, 101) [Date] ,
count(contacts.id) [Number]
FROM contacts
WHERE dateEntered > '10/06/2005'
GROUP BY CONVERT(varchar, dateEntered, 101)
)[Trend]
Order BY [Date]
This query works great in that it shows me how many people signed up
and opted out per day in one nice worksheet. However, I need for it to
do one more thing...
I am trying to get it to divide the two numbers to come up with
something called "Churn Rate" which is basically the OptOuts / Signups.
The one small other problem I'm running into is that if there are no
optouts on a certain day, it doesn't show the date and the number 0...
which would be nice.
If anyone has any ideas as to how I can get this done I would really,
really appreciate it! Thank you very much!By the way, I tried doing something like this:
SELECT ((SELECT count(contacts.id) FROM contacts WHERE WHERE
DateOptedOut > '10/06/2005')/(SELECT count(contacts.id) FROM
contacts WHERE dateEntered > '10/06/2005')) AS Percentage
But became lost when I tried to work that into the current query and
put it below the "Signup" and "Optout"... while trying to achieve this
look:
OptOuts-- Date -- Number
Signups-- Date -- Number
Churn -- Date -- Percentage
Thanks again for all your help!|||Hi, maybe something like
SELECT Title, [Date], [NumberOptOuts] + [NumberSignups] as [Number],
case when [NumberSignups] <> 0 then
[NumberOptOuts] / [NumberSignups] else
0 end
as [Churn Out]
FROM (
SELECT
'OptOuts' [Title], CONVERT(varchar, DateOptedOut, 101) [Date] ,
count(contacts.id) [NumberOptOuts], 0 as [NumberSignups]
FROM contacts
WHERE DateOptedOut > '10/06/2005'
GROUP BY CONVERT(varchar, DateOptedOut, 101)
UNION
SELECT
'Signups' [Title], CONVERT(varchar, dateEntered, 101) [Date] ,
0 as [NumberOptOuts], count(contacts.id) [NumberSignups]
FROM contacts
WHERE dateEntered > '10/06/2005'
GROUP BY CONVERT(varchar, dateEntered, 101)
)[Trend]
Order BY [Date]
It looks like it might work :)
Peter|||Thanks for the attempt Peter, but for some reason the Churn Out column
just spits out zeros... :(
Any other ideas? Thanks again for everything!|||Sorry to bump this thread, but I was just curious if anyone else had
ideas...|||On 22 Nov 2005 07:54:39 -0800, andrew.tatum@.gmail.com wrote:

>Thanks for the attempt Peter, but for some reason the Churn Out column
>just spits out zeros... :(
>Any other ideas? Thanks again for everything!
Hi Andrew,
That's because both values used in the division are integers. SQL Server
will also use an integer to store the result.
SELECT 4 / 7
SELECT 4.0 / 7
or
SELECT 4 / 7.0
SELECT CAST(4 AS numeric(5,3)) / 7
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Nah, the problem is that this is union and every row has 0 either in
NumberOptOuts or NumberSignups.
I was too fast with sending the code before :)
wouldnt something like the following work instead?
Peter
SELECT Title, [Date], [NumberOptOuts] + [NumberSignups] as [Number],
case when [NumberSignups] <> 0 then
[NumberOptOuts] / [NumberSignups] else
0 end
as [Churn Out]
FROM
(
SELECT
'OptOuts' [Title], CONVERT(varchar, a.DateOptedOut, 101) [Date] ,
count(a.id) [NumberOptOuts], b.SignUpCount as [NumberSignups]
FROM contacts a,
(select count(id) SignUpCount from contacts where dateEntered > '10/06/2005'
GROUP BY CONVERT(varchar, dateEntered, 101)) b
WHERE a.DateOptedOut > '10/06/2005'
GROUP BY CONVERT(varchar, a.DateOptedOut, 101)
UNION
SELECT
'Signups' [Title], CONVERT(varchar, dateEntered, 101) [Date] ,
b.OptOutCount as [NumberOptOuts], count(contacts.id) [NumberSignups]
FROM contacts a,
(select count(id) OptOutCount from contacts where DateOptedOut >
'10/06/2005'
GROUP BY CONVERT(varchar, DateOptedOut, 101)) b
WHERE dateEntered > '10/06/2005'
GROUP BY CONVERT(varchar, dateEntered, 101)
)[Trend]
Order BY [Date]|||On Thu, 24 Nov 2005 17:15:05 -0000, Rogas69 wrote:

>Nah, the problem is that this is union and every row has 0 either in
>NumberOptOuts or NumberSignups.
>I was too fast with sending the code before :)
>wouldnt something like the following work instead?
(snip)
Hi Peter,
I had a quick look at it, but I'm afraid you are overcomplicating
things. How aboout trying this one instead:
DECLARE @.CutOffDate datetime
SET @.CutOffDate = '20050610' -- or '20051006'; you used ambiguous format
SELECT Date,
SUM(OptOut) AS NumberOptOuts,
SUM(SignUp) AS NumberSignUps,
SUM(OptOut + SignUp) AS Number,
(1.0 * SUM(OptOut)) / NULLIF(SUM(SignUp), 0) AS ChurnOut
FROM (SELECT DateOptedOut AS Date, 1 AS OptOut, 0 AS SignUp
FROM contacts
WHERE DateOptedOut > @.CutOffDate
UNION ALL
SELECT DateEntered AS Date, 0 AS OptOut, 1 AS SignUp
FROM contacts
WHERE DateEntered > @.CutOffDate) AS x
GROUP BY Date
ORDER BY Date
(untested - see www.aspfaq.com/5006 if you prefer a tested reply)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||yes Hugo, you're right. that was just first approach as close to the
original query as possible. I supposed that there could have been better way
to implement it :)
as far as date is concerned - this date format was 'derived' from the
original query, i am using always yyyymmdd format :)
peter|||On Mon, 28 Nov 2005 11:42:24 -0000, Rogas69 wrote:

>yes Hugo, you're right. that was just first approach as close to the
>original query as possible. I supposed that there could have been better wa
y
>to implement it :)
>as far as date is concerned - this date format was 'derived' from the
>original query, i am using always yyyymmdd format :)
Hi Peter,
My apologies - when I wrote my reply, I somehow had you with
Andrew (the original poster).
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)