I have a table with vaues like
MY_ID(NUMBER) COL_1(NUMBER) COL_2(NUMBER) COL_3(VARCHAR2)
11 1001 NULL GT
11 NULL 1002 TG
11 NULL 1003 TG2
12 1004 NULL GT
12 NULL 1006 TG
12 NULL 1005 TG2
My expected result is
MY_ID(NUMBER) COL_1(NUMBER) COL_2(NUMBER) COL_3(VARCHAR2)
11 1001 1003 TG2
12 1004 1006 TG
I can use MAX for numbers, but how about the Varchar2?
How can I combine multiple rows like this?
MAX aggregate functions can be used for varchar too, see this example:
SELECT my_id, max( col_1 ), max( col_2 ), max( col_3 )
FROM Table1
GROUP BY my_id;
Demo: http://sqlfiddle.com/#!4/406e8d7/6
| MY_ID | MAX(COL_1) | MAX(COL_2) | MAX(COL_3) |
|-------|------------|------------|------------|
| 11 | 1001 | 1003 | TG2 |
| 12 | 1004 | 1006 | TG2 |
However, in your question is stated that for the record 12 must be returned value TG instead TG2. I'm guessing that you want to return not the maximum column value of COL_3, but the value from the record for which the maximum value from column COL2 exists. In such a case you can use a query like this:
SELECT my_id, max( col_1 ), max( col_2 ),
max( col_3 ) KEEP (DENSE_RANK LAST ORDER BY col_2 NULLS FIRST)
FROM Table1
GROUP BY my_id;
Demo: http://sqlfiddle.com/#!4/406e8d7/6
| MY_ID | MAX(COL_1) | MAX(COL_2) | MAX(COL_3)KEEP(DENSE_RANKLASTORDERBYCOL_2NULLSFIRST) |
|-------|------------|------------|------------------------------------------------------|
| 11 | 1001 | 1003 | TG2 |
| 12 | 1004 | 1006 | TG |
Related
I m trying to replicate this sql syntax in Vertica, but it returns "ERROR: Subqueries in the ON clause are not supported".
The aim is to join two tables, table1 and table2, on column and date, if a.date = b.date or the closest but lesser b.date.
Any hint?
SELECT *
FROM table1 a
LEFT JOIN table2 b
ON a.column = b.column
AND b.Date = (SELECT MAX (b2.Date)
FROM table2 b2
WHERE a.column = b2.column
AND b2.Date <= a.Date)
Vertica has something handier for that: the event series join. With that, you can OUTER JOIN two tables so that they match the same or the immediately preceding value of the join column of the other table. The predicate is INTERPOLATE PREVIOUS VALUE instead of an equi-predicate.
Here's an example with the oil pressure curve and the engine rpm curve with the timestamp matching only once.
DROP TABLE IF EXISTS oilpressure;
CREATE TABLE oilpressure (
op_vid,op_ts,op_psi
) AS (
SELECT 42,TIMESTAMP '2020-04-01 07:00:00', 25.356
UNION ALL SELECT 42,TIMESTAMP '2020-04-01 07:00:12', 35.124
UNION ALL SELECT 42,TIMESTAMP '2020-04-01 07:00:23', 47.056
UNION ALL SELECT 42,TIMESTAMP '2020-04-01 07:00:34', 45.225
)
;
DROP TABLE IF EXISTS revspeed;
CREATE TABLE revspeed (
rs_vid,rs_ts,rpm
) AS (
SELECT 42,TIMESTAMP '2020-04-01 07:00:00', 2201
UNION ALL SELECT 42,TIMESTAMP '2020-04-01 07:00:10', 3508
UNION ALL SELECT 42,TIMESTAMP '2020-04-01 07:00:20', 6504
UNION ALL SELECT 42,TIMESTAMP '2020-04-01 07:00:30', 6608
)
;
-- without
\pset null '(null)'
SELECT *
FROM oilpressure
LEFT OUTER JOIN revspeed
ON op_vid=rs_vid AND op_ts=rs_ts ;
-- out Null display is "(null)".
-- out op_vid | op_ts | op_psi | rs_vid | rs_ts | rpm
-- out --------+---------------------+--------+--------+---------------------+--------
-- out 42 | 2020-04-01 07:00:00 | 25.356 | 42 | 2020-04-01 07:00:00 | 2201
-- out 42 | 2020-04-01 07:00:12 | 35.124 | (null) | (null) | (null)
-- out 42 | 2020-04-01 07:00:23 | 47.056 | (null) | (null) | (null)
-- out 42 | 2020-04-01 07:00:34 | 45.225 | (null) | (null) | (null)
-- with
SELECT *
FROM oilpressure
LEFT OUTER JOIN revspeed
ON op_vid=rs_vid AND rs_ts INTERPOLATE PREVIOUS VALUE op_ts;
-- out op_vid | op_ts | op_psi | rs_vid | rs_ts | rpm
-- out --------+---------------------+--------+--------+---------------------+------
-- out 42 | 2020-04-01 07:00:00 | 25.356 | 42 | 2020-04-01 07:00:00 | 2201
-- out 42 | 2020-04-01 07:00:12 | 35.124 | 42 | 2020-04-01 07:00:10 | 3508
-- out 42 | 2020-04-01 07:00:23 | 47.056 | 42 | 2020-04-01 07:00:20 | 6504
-- out 42 | 2020-04-01 07:00:34 | 45.225 | 42 | 2020-04-01 07:00:30 | 6608
I have a table with following data
Order_no | Part_No | R_from | R_to
1001 | 1010037-00L| 1 | 5
1001 | 1010025-00L| 6 | 12
I need to get the above data to a report in below manner.
R_NO | PART_NO
------------------
1 | 1010037-00L
2 | 1010037-00L
3 | 1010037-00L
4 | 1010037-00L
5 | 1010037-00L
6 | 1010025-00L
7 | 1010025-00L
8 | 1010025-00L
9 | 1010025-00L
10 | 1010025-00L
11 | 1010025-00L
12 | 1010025-00L
Something like:
WITH r_nos ( r_no ) AS (
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= ( SELECT MAX( R_to ) FROM your_table )
)
SELECT r_no,
part_no
FROM r_nos r
INNER JOIN
your_table y
ON ( r.r_no BETWEEN y.r_from AND y.r_to )
Here's an alternative which doesn't require a separate join. You should test both solutions to see which is more performant for your data etc, though.
WITH your_table AS (SELECT 1001 order_no, '1010037-00L' part_no, 1 r_from, 5 r_to FROM dual UNION ALL
SELECT 1001 order_no, '1010025-00L' part_no, 6 r_from, 12 r_to FROM dual)
SELECT r_from + LEVEL -1 r_no,
order_no,
part_no
FROM your_table
CONNECT BY PRIOR order_no = order_no
AND PRIOR part_no = part_no
AND PRIOR sys_guid() IS NOT NULL
AND LEVEL <= r_to - r_from + 1
ORDER BY r_no;
R_NO ORDER_NO PART_NO
---------- ---------- -----------
1 1001 1010037-00L
2 1001 1010037-00L
3 1001 1010037-00L
4 1001 1010037-00L
5 1001 1010037-00L
6 1001 1010025-00L
7 1001 1010025-00L
8 1001 1010025-00L
9 1001 1010025-00L
10 1001 1010025-00L
11 1001 1010025-00L
12 1001 1010025-00L
The data structure:
sql-> desc t1
- List item
p_code number,
acc_date date ,
debit number,
credit number
Data inside my table:
sql-> select * from t1;
| p_code | acc_date | debit | credit |
| 001 | 01-01-15 | 100 | 25 |
| 001 | 02-01-15 | 0 | 125 |
| 001 | 03-01-15 | 415 | 85 |
I would like to do something like this:
select * from t1
where acc_date between :fromdate and :todate
union all
select p_code, (sum(nvl(debit,0))- sum(nvl(credit,0))) open_balance
from t1
where acc_date < :fromdate
;
But, I can't figure out what are my mistakes.
Type and number of columns in union must be the same -
select p_code, acc_date, debit, credit, null as open_balance
from t1
where acc_date between :fromdate and :todate
union all
select p_code, null as acc_date, null as debit, null as credit,
(sum(nvl(debit, 0)) - sum(nvl(credit, 0))) open_balance
from t1
where acc_date < :fromdate
I am having trouble formulating this question, so I would give an example to demonstrate. Consider my table as,
CREATE TABLE ABC
(
PID NUMBER(10,0) NOT NULL,
DISP_COL VARCHAR2(100 BYTE),
VAL_COL VARCHAR2(20 BYTE),
ORD_COL1 NUMBER(5,2),
ORD_COL2 NUMBER(5,2),
CONSTRAINT PK_PID PRIMARY KEY
(
PID
)
);
And the data I have is,
PID | DISP_COL | VAL_COL | ORD_COL1 | ORD_COL2
----------------------------------------------
1 | DISP1 | VAL1 | 1 | 14
2 | DISP2 | VAL26 | 2 | 22
3 | DISP1 | VAL8 | 1 | 17
4 | DISP1 | VAL56 | 1 | 9
5 | DISP2 | VAL9 | 2 | -10
6 | DISP3 | VAL12 | 2 | 20
7 | AISP1 | VAL7 | 2 | -3
Now based on the descending ordering of ORD_COL1, ORD_COL2, I want to get the unique DISP_COL values and then all rows of that DISP_COL value to follow. So my data should like,
PID | DISP_COL | VAL_COL | ORD_COL1 | ORD_COL2
----------------------------------------------
2 | DISP2 | VAL26 | 2 | 22
5 | DISP2 | VAL9 | 2 | -10
6 | DISP3 | VAL12 | 2 | 20
7 | AISP1 | VAL7 | 2 | -3
3 | DISP1 | VAL8 | 1 | 17
1 | DISP1 | VAL1 | 1 | 14
4 | DISP1 | VAL56 | 1 | 9
A simple ORDER BY ORD_COL1 DESC, ORD_COL2 DESC does get me the order I want DISP_COL to occur but then I want the same valued rows to follow that.
I am kind of new to oracle and pl/sql, so all help is appreciated. Thanks in advance.
SELECT * FROM ABC ORDER BY ORD_COL1 DESC, DISP_COL ASC, ORD_COL2 DESC;
http://sqlfiddle.com/#!4/40401/18
You will need to order by the DISP_COL in ASC in order to get the result you want. See the updated fiddle and the code above. This will give you what you want from you question.
The following query worked (http://sqlfiddle.com/#!4/c340b/2/0)
SELECT A1.* FROM (SELECT * FROM ABC) A1 INNER JOIN
(SELECT DISP_COL, MAX(ORD_COL1) COL1, MAX(ORD_COL2) COL2 FROM ABC
GROUP BY DISP_COL
ORDER BY COL1 DESC, COL2 DESC) A2 ON (A1.DISP_COL = A2.DISP_COL)
ORDER BY A2.COL1 DESC, A2.COL2 DESC, A2.DISP_COL, A1.ORD_COL1 DESC,
A1.ORD_COL2 DESC;
I'm looking to calculate the highest basket in my set of data but I can't get my head around how I should do it.
I have data like:
OrderID | CustomerID | BasketID | ProductID | Price
1 | 1 | 1 | 221 | 10
2 | 1 | 1 | 431 | 123
3 | 1 | 2 | 761 | 44
4 | 2 | 3 | 12 | 54
5 | 2 | 3 | 102 | 78
6 | 3 | 4 | 111 | 98
7 | 3 | 4 | 41 | 45
8 | 3 | 5 | 65 | 66
9 | 4 | 6 | 32 | 47
10 | 4 | 6 | 118 | 544
Sorry if it seems quite messy.
But I can easily get the SUM with an obvious
SELECT SUM([Price]), BasketID, CustomerID FROM table
GROUP BY BasketID, CustomerID
But how can I filter the list for only the highest priced Basket ID for that CustomerID
Thanks
You can use a CTE (Common Table Expression) with the ROW_NUMBER function:
;WITH HighestPricePerCustomerAndBasket AS
(
SELECT
ID, UserID, ClassID, SchoolID, Created,
ROW_NUMBER() OVER(PARTITION BY BasketID,CustomerID ORDER BY Price DESC) AS 'RowNum'
FROM dbo.YourTable
)
SELECT
[Price], BasketID, CustomerID
FROM HighestPricePerCustomerAndBasket
WHERE RowNum = 1
This CTE "partitions" your data by BasketID,CustomerID, and for each partition, the ROW_NUMBER function hands out sequential numbers, starting at 1 and ordered by Price DESC - so the first row (highest price) gets RowNum = 1 (for each BasketID,CustomerID "partition") which is what I select from the CTE in the SELECT statement after it.
SELECT *
FROM (SELECT *,
DENSE_RANK() OVER (PARTITION BY CustomerID ORDER BY BasketTotal DESC) AS RNK
FROM (SELECT Sum(Price) AS BasketTotal,
BasketID,
CustomerID
FROM Order a
GROUP BY BasketID,
CustomerID
) a
) b
WHERE RNK = 1
I managed to conjure something up that worked.