Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Thursday, March 29, 2012

Alternate row colors by group

I have a report in which the results are grouped. For example, the report
returnd 25 rows, the first 5 rows are one group (lets say all 'A's, the
second 5 rows are another group (lets say all 'B's), etc...
Can I add conditional formatting to alternate the backgroup color based on
groups?
I know I can alternate row colors using:
=iif(RowNumber(Nothing) Mod 2, "WhiteSmoke", "White")
But I want to alternate by group. Is that possible and if so, how?
Thanks,I had a similar requirement recently but could find nothing in help or on the
net. I wrote this embedded code which works for me but I'm curious if there's
a simpler way.
Add this to the report's embedded code window:
Public GroupRowCounter As Integer = 0
Function IncrementCounter() As Integer
GroupRowCounter += 1
Return GroupRowCounter
End Function
Function GetCounter() As Integer
Return GroupRowCounter
End Function
Right click the header row you want the alternate coloring in and select
Insert Row Above. In the new, topmost header row set the Hidden property to
True and add this to one of the cells:
=Code.IncrementCounter
For the header/detail/footer portions of the group that will be visible, add
this to the BackgroundColor property to alternate the row (group) colors:
=IIF(Code.GetCounter Mod 2, "WhiteSmoke", "White")
"BillTWD" wrote:
> I have a report in which the results are grouped. For example, the report
> returnd 25 rows, the first 5 rows are one group (lets say all 'A's, the
> second 5 rows are another group (lets say all 'B's), etc...
> Can I add conditional formatting to alternate the backgroup color based on
> groups?
> I know I can alternate row colors using:
> =iif(RowNumber(Nothing) Mod 2, "WhiteSmoke", "White")
> But I want to alternate by group. Is that possible and if so, how?
> Thanks,|||If you're grouping on Country, this is the code to use.
=Iif(RunningValue(Fields!Country.Value,CountDistinct, Nothing) Mod 2,
"LightGreen", "Cornsilk")
Mike Glaser
"BillTWD" wrote:
> I have a report in which the results are grouped. For example, the report
> returnd 25 rows, the first 5 rows are one group (lets say all 'A's, the
> second 5 rows are another group (lets say all 'B's), etc...
> Can I add conditional formatting to alternate the backgroup color based on
> groups?
> I know I can alternate row colors using:
> =iif(RowNumber(Nothing) Mod 2, "WhiteSmoke", "White")
> But I want to alternate by group. Is that possible and if so, how?
> Thanks,

Sunday, March 25, 2012

Alter Table with Variables

Hey Guys,
IS possible that I use the command alter table using only variables ?
ie : alter table @.tabelname add @.columname @.datatype
any example how to do that ?
(ive declared the variables)
Tks
Felipe TonioliThis thread on dynamic sql was pretty good:



The post by AjarnMark on Dec 26, 2001 has a couple good references.|||Tks bcr, i already found this topic and Im using the solution now !

Really tks for you help ..

Thursday, March 22, 2012

Alter table script

Hello,
I want to insert a new column in a data table using code (from VB.NET)
Is there a stored procedure or does any example script exists to do this.
I need to take in account any existing keys, indexes, constraints etc which
exist on the table.
Thanks
TimDo you want to:
1) Add a column to an SQL table through .NET? or
2) Add a column to a DataTable in .NET? or
3) Add a column through a DataTable in .NET and have the change persist into
the source table in SQL?
Christian Smith
"Tim Marsden" <TM@.UK.COM> wrote in message
news:eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl...
> Hello,
> I want to insert a new column in a data table using code (from VB.NET)
> Is there a stored procedure or does any example script exists to do this.
> I need to take in account any existing keys, indexes, constraints etc
which
> exist on the table.
> Thanks
> Tim
>
>|||Thanks for reply
I want to add a column to a SQL server table.
If I have to do it through a .NET datatable so be it.
Thanks
"Christian Smith" <csmith@.digex.com> wrote in message
news:uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl...
> Do you want to:
> 1) Add a column to an SQL table through .NET? or
> 2) Add a column to a DataTable in .NET? or
> 3) Add a column through a DataTable in .NET and have the change persist
into
> the source table in SQL?
> Christian Smith
> "Tim Marsden" <TM@.UK.COM> wrote in message
> news:eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl...
this.
> which
>|||You should be able to use the standard SQL DDL throught the SqlCommand
class. Adding a column should not affect any of the indexes or constraints
since they could not have possibly been defined to include a column that
does not yet exist. That might be an issue for deleting a column but I
can't imagine that it would affect an addition.
Alternately, I think that there is a way to modify the table structure of a
DataTable in .NET and have it push the change up to a SQL database if they
are linked. I tried to get it to work once before but couldn't get it and
did what I needed a different way.
Sorry that is all I got.
Christian Smith
"Tim Marsden" <TM@.UK.COM> wrote in message
news:OvTUBbX8DHA.3880@.tk2msftngp13.phx.gbl...
> Thanks for reply
> I want to add a column to a SQL server table.
> If I have to do it through a .NET datatable so be it.
> Thanks
>
> "Christian Smith" <csmith@.digex.com> wrote in message
> news:uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl...
> into
> this.
>|||Hi Tim,
I am reviewing you post and since I have not heard from you for some time,
I wonder whether you have solved you problem or you still have any
questions about that. I agree with our community member's suggestion, that
is to use the DDL of T-SQL as the following example:
use pubs
go
CREATE TABLE dbo.testaddcol1
(
id int NULL
) ON [PRIMARY]
go
ALTER TABLE dbo.testaddcol1 ADD col1 varchar(25) NULL
go
Hope this helps and I am waiting on your replay. Thanks.
Best regards
Baisong Wei
Microsoft Online Support
----
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Please reply to newsgroups only. Thanks.|||Thanks for all the suggestions.
I have discovered there is no easy way to alter a table in code, the
complexity of indexes, permissions etc is to great.
The simple functions can be performed easily with ALTER TABLE and a few
stored procedures, he complex stuff, I have left alone.
Tim
"Baisong Wei[MSFT]" <v-baiwei@.online.microsoft.com> wrote in message
news:N994H%23b9DHA.704@.cpmsftngxa07.phx.gbl...
> Hi Tim,
> I am reviewing you post and since I have not heard from you for some time,
> I wonder whether you have solved you problem or you still have any
> questions about that. I agree with our community member's suggestion, that
> is to use the DDL of T-SQL as the following example:
> use pubs
> go
> CREATE TABLE dbo.testaddcol1
> (
> id int NULL
> ) ON [PRIMARY]
> go
> ALTER TABLE dbo.testaddcol1 ADD col1 varchar(25) NULL
> go
> Hope this helps and I am waiting on your replay. Thanks.
> Best regards
> Baisong Wei
> Microsoft Online Support
> ----
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
> Please reply to newsgroups only. Thanks.
>|||Hello Tim,
As Christian has stated, you can use standard SQL DDL/DML thru SQL
SqlCommand Class.
You code should pretty much look like this..(modify the table/col
names etc as per your need )
Imports System
Imports System.Data
Imports System.Data.SqlClient
Try
Dim myConnString As String ="User ID=myUID;password=myPWD;Initial
Catalog=pubs;Data Source=mySQLServer"
Dim myAlterQuery As String = "ALTER TABLE dbo.testaddcol1 ADD col1
varchar(25) NULL"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(myAlterQuery, myConnection)
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
Thanks for using MSDN Newsgroup.
Vikrant Dalwale
Microsoft SQL Server Support Professional
Microsoft highly recommends to all of our customers that they visit
the http://www.microsoft.com/protect site and perform the three
straightforward steps listed to improve your computers security.
This posting is provided "AS IS" with no warranties, and confers no
rights.
--
>From: "Tim Marsden" <TM@.UK.COM>
>References: <eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl>
<uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl>
>Subject: Re: Alter table script
>Date: Thu, 12 Feb 2004 14:44:42 -0000
>Lines: 40
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <OvTUBbX8DHA.3880@.tk2msftngp13.phx.gbl>
>Newsgroups: microsoft.public.sqlserver.server
>NNTP-Posting-Host: host213-122-124-46.in-addr.btopenworld.com
213.122.124.46
>Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft
ngp13.phx.gbl
>Xref: cpmsftngxa07.phx.gbl microsoft.public.sqlserver.server:328897
>X-Tomcat-NG: microsoft.public.sqlserver.server
>Thanks for reply
>I want to add a column to a SQL server table.
>If I have to do it through a .NET datatable so be it.
>Thanks
>
>"Christian Smith" <csmith@.digex.com> wrote in message
>news:uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl...
persist
>into
VB.NET)
do
>this.
constraints etc
>
>

Alter table script

Hello,
I want to insert a new column in a data table using code (from VB.NET)
Is there a stored procedure or does any example script exists to do this.
I need to take in account any existing keys, indexes, constraints etc which
exist on the table.
Thanks
TimDo you want to:
1) Add a column to an SQL table through .NET? or
2) Add a column to a DataTable in .NET? or
3) Add a column through a DataTable in .NET and have the change persist into
the source table in SQL?
Christian Smith
"Tim Marsden" <TM@.UK.COM> wrote in message
news:eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl...
> Hello,
> I want to insert a new column in a data table using code (from VB.NET)
> Is there a stored procedure or does any example script exists to do this.
> I need to take in account any existing keys, indexes, constraints etc
which
> exist on the table.
> Thanks
> Tim
>
>|||Thanks for reply
I want to add a column to a SQL server table.
If I have to do it through a .NET datatable so be it.
Thanks
"Christian Smith" <csmith@.digex.com> wrote in message
news:uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl...
> Do you want to:
> 1) Add a column to an SQL table through .NET? or
> 2) Add a column to a DataTable in .NET? or
> 3) Add a column through a DataTable in .NET and have the change persist
into
> the source table in SQL?
> Christian Smith
> "Tim Marsden" <TM@.UK.COM> wrote in message
> news:eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl...
> > Hello,
> >
> > I want to insert a new column in a data table using code (from VB.NET)
> > Is there a stored procedure or does any example script exists to do
this.
> >
> > I need to take in account any existing keys, indexes, constraints etc
> which
> > exist on the table.
> >
> > Thanks
> > Tim
> >
> >
> >
>|||You should be able to use the standard SQL DDL throught the SqlCommand
class. Adding a column should not affect any of the indexes or constraints
since they could not have possibly been defined to include a column that
does not yet exist. That might be an issue for deleting a column but I
can't imagine that it would affect an addition.
Alternately, I think that there is a way to modify the table structure of a
DataTable in .NET and have it push the change up to a SQL database if they
are linked. I tried to get it to work once before but couldn't get it and
did what I needed a different way.
Sorry that is all I got. :)
Christian Smith
"Tim Marsden" <TM@.UK.COM> wrote in message
news:OvTUBbX8DHA.3880@.tk2msftngp13.phx.gbl...
> Thanks for reply
> I want to add a column to a SQL server table.
> If I have to do it through a .NET datatable so be it.
> Thanks
>
> "Christian Smith" <csmith@.digex.com> wrote in message
> news:uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl...
> > Do you want to:
> >
> > 1) Add a column to an SQL table through .NET? or
> > 2) Add a column to a DataTable in .NET? or
> > 3) Add a column through a DataTable in .NET and have the change persist
> into
> > the source table in SQL?
> >
> > Christian Smith
> >
> > "Tim Marsden" <TM@.UK.COM> wrote in message
> > news:eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl...
> > > Hello,
> > >
> > > I want to insert a new column in a data table using code (from VB.NET)
> > > Is there a stored procedure or does any example script exists to do
> this.
> > >
> > > I need to take in account any existing keys, indexes, constraints etc
> > which
> > > exist on the table.
> > >
> > > Thanks
> > > Tim
> > >
> > >
> > >
> >
> >
>|||Hi Tim,
I am reviewing you post and since I have not heard from you for some time,
I wonder whether you have solved you problem or you still have any
questions about that. I agree with our community member's suggestion, that
is to use the DDL of T-SQL as the following example:
use pubs
go
CREATE TABLE dbo.testaddcol1
(
id int NULL
) ON [PRIMARY]
go
ALTER TABLE dbo.testaddcol1 ADD col1 varchar(25) NULL
go
Hope this helps and I am waiting on your replay. Thanks.
Best regards
Baisong Wei
Microsoft Online Support
----
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
Please reply to newsgroups only. Thanks.|||Thanks for all the suggestions.
I have discovered there is no easy way to alter a table in code, the
complexity of indexes, permissions etc is to great.
The simple functions can be performed easily with ALTER TABLE and a few
stored procedures, he complex stuff, I have left alone.
Tim
"Baisong Wei[MSFT]" <v-baiwei@.online.microsoft.com> wrote in message
news:N994H%23b9DHA.704@.cpmsftngxa07.phx.gbl...
> Hi Tim,
> I am reviewing you post and since I have not heard from you for some time,
> I wonder whether you have solved you problem or you still have any
> questions about that. I agree with our community member's suggestion, that
> is to use the DDL of T-SQL as the following example:
> use pubs
> go
> CREATE TABLE dbo.testaddcol1
> (
> id int NULL
> ) ON [PRIMARY]
> go
> ALTER TABLE dbo.testaddcol1 ADD col1 varchar(25) NULL
> go
> Hope this helps and I am waiting on your replay. Thanks.
> Best regards
> Baisong Wei
> Microsoft Online Support
> ----
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
> Please reply to newsgroups only. Thanks.
>|||Hello Tim,
As Christian has stated, you can use standard SQL DDL/DML thru SQL
SqlCommand Class.
You code should pretty much look like this..(modify the table/col
names etc as per your need )
Imports System
Imports System.Data
Imports System.Data.SqlClient
Try
Dim myConnString As String ="User ID=myUID;password=myPWD;Initial
Catalog=pubs;Data Source=mySQLServer"
Dim myAlterQuery As String = "ALTER TABLE dbo.testaddcol1 ADD col1
varchar(25) NULL"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(myAlterQuery, myConnection)
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
Thanks for using MSDN Newsgroup.
Vikrant Dalwale
Microsoft SQL Server Support Professional
Microsoft highly recommends to all of our customers that they visit
the http://www.microsoft.com/protect site and perform the three
straightforward steps listed to improve your computer?s security.
This posting is provided "AS IS" with no warranties, and confers no
rights.
>From: "Tim Marsden" <TM@.UK.COM>
>References: <eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl>
<uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl>
>Subject: Re: Alter table script
>Date: Thu, 12 Feb 2004 14:44:42 -0000
>Lines: 40
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <OvTUBbX8DHA.3880@.tk2msftngp13.phx.gbl>
>Newsgroups: microsoft.public.sqlserver.server
>NNTP-Posting-Host: host213-122-124-46.in-addr.btopenworld.com
213.122.124.46
>Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft
ngp13.phx.gbl
>Xref: cpmsftngxa07.phx.gbl microsoft.public.sqlserver.server:328897
>X-Tomcat-NG: microsoft.public.sqlserver.server
>Thanks for reply
>I want to add a column to a SQL server table.
>If I have to do it through a .NET datatable so be it.
>Thanks
>
>"Christian Smith" <csmith@.digex.com> wrote in message
>news:uB3hKMX8DHA.3200@.TK2MSFTNGP09.phx.gbl...
>> Do you want to:
>> 1) Add a column to an SQL table through .NET? or
>> 2) Add a column to a DataTable in .NET? or
>> 3) Add a column through a DataTable in .NET and have the change
persist
>into
>> the source table in SQL?
>> Christian Smith
>> "Tim Marsden" <TM@.UK.COM> wrote in message
>> news:eDM073W8DHA.1428@.TK2MSFTNGP12.phx.gbl...
>> > Hello,
>> >
>> > I want to insert a new column in a data table using code (from
VB.NET)
>> > Is there a stored procedure or does any example script exists to
do
>this.
>> >
>> > I need to take in account any existing keys, indexes,
constraints etc
>> which
>> > exist on the table.
>> >
>> > Thanks
>> > Tim
>> >
>> >
>> >
>>
>
>

Sunday, March 11, 2012

alter table

Can you do more than one alter table alter column in one
statement? For example, I could not get the following to
work unless I did one column at a time:
alter table testalter
alter column f1 varchar(10) null,
f2 varchar(50) null,
f3 varchar(30) null
Joe,
No.
Dinesh
SQL Server MVP
--
SQL Server FAQ at
http://www.tkdinesh.com
"JoeMan" <anonymous@.discussions.microsoft.com> wrote in message
news:1c37201c4217c$c827f660$a101280a@.phx.gbl...
> Can you do more than one alter table alter column in one
> statement? For example, I could not get the following to
> work unless I did one column at a time:
> alter table testalter
> alter column f1 varchar(10) null,
> f2 varchar(50) null,
> f3 varchar(30) null
|||No, you can only send one command per ALTER TABLE statement (ADD, ALTER
COLUMN, DROP, DROP CONSTRAINT, etc).
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"JoeMan" <anonymous@.discussions.microsoft.com> wrote in message
news:1c37201c4217c$c827f660$a101280a@.phx.gbl...
> Can you do more than one alter table alter column in one
> statement? For example, I could not get the following to
> work unless I did one column at a time:
> alter table testalter
> alter column f1 varchar(10) null,
> f2 varchar(50) null,
> f3 varchar(30) null

alter table

Can you do more than one alter table alter column in one
statement? For example, I could not get the following to
work unless I did one column at a time:
alter table testalter
alter column f1 varchar(10) null,
f2 varchar(50) null,
f3 varchar(30) nullJoe,
No.
--
Dinesh
SQL Server MVP
--
--
SQL Server FAQ at
http://www.tkdinesh.com
"JoeMan" <anonymous@.discussions.microsoft.com> wrote in message
news:1c37201c4217c$c827f660$a101280a@.phx.gbl...
> Can you do more than one alter table alter column in one
> statement? For example, I could not get the following to
> work unless I did one column at a time:
> alter table testalter
> alter column f1 varchar(10) null,
> f2 varchar(50) null,
> f3 varchar(30) null|||No, you can only send one command per ALTER TABLE statement (ADD, ALTER
COLUMN, DROP, DROP CONSTRAINT, etc).
--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"JoeMan" <anonymous@.discussions.microsoft.com> wrote in message
news:1c37201c4217c$c827f660$a101280a@.phx.gbl...
> Can you do more than one alter table alter column in one
> statement? For example, I could not get the following to
> work unless I did one column at a time:
> alter table testalter
> alter column f1 varchar(10) null,
> f2 varchar(50) null,
> f3 varchar(30) null

alter table

Can you do more than one alter table alter column in one
statement? For example, I could not get the following to
work unless I did one column at a time:
alter table testalter
alter column f1 varchar(10) null,
f2 varchar(50) null,
f3 varchar(30) nullJoe,
No.
Dinesh
SQL Server MVP
--
--
SQL Server FAQ at
http://www.tkdinesh.com
"JoeMan" <anonymous@.discussions.microsoft.com> wrote in message
news:1c37201c4217c$c827f660$a101280a@.phx
.gbl...
> Can you do more than one alter table alter column in one
> statement? For example, I could not get the following to
> work unless I did one column at a time:
> alter table testalter
> alter column f1 varchar(10) null,
> f2 varchar(50) null,
> f3 varchar(30) null|||No, you can only send one command per ALTER TABLE statement (ADD, ALTER
COLUMN, DROP, DROP CONSTRAINT, etc).
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
"JoeMan" <anonymous@.discussions.microsoft.com> wrote in message
news:1c37201c4217c$c827f660$a101280a@.phx
.gbl...
> Can you do more than one alter table alter column in one
> statement? For example, I could not get the following to
> work unless I did one column at a time:
> alter table testalter
> alter column f1 varchar(10) null,
> f2 varchar(50) null,
> f3 varchar(30) null

Alter SP

If I issue an "Alter SP", What happens to calls from apps using the
exisiting SP in a production environment?
For example, will my alter statement put the actual SP offline, current
appel in a web app will error with a specific error number one, recompilation
happpens automatically......
Thanks in advance
The app will be blocked from using it until the proc is recompiled.
Depending on how long it takes to recompile the proc the application might
time out.
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"SalamElias" <eliassal@.online.nospam> wrote in message
news:8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com...
> If I issue an "Alter SP", What happens to calls from apps using the
> exisiting SP in a production environment?
> For example, will my alter statement put the actual SP offline, current
> appel in a web app will error with a specific error number one,
> recompilation
> happpens automatically......
> Thanks in advance
|||Hi Salam,
I agree with Hilary's comment.
If you want to store procudure recompile as soon as possible, you could run
sp_recompile.
Running sp_recompile on a stored procedure or a trigger causes them to be
recompiled the next time they are executed. When sp_recompile is run on a
table or a view, all of the stored procedures that reference that table or
view will be recompiled the next time they are run. sp_recompile
accomplishes recompilations by incrementing the on-disk schema version of
the object in question.
Or use the procedure_option RECOMPILE :
ALTER { PROC | PROCEDURE } [schema_name.] procedure_name [ ; number ]
[ { @.parameter [ type_schema_name. ] data_type }
[ VARYING ] [ = default ] [ [ OUT [ PUT ]
] [ ,...n ]
[ WITH <procedure_option> [ ,...n ] ]
[ FOR REPLICATION ]
AS
{ <sql_statement> [ ...n ] | <method_specifier> }
<procedure_option> ::=
[ ENCRYPTION ]
[ RECOMPILE ]
[ EXECUTE_AS_Clause ]
<sql_statement> ::=
{ [ BEGIN ] statements [ END ] }
<method_specifier> ::=
EXTERNAL NAME
assembly_name.class_name.method_name
Note : RECOMPILE
Indicates that the SQL Server 2005 Database Engine does not cache a plan
for this procedure and the procedure is recompiled at run time.
In SQL Server 2000, whenever a statement within a batch causes
recompilation, the whole batch, whether submitted through a stored
procedure, trigger, ad-hoc batch, or prepared statement, is recompiled. In
SQL Server 2005, only the statement inside the batch that causes
recompilation is recompiled. Because of this difference, recompilation
counts in SQL Server 2000 and SQL Server 2005 are not comparable. Also,
there are more types of recompilations in SQL Server 2005 because of its
expanded feature set.
Statement-level recompilation benefits performance because, in most cases,
a small number of statements causes recompilations and their associated
penalties, in terms of CPU time and locks. These penalties are therefore
avoided for the other statements in the batch that do not have to be
recompiled.
The SQL Server Profiler SP:Recompile trace event reports statement-level
recompilations in SQL Server 2005. This trace event reports only batch
recompilations in SQL Server 2000. Further, in SQL Server 2005, the
TextData column of this event is populated. Therefore, the SQL Server 2000
practice of having to trace SP:StmtStarting or SP:StmtCompleted to obtain
the Transact-SQL text that caused recompilation is no longer required.
SQL Server 2005 also adds a new trace event called SQL:StmtRecompile that
reports statement-level recompilations. This trace event can be used to
track and debug recompilations. Whereas SP:Recompile generates only for
stored procedures and triggers, SQL:StmtRecompile generates for stored
procedures, triggers, ad-hoc batches, batches that are executed by using
sp_executesql, prepared queries, and dynamic SQL.
Reference:
SQL Server 2005 Books Online - Execution Plan Caching and Reuse
http://msdn2.microsoft.com/en-us/library/ms181055.aspx
Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server
2005
http://www.microsoft.com/technet/pro...05/recomp.mspx
SQL Server 2005 Books Online - ALTER PROCEDURE (Transact-SQL)
http://msdn2.microsoft.com/en-us/library/ms189762.aspx
Sincerely,
Ray Yen
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti.../default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
| From: "Hilary Cotter" <hilary.cotter@.gmail.com>
| References: <8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com>
| Subject: Re: Alter SP
| Date: Mon, 2 Oct 2006 13:11:49 -0400
| Lines: 32
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962
| Message-ID: <ucipbWk5GHA.512@.TK2MSFTNGP06.phx.gbl>
| Newsgroups: microsoft.public.sqlserver.server
| NNTP-Posting-Host: ool-44c103e1.dyn.optonline.net 68.193.3.225
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFT NGP06.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.sqlserver.server:446875
| X-Tomcat-NG: microsoft.public.sqlserver.server
|
| The app will be blocked from using it until the proc is recompiled.
| Depending on how long it takes to recompile the proc the application
might
| time out.
|
| --
| Hilary Cotter
| Director of Text Mining and Database Strategy
| RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
|
| This posting is my own and doesn't necessarily represent RelevantNoise's
| positions, strategies or opinions.
|
| Looking for a SQL Server replication book?
| http://www.nwsu.com/0974973602.html
|
| Looking for a FAQ on Indexing Services/SQL FTS
| http://www.indexserverfaq.com
|
|
|
| "SalamElias" <eliassal@.online.nospam> wrote in message
| news:8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com...
| > If I issue an "Alter SP", What happens to calls from apps using the
| > exisiting SP in a production environment?
| > For example, will my alter statement put the actual SP offline, current
| > appel in a web app will error with a specific error number one,
| > recompilation
| > happpens automatically......
| >
| > Thanks in advance
|
|
|

Alter SP

If I issue an "Alter SP", What happens to calls from apps using the
exisiting SP in a production environment?
For example, will my alter statement put the actual SP offline, current
appel in a web app will error with a specific error number one, recompilation
happpens automatically......
Thanks in advanceThe app will be blocked from using it until the proc is recompiled.
Depending on how long it takes to recompile the proc the application might
time out.
--
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"SalamElias" <eliassal@.online.nospam> wrote in message
news:8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com...
> If I issue an "Alter SP", What happens to calls from apps using the
> exisiting SP in a production environment?
> For example, will my alter statement put the actual SP offline, current
> appel in a web app will error with a specific error number one,
> recompilation
> happpens automatically......
> Thanks in advance|||Hi Salam,
I agree with Hilary's comment.
If you want to store procudure recompile as soon as possible, you could run
sp_recompile.
Running sp_recompile on a stored procedure or a trigger causes them to be
recompiled the next time they are executed. When sp_recompile is run on a
table or a view, all of the stored procedures that reference that table or
view will be recompiled the next time they are run. sp_recompile
accomplishes recompilations by incrementing the on-disk schema version of
the object in question.
Or use the procedure_option RECOMPILE :
ALTER { PROC | PROCEDURE } [schema_name.] procedure_name [ ; number ]
[ { @.parameter [ type_schema_name. ] data_type }
[ VARYING ] [ = default ] [ [ OUT [ PUT ]
] [ ,...n ]
[ WITH <procedure_option> [ ,...n ] ]
[ FOR REPLICATION ]
AS
{ <sql_statement> [ ...n ] | <method_specifier> }
<procedure_option> ::= [ ENCRYPTION ]
[ RECOMPILE ]
[ EXECUTE_AS_Clause ]
<sql_statement> ::={ [ BEGIN ] statements [ END ] }
<method_specifier> ::=EXTERNAL NAME
assembly_name.class_name.method_name
Note : RECOMPILE
Indicates that the SQL Server 2005 Database Engine does not cache a plan
for this procedure and the procedure is recompiled at run time.
In SQL Server 2000, whenever a statement within a batch causes
recompilation, the whole batch, whether submitted through a stored
procedure, trigger, ad-hoc batch, or prepared statement, is recompiled. In
SQL Server 2005, only the statement inside the batch that causes
recompilation is recompiled. Because of this difference, recompilation
counts in SQL Server 2000 and SQL Server 2005 are not comparable. Also,
there are more types of recompilations in SQL Server 2005 because of its
expanded feature set.
Statement-level recompilation benefits performance because, in most cases,
a small number of statements causes recompilations and their associated
penalties, in terms of CPU time and locks. These penalties are therefore
avoided for the other statements in the batch that do not have to be
recompiled.
The SQL Server Profiler SP:Recompile trace event reports statement-level
recompilations in SQL Server 2005. This trace event reports only batch
recompilations in SQL Server 2000. Further, in SQL Server 2005, the
TextData column of this event is populated. Therefore, the SQL Server 2000
practice of having to trace SP:StmtStarting or SP:StmtCompleted to obtain
the Transact-SQL text that caused recompilation is no longer required.
SQL Server 2005 also adds a new trace event called SQL:StmtRecompile that
reports statement-level recompilations. This trace event can be used to
track and debug recompilations. Whereas SP:Recompile generates only for
stored procedures and triggers, SQL:StmtRecompile generates for stored
procedures, triggers, ad-hoc batches, batches that are executed by using
sp_executesql, prepared queries, and dynamic SQL.
Reference:
SQL Server 2005 Books Online - Execution Plan Caching and Reuse
http://msdn2.microsoft.com/en-us/library/ms181055.aspx
Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server
2005
http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx
SQL Server 2005 Books Online - ALTER PROCEDURE (Transact-SQL)
http://msdn2.microsoft.com/en-us/library/ms189762.aspx
Sincerely,
Ray Yen
Microsoft Online Community Support
==================================================Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
| From: "Hilary Cotter" <hilary.cotter@.gmail.com>
| References: <8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com>
| Subject: Re: Alter SP
| Date: Mon, 2 Oct 2006 13:11:49 -0400
| Lines: 32
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962
| Message-ID: <ucipbWk5GHA.512@.TK2MSFTNGP06.phx.gbl>
| Newsgroups: microsoft.public.sqlserver.server
| NNTP-Posting-Host: ool-44c103e1.dyn.optonline.net 68.193.3.225
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP06.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.sqlserver.server:446875
| X-Tomcat-NG: microsoft.public.sqlserver.server
|
| The app will be blocked from using it until the proc is recompiled.
| Depending on how long it takes to recompile the proc the application
might
| time out.
|
| --
| Hilary Cotter
| Director of Text Mining and Database Strategy
| RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
|
| This posting is my own and doesn't necessarily represent RelevantNoise's
| positions, strategies or opinions.
|
| Looking for a SQL Server replication book?
| http://www.nwsu.com/0974973602.html
|
| Looking for a FAQ on Indexing Services/SQL FTS
| http://www.indexserverfaq.com
|
|
|
| "SalamElias" <eliassal@.online.nospam> wrote in message
| news:8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com...
| > If I issue an "Alter SP", What happens to calls from apps using the
| > exisiting SP in a production environment?
| > For example, will my alter statement put the actual SP offline, current
| > appel in a web app will error with a specific error number one,
| > recompilation
| > happpens automatically......
| >
| > Thanks in advance
|
|
|

Alter SP

If I issue an "Alter SP", What happens to calls from apps using the
exisiting SP in a production environment?
For example, will my alter statement put the actual SP offline, current
appel in a web app will error with a specific error number one, recompilatio
n
happpens automatically......
Thanks in advanceThe app will be blocked from using it until the proc is recompiled.
Depending on how long it takes to recompile the proc the application might
time out.
Hilary Cotter
Director of Text Mining and Database Strategy
RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
This posting is my own and doesn't necessarily represent RelevantNoise's
positions, strategies or opinions.
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602.html
Looking for a FAQ on Indexing Services/SQL FTS
http://www.indexserverfaq.com
"SalamElias" <eliassal@.online.nospam> wrote in message
news:8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com...
> If I issue an "Alter SP", What happens to calls from apps using the
> exisiting SP in a production environment?
> For example, will my alter statement put the actual SP offline, current
> appel in a web app will error with a specific error number one,
> recompilation
> happpens automatically......
> Thanks in advance|||Hi Salam,
I agree with Hilary's comment.
If you want to store procudure recompile as soon as possible, you could run
sp_recompile.
Running sp_recompile on a stored procedure or a trigger causes them to be
recompiled the next time they are executed. When sp_recompile is run on a
table or a view, all of the stored procedures that reference that table or
view will be recompiled the next time they are run. sp_recompile
accomplishes recompilations by incrementing the on-disk schema version of
the object in question.
Or use the procedure_option RECOMPILE :
ALTER { PROC | PROCEDURE } [schema_name.] procedure_name [ ; nu
mber ]
[ { @.parameter [ type_schema_name. ] data_type }
[ VARYING ] [ = default ] [ [ OUT [ PUT ]
] [ ,...n ]
[ WITH <procedure_option> [ ,...n ] ]
[ FOR REPLICATION ]
AS
{ <sql_statement> [ ...n ] | <method_specifier> }
<procedure_option> ::=
[ ENCRYPTION ]
[ RECOMPILE ]
[ EXECUTE_AS_Clause ]
<sql_statement> ::=
{ [ BEGIN ] statements [ END ] }
<method_specifier> ::=
EXTERNAL NAME
assembly_name.class_name.method_name
Note : RECOMPILE
Indicates that the SQL Server 2005 Database Engine does not cache a plan
for this procedure and the procedure is recompiled at run time.
In SQL Server 2000, whenever a statement within a batch causes
recompilation, the whole batch, whether submitted through a stored
procedure, trigger, ad-hoc batch, or prepared statement, is recompiled. In
SQL Server 2005, only the statement inside the batch that causes
recompilation is recompiled. Because of this difference, recompilation
counts in SQL Server 2000 and SQL Server 2005 are not comparable. Also,
there are more types of recompilations in SQL Server 2005 because of its
expanded feature set.
Statement-level recompilation benefits performance because, in most cases,
a small number of statements causes recompilations and their associated
penalties, in terms of CPU time and locks. These penalties are therefore
avoided for the other statements in the batch that do not have to be
recompiled.
The SQL Server Profiler SP:Recompile trace event reports statement-level
recompilations in SQL Server 2005. This trace event reports only batch
recompilations in SQL Server 2000. Further, in SQL Server 2005, the
TextData column of this event is populated. Therefore, the SQL Server 2000
practice of having to trace SP:StmtStarting or SP:StmtCompleted to obtain
the Transact-SQL text that caused recompilation is no longer required.
SQL Server 2005 also adds a new trace event called SQL:StmtRecompile that
reports statement-level recompilations. This trace event can be used to
track and debug recompilations. Whereas SP:Recompile generates only for
stored procedures and triggers, SQL:StmtRecompile generates for stored
procedures, triggers, ad-hoc batches, batches that are executed by using
sp_executesql, prepared queries, and dynamic SQL.
Reference:
SQL Server 2005 Books Online - Execution Plan Caching and Reuse
http://msdn2.microsoft.com/en-us/library/ms181055.aspx
Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server
2005
http://www.microsoft.com/technet/pr...005/recomp.mspx
SQL Server 2005 Books Online - ALTER PROCEDURE (Transact-SQL)
http://msdn2.microsoft.com/en-us/library/ms189762.aspx
Sincerely,
Ray Yen
Microsoft Online Community Support
========================================
==========
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscript...ault.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscript...t/default.aspx.
========================================
==========
This posting is provided "AS IS" with no warranties, and confers no rights.
| From: "Hilary Cotter" <hilary.cotter@.gmail.com>
| References: <8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com>
| Subject: Re: Alter SP
| Date: Mon, 2 Oct 2006 13:11:49 -0400
| Lines: 32
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962
| Message-ID: <ucipbWk5GHA.512@.TK2MSFTNGP06.phx.gbl>
| Newsgroups: microsoft.public.sqlserver.server
| NNTP-Posting-Host: ool-44c103e1.dyn.optonline.net 68.193.3.225
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP06.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.sqlserver.server:446875
| X-Tomcat-NG: microsoft.public.sqlserver.server
|
| The app will be blocked from using it until the proc is recompiled.
| Depending on how long it takes to recompile the proc the application
might
| time out.
|
| --
| Hilary Cotter
| Director of Text Mining and Database Strategy
| RelevantNOISE.Com - Dedicated to mining blogs for business intelligence.
|
| This posting is my own and doesn't necessarily represent RelevantNoise's
| positions, strategies or opinions.
|
| Looking for a SQL Server replication book?
| http://www.nwsu.com/0974973602.html
|
| Looking for a FAQ on Indexing Services/SQL FTS
| http://www.indexserverfaq.com
|
|
|
| "SalamElias" <eliassal@.online.nospam> wrote in message
| news:8C14C710-D83B-400E-85CC-9D546ECEAF62@.microsoft.com...
| > If I issue an "Alter SP", What happens to calls from apps using the
| > exisiting SP in a production environment?
| > For example, will my alter statement put the actual SP offline, current
| > appel in a web app will error with a specific error number one,
| > recompilation
| > happpens automatically......
| >
| > Thanks in advance
|
|
|

Thursday, March 8, 2012

Alter Database statement

Is it possible to issue an alter database statement across servers?
For example
Alter Database Server.Database
Also, the same question with dbcc shrinkdatabase.
Can you do dbcc shrinkdatabase('Server.Database')?
ThanksNot directly. But if the linked server is configured so that you can execute remote stored
procedures, you can do:
EXEC Server.master.dbo.sp_executesql('ALTER DATABASE dbname...')
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Andy" <Andy@.discussions.microsoft.com> wrote in message
news:73D8641F-7DED-48CA-80C9-762AC78D20C2@.microsoft.com...
> Is it possible to issue an alter database statement across servers?
> For example
> Alter Database Server.Database
> Also, the same question with dbcc shrinkdatabase.
> Can you do dbcc shrinkdatabase('Server.Database')?
> Thanks

Wednesday, March 7, 2012

Alter Column Type.

Hello,

Easy one for the SQL experts.

I have a simple table. For the example let's say it looks like this:
CREATE TABLE doc_exb ( column_a INT, column_b VARCHAR(20) NULL)

Now I want to alter this table and make the column column_a a float
instead of an INT.
How do I do that? I cannot DROP and ADD the column I would lose some
very precious data.

Thanks a lot"luc h" <lhovhannessian@.hotmail.com> wrote in message
news:d4175164.0402250955.2802d9e3@.posting.google.c om...
> Hello,
> Easy one for the SQL experts.
> I have a simple table. For the example let's say it looks like this:
> CREATE TABLE doc_exb ( column_a INT, column_b VARCHAR(20) NULL)
> Now I want to alter this table and make the column column_a a float
> instead of an INT.
> How do I do that? I cannot DROP and ADD the column I would lose some
> very precious data.
> Thanks a lot

See ALTER TABLE in Books Online:

alter table dbo.doc_exb
alter column column_a float

Simon

Friday, February 24, 2012

Alpha Prefix - Can it be done in SQL?

Good Morning Db forum.
I am working on a query that I need to pull all fields that contain 3 alpha characters. for example BCB001, MCR001, CHP001 and so forth.
Is there a SQL alpha wildcard that I could use to pull all records that have the three alpha chars?where patindex('%[a-z][a-z][a-z]%',daColumn) > 0

:)|||Awesome thank you. Now would that be the same for say numbers/digits?

where patindex like [0-9][0-9][a-z]% if i wanted to return 145A or something along those lines?|||something along those lines -- you should test it yourself :)

Thursday, February 9, 2012

All employees with Hours or Sales

This has me a little stumped... this is not the real database situation,
just a paraphrased version that should produce the results in an example I
can then use to make my actual query.
I need to get a recordset for each store of all the hours and sales broken
down by employee... Employees can have Hours with no Sales and Sales with
no Hours that day (returns or layaways).
I can't seem to get the correct answer with this one, and I know that it is
not all that hard, but I am missing something. Please help!
I am kinda lost here, any help would be greatly appreciated.
Here is the structure and the data:
CREATE TABLE [dbo].[Employees] (
[EmpID] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Level] [tinyint] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Hours] (
[HID] [int] IDENTITY (1, 1) NOT NULL ,
[Date] [smalldatetime] NULL ,
[Hours] [int] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[EmpID] [int] NULL ,
[StoreID] [int] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Sales] (
[SID] [int] IDENTITY (1, 1) NOT NULL ,
[Date] [smalldatetime] NULL ,
[TotalSales] [float] NULL ,
[EmpID] [int] NULL ,
[StoreID] [int] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Stores] (
[StoreID] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Address] [char] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(1,'Bob',1
)
INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(2,'Sam',2
)
INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(3,'Charli
e',3)
INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(4,'Ralph'
,1)
INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(1,'EC
Main','Eaton
Center')
INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(2,'FV
Main','Fairview
Mall')
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(1,'Feb 1
2005 12:00:00:000AM','8',1,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(2,'Feb 2
2005 12:00:00:000AM','8',1,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(3,'Feb 2
2005 12:00:00:000AM','8',2,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(4,'Feb 2
2005 12:00:00:000AM','8',3,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(5,'Feb 1
2005 12:00:00:000AM','8',1,2)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(6,'Feb 3
2005 12:00:00:000AM','8',2,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(7,'Feb 3
2005 12:00:00:000AM','8',3,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(8,'Feb 4
2005 12:00:00:000AM','8',1,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(9,'Feb 4
2005 12:00:00:000AM','8',2,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(10,'Feb
4 2005 12:00:00:000AM','8',3,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(11,'Feb
5 2005 12:00:00:000AM','8',3,1)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(12,'Feb
5 2005 12:00:00:000AM','8',2,2)
INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(13,'Feb
5 2005 12:00:00:000AM','8',1,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(1,'Feb 1 2005
12:00:00:000AM',1.000000000000000e+001,1,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(2,'Feb 2 2005
12:00:00:000AM',1.300000000000000e+001,1,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(3,'Feb 1 2005
12:00:00:000AM',2.300000000000000e+001,1,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(4,'Feb 1 2005
12:00:00:000AM',3.200000000000000e+001,1,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(5,'Feb 1 2005
12:00:00:000AM',3.400000000000000e+001,2,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(6,'Feb 1 2005
12:00:00:000AM',4.500000000000000e+001,2,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(7,'Feb 1 2005
12:00:00:000AM',4.300000000000000e+001,2,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(8,'Feb 1 2005
12:00:00:000AM',3.200000000000000e+001,3,2)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(9,'Feb 1 2005
12:00:00:000AM',3.300000000000000e+001,3,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(10,'Feb 1 2005
12:00:00:000AM',2.320000000000000e+002,3,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(11,'Feb 2 2005
12:00:00:000AM',1.230000000000000e+002,2,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(12,'Feb 2 2005
12:00:00:000AM',2.300000000000000e+001,2,2)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(13,'Feb 2 2005
12:00:00:000AM',1.200000000000000e+001,1,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(14,'Feb 2 2005
12:00:00:000AM',4.300000000000000e+001,1,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(15,'Feb 3 2005
12:00:00:000AM',5.400000000000000e+001,2,1)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(16,'Feb 3 2005
12:00:00:000AM',2.300000000000000e+001,2,2)
INSERT INTO [sales]
([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(17,'Feb 3 2005
12:00:00:000AM',7.400000000000000e+001,3,1)Select S.Name StoreName, E.Name EmpName,
Sum(H.Hours) Hours,
Sum(A.TotalSales) TotSales
From Stores S Cross Join Employees E
Left Join Hours H
On H.EmpID = E.EmpID
And H.StoreID = S.StoreID
Left Join Sales A
On A.EmpID = E.EmpID
And A.StoreID = S.StoreID
Group By S.Name, E.Name
"Atley" wrote:

> This has me a little stumped... this is not the real database situation,
> just a paraphrased version that should produce the results in an example I
> can then use to make my actual query.
>
> I need to get a recordset for each store of all the hours and sales broken
> down by employee... Employees can have Hours with no Sales and Sales with
> no Hours that day (returns or layaways).
> I can't seem to get the correct answer with this one, and I know that it i
s
> not all that hard, but I am missing something. Please help!
> I am kinda lost here, any help would be greatly appreciated.
>
>
> Here is the structure and the data:
> CREATE TABLE [dbo].[Employees] (
> [EmpID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [Level] [tinyint] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Hours] (
> [HID] [int] IDENTITY (1, 1) NOT NULL ,
> [Date] [smalldatetime] NULL ,
> [Hours] [int] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [EmpID] [int] NULL ,
> [StoreID] [int] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Sales] (
> [SID] [int] IDENTITY (1, 1) NOT NULL ,
> [Date] [smalldatetime] NULL ,
> [TotalSales] [float] NULL ,
> [EmpID] [int] NULL ,
> [StoreID] [int] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Stores] (
> [StoreID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [Address] [char] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ) ON [PRIMARY]
> GO
>
>
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(1,'Bob',1
)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(2,'Sam',2
)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(3,'Charli
e',3)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(4,'Ralph'
,1)
>
> INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(1,'EC
Main','Eaton
> Center')
> INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(2,'FV
Main','Fairview
> Mall')
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(1,'Feb 1
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(2,'Feb 2
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(3,'Feb 2
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(4,'Feb 2
> 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(5,'Feb 1
> 2005 12:00:00:000AM','8',1,2)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(6,'Feb 3
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(7,'Feb 3
> 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(8,'Feb 4
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(9,'Feb 4
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(10,'Feb
> 4 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(11,'Feb
> 5 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(12,'Feb
> 5 2005 12:00:00:000AM','8',2,2)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(13,'Feb
> 5 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(1,'Feb 1 2005
> 12:00:00:000AM',1.000000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(2,'Feb 2 2005
> 12:00:00:000AM',1.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(3,'Feb 1 2005
> 12:00:00:000AM',2.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(4,'Feb 1 2005
> 12:00:00:000AM',3.200000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(5,'Feb 1 2005
> 12:00:00:000AM',3.400000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(6,'Feb 1 2005
> 12:00:00:000AM',4.500000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(7,'Feb 1 2005
> 12:00:00:000AM',4.300000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(8,'Feb 1 2005
> 12:00:00:000AM',3.200000000000000e+001,3,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(9,'Feb 1 2005
> 12:00:00:000AM',3.300000000000000e+001,3,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(10,'Feb 1 2005
> 12:00:00:000AM',2.320000000000000e+002,3,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(11,'Feb 2 2005
> 12:00:00:000AM',1.230000000000000e+002,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(12,'Feb 2 2005
> 12:00:00:000AM',2.300000000000000e+001,2,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(13,'Feb 2 2005
> 12:00:00:000AM',1.200000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(14,'Feb 2 2005
> 12:00:00:000AM',4.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(15,'Feb 3 2005
> 12:00:00:000AM',5.400000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(16,'Feb 3 2005
> 12:00:00:000AM',2.300000000000000e+001,2,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(17,'Feb 3 2005
> 12:00:00:000AM',7.400000000000000e+001,3,1)
>
>
>
>|||And if you also want it broken out by date, then
Select S.Name StoreName, E.Name EmpName, D.Date,
Sum(H.Hours) Hours,
Sum(A.TotalSales) TotSales
From Stores S Cross Join Employees E
Cross Join
(Select Distinct Date From Hours
Union
Select Distinct Date From Sales) D
Left Join Hours H
On H.EmpID = E.EmpID
And H.StoreID = S.StoreID
And H.Date = D.Date
Left Join Sales A
On A.EmpID = E.EmpID
And A.StoreID = S.StoreID
And A.Date = D.Date
Group By S.Name, E.Name, D.Date
Order By S.Name, E.Name, D.Date
"Atley" wrote:

> This has me a little stumped... this is not the real database situation,
> just a paraphrased version that should produce the results in an example I
> can then use to make my actual query.
>
> I need to get a recordset for each store of all the hours and sales broken
> down by employee... Employees can have Hours with no Sales and Sales with
> no Hours that day (returns or layaways).
> I can't seem to get the correct answer with this one, and I know that it i
s
> not all that hard, but I am missing something. Please help!
> I am kinda lost here, any help would be greatly appreciated.
>
>
> Here is the structure and the data:
> CREATE TABLE [dbo].[Employees] (
> [EmpID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [Level] [tinyint] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Hours] (
> [HID] [int] IDENTITY (1, 1) NOT NULL ,
> [Date] [smalldatetime] NULL ,
> [Hours] [int] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [EmpID] [int] NULL ,
> [StoreID] [int] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Sales] (
> [SID] [int] IDENTITY (1, 1) NOT NULL ,
> [Date] [smalldatetime] NULL ,
> [TotalSales] [float] NULL ,
> [EmpID] [int] NULL ,
> [StoreID] [int] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Stores] (
> [StoreID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [Address] [char] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ) ON [PRIMARY]
> GO
>
>
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(1,'Bob',1
)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(2,'Sam',2
)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(3,'Charli
e',3)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(4,'Ralph'
,1)
>
> INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(1,'EC
Main','Eaton
> Center')
> INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(2,'FV
Main','Fairview
> Mall')
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(1,'Feb 1
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(2,'Feb 2
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(3,'Feb 2
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(4,'Feb 2
> 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(5,'Feb 1
> 2005 12:00:00:000AM','8',1,2)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(6,'Feb 3
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(7,'Feb 3
> 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(8,'Feb 4
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(9,'Feb 4
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(10,'Feb
> 4 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(11,'Feb
> 5 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(12,'Feb
> 5 2005 12:00:00:000AM','8',2,2)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(13,'Feb
> 5 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(1,'Feb 1 2005
> 12:00:00:000AM',1.000000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(2,'Feb 2 2005
> 12:00:00:000AM',1.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(3,'Feb 1 2005
> 12:00:00:000AM',2.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(4,'Feb 1 2005
> 12:00:00:000AM',3.200000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(5,'Feb 1 2005
> 12:00:00:000AM',3.400000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(6,'Feb 1 2005
> 12:00:00:000AM',4.500000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(7,'Feb 1 2005
> 12:00:00:000AM',4.300000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(8,'Feb 1 2005
> 12:00:00:000AM',3.200000000000000e+001,3,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(9,'Feb 1 2005
> 12:00:00:000AM',3.300000000000000e+001,3,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(10,'Feb 1 2005
> 12:00:00:000AM',2.320000000000000e+002,3,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(11,'Feb 2 2005
> 12:00:00:000AM',1.230000000000000e+002,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(12,'Feb 2 2005
> 12:00:00:000AM',2.300000000000000e+001,2,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(13,'Feb 2 2005
> 12:00:00:000AM',1.200000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(14,'Feb 2 2005
> 12:00:00:000AM',4.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(15,'Feb 3 2005
> 12:00:00:000AM',5.400000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(16,'Feb 3 2005
> 12:00:00:000AM',2.300000000000000e+001,2,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(17,'Feb 3 2005
> 12:00:00:000AM',7.400000000000000e+001,3,1)
>
>
>
>|||And if you don;t want to see the rows where there are no Sales and no Hours
Add a "Having" clause...
Having Sum(H.Hours) Is Not Null Or Sum(A.TotalSales) Is Not Null
-- *************************************
Select S.Name StoreName, E.Name EmpName, D.Date,
Sum(H.Hours) Hours,
Sum(A.TotalSales) TotSales
From Stores S Cross Join Employees E
Cross Join
(Select Distinct Date From Hours
Union
Select Distinct Date From Sales) D
Left Join Hours H
On H.EmpID = E.EmpID
And H.StoreID = S.StoreID
And H.Date = D.Date
Left Join Sales A
On A.EmpID = E.EmpID
And A.StoreID = S.StoreID
And A.Date = D.Date
Group By S.Name, E.Name, D.Date
Having Sum(H.Hours) Is Not Null Or Sum(A.TotalSales) Is Not Null
Order By S.Name, E.Name, D.Date
"Atley" wrote:

> This has me a little stumped... this is not the real database situation,
> just a paraphrased version that should produce the results in an example I
> can then use to make my actual query.
>
> I need to get a recordset for each store of all the hours and sales broken
> down by employee... Employees can have Hours with no Sales and Sales with
> no Hours that day (returns or layaways).
> I can't seem to get the correct answer with this one, and I know that it i
s
> not all that hard, but I am missing something. Please help!
> I am kinda lost here, any help would be greatly appreciated.
>
>
> Here is the structure and the data:
> CREATE TABLE [dbo].[Employees] (
> [EmpID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [Level] [tinyint] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Hours] (
> [HID] [int] IDENTITY (1, 1) NOT NULL ,
> [Date] [smalldatetime] NULL ,
> [Hours] [int] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [EmpID] [int] NULL ,
> [StoreID] [int] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Sales] (
> [SID] [int] IDENTITY (1, 1) NOT NULL ,
> [Date] [smalldatetime] NULL ,
> [TotalSales] [float] NULL ,
> [EmpID] [int] NULL ,
> [StoreID] [int] NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Stores] (
> [StoreID] [int] IDENTITY (1, 1) NOT NULL ,
> [Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [Address] [char] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
> ) ON [PRIMARY]
> GO
>
>
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(1,'Bob',1
)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(2,'Sam',2
)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(3,'Charli
e',3)
> INSERT INTO [employees] ([EmpID],[Name],[Level])VALUES(4,'Ralph'
,1)
>
> INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(1,'EC
Main','Eaton
> Center')
> INSERT INTO [stores] ([StoreID],[Name],[Address])VALUES(2,'FV
Main','Fairview
> Mall')
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(1,'Feb 1
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(2,'Feb 2
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(3,'Feb 2
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(4,'Feb 2
> 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(5,'Feb 1
> 2005 12:00:00:000AM','8',1,2)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(6,'Feb 3
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(7,'Feb 3
> 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(8,'Feb 4
> 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(9,'Feb 4
> 2005 12:00:00:000AM','8',2,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(10,'Feb
> 4 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(11,'Feb
> 5 2005 12:00:00:000AM','8',3,1)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(12,'Feb
> 5 2005 12:00:00:000AM','8',2,2)
> INSERT INTO [hours] ([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(13,'Feb
> 5 2005 12:00:00:000AM','8',1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(1,'Feb 1 2005
> 12:00:00:000AM',1.000000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(2,'Feb 2 2005
> 12:00:00:000AM',1.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(3,'Feb 1 2005
> 12:00:00:000AM',2.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(4,'Feb 1 2005
> 12:00:00:000AM',3.200000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(5,'Feb 1 2005
> 12:00:00:000AM',3.400000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(6,'Feb 1 2005
> 12:00:00:000AM',4.500000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(7,'Feb 1 2005
> 12:00:00:000AM',4.300000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(8,'Feb 1 2005
> 12:00:00:000AM',3.200000000000000e+001,3,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(9,'Feb 1 2005
> 12:00:00:000AM',3.300000000000000e+001,3,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(10,'Feb 1 2005
> 12:00:00:000AM',2.320000000000000e+002,3,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(11,'Feb 2 2005
> 12:00:00:000AM',1.230000000000000e+002,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(12,'Feb 2 2005
> 12:00:00:000AM',2.300000000000000e+001,2,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(13,'Feb 2 2005
> 12:00:00:000AM',1.200000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(14,'Feb 2 2005
> 12:00:00:000AM',4.300000000000000e+001,1,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(15,'Feb 3 2005
> 12:00:00:000AM',5.400000000000000e+001,2,1)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(16,'Feb 3 2005
> 12:00:00:000AM',2.300000000000000e+001,2,2)
> INSERT INTO [sales]
> ([SID],[Date],[TotalSales],[EmpID],[Stor
eID])VALUES(17,'Feb 3 2005
> 12:00:00:000AM',7.400000000000000e+001,3,1)
>
>
>
>|||Thanks for the quick response!
Actually,
I want a list of all EmployeeID's who have either sales or hours for a
certain date range
I can't seem to get that.
"CBretana" <cbretana@.areteIndNOSPAM.com> wrote in message
news:BDDBF80C-8838-49C3-A248-7ABF9AFF2172@.microsoft.com...
> And if you don;t want to see the rows where there are no Sales and no
Hours
> Add a "Having" clause...
> Having Sum(H.Hours) Is Not Null Or Sum(A.TotalSales) Is Not Null
> -- *************************************
> Select S.Name StoreName, E.Name EmpName, D.Date,
> Sum(H.Hours) Hours,
> Sum(A.TotalSales) TotSales
> From Stores S Cross Join Employees E
> Cross Join
> (Select Distinct Date From Hours
> Union
> Select Distinct Date From Sales) D
> Left Join Hours H
> On H.EmpID = E.EmpID
> And H.StoreID = S.StoreID
> And H.Date = D.Date
> Left Join Sales A
> On A.EmpID = E.EmpID
> And A.StoreID = S.StoreID
> And A.Date = D.Date
> Group By S.Name, E.Name, D.Date
> Having Sum(H.Hours) Is Not Null Or Sum(A.TotalSales) Is Not Null
> Order By S.Name, E.Name, D.Date
> "Atley" wrote:
>
I
broken
with
is
([StoreID],[Name],[Address])VALUES(1,'EC
Main','Eaton
([StoreID],[Name],[Address])VALUES(2,'FV
Main','Fairview
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(1,'Feb 1
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(2,'Feb 2
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(3,'Feb 2
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(4,'Feb 2
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(5,'Feb 1
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(6,'Feb 3
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(7,'Feb 3
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(8,'Feb 4
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(9,'Feb 4
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(10,'Feb
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(11,'Feb
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(12,'Feb
([HID],[Date],[Hours],[EmpID],[StoreID])
VALUES(13,'Feb|||Hi...
Sorry for jumping in, I hope I'm not off-base.
I was under the impression that multiple summarizations with multiple outer
joins would lead to double-triple counting, because of the aggregation.
Any time I've ever needed multiple SUM statements, I've either used a scalar
subquery in the SELECT to return the sums, or I queried from derived tables
that summed the hours and sales.
Just wanted to make sure I wasn't missing something.
Thanks,
Kevin|||> I was under the impression that multiple summarizations with multiple
> outer
> joins would lead to double-triple counting, because of the aggregation.
> Any time I've ever needed multiple SUM statements, I've either used a
> scalar
> subquery in the SELECT to return the sums, or I queried from derived
> tables
> that summed the hours and sales.
> Just wanted to make sure I wasn't missing something.
I believe your understanding is correct. The example below uses both a
derived table and subqueries to list employees with either sales or hours
along with aggregate totals.
DECLARE
@.StartDT smalldatetime,
@.EndDT smalldatetime
SET @.StartDT = '20050202'
SET @.EndDT = '20050204'
SELECT EmpID, Name, Level, Hours, Sales
FROM
(
SELECT
E.EmpID, E.Name, E.Level,
(SELECT SUM(H.Hours) Hours
FROM Hours H
WHERE H.EmpID = E.EmpID AND
H.[Date] BETWEEN @.StartDT AND @.EndDT) AS Hours,
(SELECT SUM(A.TotalSales) Sales
FROM Sales A
WHERE A.EmpID = E.EmpID AND
A.[Date] BETWEEN @.StartDT AND @.EndDT) AS Sales
FROM Employees E
) AS EmployeeMetrics
WHERE
Hours IS NOT NULL OR
Sales IS NOT NULL
Hope this helps.
Dan Guzman
SQL Server MVP
<Kevin@.test.com> wrote in message
news:er2F9CnIFHA.1280@.TK2MSFTNGP09.phx.gbl...
> Hi...
> Sorry for jumping in, I hope I'm not off-base.
> I was under the impression that multiple summarizations with multiple
> outer
> joins would lead to double-triple counting, because of the aggregation.
> Any time I've ever needed multiple SUM statements, I've either used a
> scalar
> subquery in the SELECT to return the sums, or I queried from derived
> tables
> that summed the hours and sales.
> Just wanted to make sure I wasn't missing something.
> Thanks,
> Kevin