Solved

Postgresql select different values from the same column

Posted on 2014-04-15
7
1,404 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]
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry AngelIII , have not refreshed the page
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:eezar21
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for helping me through this - works perfectly and taught me some new query skills!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql query to last records from table 3 50
VB Access SQL question 2 23
T-SQL: Why is Syntax So Difficult 5 33
Access Query 4 33
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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