I have a working query using the PIVOT function but I am wondering if I have to define all values I want to see as column headers. Here is what I have:
select * from
(
SELECT ID, product, notebal
from accounts
where (accounts.effdate = ( Select MAX(EFFDATE) FROM accounts))
)
PIVOT
(
SUM(notebal) for product in ('NAID','UAID','SVGS','DRFT')
)
Order by 3 desc
There are 28 different products. Do I have to define all 28 in the "for product in()" statement?
Related
Oracle 19.3 on Win2019
Looking for a solution to pivot data based on the subquery. I looked for examples, but all I found is the "pivot xml" solution, which returns xml-formated column.
Here is my query:
with assign_data as
(
select --*
style_id
,color_id
,size_id
,in_whse_date
,sum(pd2_assign_so_quantity) qty
from pd2_assignment
where business_unit_id = '81'
and style_id = 'Y186F3D'
and color_id = '035Y'
and property_mark = '8FQR'
group by
style_id
,color_id
,size_id
,in_whse_date
order by size_id
)
select * from assign_data
pivot XML (
sum(qty) for (in_whse_date) in ( select distinct in_whse_date from assign_data)
)
Inner query produces this:
Pivot XML produces this:
Question: Is it possible to generate PIVOT with columns from a subquery that are not in xml format?
If not, is there another way to simulate this pivot behavior?
Add column in Table items based on left join on item_category table. I want to add column itemcategory_category in the items table based on the left join
=ADDCOLUMNS (
items, LOOKUPVALUE (
'item_category'[ITEMCATEGORY_CATEGORY],
'items'[KEY_PRODUCTCATEGORY], item_category[KEY_PRODUCTCATEGORY]
)
)
Items Table
KEY_PRODUCTCATEGORY
1
item_category table
KEY_PRODUCTCATEGORY ITEMCATEGORY_CATEGORY
1 A
If you have one to one relationship
Table = ADDCOLUMNS(items,"new",RELATED(item_category[ITEMCATEGORY_CATEGORY]))
If you have no relationship
Table 2 = ADDCOLUMNS(items,"new",CALCULATE(MAXX(filter(item_category,item_category[KEY_PRODUCTCATEGORY]=MAX(items[KEY_PRODUCTCATEGORY])),item_category[ITEMCATEGORY_CATEGORY])))
If you are only keen on LOOKUPVALUE with or without relationship. LOOKUPVALUE is not dependent upon any relationship but it has performance issues.
Table 3 = ADDCOLUMNS(items,"new",LOOKUPVALUE(item_category[ITEMCATEGORY_CATEGORY],item_category[KEY_PRODUCTCATEGORY],items[KEY_PRODUCTCATEGORY]))
I tested out with following and it works fine.
I have an oracle query that uses a created table as part of the code. Every time I need to run a report I delete current data and import the new data I receive. This is one column of id's. I need to create a report on SSRS in which the user can input this data into said table as a parameter. I have designed a simple report that they can enter some of the id's into a parameter, but there may be times when they need to enter in a few thousand id's, and the report already runs long. Here is what the SSRS code currently says:
select distinct n.id, n.notes
from notes n
join (
select max(seq_num) as seqnum, id from notes group by id) maxresults
on n.id = maxresults.ID
where n.seq_num = maxresults.seqnum
and n.id in (#MyParam)
Is there a way to have MyParam insert data into a table I would join called My_ID, joining as Join My_Id id on n.id = id.id
I do not have permissions to create functions or procedures in the database.
Thank you
You may try the trick with MATERIALIZE hint which normally forces Oracle to create a temporary table :
WITH cte1 AS
( SELECT /*+ MATERIALIZE */ 1 as id FROM DUAL
UNION ALL
SELECT 2 DUAL
)
SELECT a.*
FROM table1 a
INNER JOIN cte1 b ON b.id = a.id
I have a list of selected accounts and a table of account records (may or may not be in the selected list) across 40 months. I am able to select all the selected accounts that appear at least once in the table, but I also want to see which accounts appear in all 40 months. Like, I am hoping to do something similar to out join partition by except I want a smaller set of the data.
How can I do that? Thank you!
example:
select distinct table1.acct_no
from table1
inner join selectedAcct
on table1.acct_no = selectedAcct.acct_no
Something lik (not compiled/tested):
with acct_month_list as
(
-- get list of accts/months with missing months
-- as null
select distinct a.acctno, m.monno
from acctrecs a, mons m
where m.monno = a.monno(+)
)
select a2.acctno
from accounts a2
where not exists ( -- any accounst with a null monno have a missing
-- month
select null
from acct_month_list al
where al.acctno = a2.acctno
and a1.monno is null )
and exists ( -- ignore ones that have no records at all
select null
from acctrecs ar
where ar.acctno = a2.acctno )
I have a query like below - table names etc. changed for keeping the actual data private
SELECT inv.*,TRUNC(sysdate)
FROM Invoice inv
WHERE (inv.carrier,inv.pro,inv.ndate) IN
(
SELECT carrier,pro,n_dt FROM Order where TRUNC(Order.cr_dt) = TRUNC(sysdate)
)
I am selecting records from Invoice based on Order. i.e. all records from Invoice which are common with order records for today, based on those 3 columns...
Now I want to select Order_Num from Order in my select query as well.. so that I can use the whole thing to insert it into totally seperate table, let's say orderedInvoices.
insert into orderedInvoices(seq_no,..same columns as Inv...,Cr_dt)
(
SELECT **Order.Order_Num**, inv.*,TRUNC(sysdate)
FROM Invoice inv
WHERE (inv.carrier,inv.pro,inv.ndate) IN
(
SELECT carrier,pro,n_dt FROM Order where TRUNC(Order.cr_dt) = TRUNC(sysdate)
)
)
?? - how to do I select that Order_Num in main query for each records of that sub query?
p.s. I understand that trunc(cr_dt) will not use index on cr_dt (if a index is there..) but I couldn't select records unless I omit the time part of it..:(
If the table ORDER1 is unique on CARRIER, PRO and N_DT you can use a JOIN instead of IN to restrict your records, it'll also enable you to select whatever data you want from either table:
select order.order_num, inv.*, trunc(sysdate)
from Invoice inv
join order ord
on inv.carrier = ord.carrier
and inv.pro = ord.pro
and inv.ndate = ord.n_dt
where trunc(order.cr_dt) = trunc(sysdate)
If it's not unique then you have to use DISTINCT to deduplicate your record set.
Though using TRUNC() on CR_DT will not use an index on that column you can use a functional index on this if you do need an index.
create index i_order_trunc_cr_dt on order (trunc(cr_dt));
1. This is a really bad name for a table as it's a keyword, consider using ORDERS instead.