# Copy all records with 2 equals data from table1 to one record in table2 (second part)

Go to solution Solved by Barand,

## Recommended Posts

I think the addition of the last line will do it

```SELECT
User
, COUNT(*) as `Count`
, GROUP_CONCAT(datarichiesta,' (',result,')' ORDER BY datarichiesta SEPARATOR ' , ') as Dates
FROM
(
SELECT
datarichiesta
, @N := IF((codicepz<>@prevuser) OR (@prevres < 10) OR (risultato < 10)
OR (datarichiesta > @startdate + INTERVAL 6 MONTH)
, @N+1, @N) as groupno
, @startdate := IF((codicepz<>@prevuser) OR (@prevres < 10) OR (risultato < 10)
OR (datarichiesta > @startdate + INTERVAL 6 MONTH)
, datarichiesta, @startdate) as startdate
, @prevres:=risultato as result
, @prevuser:=codicepz as user
FROM mastertot
JOIN (
SELECT @N:=0, @prevres:=NULL, @prevuser:=NULL
) as init
WHERE descrprestaz = 'a1'
ORDER BY codicepz, datarichiesta
) as detail
GROUP BY user,groupno
HAVING `Count` >= 5
AND MIN(datarichiesta) + INTERVAL 1 MONTH < MAX(datarichiesta);
```

• Replies 54
• Created

#### Popular Days

Yes! Thanks!!

i did not think about inserting this dates restriction on groups at end, when all groups are already populated!

It is not so immediate enter in the SQL logic.

Could you suggest me a good tutorial or book about SQL ?

##### Share on other sites

Alas, I can't. I have never read one, except manuals. There is good video tutorial on data normalization

http://forums.phpfreaks.com/topic/273634-best-way-to-set-up-tables-when-multiple-values/?do=findComment&comment=1408360

If you get the data right it can make life easier, at least where SQL is concerned.

##### Share on other sites

i was also thinking about coping data in php vectors in order to search for more complex time analysis profile.

Do you think it is a good idea?

There are particular dimension limit for vectors in php or other type of negative aspects like long elaboration time?

Thanks

##### Share on other sites

You would be limited by available memory

`echo ini_get('memory_limit');`

If you want to monitor usage

##### Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×
×
• Create New...