SQL, find an ID which contains 2 values in 2 lines - sql

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

Related

SQL How to SUM rows in second column if first column contain

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.

Get max value from a joined list paired with another column in DB2

I have the following tables:
Table I:
etu | nr |
1 2
2 2
2 3
2 1
3 4
3 9
Table A:
etu | rsp | nr
2 8 2
2 7 3
2 3 1
3 2 4
3 6 9
Now what I want to have as a result table is
etu | nr | rsp
2.. 3 7
3.. 9 6
So etu and nr are linked together and if multiple equal etu entries are available only the one with the highest nr is taken and the rsp value is added in the result table. in addition if more etu entries are available in the table I there are .. added to the etu value.
Explain: For the 3 9 6 row: The last row on table I is 3 9 so 3 is the number that is looked for and 9 is the highest number for the 3 rows. So we take that and add the rsp value for that ( 6 ) and we add that to the result table. For the 2 row it is the same 2 3 being the highest 2 row in table I.
I got something like:
select x.etu, x.rsp, y.nr from(
select i.etu etu, max(i.nr) maxnr, a.rsp from i left join a on
i.etu=a.etu and i.nr=a.nr group by etu)t
inner join a x on x.etu=t.etu and x.nr=t.nr inner join y on y.etu=t.etu
and y.nr=t.nr
or
select i.etu, max(i.nr) a.rsp from i left join a on i.etu=a.etu and
i.nr=a.nr grounp by
None even get me close to get the results that I want less add the .. after the etu when having the right result.
The system is DB10.5 Windows.
Thank you for all your help in advance.
Viking
I would use a CTE here like this:
with tmp as (
select i.etu, max(i.nr) as nt, count(*) as cnt
from i
group by i.etu)
select case
when tmp.cnt = 1 then char(a.etu)
else concat(rtrim(char(a.etu)), '..')
end as etu,
a.nr,
a.rsp
from tmp
left outer join a
on a.etu = tmp.etu
and a.nr = tmp.nr
The CTE provides the information necessary to join with a to get the correct response, and append the .. as necessary.

To select a common record when adding other field

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)

SQL query to print mirror labels

I want to print labels in words as returned by a SQL query such as follow.
1 2 3
4 5 6
When I want to print the reverse of those labels, I have to print them as follow
3 2 1
6 5 4
In my real case, I have 5 colums by 2 rows, how can I formulate my query so that my records are ordered like the second one.
The normal ordering is handled by word, so my query is like
SELECT * FROM Products ORDER BY Products.id
I'm using MS Access =(
EDIT :
Just to make it clear
I'd like my records to be ordered such as
3 2 1 6 5 4 9 8 7 12 11 10
EDIT2 :
my table looks like this
ID ProductName
1 Product1
2 Product2
3 Product3
n Product[n]
I want the ids to be returned as I mentioned above
SELECT * FROM Products ORDER BY Products.id desc
Alternately if your query at the moment is really giving you this:
select col1, col2, col3 from products order by products.id;
why not use
select col3, col2, col1 from products order by products.id;

Difficulty in creating update totals query on same table

Consider the following table:
ID nonUniqueID value total
--------------------------
1 12345 5 x
2 12345 10 x
3 789 20 x
4 789 5 x
I need to make a query something like this (psuedo SQL), which will work within Access 2007:
UPDATE table
SET total = SUM(value)
WHERE nonUniqueID IS SAME;
The result should be as follows:
ID nonUniqueID value total
--------------------------
1 12345 5 15
2 12345 10 15
3 789 20 25
4 789 5 25
I've tried group bys, but I got odd results that quite frankly, I could not interpret. Does anybody know how I could achieve something like this?
Not sure if this works in Access or not, but give it a try:
update table t1
inner join (
select nonUniqueID, sum(value) as SumValue
from table
group by nonUniqueID
) t2 on t1.nonUniqueID = t2.nonUniqueID
set t1.total = t2.SumValue
Update: Based on this question, it looks like it is not going to work. But give it a shot! If it doesn't, you can use the approach suggested in that question.
Another possible option:
update t
set total = (select SUM(value) from table where nonUniqueID = t.nonUniqueID)
from table t