To be honest, I do not even know how to start my question. So I'll first put the information I have.
I have the following structure in my database.
I need to do a SELECT that returns me the following:
How can I achieve this?
To be honest, I do not even know how to start my question. So I'll first put the information I have.
I have the following structure in my database.
I need to do a SELECT that returns me the following:
How can I achieve this?
Use INNER JOIN
with any table of numbers:
CREATE TABLE NUMEROS (n INT);
INSERT INTO NUMEROS VALUES (1),(2),(3),(4),(5),(6);
SELECT
'Adulto ' + CAST(NUMEROS.n AS VARCHAR) AS PESSOA ,
TabelaB.ValorAdulto AS VALOR
FROM TabelaA
INNER JOIN NUMEROS
ON TabelaA.QtdAdulto >= NUMEROS.n
INNER JOIN TabelaB
ON TabelaA.IDTabelaB = TabelaB.IDTabelaB
Output:
| PESSOA | VALOR |
|-----------|-------|
| Adulto 1 | 200 |
| Adulto 2 | 200 |
| Adulto 3 | 200 |
Now just use UNION ALL
for columns QtdCrianca
, QtdBebe
and QtdSenior
.
See working in SQLFiddle