Dynamic Order by [variable] instead of column names? - abap

Is it possible to create a dynamic Order by? something like
Select * from ztable_name Order by variable_name
Or maybe are there any other way to do similar with this if it is not possible?
Thanks, Appreciate any help in advance.

To provide further detail to the answer of vwegert: In your example, variable_name has to be the name of a column (because you want to order by a column). But this goes not by using a text variable, but a text table. Imagine the table you want to order has 5 columns:
COL1
COL2
COL3
COL4
COL5
If you want to order it dynamically by, let's say COL2 and COL4, it goes like this:
DATA: itab_order TYPE TABLE OF char_72,
wa_order LIKE LINE OF itab_order.
wa_order = 'COL2'.
APPEND wa_order TO itab_order.
wa_order = 'COL4'.
APPEND wa_order TO itab_order.
SELECT *
FROM ztable_name
INTO itab_ztable_name
ORDER BY (itab_order).
ENDSELECT.
Hope this helps :)

Please check the documentation:
Specifying the Columns Dynamically
To specify the columns in the ORDER BY clause dynamically, use:
SELECT ... ORDER BY (<itab>). where <itab> is an internal table
with line type C and maximum length 72 characters containing the
column names.

Related

SQL selection from one of four columns?

I have a database table with four columns:
Column 1 | Column 2 | Column 3 | Column 4
The value in each of those four columns will either be a price such as ($3.99) or will have the value 'Listed'.
I would like to know the best SQL query to find the following:
Get all rows where all four rows equal 'Listed'
Get all rows where some of the columns have prices and some have the value 'Listed'
Try this Query !
When all four rows equals to 'Listed' :
SELECT *
FROM [table name]
WHERE
Column1='Listed'
AND
Column2='Listed'
AND
Column3='Listed'
AND
Column4='Listed'
When some of the columns have prices and some have the value 'Listed' :
SELECT *
FROM [table name]
WHERE
(
Column1='Listed'
OR
Column2='Listed'
OR
Column3='Listed'
OR
Column4='Listed'
)
AND
(
Column1 LIKE '$%.%'
OR
Column2 LIKE '$%.%'
OR
Column3 LIKE '$%.%'
OR
Column4 LIKE '$%.%'
)
These are actually fairly simple queries
Select where all 4 columns are listed.
SELECT * FROM {table}
WHERE Column1='Listed' AND Column2='Listed' AND Column3='Listed' AND Column4='Listed'
Select where at least one column is listed and at least one column is a price(numeric)
SELECT * FROM {table}
WHERE (Column1='Listed' OR Column2='Listed' OR Column3='Listed' OR Column4='Listed')
AND (ISNUMERIC(Column1) OR ISNUMERIC(Column2) OR ISNUMERIC(Column3) OR ISNUMERIC(Column4))
Something like these should give you what you want, no guarantees though since different database engines use some slightly different SQL.
I think you should do something like this:
SELECT * FROM `testing` WHERE Col1='Listed' AND Col2='Listed' AND Col3='Listed' AND Col4='Listed'
And for the "OR" statement:
SELECT * FROM `testing` WHERE Col1='Listed' OR Col2='Listed' OR Col3='Listed' OR Col4='Listed'
Of cours you'll need to replace Col1, Col2, Col3 and Col4 with your Column names. Also change your table name.

Insert Statement for List

I'm not too sure how to describe my SQL Insert statement so I will describe the expected result.
I'm building a data extract list and have a table that I've put all my data into. It's called _MATTER_LIST
What I am trying to Achieve is to have the Client_Number + Col1 combination repeat after every unique COL1+COL2+COL3 combination but not duplicate when there is already a CLIENT_NUMBER+COL1. So the end result would be:
thanks in advance for any tips.
Simple ORDER BY should work for you if i understand. Try this :
select Client_Number, Col1, Col2, Col3 from _MATTER_LIST
order by Client_Number, Col1
I've managed to fix my own issue. I added a unique key for the col1 + col2 + col3 , then make col2 repeat over each combination for example.
The result is: select * from _MATTER_LIST order by COL4, COL5

Iterate through a list to get strings in SQL

I have a SQL table as shown below. I want to generate strings using the 2 fields in my table.
A B
M1 tiger
M1 cat
M1 dog
M3 lion
I want to read in this table, count the number of rows, and store it in string variables like String1 = M1_tiger, String2 = M1_cat, etc. What's the best way to do this?
You could do a concat type query.
SELECT (Table.A + '_' + Table.B) AS A_B, COUNT(*) AS RowsCount FROM Table
I'm asuming the your table name is "Table", the result where you will find the strings you want would be the column named A_B, each record will have two things in each record, one would be the string you asked for, the other column would always be the same thing, the total number of records on you table.
The count part is kinda easy but check this link so you can use the specific count you need: http://www.w3schools.com/sql/sql_func_count.asp
You can try this:
SELECT CONCAT(A, '_', B) FROM yourtable
When you say "read in this table", do you mean read it into a programming language like C#? Or do you want to dynamically create sql variables?
You may want to use a table variable to store your strings rather than individual variables. Regarding getting the row number, you could use something like:
WITH CTE AS
(
SELECT A, B,
ROW_NUMBER() OVER (order by OrderDate) AS 'RowNumber'
FROM MyTable
)
SELECT A,B,RowNumber FROM CTE
See this answer for more on how you may choose to use the table variable.
SQL: Dynamic Variable Names
If your are using Oracle, you can also do it like:
select A ||'_'||B
from yourTable
Solution for PostgreSQL
CREATE SEQUENCE one;
SELECT array_to_string(array_agg(concat('String',nextval('one'),' = ',A,'_',B)), ', ')
AS result
FROM test_table;
DROP SEQUENCE one;
Explanation:
Create a temporary sequence 'one' in order to use nextval function.
nextval('sequence') - advance sequence and return new value.
concat('input1', ...) - concatenate all arguments.
array_agg('input1', ...); - input values, including nulls,
concatenated into an array.
array_to_string('array', 'delimiter') - concatenates array elements
using supplied delimiter and optional null string.
Drop the sequence 'one'.
The output of the query (for two test rows in test_table):
result
-------------------------------------------
String1 = M1_tiger, String2 = M1_cat
(1 row)

Change only single column values when we use * in sql

Is there a way to change only single column values as updated in selection of oracle sql.
SELECT ORDD.id||'sdaf' as id,ORDD.*
FROM RDT_ORDER ORDM,RDT_ORDERDETAIL ORDD;
Here in RDT_ORDERDETAIL am having around Hundered columns but only single coulmn I need as updated.
I don't want to select all Hundred columns by writing all columns in select query as below
SELECT ORDD.id||'sdaf' as id,ORDD.col1,ORDD.col2,ORDD.col3,ORDD.col4
FROM RDT_ORDER ORDM,RDT_ORDERDETAIL ORDD;
Please note I have removed where clause,I need to get single updated column values other columns as it is.But I shouldn't write all coulmns in Select.
Existing DB Records :
ID NAME COL3 COL4
1 name1 .. ..
2 name2 .. ..
3 name3 .. ..
Now Result :
ID ID NAME COL3 COL4
1_'sdaf' 1 name1 .. ..
2_'sdaf' 2 name2 .. ..
3_'sdaf' 3 name3 .. ..
Expected Result :
ID NAME COL3 COL4
1_'sdaf' name1 .. ..
2_'sdaf' name2 .. ..
3_'sdaf' name3 .. ..
If you want to select all but one rows then the answer is No - you either get all fields (*) OR specify the fields you want. This is how Oracle works.
If you still persists, then there might be some work around like Dynamic SQL using query generator or data cartridge
You can refer here for similar question.
If you want to have a string manipulation and all columns you can do using alias.
SELECT COL1||'_asd_', T.*
FROM MYTABLE T;
Your idea of getting all columns from a table with a manipulation on one or more columns without writers cramp is not possible directly. You may try dynamic SQL but I will not suggest that for anything because of low maintainability and its a head ache on a long run.
I don't think you can exclude a single column by quering with select * from.... May be a VIEW can help you on this.
CREATE OR REPLACE VIEW <view_name> AS
SELECT ordd.id||'sdaf' AS id, ordd.col1, ordd.col2, ordd.col3, ordd.col4
FROM rdt_order ordm,
rdt_orderdetail ordd
WHERE .....;

SQL query help!! I'm trying to select the row that DOESN'T start with a number

I have 10,001 rows in my table, and all of the rows except one start with a number. I need to find this one row that doesn't start with a number, or even that doesn't contain a number.
So this is what I have:
Select col1 from table1 where col1 not like '?%'
Is this even close? I need to find the row that doesn't have a number...
Thanks!!
UPDATE: I am using a sqlite database
Use:
SELECT col1
FROM table1
WHERE SUBSTR(col1, 1, 1) NOT BETWEEN 0 AND 9
Reference:
core functions (incl SUBSTR)
LIKE
On Sql Server,
Select * From table
Where col1 Like '[^0-9]%'
EDIT: Don't know if there is an equivilent on SQLLIte,
but this will work...
Select * From table
Where col1 Not Like '0%'
And col1 Not Like '1%'
...
And col1 Not Like '9%'
There is a post in code project that allow you to use Regex with Ms SQL.
Hope this help.
The easiest way might be to just note that you're not using numbers; you're using strings that happen to have numbers in them. In this case, you can do
select * from table1 where col1 not between '0' and '9:';
(where the colon is the ASCII character after '9'; this way '9999999' won't be found).
It should be a lot less expensive than some of the other suggestions (e.g., checking the value of the first character).
#Dueber got me thinking, couldn't you just do this?
SELECT * FROM table1 WHERE col1 > '9'
Grab the first character, see if it's numeric.
SELECT *
FROM table1
WHERE ISNUMERIC(SUBSTRING(col1,1,1)) = 0
SUBSTRING
ISNUMERIC