sql data change [closed] - unpivot

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
Currently my data looks like this:
A 15902 8.11 9.20 7 8 5 6
A 15902 2021 8.11 7 5
A 15902 2022 9.20 8 6
I'm quite unsure how to do this.
Any help is greatly appreciated!

You can unpivot this using CROSS APPLY (VALUES
SELECT
t.Hospital,
t.Zip,
v.Year,
v.Paid$,
v.Visits,
v.LOS
FROM [MyTable] T
CROSS APPLY (VALUES
(2021, Paid$_21, Visits21, LOS21),
(2022, Paid$_22, Visits22, LOS22)
) v(Year, Paid$, Visits, LOS)
Note that this only queries the base table once.
db<>fiddle

SELECT Hospital, Zip, '2021' As Year,
Paid$_21 As Paid$, Visits21 as Visits, LOS21 as LOS
FROM [MyTable]
UNION
SELECT Hospital, Zip, '2022' As Year,
Paid$_22 As Paid$, Visits22 as Visits, LOS22 as LOS
FROM [MyTable]
You can also try UNPIVOT

Related

Create a column with dollar sign in Oracle SQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am wondering whether I can create a column with dollar sign? The output should look like the area marked by red line. But the column can still be calculated like number.
I am not sure whether it is capable.
You can try this:
select to_char(500, '$999') from dual
Column-Name with Dollar:
CREATE TABLE t1 (ID int, "$Sal" int);
INSERT INTO t1 values(1,100);
SELECT id, "$Sal" * 10 FROM t1;

Oracle pl/sql check if number starts with 9 or 8 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
i am trying to validate data inside my database that is number.
Am i able to detect the retrieved data whether it starts with 8 or 9?
For example:
8999999 is valid
9999999 is valid
7000000 is invalid
Thanks
You can try checking the first character of the field using SUBSTR():
SELECT field_value
FROM your_table
WHERE SUBSTR(TO_CHAR(field_value), 1, 1) IN ('8','9')
The following query will return all records where column data begins with an 8 or 9:
SELECT data
FROM your_table
WHERE REGEXP_LIKE (CAST(data AS varchar2(30)), '^(8|9)(*)');
I assume here that data is a numeric type, and so I cast it to varchar before using REGEXP_LIKE.

How to display multiple rows from a table without using cursor? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an existing table. I have a select query which might return multiple rows. I want to display all rows that this select query returns, without using cursor. How can I achieve it in pl sql?
If I understand your question correctly, the following is a possible solution:
BEGIN
FOR T IN (SELECT 'data_1' col_1,
'data_2' col_2,
'data_3' col_3
FROM dual) LOOP
dbms_output.put_line('Query returns: '||T.col_1||', '||T.col_2||', '||T.col_3);
END LOOP;
END;

Function to convert a string to character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
select 'open' as "documentno" from c_order
union all
select documentno as "documentno" from c_invoice
.This is not working in oracle.
i need a query that works in both oracle and postgres
You have not provided enough information to answer your question.
My guess is that you want to include columns in a union that do not have the same data type and are looking for a way to cast a number to a character value (again I'm guessing because you didn't tell us what data type documentno is).
The following works in Oracle and Postgres:
select 'open' as "documentno" from c_order
union all
select cast(documentno as varchar(20)) as "documentno" from c_invoice;
However: the first part of the union does not make sense. Why are your retrieving the same constant value for each and every row in c_order without any additional information from that table?

Convert Row into columns [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how can I convert rows of my table as column value
for eg I have a table a
emp id
1
2
3
4
and I want my output as
1 2 3 4
i am also using pivot in oracle and crosstab in postgres but not able to get desired solution as shown above.
Check the listagg function. Note you need at least Oracle 11 for this.
Use this script and change it to your values to get desired results
SELECT * FROM (SELECT emp_id, emp_points
FROM emp_data)
PIVOT (
SUM(emp_points) AS sum_emp_points
FOR (emp_id) IN (1 AS [1], 2 AS [2],3 AS [3],4 AS [4])
);
select * from (select empno from t7) pivot(min(empno) for empno in (1,2,3,4));

Resources