Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
CNo Wno Lno
12 1 1
12 1 2
12 2 3
12 3 15
9 1 1
13 1 1
13 2 2
13 3 5
10 1 1
10 1 2
10 1 3
10 2 4
11 1 1
for Cno i need the missing sequence numbers in Lno
Eg:
for Cn0=12
the line no is missing from 4 to 14
and for Cno=13 the sequence number(3,4) of Lno is missing
i need to find out the missing sequence no's for the clno
You can use common table expression for generating range of numbers and then find missing ones:
with cte as (
select t.CNo, min(t.Lno) as Lno, max(t.Lno) as max_Lno from Table1 as t
group by t.CNo
union all
select c.CNo, c.Lno + 1 as Lno, c.max_Lno
from cte as c
where c.Lno < c.max_Lno
)
select c.Cno, c.Lno
from cte as c
where
not exists (
select *
from Table1 as t
where t.CNo = c.CNo and t.Lno = c.Lno
)
order by 1, 2
option (maxrecursion 0);
if you have tables with sequential numbers, you can do this:
select c.Cno, n.n as Lno
from numbers as n
inner join (
select
tt.CNo, min(tt.Lno) as min_Lno, max(tt.Lno) as max_Lno
from Table1 as tt
group by tt.CNo
) as c on c.min_Lno <= n.n and c.max_Lno >= n.n
where
not exists (
select *
from Table1 as t
where t.CNo = c.CNo and t.Lno = n.n
)
order by 1, 2;
sql fiddle demo
not 100% sure what you are trying to do but you could use a number table to help with this.
if you have a table called numbers like this:
number
1
2
3
4
5.. up to highest number you are interested in
then you could do something like:
select
n.number
from
numbers as n
left outer join table as t
on n.number = t.Lno
and t.cno = 12
where
n.number <= (select max(lno) from table where cno = 12)
and t.nco is null
I don't know what type of output you are looking for or how you want to select the missing numbers you are looking for.
Related
I currently have a table with a quantity in it.
ID Code Quantity
1 A 1
2 B 3
3 C 2
4 D 1
Is there anyway to write a sql statement that would get me
ID Code Quantity
1 A 1
2 B 1
2 B 1
2 B 1
3 C 1
3 C 1
4 D 1
I need to break out the quantity and have that many number of rows
Thanks
Here's one option using a numbers table to join to:
with numberstable as (
select 1 AS Number
union all
select Number + 1 from numberstable where Number<100
)
select t.id, t.code, 1
from yourtable t
join numberstable n on t.quantity >= n.number
order by t.id
Online Demo
Please note, depending on which database you are using, this may not be the correct approach to creating the numbers table. This works in most databases supporting common table expressions. But the key to the answer is the join and the on criteria.
One way would be to generate an array with X elements (where X is the quantity). So for rows
ID Code Quantity
1 A 1
2 B 3
3 C 2
you would get
ID Code Quantity ArrayVar
1 A 1 [1]
2 B 3 [1,2,3]
3 C 2 [2]
using a sequence function (e.g, in PrestoDB, sequence(start, stop) -> array(bigint))
Then, unnest the array, so for each ID, you get a X rows, and set the quantity to 1. Not sure what SQL distribution you're using, but this should work!
You can use connect by statement to cross join tables in order to get your desired output.
check my solution it works pretty robust.
select
"ID",
"Code",
1 QUANTITY
from Table1, table(cast(multiset
(select level from dual
connect by level <= Table1."Quantity") as sys.OdciNumberList));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
This might be SQL 101, but it's stumping me.
I have data like this:
ID ZipCode Value
1 12345 1
2 12346 Null
3 12347 Null
4 12348 2
5 12349 3
6 12350 Null
7 12351 Null
8 12352 4
I need a way to update records that have a null 'value' is updated to the NEXT available value.
ie:
ID ZipCode Value
1 12345 1
2 12346 2
3 12347 2
4 12348 2
5 12349 3
6 12350 4
7 12351 4
8 12352 4
I think this can be done easily enough with a cursor, but there has to be a better way.
There is no need to use cursor.
Updating the table in a single statement can be tricky.
So for safety I would first get the result set with the values to assign for all NULL values:
WITH B AS
(
SELECT ID, (SELECT MIN(Value)
FROM MyTable
WHERE ID > A.ID AND MyTable.Value IS NOT NULL) ValueToAssign
FROM MyTable A
WHERE Value IS NULL
)
UPDATE MyTable
SET Value = B.ValueToAssign
FROM MyTable JOIN B ON MyTable.ID = B.ID
It works if there are gaps between ID.
Here's a Demo on SqlFiddle.
;with cte
as
(
select ID, ZipCode, Value, ROW_NUMBER() OVER(ORDER BY ID) rn
from tb o
)
, ct
as
(
select top 1 *
from cte
order by rn desc
union all
select t.ID, t.ZipCode,
case when t.Value is null then o.Value else t.Value end 'Value', t.rn
from cte t inner join ct o on t.rn = o.rn - 1
)
update tb
set Value = ct.Value
from tb inner join ct on tb.ID = ct.ID
where tb.Value is null
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let's assume I have a table: Where x is value of chose1 and y value of chose2
ID Chose1 Chose2 x y
1 A B 2 3
2 C D 3 5
From this I would like to get something like this: Where z is x*y (if chose is empty,y=1).
NR ID Chose1 Chose2 z
1 1 A 2
2 1 A B 6
3 1 B 3
4 2 C 3
5 2 C D 15
6 2 D 5
This select statement will get the data above with the examples you gave:
select (ID-1)*3+1 as NR, id, Choose1, null as Choose2, x as z from tbl_values
union
select (ID-1)*3+2, id, Choose1, Choose2, x*y from tbl_values
union
select (ID-1)*3+3, id, null, Choose2, y from tbl_values
order by NR
Demo here (tested in MySQL, SQL Server): http://sqlfiddle.com/#!2/5af4b/3
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have the following scenario
pid & month form a composite primary key .
pid month amount
1 1 10
2 2 15
1 2 20
1 3 10
3 3 4
2 3 6
Now the column to be generated with the table will be like this
pid month amount sum
1 1 10 10
2 2 15 15
1 2 20 30
1 3 10 40
3 3 4 4
2 3 6 21
What should be the query ?
This query will do the trick :
SELECT t1.*, sum(t2.amount)
FROM Table1 t1
INNER JOIN Table1 t2 ON t1.pid = t2.pid AND t1.month >= t2.month
GROUP BY t1.pid, t1.month, t1.amount
See SQLFIDDLE : http://www.sqlfiddle.com/#!3/db350/7/0
If using SQL Server 2012:
SELECT *,SUM(amount) OVER(PARTITION BY pid ORDER BY month ROWS UNBOUNDED PRECEDING)'Total'
FROM YourTable
You did not specify what version of SQL Server you are using but you should be able to use the following to get the running total in any version:
select t1.pid,
t1.month,
t1.amount,
(select sum(t2.amount)
from yourtable t2
where t1.pid = t2.pid
and t2.month <= t1.month) total
from yourtable t1;
See SQL Fiddle with Demo
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
ID QuestionNo AnswerPercent
1 15 10
1 16 10
1 17 20
1 18 25
2 15 30
2 16 0
2 17 15
2 18 25
Output
ID QuestionNo AnswerPercent
1 15 10
1 16 30
1 17 20
1 18 25
2 15 30
2 16 15
2 17 15
2 18 25
For each id the answer percent for questions 16 and 17 need to be merged into 16. Chances are for some ids there may not be any 16 or 17 question numbers.
Can anyone help me out in this. Thanks!.
I believe this is what you're after, an UPDATE with a JOIN to a subquery:
UPDATE A
SET A.AnswerPercent = B.AnswerPercent
FROM YourTable A
JOIN (SELECT ID, SUM(AnswerPercent)'AnswerPercent'
FROM YourTable
WHERE QuestionNo IN ('16','17')
GROUP BY ID
)B
ON A.ID = B.ID
WHERE A.QuestionNo = '16'
Demo: SQL Fiddle
try adding the table twice...
table aliased as a has all rows except the ones for question 17, and
table aliased as b has the rows for question 17
Select a.Id, a.QuestionNo,
a.AnswerPercent +
case A.QuestionNo When 16
then coalesce(b.AnswerPercent, 0) End
else 0 End AnswerPercent
From table a
left Join table b
on a.id = b.Id
And a.QuestionNo != 17
And b.QuestionNo = 17
if all you want is to update the existing table, then you need an update and a delete.
update a set
AnswerPercent = a.AnswerPercent +
IsNull(b.AnswerPercent, 0)
from table a
left Join table b
on a.id = b.Id
And a.QuestionNo = 16
And b.QuestionNo = 17
--and then ...
delete table where QuestionNo = 17
with aaa as(
select sum(AnswerPercent) as AnswerPercent,ID
from Table
where QuestionNo in (16,17)
group by ID)
select * from Table where QuestionNo <> 16
union
select ID, 16, sum
from aaa
order by ID,AnswerPercent