Showing posts with label views. Show all posts
Showing posts with label views. Show all posts

Wednesday, March 21, 2012

playing around views

hi
I just want to be sure there is no way to do it inside the DB:
I have a view which return 1000 numbers and amounts like this:
Number Amount
000 234
001 3456
... ...
999 464
I have only two very long columns. is it possible to re-arrange this into a
view to get something like this:
number amount number amount number amount
000 345 001 9861 002 865
003 4564 004 45 005 9865
thks.On Tue, 25 Jan 2005 09:05:03 -0800, Kenny M. wrote:

>I have a view which return 1000 numbers and amounts like this:
>Number Amount
>000 234
>001 3456
>... ...
>999 464
>I have only two very long columns. is it possible to re-arrange this into
a
>view to get something like this:
>number amount number amount number amount
> 000 345 001 9861 002 865
> 003 4564 004 45 005 9865
Hi Kenny,
This is a presentation task, typically done at the client. However, if you
really want to burden the server with it, try:
SELECT a.number, a.amount, b.number, b.amount, c.number, c.amount
FROM MyTable AS a
LEFT OUTER JOIN MyTable AS b
ON b.Number = a.Number + 1
LEFT OUTER JOIN MyTable AS c
ON c.Number = a.Number + 1
WHERE a.Number % 3 = 0
ORDER BY a.Number
(untested)
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Why to stress your server trying to do this?. Use your client app / reportin
g
tool or programming language.
use northwind
go
select
identity(int, 0, 1) as number,
0 as amount
into
t
from
sysobjects as a cross join sysobjects as b
delete t where number > 999
update t set amount = power(2, number % 8)
select
*
from
(
select
number, amount
from
t
where
number % 3 = 0
) as a
left join
(
select
number, amount
from
t
where
number % 3 = 1
) as b
on a.number = b.number - 1
left join
(
select
number, amount
from
t
where
number % 3 = 2
) as c
on b.number = c.number - 1
drop table t
go
AMB
"Kenny M." wrote:

> hi
> I just want to be sure there is no way to do it inside the DB:
> I have a view which return 1000 numbers and amounts like this:
> Number Amount
> 000 234
> 001 3456
> ... ...
> 999 464
> I have only two very long columns. is it possible to re-arrange this into
a
> view to get something like this:
> number amount number amount number amount
> 000 345 001 9861 002 865
> 003 4564 004 45 005 9865
> thks.
>
>

Monday, March 12, 2012

Placing Comments In Views

Is it possible to add comment lines to views? I tried using -- and /* . . .
but these lines were removed upon save. I tried using the property window,
but that cleared out my query.
Yes, make sure you have the comment inside the CREATE VIEW, such as
CREATE VIEW v
AS
--MyComment
SELECT 1 AS a
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
> Is it possible to add comment lines to views? I tried using -- and /* . . .
> but these lines were removed upon save. I tried using the property window,
> but that cleared out my query.
|||I did what you suggested, but it still didn't do what I expected. So, I used
a query as simple as yours . . . and it did do what I expected. Could it be
that the comments in the property window don't work on views with UNION
queries?
Just to be clear: If I access the property window within DESIGN mode, the
comments do not appear. But if I access the property box from the list of
all views window, I do see my comment after the CREATE VIEW statement.
"Tibor Karaszi" wrote:

> Yes, make sure you have the comment inside the CREATE VIEW, such as
> CREATE VIEW v
> AS
> --MyComment
> SELECT 1 AS a
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
>
|||Probably some GUI problem with Enterprise Manager. I did a simple test with below view:
CREATE VIEW v2
AS
--MyComment
SELECT 1 AS a
UNION ALL
SELECT 1 AS a
And the comment does not show, as you say. I don't use EM for view design myself. My guess is that
with a more complex query, the parsing that EM does of the view isn't smart enough to extract the
comment. I guess you could call it a bug and open a case with MS for it. I prefer writing the views
from Query analyzer...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:04C89683-D9BF-482A-AFBF-7915F0621F85@.microsoft.com...[vbcol=seagreen]
>I did what you suggested, but it still didn't do what I expected. So, I used
> a query as simple as yours . . . and it did do what I expected. Could it be
> that the comments in the property window don't work on views with UNION
> queries?
> Just to be clear: If I access the property window within DESIGN mode, the
> comments do not appear. But if I access the property box from the list of
> all views window, I do see my comment after the CREATE VIEW statement.
> "Tibor Karaszi" wrote:
|||Just one more note: if I add the comments via script, but then make a change
via EM and/or Visual Studio - saving the changes loses the comment lines.
But thanks, I got what I need . . . I'll just make sure the comments are in
the scripts we deliver to our customers.
"Tibor Karaszi" wrote:

> Probably some GUI problem with Enterprise Manager. I did a simple test with below view:
> CREATE VIEW v2
> AS
> --MyComment
> SELECT 1 AS a
> UNION ALL
> SELECT 1 AS a
> And the comment does not show, as you say. I don't use EM for view design myself. My guess is that
> with a more complex query, the parsing that EM does of the view isn't smart enough to extract the
> comment. I guess you could call it a bug and open a case with MS for it. I prefer writing the views
> from Query analyzer...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:04C89683-D9BF-482A-AFBF-7915F0621F85@.microsoft.com...
>

Placing Comments In Views

Is it possible to add comment lines to views? I tried using -- and /* . . .
but these lines were removed upon save. I tried using the property window,
but that cleared out my query.Yes, make sure you have the comment inside the CREATE VIEW, such as
CREATE VIEW v
AS
--MyComment
SELECT 1 AS a
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
> Is it possible to add comment lines to views? I tried using -- and /* . . .
> but these lines were removed upon save. I tried using the property window,
> but that cleared out my query.|||I did what you suggested, but it still didn't do what I expected. So, I used
a query as simple as yours . . . and it did do what I expected. Could it be
that the comments in the property window don't work on views with UNION
queries?
Just to be clear: If I access the property window within DESIGN mode, the
comments do not appear. But if I access the property box from the list of
all views window, I do see my comment after the CREATE VIEW statement.
"Tibor Karaszi" wrote:
> Yes, make sure you have the comment inside the CREATE VIEW, such as
> CREATE VIEW v
> AS
> --MyComment
> SELECT 1 AS a
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
> > Is it possible to add comment lines to views? I tried using -- and /* . . .
> > but these lines were removed upon save. I tried using the property window,
> > but that cleared out my query.
>|||Probably some GUI problem with Enterprise Manager. I did a simple test with below view:
CREATE VIEW v2
AS
--MyComment
SELECT 1 AS a
UNION ALL
SELECT 1 AS a
And the comment does not show, as you say. I don't use EM for view design myself. My guess is that
with a more complex query, the parsing that EM does of the view isn't smart enough to extract the
comment. I guess you could call it a bug and open a case with MS for it. I prefer writing the views
from Query analyzer...
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:04C89683-D9BF-482A-AFBF-7915F0621F85@.microsoft.com...
>I did what you suggested, but it still didn't do what I expected. So, I used
> a query as simple as yours . . . and it did do what I expected. Could it be
> that the comments in the property window don't work on views with UNION
> queries?
> Just to be clear: If I access the property window within DESIGN mode, the
> comments do not appear. But if I access the property box from the list of
> all views window, I do see my comment after the CREATE VIEW statement.
> "Tibor Karaszi" wrote:
>> Yes, make sure you have the comment inside the CREATE VIEW, such as
>> CREATE VIEW v
>> AS
>> --MyComment
>> SELECT 1 AS a
>>
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>> Blog: http://solidqualitylearning.com/blogs/tibor/
>>
>> "Jim" <Jim@.discussions.microsoft.com> wrote in message
>> news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
>> > Is it possible to add comment lines to views? I tried using -- and /* . . .
>> > but these lines were removed upon save. I tried using the property window,
>> > but that cleared out my query.
>>|||Just one more note: if I add the comments via script, but then make a change
via EM and/or Visual Studio - saving the changes loses the comment lines.
But thanks, I got what I need . . . I'll just make sure the comments are in
the scripts we deliver to our customers.
"Tibor Karaszi" wrote:
> Probably some GUI problem with Enterprise Manager. I did a simple test with below view:
> CREATE VIEW v2
> AS
> --MyComment
> SELECT 1 AS a
> UNION ALL
> SELECT 1 AS a
> And the comment does not show, as you say. I don't use EM for view design myself. My guess is that
> with a more complex query, the parsing that EM does of the view isn't smart enough to extract the
> comment. I guess you could call it a bug and open a case with MS for it. I prefer writing the views
> from Query analyzer...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:04C89683-D9BF-482A-AFBF-7915F0621F85@.microsoft.com...
> >I did what you suggested, but it still didn't do what I expected. So, I used
> > a query as simple as yours . . . and it did do what I expected. Could it be
> > that the comments in the property window don't work on views with UNION
> > queries?
> >
> > Just to be clear: If I access the property window within DESIGN mode, the
> > comments do not appear. But if I access the property box from the list of
> > all views window, I do see my comment after the CREATE VIEW statement.
> >
> > "Tibor Karaszi" wrote:
> >
> >> Yes, make sure you have the comment inside the CREATE VIEW, such as
> >>
> >> CREATE VIEW v
> >> AS
> >> --MyComment
> >> SELECT 1 AS a
> >>
> >>
> >>
> >> --
> >> Tibor Karaszi, SQL Server MVP
> >> http://www.karaszi.com/sqlserver/default.asp
> >> http://www.solidqualitylearning.com/
> >> Blog: http://solidqualitylearning.com/blogs/tibor/
> >>
> >>
> >> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> >> news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
> >> > Is it possible to add comment lines to views? I tried using -- and /* . . .
> >> > but these lines were removed upon save. I tried using the property window,
> >> > but that cleared out my query.
> >>
> >>
>

Placing Comments In Views

Is it possible to add comment lines to views? I tried using -- and /* . . .
but these lines were removed upon save. I tried using the property window,
but that cleared out my query.Yes, make sure you have the comment inside the CREATE VIEW, such as
CREATE VIEW v
AS
--MyComment
SELECT 1 AS a
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
> Is it possible to add comment lines to views? I tried using -- and /* . .
.
> but these lines were removed upon save. I tried using the property window
,
> but that cleared out my query.|||I did what you suggested, but it still didn't do what I expected. So, I use
d
a query as simple as yours . . . and it did do what I expected. Could it be
that the comments in the property window don't work on views with UNION
queries?
Just to be clear: If I access the property window within DESIGN mode, the
comments do not appear. But if I access the property box from the list of
all views window, I do see my comment after the CREATE VIEW statement.
"Tibor Karaszi" wrote:

> Yes, make sure you have the comment inside the CREATE VIEW, such as
> CREATE VIEW v
> AS
> --MyComment
> SELECT 1 AS a
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:53FCF340-8E7A-495C-BD84-0C8A7882B001@.microsoft.com...
>|||Probably some GUI problem with Enterprise Manager. I did a simple test with
below view:
CREATE VIEW v2
AS
--MyComment
SELECT 1 AS a
UNION ALL
SELECT 1 AS a
And the comment does not show, as you say. I don't use EM for view design my
self. My guess is that
with a more complex query, the parsing that EM does of the view isn't smart
enough to extract the
comment. I guess you could call it a bug and open a case with MS for it. I p
refer writing the views
from Query analyzer...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Jim" <Jim@.discussions.microsoft.com> wrote in message
news:04C89683-D9BF-482A-AFBF-7915F0621F85@.microsoft.com...[vbcol=seagreen]
>I did what you suggested, but it still didn't do what I expected. So, I us
ed
> a query as simple as yours . . . and it did do what I expected. Could it
be
> that the comments in the property window don't work on views with UNION
> queries?
> Just to be clear: If I access the property window within DESIGN mode, the
> comments do not appear. But if I access the property box from the list of
> all views window, I do see my comment after the CREATE VIEW statement.
> "Tibor Karaszi" wrote:
>|||Just one more note: if I add the comments via script, but then make a change
via EM and/or Visual Studio - saving the changes loses the comment lines.
But thanks, I got what I need . . . I'll just make sure the comments are in
the scripts we deliver to our customers.
"Tibor Karaszi" wrote:

> Probably some GUI problem with Enterprise Manager. I did a simple test wit
h below view:
> CREATE VIEW v2
> AS
> --MyComment
> SELECT 1 AS a
> UNION ALL
> SELECT 1 AS a
> And the comment does not show, as you say. I don't use EM for view design
myself. My guess is that
> with a more complex query, the parsing that EM does of the view isn't smar
t enough to extract the
> comment. I guess you could call it a bug and open a case with MS for it. I
prefer writing the views
> from Query analyzer...
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Jim" <Jim@.discussions.microsoft.com> wrote in message
> news:04C89683-D9BF-482A-AFBF-7915F0621F85@.microsoft.com...
>