WITH prod AS
(SELECT points_spent,price_of_item,channel FROM transaction_history
where channel=?
)
SELECT *
from prod pivot (sum(points_spent) for (channel) in ('AP' as rpts_admin_portal , 'MB' as rpts_member_portal ),
SUM(price_of_item) FOR (channel) IN ('AP' fifty_percent_cashback_price,'MB' retail_price)
I have tried to achieve to show the column name dynamically using pivot. Here i need to show above two columns name such as when user purchasing an item via member portal need to show the rpts_member_portal,if it admin portal need to show like rpts_admin_portal. price_of_item also similar. But i can achieve for only one column in oracle. how to do this for price_of_item also. Help me to overcome this problem.
Actual data is
points_spent price_of_item channel
============ ============= ==============
100 100 AP
50 100 MB
Expected result set is if channel is Admin portal
rpts_admin_portal fifty_percent_cashback_price channel
================= ============================= ==========
100 100 AP
if it is member portal expected result set is
rpts_member_portal retail_price channel
================= ============================= ==========
50 50 MB
Related
i have an item table that has an ID ,Product, cost ,quantity and status field. what i want is to up date an item based on the sum of the quantity of the products where the ID is the same.
so for example
ID Product cost quantity status
1 computer 100 10
1 tablet 200 10
2 ipad 50 15
now when the supplier is delivering these items i want when the quantity reaches 0 for the same Id the status must change to Received other than that it should say receiving.
eg if the supplier brings 2 computers the quantity will change to 8 and the status for ID 1 will say receiving, if the supplier brings 8 computers the status should still say receiving because the tablet has an outstanding balance of 10. but when the supplier brings 10 tablets and both ID 1 quantity is 0 the status for both should say received
ID Product cost quantity status
1 computer 100 0 Received
1 tablet 200 0 Received
2 ipad 50 15
what i have tried is
UPDATE item
SET update_status = CASE
when sum (quantity) >0 THEN
'Receiving'
ELSE
'Received'
END
where approvalid =:p5_approvalid;
but i am getting group function is not allowed here
I fully support mathguys comment. Also, reconsider your naming choices. A table column named ID better be a unique primary key.
If you still want to do this, use MERGE:
merge into item
using (
select id, sum(quantity) as sum_quantity
from item
group by id
) u
on (item.id = u.id)
when matched then update
set status = case when sum_quantity > 0 then 'Receiving' else 'Received' end
The using clause will calculate the quantity sums by ID, this way it wont be necessary to use any aggregation function in the set clause.
I have two table of my store and working on Oracle. Image First table describe about my transaction in store, there are two types of transaction (MR & SR), MR means adding products in Store and SR means removing products from my storage. What I wanted to do get the final closing of my storage. After transaction final Quantity every products as shown in Image. I have tried many solution but can't finish it. so I could not show now. Please help me to sort this problem. Thanks
You can use case as below to decrease and increase the quantity based on type and then group by Name and find the sum of quantity derived from the case statement to get your desired result.
select row_number() over (order by a.Name) as Sl,a.Name, sum(a.qntity) as qntity
from
(select t2.Name,case when t1.type='MR' then t2.qntity else -(t2.qntity) end as qntity
from table1 t1,table2 t2 where t1.oid=t2.table01_oid) a
group by a.Name;
This query will provide result as below:
SL NAME QNTITY
1 Balls 0
2 Books 6
3 Pencil 13
I have following data table.
ID salary occupation
1 5000 Engineer
2 6000 Doctor
3 8000 Pilot
4 1000 Army
1 3000 Engineer
2 4000 Teacher
3 2000 Engineer
1 1000 Teacher
3 1000 Engineer
1 5000 Doctor
Now I want to add another column flag to this table so that it looks in the following way.
ID salary occupation Flag
1 5000 Engineer 0
2 6000 Doctor 0
3 8000 Pilot 0
4 1000 Army 0
1 3000 Engineer 1
2 4000 Teacher 1
3 2000 Engineer 1
1 1000 Teacher 2
3 1000 Engineer 2
1 5000 Doctor 3
Now how can I update my original table to the above format using HIVE?
Kindly help me.
Provided that you have data in your files for the additional column you can use Add Column clause for Alter Table.
In your example do something like this:
Alter table Test ADD COLUMNS (flag TINYINT);
Or you can try REPLACE COLUMNS as well:
Alter Table test REPLACE COLUMNS (id int, salary int, occupation String, flag tinyint);
You might need to load(overwrite) your dataset again though(just a speculation!!!).
You can definitely add new columns in HIVE table using alter command as told above
hive>Alter table Test ADD COLUMNS (flag TINYINT);
In Hive 0.13 and earlier releases, column will have NULL values but HIVE 0.14.0 and later release, you can update the column values using UPDATE command
Another way is, after adding column using ALTER command, you can overwrite the existing data with the new data(having Flag column)
hive> LOAD DATA LOCAL INPATH 'flagfile.txt' OVERWRITE INTO TABLE <tablename>;
I have a table with following structure
SubId Status
---------------------------------------
001 Active
001 Partially Active
While displaying this record, I need to display this like this
SubId Status
---------------------------------------
001 Active/Partially active
I tried using distinct, but this returns both rows.
Any idea how can I get only 1 row instead of 2. I know it would be simple. I just cannot find it.
select subid,
listagg(status, '/') within group (order by null) as status
from the_table
group by subid;
I have this queries:
select idCustumer, Name, Address from Customer
This give me the header of the report, and this one:
select idCustomer, Description, Sum(Total) as Total from Product group by Description
First query runs:
idCustomer Name Address
1 Phil Fake Av. 1234
2 John Fake Av. 4321
The second:
idCustomer Description Sum(Total)
1 PROD01 10
1 PROD02 20
2 PROD01 30
When i use generate a report using one customer it's ok. The problem is with 2 or more customers in the same report.
What i do?
I create 1 dataset with 2 tables (1 per query) linking them by idCustomer. I create a report using this dataset, the first table (header) works fine, it prints my four test customers.
Then i add a SubReport.
rpt.Subreports(0).SetDataSource(subReportes.Tables(1)) 'Tables(1) is 2nd Query
But this print the same info again and again.
My Report:
Customer: Phil Address: Fake Av. 1234
Order:
PROD01 10
PROD02 20
PROD01 30
Total 60
Customer: John Address: Fake Av. 4321
Order:
PROD01 10
PROD02 20
PROD01 30
Total 60
Is there a guide or link where i can learn how to include this subreport so i get (or if there is a another way to accomplish this?)
Customer: Phil Address: Fake Av. 1234
Order:
PROD01 10
PROD02 20
Total 30
Customer: John Address: Fake Av. 4321
Order:
PROD01 10
Total 10
I'm using VS 2010 (VB), SQL Server 2008 R2, Crystal Report 2010
Thanks!
Your design is totally wrong. First you need to write a procedure in TSQL like this
SELECT C.idCustomer CustomerId, C.Name Name, C.Address Address, P.Description Description, P.Total Total
FROM CUSTOMER C
INNER JOIN PRODUCT P
ON C.idCustumer = P.idCustomer
And add set this proc as the datasource for the report.
Then you have to do a grouping for field, CustomerId in the report. After that you need to place the customer details in the group header section and the order details in the Details section. And also to get the total value for the you have to use the summary field in crystal reports