Oracle SQL create variable and use output within another query [duplicate] - sql

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));

Related

How to get min value by comparing multiple columns in SQL [duplicate]

This question already has answers here:
What's the best way to select the minimum value from several columns?
(20 answers)
Closed 4 months ago.
I'm trying to get the min value from the table by comparing four adjacent columns (Date-1 to Date-4) in that table and updating the min value in the final column. But I'm receiving only NULL as the min value instead of getting the min value from four columns.
I'm using case for comparing the columns.
As SQL Server does not yet support LEAST in its current version SQL Server 2019, you can apply a lateral cross join on an aggregation query on the values instead.
select *
from mytable t
cross apply
(
select min(dt) as final_min_date
from (values (t.date_1), (t.date_2), (t.date_3), (t.date_4)) all_dates (dt)
) min_date;

Dynamic oracle sql query based on start and end date [duplicate]

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

Why can we not use the Where Clause on a Grouped statement? [duplicate]

This question already has answers here:
SQL - WHERE Condition on SUM()
(4 answers)
Closed 5 years ago.
For a simplified example of my issue, why could I not do this?
select id_number, sum(revenue)
from table A
where sum(revenue)>1000
group by id_number
(In case this causes any confusion, why can I not only return the results that have over 1000 in revenue?)
Disclaimer, I'm somewhat new to SQL but couldn't find any documentation regarding this.
Thanks,
This is by design of SQL. By using WHERE You filter the source table. And the sequence of statement fragments is as written. That means You would like to filter the SUM which is applied on filtered table. That means You must use filter on already grouped result using HAVING clause. Use
select id_number, sum(revenue)
from table A
group by id_number
having sum(revenue) > 1000
Simple answer is because the WHERE clause is evaluated before the aggregation clause. Therefore, you are trying to filter based on something that doesn't exist yet. However, you can solve that problem by making it exist first. Write a subquery, then select from that:
WITH RevenueTotals AS (SELECT id_number, sum(revenue) AS Rev_Total
FROM table A
GROUP BY id_number)
SELECT id_number, Rev_Total
FROM RevenueTotals
WHERE Rev_Total > 1000

SQL Select all rows with specific value [duplicate]

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'

Multiple result-sets using WITH clause [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Multiple Select Statements using SQL Server 2005 “WITH” Statement
There's any way to get multiple result-sets from a query using WITH clause?
I'm using MS SQL 2005.
;with temp as
(
SELECT '1' as [Sample]
)
--SELECT COUNT(*) FROM temp
SELECT * FROM temp
This works with each select (count or *), but I need to return both result-sets from the same "temp".
Is that possible?
Use UNION ALL:
;with temp as
(
SELECT '1' as [Sample]
)
SELECT COUNT(*) FROM temp
UNION ALL
SELECT * FROM temp
I'd suggest you need to re-work whatever was meant to consume this pair of result sets so that it doesn't need to know how many results are going to be returned before the results arrive. If that's not the case, I can't think of where else you'd need to retrieve the COUNT of the result set as well as the result set itself.