I've searched but can't find the right answer, and I'm going round in circles.
I have
CREATE TABLE History (yr Int, output Int, cat Text);
yr output cat
---------- ---------- ----------
2015 10 a
2016 20 a
2017 30 a
2018 50 a
2019 70 a
2015 100 b
2016 200 b
2017 300 b
2018 500 b
2019 700 b
2015 1000 c
2016 2000 c
2017 3000 c
2018 5000 c
2019 7000 c
2015 10000 d
2016 20000 d
2017 30000 d
2018 50000 d
2019 70000 d
I've created two views
CREATE VIEW Core AS select * from History where cat = "c" or cat = "d";
CREATE VIEW Plus AS select * from History where cat = "a" or cat = "b";
My query is
select distinct yr, sum(output), (select sum(output) from core group by yr) as _core, (select sum(output) from plus group by yr) as _plus from history group by yr;
yr sum(output) _core _plus
---------- ----------- ---------- ----------
2015 11110 11000 110
2016 22220 11000 110
2017 33330 11000 110
2018 55550 11000 110
2019 77770 11000 110
Each of the individual queries works but _core and _plus columns are wrong when it's all put together. How should I approach this please.
You may generate your expected output without a view, using a single query with conditional aggregation:
SELECT
yr,
SUM(output) AS sum_output,
SUM(CASE WHEN cat IN ('c', 'd') THEN output ELSE 0 END) AS _core,
SUM(CASE WHEN cat IN ('a', 'b') THEN output ELSE 0 END) AS _plus
FROM History
GROUP BY
yr;
If you really wanted to make your current approach work, one way would be to just join the two views by year. But that would leave open the possibility that each view might not have every year present.
In PL SQL is there a way to produce the Order Count per customer as follows... Thanks for your help.
Cust Order# Order Count
ABC1 011 1
ABC1 052 2
ABC1 199 3
BBA1 150 1
BBA1 158 2
Thanks
Gavin
If I understood you correctly, a little bit of analytics might do the job. Here's an example:
SQL> with test (cust, order#) as
2 (select 'ABC1', '011' from dual union all
3 select 'ABC1', '052' from dual union all
4 select 'ABC1', '199' from dual union all
5 select 'BBA1', '150' from dual union all
6 select 'BBA1', '158' from dual
7 )
8 select cust, order#,
9 row_number() over (partition by cust order by order#) order_count
10 from test;
CUST ORD ORDER_COUNT
---- --- -----------
ABC1 011 1
ABC1 052 2
ABC1 199 3
BBA1 150 1
BBA1 158 2
SQL>
sounds like you want a GROUP BY such as
select cust, SUM(order_count)
from MyTable
group by cust;
which should yield
cust SUM
ABC1 6
BBA1 3
I want to group by lead function by two column. Here is my table data.
Id Name_Id Name Item_Id Item_Name date
1 1 Car 1 SUV 1-Jan-2015
2 1 Car 1 SUV 12-March-2015
3 1 Car 1 SUV 20-April-2015
4 1 Car 2 Sport 23-April-2015
5 2 Bike 1 SUV 18-July-2015
6 2 Bike 1 SUV 20-Aug-2015
7 2 Bike 2 Sport 18-Sept-2015
8 2 Bike 3 Honda 20-OCT-2015
And I need result from above table like.
Id Name_Id Name Item_Id Item_Name start date end date
1 1 Car 1 SUV 1-Jan-2015 20-April-2015
2 1 Car 2 Sport 20-April-2015 23-April-2015
3 2 Bike 1 SUV 18-July-2015 20-Aug-2015
4 2 Bike 2 Sport 20-Aug-2015 18-Sept-2015
5 2 Bike 3 Honda 18-Sept-2015 20-OCT-2015
Any suggestion really appreciated.
I don't think you need to use LEAD here. The CTE below computes, for each Item_Id, the earliest and latest date. This is then joined to your original table to restrict to records corresponding to the earliest Item_Id. At the same time, the end date is also pulled in during the join.
WITH cte AS (
SELECT Name,
Item_Id,
MIN(date) AS start_date,
MAX(date) AS end_date
FROM yourTable
GROUP BY Name, Item_Id
)
SELECT t1.Id, t1.Name_Id, t1.Name, t1.Item_Id, t1.Item_Name,
t2.start_date,
t2.end_date
FROM yourTable t1
INNER JOIN cte t2
ON t1.Item_Id = t2.Item_Id AND
t1.Name = t2.Name AND
t1.date = t2.start_date
i have data as below
A B
1001 601
1002 1001
1003 1002
1004 1002
1005 1002
I need output as
1001
1002
1003
1001
1002
1004
1001
1002
1005
please help me
Try below code.
WITH sample_data AS(
SELECT '1001' A, '601' B FROM dual UNION ALL
SELECT '1002' A, '1001' B FROM dual UNION ALL
SELECT '1003' A, '1002' B FROM dual UNION ALL
SELECT '1004' A, '1002' B FROM dual UNION ALL
SELECT '1005' A, '1002' B FROM dual
)
SELECT TRIM(COLUMN_VALUE) tree_path
FROM
(
SELECT SUBSTR(SYS_CONNECT_BY_PATH(A, ','),2) tree_path
FROM sample_data
WHERE CONNECT_BY_ISLEAF = 1
start with A = '1001'
CONNECT BY PRIOR A = B
), XMLTABLE(('"' || REPLACE(tree_path, ',', '","') || '"'))
I need some help to build SQL Query. I have table having data like:
ID Date Value1 Value2 Code
1 12/01/2009 4 3.5 abc
2 12/02/2009 3 4.0 abc
3 11/03/2009 6 8.5 xyz
4 11/01/2009 2 5.5 abc
5 11/02/2009 4 6.0 xyz
6 12/03/2009 5 7.0 xyz
I need to show result something like...
---------
Code | Data | November(Sum of Values in month) December Jan Feb
abc | Value1 | 2 7 0 0
| Value2 | 5 7 0 0
xyz | Value1 | 10 5 0 0
| Value2 | 14 7 0 0
----------
I need sum of value in each month as in above data in columns group by code.
Have a look at this solution, and let me know what you think.
You have to use both PIVOT and UNPIVOT in this instance to get the result you are looking for. Hope this helps.
DECLARE #Table TABLE(
ID INT,
Date DATETIME,
Value1 INT,
Value2 FLOAT,
Code VARCHAR(10)
)
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 1,'12/01/2009',4,3,'abc'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 2,'12/02/2009',3,4,'abc'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 3,'11/03/2009',6,8,'xyz'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 4,'11/01/2009',2,5,'abc'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 5,'11/02/2009',4,6,'xyz'
INSERT INTO #Table (ID,Date,Value1,Value2,Code) SELECT 6,'12/03/2009',5,7,'xyz'
;WITH UnPvt AS (
SELECT *
FROM (
SELECT Code,
DATENAME(MM, Date) MonthNameVal,
SUM(Value1) Value1,
SUM(Value2) Value2
FROM (
SELECT Code,
Date,
CAST(Value1 AS FLOAT) Value1,
Value2
FROM #Table
) v
GROUP BY Code,
DATENAME(MM, Date)
) Sub
UNPIVOT
(
Vals FOR RowValues IN (Value1, Value2)
) AS UnPvt
)
SELECT *
FROM UnPvt
PIVOT (
SUM(Vals)
FOR MonthNameVal IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November], [December])
) AS pvt
ORDER BY Code, RowValues
Have a look at
SQL SERVER – PIVOT and UNPIVOT Table
Examples
Give the New PIVOT and UNPIVOT
Commands in SQL Server 2005 a
Whirl
Using PIVOT and UNPIVOT
This isn't quite what you asked for because the number of columns is fixed, but I think it's a better way to what you want in SQL:
SELECT Code, 'Value1' As Data, MONTH(Date) AS Month, YEAR(Date) AS Year, SUM(Value1) AS Sum
FROM Table1
GROUP BY Code, MONTH(Date), YEAR(Date)
UNION ALL
SELECT Code, 'Value2' As Data, MONTH(Date) AS Month, YEAR(Date) AS Year, SUM(Value2) AS Sum
FROM Table1
GROUP BY Code, MONTH(Date), YEAR(Date)
ORDER BY Code, Data, Month, Year
Example output:
Code Data Month Year Sum
abc Value1 11 2009 2
abc Value1 12 2009 7
abc Value2 11 2009 5
abc Value2 12 2009 7
xyz Value1 11 2009 10
xyz Value1 12 2009 5
xyz Value2 11 2009 14
xyz Value2 12 2009 7
I'd recommend that you use a bit of non-SQL code to reformat the result into exactly what you asked for before displaying it to the user rather than trying to return a variable number of columns in SQL.