A | B
1-1 10
1-1 10
1-1 12
1-1 22
1-4 20
1-4 20
1-4 10
Output Table:
A | B
1 104
I have given the table and the output table. I am not able to generate that output. The output I get is given below and the code I used as well.
A | B
1 54
1 50
The code I used is : select left(A,2) as A, SUm(KSL05) as B from LKP_FAGLFLEXT where condition group by A
you can use group by as below:
select left(a,2), sum(b) from yourtable
group by left(a,2)
Related
View of a table
ID
kWh
1
3
1
10
1
8
1
11
2
12
2
4
2
7
2
8
3
3
3
4
3
5
I want to recive
ID
kWh
1
32
2
31
3
12
The table itself is more complex and larger. But the point is this. How can this be done? And I can't know in advance the ID numbers of the first column.
SELECT T.ID,SUM(T.KWH)SUM_KWH
FROM YOUR_TABLE T
GROUP BY T.ID
Do you need this one?
Let's assume your database name is 'testdb' and table name is 'table1'.
SELECT * FROM testdb.table1;
SELECT id, SUM(kwh) AS "kwh2"
FROM stack.table1
WHERE id = 1
keep running the query will all (ids). you will get output.
By following this query you will get desired output.
Hope this helps.
Here's how my data looks like:
ID month desired_month val
1 6 4 101
1 4 6 102
2 12 4 103
2 4 3 104
2 3 12 105
I want look across all rows for the same ID and find the row which has month equal to the desired_month of the row in question and then return the val from the row that matched.
Resultant dataset would look like this:
ID month desired_month val val_from_row_that_matched_desired_month
1 6 4 101 102
1 4 6 102 101
2 12 4 103 104
2 4 3 104 105
2 3 12 105 103
I would like to achieve this using analytic functions in Oracle SQL
you can get desire output by using self join into same table like this.let suppose your table name is tab.
select t1.*,t2.val from tab t1
inner join tab t2
on(t1.desired_month=t2.month);
Assuming there is just one and only one row with desired month matching each (ID, month) combination in the data
select
a.id, a.month,
a.desired_month,
a.val, b.val as val_from_desired_month
from input a, input b
where
a.id = b.id
and a.desired_month = b.month
MAY BE ANSWER SHOULD BE LIKE THIS:-
SELECT E.ID,E.MONTH,M.DESIRED_MONTH, E.VAL,M.VAL AS "val_matched_desired_month"
FROM ID_MONTH_DESIRED_MON_VAL E,ID_MONTH_DESIRED_MON_VAL M
WHERE E.ID =M.DESIRED_MONTH
ORDER BY ID
I have two tables:
A
ID VALUE
----------
1 7
2 5
3 44
4 982
5 1
6 0
7 671
B
ID VALUE
---------------
1 6
2 6
3 77
4 22
How do I copy data from #B to #A to get a different ID (one bigger than the MAX in #A)? For example I need to get
ID VALUE
1 7
2 5
3 44
4 982
5 1
6 0
7 671
8 6
9 6
10 77
11 22
Either make it an IDENTITY column which auto-increments, or this:
INSERT INTO A
SELECT b.ID + (SELECT MAX(ID) FROM A) AS ID, b.Value
FROM B
DEMO
The select is slightly different if the ID in table B has gaps. Then those gaps are transferred.
If the ID column in TableA is not already set to auto-increment, do the following command:
ALTER TABLE TableA MODIFY COLUMN ID INT auto_increment
Now you can just insert all the records from TableB into TableA:
INSERT INTO TableA (VALUE)
SELECT VALUE
FROM TableB
It is not a great idea to rely on the business logic in your query to maintain the order of the ID column. Instead, let SQL take care of it for you; it was designed for this purpose.
My BDD looks like that :
id_feature | id_product | id_feature_value
1 1 20
2 2 21
3 3 20
4 2 20
I need to get the product which have id_Feature_Value 20 AND 21.
I can't find the rigth syntax the have my result...
Thanks
This should work:
select
a.id_product
from
yourtable a,
yourtable b
where
a.id_product=b.id_product and
a.id_feature_value=20 and
b.id_feature_value=21
I have found how to do.
My request check all the product which have the value 20 OR 21. Si if they have the two values, the id_product will be display twice because there is no group by.
I just had to add a where clause with HAVE(id_product) > 1
I have a (large ~1 000 000 rows) table that potentially contains duplicate rows (possible NULL values).
What I want to do is this:
Select only distinc rows.
Remove rows with duplicate 'id' field.
Let's have a table:
id | a | b
1 | 2 | 3
2 | 8 | 7
3 | 9 | 10
2 | 8 | 7
3 | 20| 12
What I want to get is:
id | a | b
1 | 2 | 3
2 | 8 | 7
Row with id 2 is preserved in one copy, while rows with id 3 were removed.
I was thinking about:
SELECT DISTINCT id, a, b FROM table; to get only distinct rows.
Somehow filter the result of (1) to remove duplicate ids.
What would be the best way to approach this?
Third Answer now that the question is slightly clearer:
SELECT id, min(a) as a, min(b) as b
FROM (SELECT DISTINCT id, a, b FROM table) t
GROUP BY id
HAVING count(*) =1
Petr, it looks like per the comments, you want a COMBINATION...
Include:
All rows where the ID occurs ONLY ONCE
All rows where the ID occurs MORE than once -- AND all the other fields on the record are the same
EXCLUDE:
Any row where the ID occurs more than once -- AND the other fields do not exactly match.
select ID, min(a) a, min(b) b
from YourTable
group by ID
having min(a) = max(a)
and min(b) = max(b)
If you have more columns aside from a and b to compare, just add the respective values to the select field list and the corresponding having. From the data sample you've provided, the values return from the query would be
ID MIN(A) MIN(B) Having MIN(A) MAX(A) MIN(B) MAX(B)
1 2 3 2 2 3 3
2 8 7 8 8 7 7
3 9 10 9 20 10 12
So the row ID = 3 will get tossed since the having will fail on a same min() and max() of the same column across BOTH columns. Then, you can copy this into a new table. Only one pass through the table...
Can you rebuild the database, or if not build a new one from the original, with id as a primary key? SQL can take care of the rest.