Showing posts with label single. Show all posts
Showing posts with label single. Show all posts

Wednesday, March 28, 2012

Order by of a table passed as parameters?

Is it possible to parametrise the 'order by' options of a table/group so
that a single report could be ordered by users on run time depending of
their needs?
The alternative is to write/copy the same report once and again and have it
repeated (one instance) for each orderable column.
Regards.Sorry for this post. I just found it on RS' BOL.
Regards.
"David Lightman Robles" <dlightman@.NOSPAMiname.com> escribió en el mensaje
news:eXv4RWQXFHA.3572@.TK2MSFTNGP12.phx.gbl...
> Is it possible to parametrise the 'order by' options of a table/group so
> that a single report could be ordered by users on run time depending of
> their needs?
> The alternative is to write/copy the same report once and again and have
> it repeated (one instance) for each orderable column.
> Regards.
>

Monday, March 26, 2012

Order By in Subquery + UNION

All,

I've seen several posts regarding using UNION or UNION ALL to mash
together two or more resultsets into a single result set, but can't
seem to find enough info here to help me answer my particular
question.

I have a stored procedure that gets the column names in a particular
format (i.e. "chassis_id"|"chassis_description"|"modify_date") as well
as actual data for a given table (in a quote-separated, pipe-delimited
manner i.e. "1"|"description for the chassis"|"2004-09-08").

I'd like to get both of these resultsets and mash them together. This
works, but when I need to order the second resultset (i.e. select *
from chassis order by chassis_id), SQL Server returns an error
complaining about the chassis_id column name (invalid column name
'chassis_id') in the Order By clause.

From what I can tell, I'm using the UNION and Order By in correctly,
but I'm not sure exactly what's wrong with it. If I take out the Order
By, everything works great. Although I would like to be able to order
my second resultset (in the same sproc) if possible.

The actual queries I'm running are actually quite long, but here's one
that's a bit shorter to help illustrate:

SELECT '"app_group_id"|"app_group_name"|"create_date"|"create_by"|"modify_date"|"modify_by"'
UNION ALL
SELECT + ISNULL(CONVERT(varchar,app_group_id), '') + '|'
+ SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 1))), 1,0)
+ '"' + ISNULL(CONVERT(varchar(1000), +
REPLACE(CONVERT(nvarchar(4000),app_group_name), '"', '""')), '') +
'"|'
+ SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 2))), 1,0)
FROM app_group
order by app_group_id

Thank for any help on this.

/bcIn a UNION any columns in the ORDER BY list must appear in the result. If
you don't want to include the ORDER BY column in the result then nest the
UNION as a derived table:

SELECT x
FROM
(SELECT NULL /* app_group_id column */ ,
... etc
UNION ALL
SELECT app_group_id,
... etc
FROM app_group) AS T(app_group_id,x)
ORDER BY app_group_id

--
David Portas
SQL Server MVP
--|||CREATE A VIEW WITHOUT THE ORDER BY CLAUSE. THEN SELECT COLUMNS FROM
VIEW USING THE ORDER BY CLAUSE. IF I REMEMBER RIGHT, UNION DOES NOT
LIKE ORDER BY.

Thanks, Girish

blake@.caraways.net (Blake Caraway) wrote in message news:<9089d10f.0409080819.5fb0ca55@.posting.google.com>...
> All,
> I've seen several posts regarding using UNION or UNION ALL to mash
> together two or more resultsets into a single result set, but can't
> seem to find enough info here to help me answer my particular
> question.
> I have a stored procedure that gets the column names in a particular
> format (i.e. "chassis_id"|"chassis_description"|"modify_date") as well
> as actual data for a given table (in a quote-separated, pipe-delimited
> manner i.e. "1"|"description for the chassis"|"2004-09-08").
> I'd like to get both of these resultsets and mash them together. This
> works, but when I need to order the second resultset (i.e. select *
> from chassis order by chassis_id), SQL Server returns an error
> complaining about the chassis_id column name (invalid column name
> 'chassis_id') in the Order By clause.
> From what I can tell, I'm using the UNION and Order By in correctly,
> but I'm not sure exactly what's wrong with it. If I take out the Order
> By, everything works great. Although I would like to be able to order
> my second resultset (in the same sproc) if possible.
> The actual queries I'm running are actually quite long, but here's one
> that's a bit shorter to help illustrate:
> SELECT '"app_group_id"|"app_group_name"|"create_date"|"create_by"|"modify_date"|"modify_by"'
> UNION ALL
> SELECT + ISNULL(CONVERT(varchar,app_group_id), '') + '|'
> + SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 1))), 1,0)
> + '"' + ISNULL(CONVERT(varchar(1000), +
> REPLACE(CONVERT(nvarchar(4000),app_group_name), '"', '""')), '') +
> '"|'
> + SUBSTRING(RTRIM(LTRIM(CONVERT(varchar, 2))), 1,0)
> FROM app_group
> order by app_group_id
>
> Thank for any help on this.
> /bc|||Girish (kattukuyil@.hotmail.com) writes:
> CREATE A VIEW WITHOUT THE ORDER BY CLAUSE. THEN SELECT COLUMNS FROM
> VIEW USING THE ORDER BY CLAUSE. IF I REMEMBER RIGHT, UNION DOES NOT
> LIKE ORDER BY.

You remember wrong. But it is imporant to understand that ORDER BY
applies to the entire query, not the individual SELECT clauses in the
query.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Friday, March 23, 2012

Order by clause

hello everybody. i have a table named employee. where there is just a single column named id which is of type varchar. this is because i just wanted to play with order by clause. :D

in the table, i have CCA, cbC, cBC, CbC, CBC. when i queried with select * from employee order by id; the result is as followed-
CBC
cBC
cbC
CbC
CCA

how order by clause worked here, can anybody explain? thanks in advance...for the purpose of the ORDER BY, the collation you are using means that CBC, cBC, cbC, and CbC are all equivalent

CCA, of course, would come after all of them|||some databases treat upper & lower case the same, some dont, some can do either
some front ends likewise

so read the documentation with your db and your front end and see if you can select which order you want.

for example if you are using microsoft access as your front end 'option compare' may be worth investigating

MySQL has a mechanism at the database level to handle this sort of issue, and Im sure that will be the same for most other serious servers|||But if i write select * from employee order by id desc then the result is:
CCA
CBC
cBC
cbC
CbC

i don't have any idea how order by is working here...|||with DESC, CCA comes first, then all the rows with CBC equivalents|||if all rows with CBC are equivalent then why CbC comes last?? didn't get your point. plz explain.|||Yeah, that is a good point...
if SELECT * FROM employee ORDER BY id; gives the result
CBC
cBC
cbC
CbC
CCA

then SELECT * FROM employee ORDER BY id DESC; should provide-
CCA
CbC
cbC
cBC
CBC

why instead of it, the result is like this-
CCA
CBC
cBC
cbC
CbC?

or is it like the order of 4 CBCs doesn't really matter?:shocked:|||you know, it's beginning to look as if the order of the CBCs doesn't really matter because they're all equivalent

:)|||The sort order in databases is related with the Collation of that database.
For example Latin1_General_CI_AS is a Case Insensitive Accent Sensitive collation.

For a list of collations in MS SQL Server, you can use fn_helpcollations() function.
You can read the article http://www.kodyaz.com/content/fnhelpcollations.aspx on fn_helpcollations()

Eralper
http://www.kodyaz.com|||If the collation is case-insensitive, then to your database CBC and cbc appear to be the same thing... It doesn't matter which order they appear because for the purposes of comparision they are equal.

-PatP