Query to find rows with 2 consecutive months (colums) that are negative - sql-server-2008

I have a SQL Server 2008 table with columns representing each 12 months. I want to find rows where there are 2 consecutive month with a negative balance.
Edit: I needed to sum all the positive columns so I used this. If anybody knows an easier way let me know.
SELECT SUM(case when Oct > 0 then Oct else 0 end) +
SUM(case when Nov > 0 then Nov else 0 end) +
SUM(case when Dec > 0 then Dec else 0 end) +
SUM(case when Jan > 0 then Jan else 0 end) +
SUM(case when Feb > 0 then Feb else 0 end) +
SUM(case when Mar > 0 then Mar else 0 end) +
SUM(case when Apr > 0 then Apr else 0 end) +
SUM(case when May > 0 then May else 0 end) +
SUM(case when Jun > 0 then Jun else 0 end) +
SUM(case when Jul > 0 then Jul else 0 end) +
SUM(case when Aug > 0 then Aug else 0 end) +
SUM(case when Sep > 0 then Sep else 0 end) as 'Sum of positive months'

You can do it straightforward:
select * from tablename
where (Oct < 0 and Nov < 0) or
(Nov < 0 and Dec < 0) or
(Dec < 0 and Jan < 0) or
(Jan < 0 and Feb < 0) or
...

Related

How to change result SQL syntax for age group from colomn to new row

I Tried to grouping age from and found this answer(SQL Group by Age Range)
But the result is as a colomn not a row Like this
I Just want to convert the colomn into a row Like This
The query is:
SELECT SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') < 18 THEN 1 ELSE 0 END) AS '< 18',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 18 AND 25 THEN 1 ELSE 0 END) AS '18-25 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 26 AND 35 THEN 1 ELSE 0 END) AS '26-35 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 36 AND 45 THEN 1 ELSE 0 END) AS '36-45 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 46 AND 55 THEN 1 ELSE 0 END) AS '46-55 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 56 AND 65 THEN 1 ELSE 0 END) AS '56-65 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 66 AND 200 THEN 1 ELSE 0 END) AS '>66 Tahun'
FROM user_detail
My table for tanggal_lahir:

Get Data for Grouping all Months in a year using MySql and DB2

below is my query
SELECT DISTINCT e2.status as status, e2.year as year, e2.month as month,e2.Jan,e2.Feb,e2.Mar,e2.Apr,e2.May,e2.Jun,e2.Jul,e2.Aug,e2.Sep,e2.Oct,e2.Nov, e2.Dece, e2.countTotal FROM tranx_history e1 INNER JOIN (
SELECT YEAR(e.create_dt) AS year, MONTH(e.create_dt) AS month,
SUM(CASE WHEN MONTH(e.create_dt) = 01 THEN 1 ELSE 0 END) AS Jan,
SUM(CASE WHEN MONTH(e.create_dt) = 02 THEN 1 ELSE 0 END) AS Feb,
SUM(CASE WHEN MONTH(e.create_dt) = 03 THEN 1 ELSE 0 END) AS Mar,
SUM(CASE WHEN MONTH(e.create_dt) = 04 THEN 1 ELSE 0 END) AS Apr,
SUM(CASE WHEN MONTH(e.create_dt) = 05 THEN 1 ELSE 0 END) AS May,
SUM(CASE WHEN MONTH(e.create_dt) = 06 THEN 1 ELSE 0 END) AS Jun,
SUM(CASE WHEN MONTH(e.create_dt) = 07 THEN 1 ELSE 0 END) AS Jul,
SUM(CASE WHEN MONTH(e.create_dt) = 08 THEN 1 ELSE 0 END) AS Aug,
SUM(CASE WHEN MONTH(e.create_dt) = 09 THEN 1 ELSE 0 END) AS Sep,
SUM(CASE WHEN MONTH(e.create_dt) = 10 THEN 1 ELSE 0 END) AS Oct,
SUM(CASE WHEN MONTH(e.create_dt) = 11 THEN 1 ELSE 0 END) AS Nov,
SUM(CASE WHEN MONTH(e.create_dt) = 12 THEN 1 ELSE 0 END) AS Dece,
SUM(CASE WHEN MONTH(e.create_dt) >= 01 AND MONTH(e.create_dt) <= 12 THEN 1 ELSE 0 END) as countTotal,
e.trnx_status as status
from tranx_history e
GROUP BY e.trnx_status, MONTH(e.create_dt),YEAR(e.create_dt) ) e2 ON YEAR(e1.create_dt) = e2.year AND MONTH(e1.create_dt) = e2.month ORDER BY e2.year asc, e2.month asc
For the Above query, The Output we are getting is first it is grouping by Jan then in the next row it is grouping by Feb even though status and year are same
i want the output Jan and Feb with same status and year in a single row not in different rows

SQL Group by "Folder Name" by month and then total

My new query is giving me few errors:
Error: ORA-0093: SQL command not properly ended.
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month (a.sent_date)=1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date)=2 then a.total_sent else 0 end) as February,
sum(case when month(a.sent_date)=3 then a.total_sent else 0 end) as March,
sum(case when month(a.sent_date)=4 then a.total_sent else 0 end) as April,
sum(case when month(a.sent_date)=5 then a.total_sent else 0 end) as May,
sum(case when month(a.sent_date)=6 then a.total_sent else 0 end) as June,
sum(case when month(a.sent_date)=7 then a.total_sent else 0 end) as July,
sum(case when month(a.sent_date)=8 then a.total_sent else 0 end) as August,
sum(case when month(a.sent_date)=9 then a.total_sent else 0 end) as September,
sum(case when month(a.sent_date)=10 then a.total_sent else 0 end) as October,
sum(case when month(a.sent_date)=11 then a.total_sent else 0 end) as November,
sum(case when month(a.sent_date)=12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id=123 AND
a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;
=========
This is my first time posting here and also a beginner at queries.
I am running a query which returns various folder names for all days. I want to group by the folder name and do a sum of the totals for each folder name by months and then a total of each column at the bottom. This is the query I am running:
select a.group_name, a.sent_date, a.total_sent
from
c_group a
where
a.partner_id=123
and a.sent_date >= '01-JAN-2012'
and a.sent_date <= '31-DEC-2012'
Displays as follows:
GROUP_NAME SENT_DATE TOTAL_SENT
Group A 1-Jan-12 37
Group B 3-Jan-12 25
Group C 1-May-12 10
Group D 1-May-12 8
Group D 1-Jan-12 11
Group A 1-Dec-12 9
I need the results to display as:
January February March April May June July August September October November December
Group A
Group B
Group C
Group D
...
....
...
....
Total Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above Sum of above
You want to combine conditional aggregation with rollup:
select coalesce(a.group_name, 'Total') as group_name,
sum(case when month(a.sent_date) = 1 then a.total_sent else 0 end) as January,
sum(case when month(a.sent_date) = 2 then a.total_sent else 0 end) as February,
. . .
sum(case when month(a.sent_date) = 12 then a.total_sent else 0 end) as December
from c_group a
where a.partner_id = 123 AND
a.sent_date >= '01-JAN-2012' AND
a.sent_date <= '31-DEC-2012'
group by a.group_name with rollup;

SQL - Make a custom table with month based columns and year based rows

I am trying to make an awesome custom table which counts the amount of rows and organises them so that if there are three rows with a date in January 2013, four in March 2014 and five in October 2014 the table would show up as:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2013 3 0 0 0 0 0 0 0 0 0 0 0
2014 0 0 4 0 0 0 0 0 0 5 0 0
I would recommend using a view, rather than a new table, this way when your underlying data changes your new table won't be out of sync.
Since you have not given much sample data I have had to assume a structure, but you would want something like this:
CREATE TABLE T (`Date` DATETIME);
INSERT T (`Date`)
VALUES
('2013-01-01'), ('2013-01-02'), ('2013-01-03'),
('2014-03-01'), ('2014-03-02'), ('2014-03-03'),
('2014-10-01'), ('2014-10-01'), ('2014-10-01'),
('2014-10-01'), ('2014-10-01');
CREATE VIEW V
AS
SELECT YEAR(`Date`) AS `Year`,
COUNT(CASE WHEN MONTH(`Date`) = 1 THEN 1 END) AS `Jan`,
COUNT(CASE WHEN MONTH(`Date`) = 2 THEN 1 END) AS `Feb`,
COUNT(CASE WHEN MONTH(`Date`) = 3 THEN 1 END) AS `Mar`,
COUNT(CASE WHEN MONTH(`Date`) = 4 THEN 1 END) AS `Apr`,
COUNT(CASE WHEN MONTH(`Date`) = 5 THEN 1 END) AS `May`,
COUNT(CASE WHEN MONTH(`Date`) = 6 THEN 1 END) AS `Jun`,
COUNT(CASE WHEN MONTH(`Date`) = 7 THEN 1 END) AS `Jul`,
COUNT(CASE WHEN MONTH(`Date`) = 8 THEN 1 END) AS `Aug`,
COUNT(CASE WHEN MONTH(`Date`) = 9 THEN 1 END) AS `Sep`,
COUNT(CASE WHEN MONTH(`Date`) = 10 THEN 1 END) AS `Oct`,
COUNT(CASE WHEN MONTH(`Date`) = 11 THEN 1 END) AS `Nov`,
COUNT(CASE WHEN MONTH(`Date`) = 12 THEN 1 END) AS `Dec`
FROM T
GROUP BY YEAR(`Date`);
Example on SQL Fiddle

Mysql Case - Count Fields thats fall in curtain hours

I am trying to set up a report that has the following fields :
Pretty much need the report to show the date, the total records that fall with in that date (so I group by Date) and then on a per hour basis for a 12 hour work day (from 8AM - 8PM) I need to count when a records is present within those times. After some brain storming and brain farts I thought why not use a case. This is what I have tried :
SELECT
DATE_FORMAT(signintime, '%b %d, %Y') Date,
COUNT(session_id) as Total,
SUM(CASE WHEN HOUR(signintime) > 08 AND HOUR(signintime) < 09 THEN 1 ELSE 0 END) '8AM-9PM',
SUM(CASE WHEN HOUR(signintime) > 09 AND HOUR(signintime) < 10 THEN 1 ELSE 0 END) '9AM-10AM',
SUM(CASE WHEN HOUR(signintime) > 10 AND HOUR(signintime) < 11 THEN 1 ELSE 0 END) '10AM-11AM',
SUM(CASE WHEN HOUR(signintime) > 11 AND HOUR(signintime) < 12 THEN 1 ELSE 0 END) '11AM-12PM',
SUM(CASE WHEN HOUR(signintime) > 12 AND HOUR(signintime) < 13 THEN 1 ELSE 0 END) '12PM-1PM',
SUM(CASE WHEN HOUR(signintime) > 13 AND HOUR(signintime) < 14 THEN 1 ELSE 0 END) '1PM-2PM',
SUM(CASE WHEN HOUR(signintime) > 14 AND HOUR(signintime) < 15 THEN 1 ELSE 0 END) '2PM-3PM',
SUM(CASE WHEN HOUR(signintime) > 15 AND HOUR(signintime) < 16 THEN 1 ELSE 0 END) '3PM-4PM',
SUM(CASE WHEN HOUR(signintime) > 16 AND HOUR(signintime) < 17 THEN 1 ELSE 0 END) '4PM-5PM',
SUM(CASE WHEN HOUR(signintime) > 17 AND HOUR(signintime) < 18 THEN 1 ELSE 0 END) '5PM-6PM',
SUM(CASE WHEN HOUR(signintime) > 18 AND HOUR(signintime) < 19 THEN 1 ELSE 0 END) '6PM-7PM',
SUM(CASE WHEN HOUR(signintime) > 19 AND HOUR(signintime) < 20 THEN 1 ELSE 0 END) '7PM-8PM'
FROM session
WHERE session.status = '3'
GROUP by HOUR(signintime), Date;
If you notice the picture above, the total is 1 for that entire day (Apr 19, 2013) now if you notice the times (8AM - 8PM) they are all zeroed out. I am unsure as to where to turn to / where to debug this. Hopefully with another set of eyes I can get this moving along.
Regards.
You should not need to use both the > and < to get the result, you can just check the hour value:
select DATE_FORMAT(signintime, '%b %d, %Y') Date,
count(session_id) as Total,
SUM(CASE WHEN HOUR(signintime) = 08 THEN 1 ELSE 0 END) '8AM-9AM',
SUM(CASE WHEN HOUR(signintime) = 09 THEN 1 ELSE 0 END) '9AM-10AM',
SUM(CASE WHEN HOUR(signintime) = 10 THEN 1 ELSE 0 END) '10AM-11AM',
SUM(CASE WHEN HOUR(signintime) = 11 THEN 1 ELSE 0 END) '11AM-12PM',
SUM(CASE WHEN HOUR(signintime) = 12 THEN 1 ELSE 0 END) '12PM-1PM',
SUM(CASE WHEN HOUR(signintime) = 13 THEN 1 ELSE 0 END) '1PM-2PM',
SUM(CASE WHEN HOUR(signintime) = 14 THEN 1 ELSE 0 END) '2PM-3PM',
SUM(CASE WHEN HOUR(signintime) = 15 THEN 1 ELSE 0 END) '3PM-4PM',
SUM(CASE WHEN HOUR(signintime) = 16 THEN 1 ELSE 0 END) '4PM-5PM',
SUM(CASE WHEN HOUR(signintime) = 17 THEN 1 ELSE 0 END) '5PM-6PM',
SUM(CASE WHEN HOUR(signintime) = 18 THEN 1 ELSE 0 END) '6PM-7PM',
SUM(CASE WHEN HOUR(signintime) = 19 THEN 1 ELSE 0 END) '7PM-8PM'
from session
WHERE session.status = '3'
group by DATE_FORMAT(signintime, '%b %d, %Y');
See SQL Fiddle with Demo