Locked. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
I am new to SQL and would like to know how to approach writing a query for this question.
Lets say we have these fields:
date_created date_unsubscribed subscriberid
How to write a SQL query that lists, by month, how many people subscribed to the list, unsubscribed from the list, and how many net subscribers there were (new subscribers minus unsubscribers).
All in a single query...
Here's one option using conditional aggregation and union all:
select month(dt),
count(case when subscribe = 1 then 1 end) subscribecount,
count(case when subscribe = -1 then 1 end) unsubscribecountt,
sum(subscribe) overallcount
from (
select date_created as dt, 1 as subscribe
from yourtable
union all
select date_unsubscribed, -1
from yourtable
where date_unsubscribed is not null
) t
group by month(dt)
The subquery creates a list of dates with a flag for subscribe or unsubscribe. Then you can use count with case to determine the appropriate number of subscribers/unsubscribers.
SQL Fiddle Demo
You could write a sum(case) (a sum with conditions) to aggregate - assuming the date_created column is never null. For instance:
ORACLE:
SELECT
TO_CHAR(DATE_CREATED,'MM-YYYY') CREATE_MONTH
,SUM(CASE WHEN date_unsubscribed is not null then 1 else 0 end) unsubscribed
,SUM(CASE WHEN date_unsubscribed is null then 1 else 0 end) subscribed
,COUNT(SUBSCRIBER_ID)
FROM
--YOURTABLENAME
--WHERE
--WHATEVER OTHER CONDITIONS YOU HAVE APPLY
GROUP BY TO_CHAR(DATE_CREATED,'MM-YYYY')
MYSQL:
SELECT
DATE_FORMAT(DATE_CREATED,'%m-%Y') CREATE_MONTH
,SUM(CASE WHEN date_unsubscribed is not null then 1 else 0 end) unsubscribed
,SUM(CASE WHEN date_unsubscribed is null then 1 else 0 end) subscribed
,COUNT(SUBSCRIBER_ID)
FROM
--YOURTABLENAME
--WHERE
--WHATEVER OTHER CONDITIONS YOU HAVE APPLY
GROUP BY DATE_FORMAT(DATE_CREATED,'%m-%Y')
Oracle solution
Here is a query using the PIVOT operator, which was created exactly for this kind of work, and ROLLUP to get the net number. This is just for illustration; I assume the year is a user or application input (bind variable :year, set to 2015 for the output), and I show the summary for January through June.
with
test_data ( date_created, date_unsubscribed, subscriber_id ) as (
select date '2015-05-10', null , 330053448 from dual union all
select date '2015-04-28', null , 330053457 from dual union all
select date '2015-05-10', null , 330053466 from dual union all
select date '2015-04-28', null , 220053475 from dual union all
select date '2015-04-28', date '2015-05-10', 330053484 from dual
),
prep ( type, val, mth ) as (
select 'Subscribed' , 1, extract(month from date_created) from test_data
where extract(year from date_created) = :year
union all
select 'Unsubscribed', -1, extract(month from date_unsubscribed) from test_data
where extract(year from date_unsubscribed) = :year
)
select nvl(type, 'Net Subscr') as description,
nvl(sum(jan), 0) as jan, nvl(sum(feb), 0) as feb, nvl(sum(mar), 0) as mar,
nvl(sum(apr), 0) as apr, nvl(sum(may), 0) as may, nvl(sum(jun), 0) as jun
from prep
pivot (
sum(val)
for mth in (1 as jan, 2 as feb, 3 as mar, 4 as apr, 5 as may, 6 as jun)
)
group by rollup(type)
order by case type when 'Subscribed' then 1 when 'Unsubscribed' then 2 else 3 end
;
DESCRIPTION JAN FEB MAR APR MAY JUN
------------ ---------- ---------- ---------- ---------- ---------- ----------
Subscribed 0 0 0 3 2 0
Unsubscribed 0 0 0 0 -1 0
Net Subscr 0 0 0 3 1 0
3 rows selected.
Related
I would like to get all the list of users who placed requests in the last three years.
Requests( request_id, request_day, user_id, userprofile_id )
Am I doing it right?
SELECT user_id
FROM requests
WHERE EXTRACT(YEAR FROM request_day) IN ( 2014,2015,2016 )
GROUP BY user_id
HAVING COUNT(*) = 3;
Your current approach is almost correct. All you need to do is to count by the distinct number of years:
SELECT user_id
FROM requests
WHERE YEAR(request_day) IN (2014, 2015, 2016)
GROUP BY user_id
HAVING COUNT(DISTINCT YEAR(request_day)) = 3;
If the DISTINCT count of years is 3, then it implies that a user has all three of the years in your WHERE IN clause.
Note that another way to do this would be conditional aggregation:
SELECT user_id
FROM requests
GROUP BY user_id
HAVING SUM(CASE WHEN YEAR(request_day) = 2014 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN YEAR(request_day) = 2015 THEN 1 ELSE 0 END) > 0 AND
SUM(CASE WHEN YEAR(request_day) = 2016 THEN 1 ELSE 0 END) > 0
This approach would scale better if your query were to get more complex.
I have a query which returns this data back from lets say DefaultersTable
Select CustomerID, RoleID FROM DefaultersTable Where DefaulterValue = 1
CustomerID, RoleID
10034 34
15481 37
Now I have got another Table "DefaultersDetails" which have individual monthly values of them,
so I do
Select * from DefaultersDetails Where CustomerID = 10034 AND RoleID = 34.
and get the data
CustomerID, RoleID, ValueForJan, ValueForFeb, ValueforMar
10034 34 45 0 32
Please note that I got this Entry in the first case only because one of the Value was 0.
Now How Can I get both the data in single Query, I want something like this
CustomerID, RoleID, ZeroValueForMonth
10034 34 ValueForFeb
15481 37 ValueForJan
I guess it can be done via temprory Tables but I am not sure how to do this
You can use COALESCE function. Try this :
SELECT d.CustomerID, d.RoleID,
COALESCE(detail.JAN,detail.FEB,detail.MAR) AS ZeroValueForMonth
FROM DefaultersTable d
INNER JOIN
(
SELECT CustomerID, RoleID,
(CASE WHEN ValueForJan <> 0 THEN NULL ELSE 'ValueForJan' END) JAN,
(CASE WHEN ValueForFeb <> 0 THEN NULL ELSE 'ValueForFeb' END) FEB,
(CASE WHEN ValueForMar <> 0 THEN NULL ELSE 'ValueForMar' END) MAR
FROM Defaultersdetail
) AS detail
ON detail.CustomerID = d.CustomerID AND detail.RoleID = d.RoleId
where d.DefaulterValue = 1
You can add other month like MAY, JUN etc on subquery like this :
(CASE WHEN ValueForApr <> 0 THEN NULL ELSE 'ValueForApr' END) APR
I have the following database structure:
FieldID|Year|Value
a|2011|sugar
a|2012|salt
a|2013|pepper
b|2011|pepper
b|2012|pepper
b|2013|pepper
c|2011|sugar
c|2012|salt
c|2013|salt
now I would like to run a query that counts the number of fields for every item in the particular year looking something like this:
value|2011|2012|2013
sugar|2|0|0
salt |0|2|1
pepper|1|1|2
I used multiple tables for every year before. However the distinct values for 2011,2012 and 2013 might be different (e.g. sugar would only be present in 2011)
For individual years I used:
SELECT `Value`, COUNT( `FieldID` ) FROM `Table` WHERE `Year`=2011 GROUP BY `Value`
A1ex07's answer is fine. However, in MySQL, I prefer this formulation:
SELECT Value,
sum(`Year` = 2011) AS cnt2011,
sum(`Year` = 2012) AS cnt2012,
sum(`Year` = 2013) AS cnt2013
FROM t
GROUP BY value;
The use of count( . . . ) produces the correct answer, but only because the else clause is missing. The default value is NULL and that doesn't get counted. To me, this is a construct that is prone to error.
If you want the above in standard SQL, I go for:
SELECT Value,
sum(case when `Year` = 2011 then 1 else 0 end) AS cnt2011,
sum(case when `Year` = 2012 then 1 else 0 end) AS cnt2012,
sum(case when `Year` = 2013 then 1 else 0 end) AS cnt2013
FROM t
GROUP BY value;
You can do pivoting :
SELECT `Value`,
COUNT(CASE WHEN `Year` = 2011 THEN FieldID END) AS cnt2011,
COUNT(CASE WHEN `Year` = 2012 THEN FieldID END) AS cnt2012,
COUNT(CASE WHEN `Year` = 2013 THEN FieldID END) AS cnt2013
FROM `Table`
GROUP BY `Value`
It is called Pivot Table, achieve with a chain of CASE statements which apply a 1 or 0 for each condition, then SUM() up the ones and zeros to retrieve a count.
SELECT
Value,
SUM(CASE WHEN Year = 2011 THEN 1 ELSE 0 END) AS 2012,
SUM(CASE WHEN Year = 2012 THEN 1 ELSE 0 END) AS 2012,
SUM(CASE WHEN Year = 2013 THEN 1 ELSE 0 END) AS 2013
FROM Table
GROUP BY Value
I'm trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below.
BusinessUnit OrderDate Canceled
UnitA 1/15/2013 N
UnitA 10/1/2013 N
UnitB 10/15/2013 N
UnitB 10/22/2013 N
UnitB 10/22/2013 N
Based on the records above, I'd like the result to appear like below
BusinessUnit TodaysOrders ThisMonthsOrders ThisYearsOrders
UnitA 0 1 2
UnitB 2 3 3
My current code is below. It's giving me error (something about DatabaseName.sum does not exist. Check the
Function Name Parsing and Resolution' section... )
Select
SUM (CASE WHEN (OrderDate)=DATE(NOW()) THEN 1 ELSE 0 END) AS TodaysOrders,
SUM (CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) AND MONTH(OrderDate) = MONTH(CURDATE()) THEN 1 ELSE 0 END) AS ThisMonthsOrders,
SUM (CASE WHEN YEAR(main_order_managers.creation_date) = YEAR(CURDATE()) THEN 1 ELSE 0 END) AS ThisYearsOrders
code continues
FROM OrderTable WHERE OrderTable.Canceled. <> 'Y';
Is Sum Case the best use here?
The error is caused by the space between function name and parenthesis
SUM (CASE WHEN ...
^^
Read more Function Name Parsing and Resolution
Try
SELECT BusinessUnit,
SUM(CASE WHEN OrderDate = CURDATE() THEN 1 ELSE 0 END) TodaysOrders,
SUM(CASE WHEN DATE_FORMAT(OrderDate, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') THEN 1 ELSE 0 END) ThisMonthsOrders,
SUM(CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) THEN 1 ELSE 0 END) ThisYearsOrders
FROM OrderTable
WHERE Canceled <> 'Y'
GROUP BY BusinessUnit
Here is SQLFiddle demo
can anyone tell me the sql query(MySQL) that will result following output from the table .
You're after a PIVOT TABLE QUERY - although it's often better to perform the basic aggregation in (My)SQL and then handle problems of display at the application level (e.g. with a bit of PHP).
A standard query might look as follows, although MySQL supports shorthand deviations from this standard...
SELECT DATE_FORMAT(date,'%M') month
, COALESCE(SUM(CASE WHEN status = 'Rahul' THEN value END),0) Rahul
, COALESCE(SUM(CASE WHEN status = 'Vijay' THEN value END),0) Vijay
, COALESCE(SUM(CASE WHEN status = 'Loki' THEN value END),0) Loki
FROM my_table
GROUP
BY MONTH(date);
WITH w AS (
SELECT 10 AS value, 'Rahul' AS status, TO_DATE('20/01/2013', 'DD/MM/YYYY') AS date_time FROM dual
UNION ALL
SELECT 15, 'Vijay', TO_DATE('28/02/2013', 'DD/MM/YYYY') FROM dual
UNION ALL
SELECT 20, 'Loki', TO_DATE('03/02/2013', 'DD/MM/YYYY') FROM dual
UNION ALL
SELECT 25, 'Kiran', TO_DATE('05/01/2013', 'DD/MM/YYYY') FROM dual
UNION ALL
SELECT 5, 'Rahul', TO_DATE('02/01/2013', 'DD/MM/YYYY') FROM dual
UNION ALL
SELECT 15, 'Vijay', TO_DATE('10/01/2013', 'DD/MM/YYYY') FROM dual
UNION ALL
SELECT 08, 'Loki', TO_DATE('01/01/2013', 'DD/MM/YYYY') FROM dual
)
SELECT TO_CHAR(date_time, 'Mon') AS m
, SUM(CASE status WHEN 'Rahul' THEN value ELSE 0 END) AS Rahul
, SUM(CASE status WHEN 'Vijay' THEN value ELSE 0 END) AS Vijay
, SUM(CASE status WHEN 'Loki' THEN value ELSE 0 END) AS Loki
, SUM(CASE status WHEN 'Kiran' THEN value ELSE 0 END) AS Kiran
, SUM(value) AS Total
FROM w
GROUP BY TO_CHAR(date_time, 'Mon')
ORDER BY TO_DATE(TO_CHAR(date_time, 'Mon'), 'Mon')
;