I would like to generate this sequence
col sequence
01 01
01 02
01 03
01 04
01 05
02 01
02 02
02 03
02 04
02 05
..
..
12 01
12 02
12 03
12 04
12 04
And add this as a sequence to another select.
Hierarchical query might help to produce result you posted:
SQL> with
2 c1 as
3 (select lpad(1 + level - 1, 2, '0') col1
4 from dual
5 connect by level <= 12
6 ),
7 c2 as
8 (select lpad(1 + level - 1, 2, '0') col2
9 from dual
10 connect by level <= 5
11 )
12 select c1.col1, c2.col2
13 from c1 cross join c2
14 order by c1.col1, c2.col2;
CO CO
-- --
01 01
01 02
01 03
01 04
01 05
02 01
02 02
02 03
02 04
02 05
03 01
03 02
<snip>
11 04
11 05
12 01
12 02
12 03
12 04
12 05
60 rows selected.
SQL>
You want row_numner() :
select col, row_number() over (partition by col order by col) as sequence
from table t;
You can use sub-query for further operation.
Related
I have a table that looks something like this:
Agency Year Total PopGroup
01 2017 3467 3C
01 2018 3444 3C
01 2019 3567 3C
02 2017 1000 1C
02 2018 1354 1C
02 2019 1333 1C
03 2017 6784 2C
03 2018 3453 2C
04 2017 3333 2C
If an agency has a row for year 2019, I want to duplicate this row and call it 2020 (basically, an estimate population for 2020). Desired result:
Agency Year Total PopGroup
01 2017 3467 3C
01 2018 3444 3C
01 2019 3567 3C
01 2020 3567 3C
02 2017 1000 1C
02 2018 1354 1C
02 2019 1333 1C
02 2020 1333 1C
03 2017 6784 2C
03 2018 3453 2C
04 2017 3333 2C
I think I should use something like:
INSERT INTO table
WHERE Year = 2019
but I'm a little stuck. What can I try next?
You are on the right track:
INSERT INTO table (Agency, Year, Total, PopGroup)
SELECT Agency, 2020 as Year, Total, PopGroup
FROM table t
WHERE Year = 2019 ;
you can use the following insert to add a new row exactly same as another one but with some modified data.
insert into table (select agency, 2020, total, popgroup from table where year = 2019)
this will insert a new record exactly same as 2019 but with year 2020
I have
ID ID2 Amount
-------- -------- ------
01 02 20
01 03 30
02 04 40
02 06 30
03 05 70
03 06 60
03 07 60
04 08 100
04 09 110
I want to query the data above into 2 resultset (return max 5 rows) which are:
result 1:
ID1 ID2 Amount
-------- -------- ------
01 02 20
01 03 30
02 04 40
02 06 30
result 2:
ID1 ID2 Amount
-------- -------- ------
03 05 70
03 06 60
03 07 60
04 08 100
04 09 110
How should I construct my query?
use row_number() to generate a sequential no. (row_number() over (order by ID1) - 1) / 5 + 1 will gives you set of 5 per each result
; WITH CTE AS
(
SELECT result_no = (row_number() over (order by ID1) - 1) / 5 + 1,
ID1, ID2, Amount
FROM yourtable
)
SELECT *
FROM CTE
WHERE result_no = #result_no
I am really learning a lot here. Thanks to all the support. I have another challenge however.
I want to calculate a weighted Index for a group of rows 'Div' within a particular month as shown below:
Wght tMonth tYear Div Indices
37.5 01 2015 01 157.27
2.7 01 2015 01 127.36
58.7 01 2015 01 142.48
DivIndex 146.11
34.9 01 2015 02 133.33
6.7 01 2015 02 136.49
52.4 01 2015 02 131.34
DivIndex 124.43
43.9 02 2015 01 157.18
44.8 02 2015 01 127.42
DivIndex 126.09
58.7 02 2015 03 145.67
7.5 02 2015 03 134.70
6.7 02 2015 03 137.24
DivIndex 104.72
54.0 03 2015 05 160.61
DivIndex 86.73
48.1 03 2015 04 127.49
58.7 03 2015 04 148.62
DivIndex 148.58
I used Excel to compute the 'DivIndex' and that is what I want to come up with in Sql Server 2008R2.
Thanks in advance for any help.
You can do the calculation as:
select tyear, tmonth,
sum(weight * dev_indices) / sum(weight)
from t
group by tyear, tmonth
order by tyear, tmonth;
I have the following table (simplified):
RecordID bigint IDENTITY
ItemID bigint
ConfigID varchar
Status varchar
StatusDate date
Sample Data:
001 04 1E 10 2007-08-14 13:57:54
002 04 1E 12 2007-08-21 16:10:21
003 04 1D 10 2007-09-27 17:14:13
004 04 1D 43 2011-01-10 13:44:50
005 04 1B 50 2008-06-20 09:09:51
006 05 1A 17 2007-12-18 14:04:56
007 05 1A 11 2007-10-17 08:23:52
008 05 1A 12 2007-10-19 12:54:18
009 05 1B 12 2007-11-02 09:23:54
010 05 1B 40 2010-06-17 09:34:33
011 07 1A 12 2007-11-19 14:48:06
012 07 1A 12 2007-11-19 15:02:48
013 07 1B 40 2011-01-10 14:36:16
014 08 1B 10 2009-05-22 11:14:42
015 08 1B 12 2007-11-20 17:02:44
016 08 1A 12 2007-12-12 16:11:57
017 08 1A 10 2009-11-12 11:12:45
018 08 1C 35 2011-01-10 18:30:10
019 08 1D 12 2009-10-14 14:34:47
020 08 1D 10 2009-10-14 14:35:09
Herein lies my problem:
I need to be able to have end-users query this data to return records that will display the latest status and statusdate for each unique combination of itemid and config. So, using the above sample I want to return a record set matching the following:
002 04 1E 12 2007-08-21 16:10:21
004 04 1D 43 2011-01-10 13:44:50
005 04 1B 50 2008-06-20 09:09:51
006 05 1A 17 2007-12-18 14:04:56
010 05 1B 40 2010-06-17 09:34:33
012 07 1A 12 2007-11-19 15:02:48
013 07 1B 40 2011-01-10 14:36:16
017 08 1A 10 2009-11-12 11:12:45
018 08 1C 35 2011-01-10 18:30:10
020 08 1D 10 2009-10-14 14:35:09
In plain english: I need to be able to return the latest status and status date for each item's configuration(s).
Any help in this regard would be extremely appreciated. Thank you in advance.
You can do the GROUP BY clause in a subquery. Try,
SELECT a.*
FROM yourtable a
INNER JOIN
(
SELECT ItemID, ConfigID, MAX([StatusDate]) maxStat
FROM yourtable
GROUP BY ItemID,ConfigID
) b on a.configID = b.configID AND
a.[statusDate] = b.MaxStat AND
a.ItemID = b.ItemID
Order by RecordID
SQLFiddle Demo
in SQL server 2005+, you can use CTE for this:
;with cte as
(
select recordid,
itemid,
configid,
status,
statusdate,
row_number() over(partition by itemid, configid
order by statusdate desc) rn
from yourtable
)
select *
from cte
where rn = 1
order by recordid
see SQL Fiddle with Demo
SELECT
RecordID,
ItemID,
ConfigID,
Status,
StatusDate
FROM
(SELECT
RecordID,
ItemID,
ConfigID,
Status,
StatusDate,
ROW_NUMBER() OVER(PARTITION BY ItemId,ConfigID ORDER BY StatusDate DESC) As StatusOrder
FROM
MyTable) Statuses
WHERE
StatusOrder= 1
I've got weird issue with Oracle, while I try do PIVOT. Basically it return NULL for all pivoted columns while it shouldn't
WITH
"Customer" AS (
SELECT * FROM "doCustomer" WHERE __some_conditions_here__
)
SELECT
*
FROM
"Customer"
PIVOT (
MIN("ChargeCommission") AS "ChargeCommission"
FOR ("Index") IN (
01 AS C01, 02 AS C02, 03 AS C03, 04 AS C04, 05 AS C05, 06 AS C06, 07 AS C07, 08 AS C08, 09 AS C09, 10 AS C10,
11 AS C11, 12 AS C12, 13 AS C13, 14 AS C14, 15 AS C15, 16 AS C16, 17 AS C17, 18 AS C18, 19 AS C19, 20 AS C20
)
)
"Customer" always return not null data, so it's not problem with the data.
But result of whole query depends on what I enter in the some_conditions_here. I've found dependence - less data in "Customer" -> less nulls.
I know that I can rewrite this query that way it'll use decode/group by, but I'm just curious why it behaves like that.
Version of Oracle is 11g.
<FOR ("Index") IN (
01 AS C01, 02 AS C02, 03 AS C03, 04 AS C04, 05 AS C05, 06 AS C06, 07 AS C07, 08 AS C08, 09 AS C09, 10 AS C10,
11 AS C11, 12 AS C12, 13 AS C13, 14 AS C14, 15 AS C15, 16 AS C16, 17 AS C17, 18 AS C18, 19 AS C19, 20 AS C20
)>
Try this, without using aliases ... it should work