I have a table structure like this in Oracle
Product_ID Product_SKU Product_Zone Product_Price
1 123 Zone1 10
2 123 Zone2 12
3 456 Zone1 13
4 456 Zone2 14
How I can get the result in horizontal format like this
Product_SKU Zone1_Price Zone2_Price
123 10 12
456 13 14
Thanks
Here's a simple, aggregation option:
SQL> with test (product_id, product_sku, product_zone, product_price) as
2 -- sample data
3 (select 1, 123, 'zone1', 10 from dual union all
4 select 2, 123, 'zone2', 12 from dual union all
5 select 3, 456, 'zone1', 13 from dual union all
6 select 4, 456, 'zone2', 14 from dual
7 )
8 select product_sku,
9 max(case when product_zone = 'zone1' then product_price end) zone1_price,
10 max(case when product_zone = 'zone2' then product_price end) zone2_price
11 from test
12 group by product_sku;
PRODUCT_SKU ZONE1_PRICE ZONE2_PRICE
----------- ----------- -----------
123 10 12
456 13 14
SQL>
I need to select a column which have same values with exactly three counts and having different values in another column and finally order by updated_time.
For this example below, I need to fetch only the value 15 from column1
column1 column2 updated_time
12 21 2019-01-05 01:36:50.995476
12 21 2018-04-05 01:36:50.995476
12 21 2019-02-05 01:36:50.995476
11 25 2019-03-05 01:36:50.995476
11 25 2019-02-05 01:36:50.995476
11 25 2019-04-04 01:36:50.995476
11 25 2019-05-05 01:36:50.995476
15 27 2019-01-05 01:36:50.995476
15 26 2019-01-05 01:36:50.995476
15 29 2019-02-05 01:36:50.995476
16 29 2019-04-03 01:36:50.995476
17 31 2019-04-03 01:36:50.995476
To get the values of column1 you must group by column1.
same values with exactly three counts
means count(*) = 3, and
having different values in another column
means count(distinct column2) = 3:
select * from tablename
where column1 in (
select column1
from tablename
group by column1
having
count(*) = 3
and
count(distinct column2) = 3
)
order by updated_time
I have two tables:
table1
emp empname mangr mangrname
1 emp1 3 emp3
2 emp2 3 emp3
3 emp3 6 emp6
4 emp4 5 emp5
5 emp5 7 emp7
table 2
emp mngr score
1 3 18
2 3 19
3 6 15
4 5 18
5 7 15
1 6 18
2 6 16
3 7 18
4 3 19
output will be
empname mangrname score_by_mangr mangr_of_mangr score_by_mangr_of_mangr avg_score_by_others
I have been trying using the below query, but its going no where!!!
SELECT UD.emp, UD.empname, UD.mangrname,
(SELECT score FROM TABLE2 WHERE mngr =(SELECT DISTINCT mangr FROM TABLE1 WHERE mangrname=UD.mangrname) ),
AVG(VAL(US.score))
FROM table2 US, table1 UD
WHERE US.EMP_ID=UD.EMP_ID
AND US.mangr=UD.mangr
GROUP BY UD.emp, UD.empname, UD.mangrname;
I have a table structure
ID Col_1 col_2 col_3 col_4
1 34 23 45 32
2 20 19 67 18
3 40 10 76 86
I here want the max value from col_1,col_,col_3,col_4 so my output looks like
ID Col_1 col_2 col_3 col_4 max
1 34 23 45 32 45
2 20 19 67 18 67
3 40 10 76 86 86
any help would be much appreciated.
Use a Modified Java Script Value step with the following code:
var max = Math.max(col_1,col_2,col_3,col_4);
You can use Memory Group By or Group By steps in Pentaho. Use the aggregation method as "Maximum" based on your grouping id.
I have two tables, T_TEST and T_DEFAULT_DATE. T_TEST contains date and amount, and T_DEFAULT_DATE contains just P_DATE.
First table T_TEST:
DATE AMOUNT
-------- ----------
01.01.99 77
16.02.99 59
01.01.00 12
15.01.00 32
01.02.00 144
15.02.00 320
16.02.00 521
01.03.00 98
15.03.00 76
16.03.00 33
01.01.01 65
15.01.01 78
01.02.01 95
15.02.01 39
16.02.01 97
02.02.02 63
07.03.02 75
And second table T_DEFAULT_DATE:
P_DATE
--------
16.02.01
What I want to get is two queries established in a single query :
1. what is the amount of sale achieved on the same day last year (- 12 mounths)
2. amount of sales for whole past year (based on table T_DEFAULT_DATE)
3. the amount (sum) for whole mounth (default mounth : 1.2. 2001 - 28.2.2001)
Expected output is :
P_SDLY P_LY P_MS
-----------
521 1236 231
I tryed with add_months(t_default_date.p_date, -12) , but I didn't get expected result. Please help
You can try something like this, assuming that your fields are stored in date columns.
SQL> with t_test(date_, amount) as
2 (
3 select to_date('01.01.99', 'dd.mm.rr'), 77 from dual union all
4 select to_date('16.02.99', 'dd.mm.rr'), 59 from dual union all
5 select to_date('01.01.00', 'dd.mm.rr'), 12 from dual union all
6 select to_date('15.01.00', 'dd.mm.rr'), 32 from dual union all
7 select to_date('01.02.00', 'dd.mm.rr'), 144 from dual union all
8 select to_date('15.02.00', 'dd.mm.rr'), 320 from dual union all
9 select to_date('16.02.00', 'dd.mm.rr'), 521 from dual union all
10 select to_date('01.03.00', 'dd.mm.rr'), 98 from dual union all
11 select to_date('15.03.00', 'dd.mm.rr'), 76 from dual union all
12 select to_date('16.03.00', 'dd.mm.rr'), 33 from dual union all
13 select to_date('01.01.01', 'dd.mm.rr'), 65 from dual union all
14 select to_date('15.01.01', 'dd.mm.rr'), 78 from dual union all
15 select to_date('01.02.01', 'dd.mm.rr'), 95 from dual union all
16 select to_date('15.02.01', 'dd.mm.rr'), 39 from dual union all
17 select to_date('16.02.01', 'dd.mm.rr'), 97 from dual union all
18 select to_date('02.02.02', 'dd.mm.rr'), 63 from dual union all
19 select to_date('07.03.02', 'dd.mm.rr'), 75 from dual
20 ),
21 t_default_date(p_date) as
22 (
23 select to_date('16.02.01', 'dd.mm.rr') from dual
24 )
25 select sum(
26 case
27 when date_ between add_months(trunc(p_date, 'yyyy'), -12)
28 and trunc(p_date, 'yyyy')-1
29 then amount
30 else 0
31 end
32 ) as year,
33 sum( decode (date_, add_months(p_date, -12), amount, 0) ) as day,
34 sum( case
35 when date_ between
36 trunc(p_date, 'MM') and
37 last_day(p_date)
38 then amount
39 else
40 0
41 end
42 ) as month
43 from t_test
44 inner join t_default_date on (date_ between add_months(trunc(p_date, 'yyyy'), -12) and last_day(p_date) );
YEAR DAY MONTH
---------- ---------- ----------
1236 521 231
SQL>
This makes use of add_months to get exactly "one year ago"; if you need "365 days ago" (think of leap years), consider using something like date - 365