inserting sql with loop - sql

I have the table PERSONAL
ID_PERS NAME
---------------
11 azerty
22 uiop
and the table TOURNE_LABEL
ID_TOUR NAME
--------------
1 w
2 p
3 v
I want to loop over all of person and then join it with tourne and insert to a new table.
I have created empty table LS_PDA
ID_PERS ID_TOURN
-------------------
11 1
11 2
11 3
22 1
22 2
22 3
how can I do that ?

SQL is all about set based operations. If you're thinking about a loop, chances are you're heading in the wrong direction. For this problem, you could cross-join the tables, thus producing all the possible combinations, and use the insert-select syntax:
INSERT INTO ls_pda
SELECT id_pres, id_tour
FROM personal
CROSS JOIN tourne_label

Related

Group Rows Together in Join [duplicate]

This question already has answers here:
How do I create a comma-separated list using a SQL query?
(11 answers)
Closed 18 days ago.
I have a table that contains a bunch of keys with different IDs. Some keys have more than 1 ID.
ID Table:
key id
a 1
a 11
a 12
b 2
c 3
c 33
2nd table:
key count location
a 17 123 Test Rd
b 10 12 Smith St
c 18 999 Fire Rd
Desired result:
key count location id
a 17 123 Test Rd 1, 11, 12
b 10 12 Smith St 2
c 18 999 Fire Rd 3, 33
I am trying to join this table with another table that I have so that the ID is carried over, but the issue I am running in to is that my join result is getting a larger number of rows than desired simply because it is creating a row for each unique ID that a key has. Is there a way to do the JOIN in a way where it groups each id together (perhaps comma delimited) in one single row?
You can use STRING_AGG:
SELECT t.key, s.count, s.location, STRING_AGG(t.id, ',') as id
FROM YourTable t
JOIN YourTable2 s
ON(t.key = s.key)
GROUP BY t.key, s.count, s.location

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.

SQL copy from one table to another with changing ID value

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.

Multiple AVG in single SQL query

I've searched for adding multiple AVG calculations and have found a few entries, however I'm having to join another table and the examples of that are scarce.
closest answer I can find is this
but it deals with dates and no joins
here are my tables:
indicators:
StandardScore IndicatorID NID DID
0.033333 7 1 1
0.907723 9 1 1
0.574739 26 1 1
0.917391 21 1 1
.....
indexindicators:
IndexID IndicatorID
1 7
1 26
2 21
3 7
4 9
4 21
4 7
5 9
.......
My goal is to get the average for each IndexID (indexindicators) related to NID/DID (indicators) combination
a query to retrieve a single value would be
SELECT AVG(StandardScore) FROM `indicators` INNER JOIN indexindicators ON indicators.IndicatorId=indexindicators.IndicatorId WHERE nid=1 AND did=1 AND indexindicators.IndexId=1
ultimately there will be 6 (indexID) averages which then have to be rounded then * by 100 (should I do that part with PHP?)
This seems like such a simple query, but i just can't seem wrap my mind around it.
Thanks in advance for your help!
SELECT nid, did, indexid, 100.0 * AVG(StandardScore)
FROM 'indicators'
INNER JOIN 'indexindicators'
ON indicators.IndicatorId=indexindicators.IndicatorId
group by nid, did, indexid