This question already has answers here:
Pivot in Oracle 11g
(2 answers)
Closed 2 years ago.
My table:
Based on Start and End date, I need to get output something like this:
Assume start date as 1-sep-2020 and end date as 09-sep-2020 (provided dynamically).
Based on above dates, date should be column name and id should be data under respective column.
If id is not present on respective date, in input table it should be null.
I'm not able to figure out query for this. Please, anyone suggest me in writing the query for above.
Thanks in Advance.
You can try this:
SELECT * FROM
(
SELECT name, dat, id
FROM tbl
)
PIVOT
(
max(id)
FOR dat IN ('01-Sep-2020', '02-Sep-2020', '03-Sep-2020', '04-Sep-2020', '05-Sep-2020', '06-Sep-2020', '07-Sep-2020')
)
ORDER BY name;
https://dbfiddle.uk/?rdbms=oracle_18&fiddle=63413c3d5ce0e3a67e9c18d0fe39b21f
Related
This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 1 year ago.
I am trying to solve a duplicate issue in SQL server.
I have a lot of duplicate records with the exact information except the column time like:
I would like to select the record that has the max datetime for each subset of duplicates.
I tried to use the MAX() aggregation but that does not work.
Any ideas?
In MS SQL Server, you would do something like this:
SELECT [id], [name], MAX([date])
FROM [table_name]
GROUP BY [id], [name]
SELECT id, name, MAX(date) as maxdate FROM mytable GROUP BY id, name
This should give you exactly the results you describe, in SQL Server.
This question already has answers here:
Dynamic Pivot in Oracle's SQL
(10 answers)
Dynamic pivot in oracle sql - Procedure
(1 answer)
Closed 3 years ago.
I want to assign a specific date to a variable based on the output of a query and then use that variable in another query. I am doing this because using sql pivot tables requires a defined element/date in order to utilize. I can write the dates in, but i am hoping to automate this process. Also, i would prefer not to create a temporary table or a standalone table to reference, i would like to keep this as dynamic as possible. Can a CTE or dual table be used? I have seen many examples, but i cannot tie all of them together to solve this issue.
Variables:
Variable 1: SELECT Period from Period_tbl WHERE order_id in '1' -- 1 = current month
Variable 2: SELECT Period from Period_tbl WHERE order_id in '2' -- 2 = previous month
code i want to apply this to:
SELECT *
From(
SELECT Period, Team, Description, dollars
from table_1)
PIVOT(
SUM(dollars) as Period for Period in (
to_date(variable 1, 'mm/dd/yyyy') current_mo
,to_date(variable 2, 'mm/dd/yyyy') prior_mo));
This question already has answers here:
SQL - Getting a column from another table to join this query
(2 answers)
Closed 4 years ago.
Looking for some help here on adding a column of data to my query results from another table.
So the key on both tables is customer_number.
I have a query that is the following:
SELECT [CUSTOMER_NUMBER]
,[CUSTOMER_NAME]
,count(*) 'YEARS'
FROM [DB].[dbo].[tablefromxlsimport$]
group by [CUSTOMER_NUMBER]
,[CUSTOMER_NAME]
This returns the information I am looking for from the tableformxlsimport$ but I would like to add a column of data from another table fullcustomerlist$ based on the results of the query that will append a column of data that is customer_Current_title.
Hopefully I explained this correctly
Please see the below query:
SELECT
A.[CUSTOMER_NUMBER]
,A.[CUSTOMER_NAME]
,A.YEARS
B.customer_Current_title
FROM
( SELECT
A.[CUSTOMER_NUMBER]
,A.[CUSTOMER_NAME]
,count(*) 'YEARS'
FROM [DB].[dbo].[tablefromxlsimport$] AS A
group by [CUSTOMER_NUMBER]
,[CUSTOMER_NAME]
) AS A
INNER JOIN dbo.fullcustomerlist$ AS B on A.CUSTOMER_NUMBER = B.CUSTOMER_NUMBER
This question already has an answer here:
How can I select a value from one row in SQL Server?
(1 answer)
Closed 6 years ago.
Trying to select all rows with the value 33487MO in the column ORMSTRID
This is my current query -
SELECT * from MyTable
Where ORMSTRID = "33487MO"
But it gives me an error that is saying Invalid column name '33487MO'
SELECT * from MyTable
shows me all of the rows and columns from the table, and ORMSTRID is definitely the column name.
You should use single quote with string data
According your example your query should be:
select * from mytable
where ORMSTRID like '33487MO'
This question already has answers here:
SQL multiple column ordering
(9 answers)
PostgreSQL ORDER BY issue - natural sort
(9 answers)
Closed 7 years ago.
I have a table that has a column as primary key and the values are
G1,G2,G3,...Gn
I would like to order the data but the problem is when I use the ORDER BY clause it displays data as:
G1,G10,G11... G2,G20,G21, ... G3,G30,G31....
The query I use is:
select * from myTable order by id asc;
Your id column is obviously of some text data type so the ordering is alphabetical, not by the number. To get it to work, strip the 'G' from the id column when ordering:
SELECT * FROM mytable
ORDER BY right(id, -1)::integer;