Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hi all
i am facing a problem and need a query for that ,
I have a table and data like
----------
seq_id run_id mark_Flag
1 10 A
2 11 A
3 12 A
4 13 Z
5 14 A
6 15 A
7 16 Z
8 17 Z
9 18 A
10 19 A
11 20 Z
----------
Now i required the output like
seq runidFrom runidTo mark_Flag
1 10 12 A
2 13 13 Z
3 14 15 A
4 16 17 Z
5 18 19 A
6 20 20 Z
Thanks in advance ....
Try this query the main idea is to group by count of previous Mark_flag not equal to current
SELECT min(run_id),max(run_id),max(mark_Flag)
FROM T as T1
GROUP BY mark_flag,
(
SELECT COUNT(*) FROM T
WHERE seq_id<T1.seq_id
and
mark_flag<>T1.Mark_flag
)
ORDER BY MIN(seq_id)
SQLFiddle demo
This query have to work on any database system but when you post a question please add a tag with your RDBMS (not just SQL) so a query can be optimized for your data base system.
UPD: Here is the MS SQL version:
SELECT min(run_id),max(run_id),max(mark_Flag)
FROM
(
Select run_id, mark_flag,seq_id,
(
SELECT COUNT(*) FROM T
WHERE seq_id<T1.seq_id
and mark_flag<>T1.Mark_flag
) as group_id
From t as T1
) as T2
GROUP BY mark_flag,group_id
ORDER BY MIN(seq_id)
SQLFiddle demo
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Need to write SQL query for getting below output with the given input table:
Input Table:
Col1
1
2
3
Output:
Col1
1
2
2
3
3
3
WITH CTE(NN)AS
(
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5
)
SELECT C.NN
FROM CTE AS C
CROSS JOIN CTE C2
WHERE C2.NN<=C.NN
CTE is your input table
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have mapping table for RFQ(request for quotation) and Vendor's bid amount.
rfq_vendor_mapping :
id rfq_id(FK) vendor_id(FK) amount
---------------------------------------
1 1 1 100
2 1 2 50
3 2 1 200
4 2 3 300
5 2 2 40
6 3 4 70
7 3 1 90
8 3 2 250
9 4 3 30
10 5 1 500
In above table, I want analysis for how many times vendor has submitted minimum and maximum bid for each RFQ.
Expected Output :
vendor_id min_bid_count max_bid_count
-----------------------------------------
1 1 2
2 2 1
3 1 2
4 1 0
http://sqlfiddle.com/#!15/60198/1
Compare the vendor's amount with min and max from a window function and run a conditional count on outer query level:
SELECT vendor_id
, count(min_bid OR NULL) AS min_bid_count
, count(max_bid OR NULL) AS max_bid_count
FROM (
SELECT vendor_id
, amount = min(amount) OVER w AS min_bid
, amount = max(amount) OVER w AS max_bid
FROM rfq_vendor_mapping
WINDOW w AS (PARTITION BY rfq_id)
) sub
GROUP BY 1
ORDER BY 1;
SQL Fiddle.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can we do cross product of columns of a table.
Suppose I have table with this data.
Name Age spend Gender
De 26 10 M
Fu 27 29 F
I want output should be
De 26 10 M
Fu 26 10 M
De 26 29 M
Fu 26 29 M
De 27 10 M
Fu 27 10 M
De 27 29 M
Fu 27 29 M
....
I am doing the same by putting all the columns in different-different table in a procedure.
can you try this?:
updated
you can test here. http://sqlfiddle.com/#!2/9264d7/2/0
SELECT *
FROM
(
SELECT DISTINCT name
FROM test
) x CROSS JOIN
(
SELECT DISTINCT age
FROM test
) y CROSS JOIN
(
SELECT DISTINCT spent
FROM test
) z CROSS JOIN
(
SELECT DISTINCT gender
FROM test
) a;
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.
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