B
B
bugagawenka2014-10-09 08:33:00
MySQL
bugagawenka, 2014-10-09 08:33:00

Implement on Mysql: Selection by 2 periods (previous, current) of one contract?

Good day! I ask for your help with a query in Mysql that is difficult for me.
It is necessary to fill the table with different data for the periods.
The original table looks like this:

id | nomer | sostoyanienadatu | summa | 
------------------------------------------
1  |номер 1| 2014-09-15       | 100   |
2  |номер 2| 2014-10-10       | 200   |
3  |номер 1| 2014-10-05       | 300   |
4  |номер 3| 2014-10-11       | 400   |
5  |номер 4| 2014-09-06       | 500   |

for example:
prmysyac - 2014-09
tekywiymesiac - 2014-10
It should look like this:
nomer   | prmysyac | tekywiymesiac |
------------------------------------
номер 1 | 100      | 300           |
номер 2 |          | 200           |
номер 3 |          | 400           |
номер 4 | 500      |               |

what I have mastered and can come in handy (in my opinion):
I transfer data about the current and past period to sql from php with variables and fill it in such a way that I get the following query:
SELECT `nomer`,`zakazchik`, FROM database.table WHERE sostoyanienadatu LIKE '2014-09%';

I use this query to find the last contract:
SELECT * FROM database.table WHERE (nomer,sostoyanienadatu) IN (SELECT nomer, MAX(sostoyanienadatu) FROM database.table GROUP BY nomer);

I would be extremely grateful for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bugagawenka, 2014-10-09
@bugagawenka

On one good forum , they suggested how to do everything :)
I'll post the result here, suddenly it will come in handy for someone :)

select t.nomer, t1.`summa` prmysyac , t2.`summa` tekywiymesiac from database.table t left join
(select nomer, `summa` from database.table where month(sostoyanienadatu)=9 ) t1 using(nomer) left join
(select nomer, `summa` from database.table where month(sostoyanienadatu)=10 ) t2 using (nomer) GROUP BY nomer;

W
whats, 2014-10-09
@whats

If we have more than 1 entry per number in the current month, what should we enter in the column? And if this record has more than 1 record in the last month, then what?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question