SQL help please

My programming skills of all kinds are terrible :smiley:

I am trying to extract data from the activity log with this query:

-- [params]
-- int:lunar_months_ago

SELECT
REPLACE (raw, 'opened the door.', '') as member,
COUNT (DISTINCT DATE(created_at)) as days
FROM posts
WHERE topic_id = 2491
AND DATE(current_date) - DATE(created_at) < :lunar_months_ago*28    
AND DATE(current_date) - DATE(created_at) > (:lunar_months_ago-1)*28
AND raw LIKE '%opened the door%'
GROUP BY raw
ORDER BY days DESC

Which very nicely spits out a list of members and how many days they visited the space in a 4 week period like so:

member			days
Jo Atkin		23
Howard Batchen	19
Mark Johnson	15
Duncan Fairall	14
Tom Newsom		12
Paul Youthed	8
Tom Hedges		7
Andy Paine		7

…and so on

But what I’d like is multiple columns, for each integer value of lunar_months_ago so I don’t have to mess around copy and pasting and reconciling rows in a spreadsheet.

I would be tempted to include the name/number of the month in the select and group statements, and lengthen the time period in the where clause to go back as far as you are interested. So that should give you a long list of how many times a member has opened the door in each month.
You could then wrap that in either use SQL Pivot or Case statements to give you columns for each member name and month. Precise solution depends on your environment. This might be a useful resource:


I’m on phone - otherwise would try to write that in SQL. Happy to help later if I can!

1 Like

Yes that’s exactly what I want to do, but

So [quote=“TomHedges, post:2, topic:5218”]
I’m on phone - otherwise would try to write that in SQL. Happy to help later if I can!
[/quote]

Yes please :smiley:

Something like

Select
Substr(yourDateVariable,8,4) as 'year’
Substr(yourDateVariable,4,3) as ‘month’,
Member, (as before)
Days (as before)

From
(What you had before)
And creataded date >= (sysdate - 365 or whatever)

Group by 'year, ‘month’, 'member’
Order by ‘year’, ‘month’, days, member

A better way would be using the membership system as it captures the data as events of a specific ID, against a specific user with a timestamp.

Basically it’s really easy to throw this together as a page in your admin area, however you’ll need to first describe what you want, and second reinstate my access to the membership system.

I was going to say have you tried DROP TABLES IF EXISTS * - will make all the problems go away very quickly but I think @TomHedges suggestion is better :smile:

Courty

1 Like

Luckily, a robust sanitiser exists between me and my fumblings :smiley:

1 Like

4 Likes

@tomnewsom - what environment are you working in? Will try not to suggest SQL commands that won’t work!
@unknowndomain - I’d be interested to learn more about the membership system - perhaps you could give me a tour one evening?

If you’re interested, and interested in getting involved with dev and maintenance, then maybe speak to @jonathanjo who is also @systems

1 Like

Thanks Dermot - will do!

It’s mySQL via the Data Explorer plugin

(PS: For the record, Tom Lynch unknowndomain, is not currently a member of SLMS, so he can’t actually access the membership system right now)

It sounds like a meeting with @unknowndomain @systems and interested parties might be productive to get the membership system documented and handed over. Or is this already in hand?

To revisit a Tommelian point: can we say Makerspace? It’s much nicer.

2 Likes

Ah, I do try :slight_smile:

1 Like

I’ve had a quick go at this for displaying the previous 4 months. It should be easy enough to expand for more. I normally write in t-sql (microsofts flavour of sql) so I’m probably a little off but think this will get you what you need.
Give me a shout if this doesn’t work I’m happy to trouble shoot!

 select
   replace(raw ,' opened the door.') ,'') as member
  ,count(distinct case when  0*28 < date(current_date) - date(created_at) and date(current_date) - date(created_at) <= 1*28 then date(current_date) else null end) as within_last_month
  ,count(distinct case when  1*28 < date(current_date) - date(created_at) and date(current_date) - date(created_at) <= 2*28 then date(current_date) else null end) as one_month
  ,count(distinct case when  2*28 < date(current_date) - date(created_at) and date(current_date) - date(created_at) <= 3*28 then date(current_date) else null end) as two_month 
  ,count(distinct case when  3*28 < date(current_date) - date(created_at) and date(current_date) - date(created_at) <= 4*28 then date(current_date) else null end) as three_month
  ,count(distinct case when  4*28 < date(current_date) - date(created_at)                                                   then date(current_date) else null end) as four_month_and_more
from
   posts
where
   topic_id = 2491
   and raw like '%opened the door.'
group by 
  raw
2 Likes

Thanks for the help everyone :slight_smile:
3D bar charts incoming!

1 Like

Kind of off-topic…

A SQL query walks into a bar and sees two tables. He walks up to them and says ‘Do you mind if I join you?’

3 Likes

That’s the best joke ever!!!
I’m using it all week at work!!!

SELECT *
FROM [Users]
WHERE [Clue] > 0

No records found.

1 Like