set Order By direction using selectparameters -- ASC or DESC - sql-order-by

I'm trying to set the order by direction (ASC or DESC) by using SelectParameters and I get this error:
Incorrect syntax near '#LicenseStatusSortOrder'.
When I replace #LicenseStatusSortOrder with ASC or DESC then it works. The Label_LicenseStatusSortOrder.Text does indeed contain either ASC or DESC so the value is present. See code below and thanks!
SelectCommand:
SelectCommand="SELECT SoftwareLicense.Name, SoftwareLicenseByApplication.ComplianceStatus FROM UserInfo ORDER BY SoftwareLicenseByApplication.ComplianceStatus #LicenseStatusSortOrder"
SelectParameter:
> <asp:ControlParameter ControlID="Label_LicenseStatusSortOrder"
> DefaultValue="ASC" Name="LicenseStatusSortOrder"
> PropertyName="Text"/>
--Clark Bohs

Related

SELECT Date with max date in Oracle

I have expression with db_link to MS SQL:
select b."Str" as "State" ,a."_Fld9059" as "Date" from
"_InfoRg9050"#SQLSERVER.UISLAB.COM a INNER JOIN
"EnumTexts"#SQLSERVER.UISLAB.COM b
on a."_Fld9052RRef" = b."_IDRRef"
where a."_Fld10998" = '1104000009' and
a."_Fld10998" = to_date(max(a."_Fld9059"),'dd.mm.yyyy')
order by a."_Fld9059" desc;
I want to upload value with maximum date. Can anybody help me ?
When I run this query I get ORA-00934 error.
The immediate cause of the error you are getting is that MAX() appears in the WHERE clause. One possible workaround, which might be what you intended, would be to use a subquery in the WHERE clause to identify the maximum date:
SELECT b.Str AS State,
a._Fld9059 AS Date
FROM _InfoRg9050 a
INNER JOIN EnumTexts b
ON a._Fld9052RRef = b._IDRRef
WHERE a._Fld10998 = '1104000009' AND
a._Fld10998 = (SELECT MAX(TO_DATE(_Fld9059, 'dd.mm.yyyy')) FROM _InfoRg9050)
ORDER BY a._Fld9059 DESC
However, it is not clear why you are comparing _InfoRg9050._Fld10998 to both the string '1104000009' and a date. You will need to resolve this on your own I believe to get a meaningful result.
Thank you for your help. I got it.
SELECT b."Str" AS "State"
FROM "_InfoRg9050"#SQLSERVER.UISLAB.COM a
INNER JOIN "EnumTexts"#SQLSERVER.UISLAB.COM b
ON a."_Fld9052RRef" = b."_IDRRef"
WHERE a."_Fld10998" = '1104000009' AND
a."_Fld9059" = (select MAX(a."_Fld9059") from "_InfoRg9050"#SQLSERVER.UISLAB.COM a
INNER JOIN "EnumTexts"#SQLSERVER.UISLAB.COM b
on a."_Fld9052RRef" = b."_IDRRef"
where a."_Fld10998" = '1104000009')
ORDER BY a."_Fld9059" DESC

Using TOP in ORACLE SQL 9

Hello I'am very new to writing SQL and I am trying to find the appropriate way to use TOP in Oracle SQl 9:
My example:
select * from example e, test t
where e.id = t.id
and country = 'USA'
order by state ASC;
What I am trying to do is take the bottom 20 % of my query but I know you cannot use TOP. After researching I still have not found an applicable answer. I know you have to first order them but am unsure of how to then take the bottom 20%(which would be TOP since the order is ASC)
In general (like if you want the top or bottom 17.2% of the rows) you can use row_number() and count() (analytic functions) to get the result.
20% is easier - you are looking for the top (or bottom) quintile. For this, you can use the ntile() function, like so:
select [column_names]
from (
select e.*, t.*, ntile(5) over (order by state) as nt
from ..... etc
)
where nt = 1;
The subquery is your query. The column_names in the outer query are whatever you actually need; you could also use select * but that will show the ntile too (which will be 1 in all rows).
If sorting something in ASCending order gives us the top set then surely sorting in DESCending order can give us the bottom set.
This solution uses the function NTILE() to divide the records into five buckets. The first bucket is the set we want (because sorted in descending order). Sorting in ascending order and taking the fifth quintile would have the same outcome.
select * from (
select e.*
, t.*
, ntile(5) over (order by state desc) nt
from example e, test t
where e.id = t.id
and country = 'USA'
)
where nt = 1
order by state desc
/
You don't say what your sort criteria are, so I've guessed.

ORDER BY CASE WHEN: ORDER BY items must appear in the select list

I have this example code of something I'm trying to run. Only he table names and column names were changed. What I want to do is have a result set of states and have 'NULL' be the first value and the rest of the results appear below 'NULL' in ascending order and I can't for the life of me make it work. I get the error at the bottom. This may be a very "noobish" question, but can anyone help? Much appreciated everyone!
SELECT DISTINCT
State
FROM TABLE1 (NOLOCK)
WHERE COLUMN1 NOT LIKE '%THAT%'
AND COLUMN1 NOT LIKE '%THIS%'
UNION
SELECT 'NULL'
ORDER BY ( CASE WHEN State = 'NULL' THEN 0
ELSE 1
END );
Error Message:
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.
You need to select that column... even if you don't use it later, the order by requires it to be present in the select.
SELECT DISTINCT
State,
CASE WHEN State = 'NULL' THEN 0
ELSE 1 END orderId
FROM TABLE1 (NOLOCK)
WHERE COLUMN1 NOT LIKE '%THAT%'
AND COLUMN1 NOT LIKE '%THIS%'
ORDER BY ( CASE WHEN State = 'NULL' THEN 0
ELSE 1
END );

ORA-00904: invalid column name but I am using the correct column name

Can someone see where I am going wrong in the below query? I am getting the error message that the GROUP BY column doesn't exist, but it clearly does as I see that column name in the output when I don't use the GROUP BY.
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode, SUM(orders.order_no) orders
FROM
orders, customer_address
WHERE
orders.delivery_date = '27-MAY-15'
GROUP BY
postcode;
The answer is: You cannot use an alias name in GROUP BY.
So:
GROUP BY (SELECT customer_address.post_code ...);
Or:
select postcode, sum(order_no)
from
(
SELECT
(SELECT customer_address.post_code FROM customer_address WHERE customer_address.address_type = 0 AND customer_address.customer_no = orders.customer_no) postcode,
orders.order_no
FROM orders, customer_address
WHERE orders.delivery_date = '27-MAY-15'
)
GROUP BY postcode;
EDIT:
However, your query seems wrong. Why do you cross-join orders and customer_address? By mistake I guess. Use explicit joins (INNER JOIN customer_address ON ...), when using joins to avoid such errors. But here I guess you'd just have to remove , customer_address.
Then why do you add order numbers? That doesn't seem to make sense.

how to get alphabetically next and prev records wiht minimal fetched records?

I have a page that is displaying a company name and its details from a table A.
Now say i have a company displayed,and its name is 'Company_one' now i want to have alphabetically sorted next company and previous company and their details etc.
The data in my table is not sorted.Its stored as it gets the data.
So now what kind of query should i write that it gives only one previous and one next alphabetically sorted record??
Plz help!!
There's no nice way to do that in a single query. Just do two queries.
To get the previous one:
SELECT * FROM companies
WHERE name < variable_with_current_name
ORDER BY name DESC
LIMIT 1
To get the next one along:
SELECT * FROM companies
WHERE name > variable_with_current_name
ORDER BY name ASC
LIMIT 1
You need to use the sort clause to sort your table. The prototype for sort is:
sort by fieldname
Example Query:
select * from your_table sort by company asc
If you want to limit records, use limit clause:
select * from your_table sort by company asc limit 0, 1
Based on Dominic's answer, you can achieve the same result using a single query by combining them with WHERE and OR.
For example:
SELECT * FROM `companies`
WHERE (
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` < 'variable_with_current_name'
ORDER BY `name` DESC
LIMIT 1)
, 0)
OR
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` > 'variable_with_current_name'
ORDER BY `name` ASC
LIMIT 1)
, 0)
)
Hope that helps.

Resources