Solved

Postgresql select different values from the same column

Posted on 2014-04-15
7
1,424 Views
Last Modified: 2014-04-15
Hopefully a simple question, but I am stuck

I need to select and display in separate columns the number of true and false counts from a single table column e.g.

request    Date            delivered
1              01/04/14     t
2              01/04/14     f
3              01/04/14     f
4              02/04/14     f
5              02/04/14     t
6              03/04/14     f
etc...

right now I can only do something like this:

SELECT
date_part(d, date) AS day,
count(request) AS requests,
delivered
FROM table
WHERE date > '2014-03-31 23:59:59'
and delivered = true
GROUP BY 1, delivered
ORDER BY 1

but I want to have a result that looks like this

date          delivered =t    delivered=f
01/04/14  1                      2
02/04/14  1                      1
03/04/14  0                      1

Can anyone help?
0
Comment
Question by:eezar21
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40001077
this is the basic code:
select date
, sum(case when delivered='t' then 1 else 0 end) t
, sum(case when delivered='f' then 1 else 0 end) f
from yourtable
group by date
order by date

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 40001080
Select Date ,

Sum ( Case when delivered = 't' then 1 else 0 end) as T,
Sum ( Case when delivered = 'f' then 1 else 0 end) as F


From table
Group by Date
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 40001081
Sorry AngelIII , have not refreshed the page
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:eezar21
ID: 40001153
Thanks guys but this gives me 2 different date lines for each date, rather than just one?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 153 total points
ID: 40001161
only if the date field actually has date AND time, and you display only date part ...
see this article (though not targeted for postgresql):
http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don%27t-be-scared-and-do-it-right-the-first-time.html

for your sql, you will need to extract/cast to only date, then the group by will work, for example date_trunc() function will do:
http://www.postgresql.org/docs/9.1/static/functions-datetime.html
select date_trunc('day', date)
, sum(case when delivered='t' then 1 else 0 end) t
, sum(case when delivered='f' then 1 else 0 end) f
from yourtable
group by date_trunc('day', date)
order by date_trunc('day', date) 

Open in new window

0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 152 total points
ID: 40001169
Select Date::timestamp::date ,

Sum ( Case when delivered = 't' then 1 else 0 end) as T,
Sum ( Case when delivered = 'f' then 1 else 0 end) as F


From table
Group by Date::timestamp::date

refer
http://stackoverflow.com/questions/6133107/extract-date-yyyy-mm-dd-from-a-timestamp-in-postgresql
0
 

Author Closing Comment

by:eezar21
ID: 40001299
Thanks for helping me through this - works perfectly and taught me some new query skills!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now