Solved

Postgresql select different values from the same column

Posted on 2014-04-15
7
1,449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 143

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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