I am using below query to create a table
CREATE TABLE FINAL_TBL AS SELECT /*+ leading(e) use_hash(t) full(t) parallel(t,2) */
t.customer_id
, max(decode(type,'C',t.num,null)) AS CONTACT_TELNO
, max(decode(type,'H',t.num,null)) AS HOME_TELNO
, max(decode(type,'W',t.num,null)) AS WORK_TELNO
, max(decode(type,'O',t.num,null)) AS OTHER_TELNO
from TABLE_A e, TMP_NUMBERS t
where t.num = e.num
and e.status = 'P'
group by t.customer_id
order by 1;
But the table is getting created with the truncated column name as below
CUSTOMER_ID CONTACT_TE HOME_TELNO WORK_TELNO OTHER_TELN
Why is So? How can I fix it?
It sounds like you must be running this in SQL*Plus... you need to use the command COLUMN with the FORMAT option, like so: column CONTACT_TELEPHONE format a18 (notice no semicolon since this is not a SQL command, it only applies to your interface). The SQL query runs as you expected, but then SQL*Plus formats the result according to its own settings.
Related
I have this sql statment stored in a string variable
SELECT
o.id_order,
o.registerdate,
i.latest_invoice,
d.latest_delivery
FROM
order o
JOIN
(
SELECT
id_address,
max(registerdate) as latest_invoice
FROM
invoice
GROUP BY
id_address
) i
ON o.id_address = i.id_address
JOIN
(
SELECT
id_address,
max(registerdate) as latest_delivery
FROM
delivery
GROUP BY
id_address
) d
ON o.id_address = d.id_address
WHERE
o.id_address = '189'
When I execute the query I get an error at the position of order o, because the compiler thinks that it is the ORDER BY command. Do you know how can I avoid this?
You shouldn't be using reserved keywords as table names, but if you insist you have to use double quotes:
from "order" o
Note that with double quotes the name is case-sensitive. Depending on how you created that table, you might need "ORDER" or "Order". In psql you should check the correct case using the \d command.
I am currently working with a MS SQL database on Windows 2012 Server
I need to query only 1 column from a table that I only have access to read, not make any kind of changes.
Problem is that the name of the column is "Value"
My code is this:
SELECT 'Value' FROM table
If I add
`ORDER BY 'Value'`
The issue is that the query is returning an empty list of results.
Things I've tried already
I tried replacing ' with `"' but this didn't work either.
I also tried writing SELECT * instead of SELECT VALUE
Using the table name in the SELECT or ORDER clauses again didn't help
You are claiming that this query:
SELECT 'Value'
FROM table
ORDER BY 'Value'
Is returning no rows. That's not quite correct. It is returning an error because SQL Server does not allow constant expressions as keys for ORDER BY (or GROUP BY for that matter).
Do not use single quotes. In this case:
SELECT 'Value' as val
FROM table
ORDER BY val;
Or, if value is a column in the table:
SELECT t.Value
FROM table t
ORDER BY t.Value;
Value is not a reserved word in SQL Server, but if it were, you could escape it:
SELECT t.[Value]
FROM table t
ORDER BY t.[Value];
it looks like your table has null values. and because of the order by all null values come first.
try to add filter like this
select Value FROM table
where Value is not null and Value <> ''
order by Value
I am using Aginity workbench to query a netezza database and trying to create simple query. Basically, within a table there are two columns that I want to combine to create an ID (they do make up two parts of actual unique IDs). Within the query I want to search unique IDs e.g. the IDRef value. So in the below example, I want to return all rows within the table where column X equals 282 and Z equals 55. The below code isnt returning anything.
SELECT T_ROUTE || CSC as IDRef, *
FROM HUB_DATABASE
WHERE POSTCODE like 'A%'
AND CURRENT_RECORD_FLAG = '1'
AND IDRef = 28255
LIMIT 100
;
So the below code works ok in the same way, but just trying to use a smarter method of doing this and of course furthering my sql knowledge.
SELECT * FROM HUB_DATABASE
WHERE T_ROUTE = '282'
AND CSC = '55'
AND POSTCODE like 'A%'
AND CURRENT_RECORD_FLAG = 1
LIMIT 100;
Use a subquery. You cannot refer to a column alias in the same level where it is defined:
SELECT hb.*
FROM (SELECT T_ROUTE || CSC as IDRef, hd.*
FROM HUB_DATABASE hd
WHERE POSTCODE like 'A%' AND
CURRENT_RECORD_FLAG = '1'
) hb
WHERE IDRef = 28255
LIMIT 100 ;
You will get an error if IDRef is already defined in HUB_DATABASE. If so, you need to use a different name or remove that column from the subquery.
I currently have the table as follows on the picture.
I would like to write a query which returns all the names and the travel_date with the maximal 'total' value for each name. For example, I would like the query to return in this case:
Armand 2012-07-18 and Elish 2012-06-18. How could I do that ? Thanks in advance
In most cases, you'll find that the procedure for this is relatively universal. The following example will work in MySQL, MSSQL and DB2 (among others).
SELECT
a.name,
a.travel_date,
a.total
FROM test_table AS a
INNER JOIN ( SELECT `name`, MAX(total) AS `total` FROM test_table GROUP BY `name` ) AS b
ON a.name = b.name
AND a.total = b.total;
Here's an example of the sample data I worked with including the results after running the query.
-
Edit: As jarlh, the initial query I wrote was indeed wrong. The following query should provide the results that you requested in the comment below.
SELECT name, MAX(travel_date)
FROM my_table
GROUP BY name;
I have query like this:
SELECT
xmlelement("objects",
xmlagg(
xmlelement("object",
xmlelement("accountId", ACCOUNTS.accountId),
xmlelement("address", ACCOUNTS.ADDRESS)
)
)
INTO obj_info_xml
FROM
ACCOUNTS
WHERE account_code IN (SELECT EXTRACTVALUE(VALUE(accountCodes), '/accountCode/text()') as accountCode
FROM TABLE(XMLSEQUENCE(EXTRACT(X, '//accountCodes/accountCode'))) accountCodes);
When I hardcode values inside IN clause then query executes fast, but when I use subquery to select from xml then I can't get results because it executes very slow. Do you have any suggestions?
Assuming that x is a PL/SQL variable containing the list of your account codes, something like this ought to speed things up a little bit:
select xmlelement("objects",
xmlagg(xmlelement("object",
xmlelement("accountId", accounts.accountid),
xmlelement("address", accounts.address))
)
) account_xml
into obj_info_xml
from accounts a
inner join xmltable('//accountCodes/accountCode'
passing x
columns account_code varchar2(30) path '.') xdata -- amend datatype as appropriate
on a.account_code = xdata.account_code;
N.B. untested, due to lack of sample data.
Ok, what does the following give you?
select xmlelement("objects",
xmlagg(xmlelement("object",
xmlelement("accountId", accounts.accountid),
xmlelement("address", accounts.address))
)
) account_xml
into obj_info_xml
from accounts a
where a.account_code in (select /*+ dynamic_sampling(xdata 10) */
account_code
from xmltable('//accountCodes/accountCode'
passing x
columns account_code varchar2(30) path '.') xdata); -- amend datatype as appropriate
Another suggestion would be to replace /*+ dynamic_sampling(xdata 10) */ with /* cardinality(xdata <roughly expected number of rows>) */ (overwriting with the relevant number, of course).
Also, can you edit your question to provide the execution plans for the query with and without the hardcoded variables, please?