SQL Tools Include Rows that has null or value of 0 - sql

I have been working on this practice problem for SQL class for the past 30 minutes. I am having a hard time including rows that have NULL values in it or a numerical value of 0. I will post the question and then the query that I have written:
"Write a query to display the tour name, outing date, and number of registered clients for each
outing of that tour on each date. Include only outings that were scheduled to occur after
October 27, 2013. Include tours with no outings and outings with no registered clients. Sort the result by the number of clients in descending order, and then by outing date in ascending order."
SELECT TOUR_NAME,OUT_DATE,Count(DISTINCT CLIENT_NUM) AS "Num Clients"
FROM TOUR RIGHT JOIN OUTING USING (TOUR_ID) JOIN REGISTER USING (OUT_ID)
WHERE To_Char(OUT_DATE,'YYYY-MM-DD') > '2013-10-27'
GROUP BY TOUR_NAME,OUT_DATE
ORDER BY "Num Clients" DESC,OUT_DATE;
I cannot figure out how to pull rows with empty cells. It currently only pulls complete rows.
-EXPECTED RESULTS:
--TOUR_NAME --OUT_DATE --Num Clients
Weekend Weekday 29-OCT-13 26
Downtown 28-OCT-13 25
Deluxe Day Away 28-OCT-13 23
Quick Break 30-OCT-13 19
Downtown 27-OCT-13 18
Downtown 30-OCT-13 18
Deluxe Day Away 31-OCT-13 12
Washington Heights 31-OCT-13 10
Weekend Weekday 13-NOV-13 0
Downtown 14-NOV-13 0
Beltway 15-NOV-13 0
Weekend Weekday 15-NOV-13 0
Quick Break 16-NOV-13 0
Power Shots 0
Perfect Endings 0
Primary Point 0
MY ACTUAL RESULTS:
TOUR_NAME OUT_DATE Num Clients
Weekend Weekday 29-OCT-13 26
Downtown 28-OCT-13 25
Deluxe Day Away 28-OCT-13 23
Quick Break 30-OCT-13 19
Downtown 27-OCT-13 18
Downtown 30-OCT-13 18
Deluxe Day Away 31-OCT-13 12
Washington Heights 31-OCT-13 10
It's not including any rows that has a null value or a zero count value within that row.
I appreciate any help. Thank you.

Related

SSRS: Horizontal alignment on a group

On my dataset I select information from four different years sorted by date and how many subscriptions I had on said date, which looks something like this:
Date Year Subs Day
15/09/2014 2015 57 1
16/09/2014 2015 18 2
17/09/2014 2015 16 3
14/09/2015 2016 10 1
15/09/2015 2016 45 2
16/09/2015 2016 28 3
12/09/2016 2017 32 1
13/09/2016 2017 11 2
14/09/2016 2017 68 3
24/08/2017 2018 23 1
25/08/2017 2018 53 2
26/08/2017 2018 13 3
What I'm trying to do is create an 'Year' Column Group to align them horizontally, but when I do that, this is the result:
result
Expected result:
expected result
Is this achievable in SSRS? I've tried removing the group =(Details), which gives me the desired result, except it only returns one line of information.
Any insight aprreciated.
By default, the Details group causes you to get one row per row in the dataset. In your case, I would suggest grouping the Rows by the Day column and create a column group by Year.
First, create the two groups and add columns inside the column group.
Then, add a row outside and above the Day row group. Place the headings here and then delete the top row. It should look like this:
Now these 4 columns will repeat to the right for each year and you will get rows based on the number of days in your dataset.

Determining Sick Periods from ranges

I have the below set of data which represents employee sick/absence days over a period (12 months) of time, in a table named Absence:
Day Date DaysSick OccasionsSick Notes
Tuesday 2016-09-27 1 Lisa A working today
Thursday 2016-09-29 1 Lisa sick today Celeste
Thursday 2017-01-05 1 Lisa sick today
I would like to update the OccasionsSick column based upon the instances of being sick. So i would have the following:
Day Date DaysSick OccasionsSick Notes
Tuesday 2016-09-27 1 1 Lisa A working today
Thursday 2016-09-29 1 Lisa sick today Celeste
Thursday 2017-01-05 1 1 Lisa sick today
So, the first two entries are the same period of sick leave, so i need a 1 in the first row, and the last entry is a separate sick period, so a 1 again.
Now, in order to establish a sick period there would be a reference to a roster table containing the below:
Date RosterType
2016-09-27 Sick
2016-09-28 Day Off
2016-09-29 Sick
2016-09-30 Normal
So the 27th and 29th were sick days, but the 28th was a standard day off, which is a likely occurrence, so using consecutive days is not an option. I need to be able to look for sick days until a "normal" RosterType is found, this then breaks the sick period. This 1 then needs to be assigned as per the desired result set.
What is the best way of updating the data here? I have come up with a big blank on this, apart from the logic of determining a sick period.
I am presenting this data in Excel with VBA, so it could also be easier to assign the sick periods in VBA, as opposed to SQL for the raw data
Please check this out.
This assumes that there is an entry in the roster for each day.
Basically I'm just building a period and counting the days in the roster.
If there are normal days in between it counts as a new period.
WITH CTE AS (
SELECT
[day]
,[date]
,LAG(date, 1) over (order by date) datebefore
,[dayssick]
FROM [dbo].[absence]
)
SELECT
*
,CASE WHEN ((SELECT COUNT(1) FROM [dbo].[rostertype] WHERE date < c.date AND date > c.datebefore AND rostertype = 'Normal') > 0
OR c.datebefore IS NULL) THEN 1 ELSE 0 END OccasionsSick
FROM CTE c

sql running total math current quarter

Im trying to figure out the total for the quarter when the only data shown is a running total for the year:
Id Amount Periods Year Type Date
-------------------------------------------------------------
1 65 2 2014 G 4-1-12
2 75 3 2014 G 7-1-12
3 25 1 2014 G 1-1-12
4 60 1 2014 H 1-1-12
5 75 1 2014 Y 1-1-12
6 120 3 2014 I 7-1-12
7 30 1 2014 I 1-1-12
8 90 2 2014 I 4-1-12
In the data shown above. The items in type G and I are running totals for the period (in qtrs). If my query returns period 3, is there a sql way to get the data for the qtr? The math would involve retrieving the data for the 3rd period - 2nd period.
Right now my sql is something like:
SELECT * FROM data WHERE Date='4-1-12';
In this query, it will return row #1, which is a total for 2 periods. I would like it to return just the total for the 2nd period. Im looking to make this happen with SQLite.
Any help would be appreciated.
Thank alot
You want to subtract the running total of the previous quarter:
SELECT Id,
Year,
Type,
Date,
Amount - IFNULL((SELECT Amount
FROM data AS previousQuarter
WHERE previousQuarter.Year = data.year
AND previousQuarter.Type = data.Type
AND previousQuarter.Periods = data.Periods - 1
), 0) AS Amount
FROM data
The IFNULL is needed to handle a quarter that has no previous quarter.

SQL count ocurrences within time periods

I recently started up working with Access database in a project at my work (hospital) and got lots of useful tips from this site. However, I have now a problem that I can't figure out how to solve.
I have a table containing treatment dates (as well as other data) for lots of patients. My task is to count the number of treatments given within each week (and possibly month/quarter/year as well). The treatment dates are located in the column 'TreatDate' in the table 'Data'.
I have used DatePart to separate out year and week number as:
SELECT
DatePart('yyyy',[TreatStart]) AS Year, DatePart('ww',[TreatStart]) AS Week
FROM Data
ORDER BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart]);
Which gives me:
Year Week
2006 16
2006 16
2006 16
2006 17
2006 17
2006 18
2006 19
2006 19
2006 19
... ...
How do I count the number of occurances in each week in order to get something like:
Year Week N
2006 16 3
2006 17 2
2006 18 1
2006 19 3
... ... ...
Best regards,
Mattias
Just add the group by on the same values by which you want count and add the count aggregate function to get the total count of the appointments at that given group.
SELECT DatePart('yyyy',[TreatStart]) AS Year, DatePart('ww',[TreatStart]) AS Week , COUNT(*) As N
FROM Data
GROUP BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart])
ORDER BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart]);
Use GROUP BY and COUNT:
SELECT DatePart('yyyy',[TreatStart]) AS Year,
DatePart('ww',[TreatStart]) AS Week,
COUNT(*) as N
FROM Data
GROUP BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart])
ORDER BY DatePart('yyyy',[TreatStart]),DatePart('ww',[TreatStart]);
The output will be:
Year Week N
2006 16 3
2006 17 2
2006 18 1
2006 19 3

Counting columns in the same table

I've been trying to develop a cashflow statement in access 2007. This would be so easily done in excel using formulas such as
= SUM (B6:M6) / CountIF(B6:M6)>0
but I cant seem to wrap my head around this when it comes to access. And I need this for every company we enter data on. The cashflow statement is supposed to look like this (Since I can't yet post a pic):
----------------------------------------------------------------------------------------------------------------
Particulars | Jan | Feb | Mar | Apr | Jun | Jul | Aug | Sep | Oct | Nov | Dec | Average |
Sales---------------------->
Salary------>
Transportation----->
and about 10 other items in the row, all with entries for Jan till Dec, however, sometimes we take 6 months worth of data and sometimes for all 12 months. (Imagine a basic excel sheet with items on the first column and headers for the next 12-13 columns).
In access, I made tables for each Item with columns as the months, eg. tblRcpt--> |rcpt_ID|Jan|Feb|... and so on till dec for all the items. Then they will be arranged and presented in an entry form which would be designed to look similar to the above table while later I would query and link them together to presentthe complete cashflow statement.
Now comes the question, I need to Average together the columns (as you can see in the right most column), BUT I only want to average together those months that have been filled (Sometimes in accounting people enter '0' where there is no data), so I cant just sum the columns and divide by twelve. It has to be dynamic, all functions seem to center around counting and averaging ROWs, not COLUMNs.
Thanks for just bearing with me and reading this, any help would be much appreciated.
Try this
(Jan + Feb + ... + Dec) /
( case when Jan = 0 then 0 else 1 end
+ case when Feb = 0 then 0 else 1 end
+ case when Dec = 0 then 0 else 1 end )
as Avg
Your table structure should be:
Particulars | Month | Amount
Sales 1 500
Sales 2 1000
Salary 1 80000
...and so on. You can either not enter rows when you don't have a value for that month, or you can handle them in the SQL statement (as I have below):
SELECT Particulars, AVG(Amount) AverageAmount
FROM MyTable
WHERE NULLIF(Amount, 0) IS NOT NULL
GROUP BY Particulars;