Grabbing/rearranging data from SQL for table - sql

I have data in sql that looks like so:
Month PersonID Level
01 102 2
01 506 1
02 617 3
02 506 1
03 297 2
And I need to query this data to receive it for use in a table that would look like this
Jan Feb March ...etc
Level 1
Level 2
Level 3
with the values being how many people are in each level each month.
I'm a complete noob with SQL so any help and relevant links to explain answers would be much appreciated.

Try this:
SELECT 'Level' + CAST(level as varchar), [January], [February], [March]
FROM (SELECT DATENAME(month, '2013'+Month+'01') Month, PersonID, Level FROM Tbl) T
PIVOT
(
COUNT(PersonID) FOR Month IN ([January], [February], [March])
) A
SQL FIDDLE DEMO

SELECT 'Level ' + CAST("Level" AS VARCHAR(2)),
SUM(CASE Month WHEN '01' THEN 1 ELSE 0 END) AS Jan,
SUM(CASE Month WHEN '02' THEN 1 ELSE 0 END) AS Feb,
SUM(CASE Month WHEN '03' THEN 1 ELSE 0 END) AS Mar,
...
FROM myTable
GROUP BY "Level"
SQL Fiddle Example
This is basically a poor man's pivot table, which should work on most RDBMS. What it does is use a SUM with a CASE to achieve a count-if for each month. That is, for January, the value for each row will be 1 if Month = '01', or 0 otherwise. Summing these values gets the total count of all "January" rows in your table.
The GROUP BY Level clause tells the engine to produce one result row for each distinct value in Level, thus separating your data by the different levels.
Since you are using SQL Server 2005, which supports PIVOT, you can simply do:
SELECT 'Level ' + CAST("Level" AS VARCHAR(2)),
[01] AS [Jan], [02] AS [Feb], [03] AS [Mar], ...
FROM myTable
PIVOT
(
COUNT(PersonId)
FOR Month IN ([01], [02], [03], ...)
) x
SQL Fiddle Example

Related

Calculate difference of data in respective weeks using week number

My data is stored in Google Big QUery in a database. This is how my table looks like. Here Epid_ID is unique for each row and the count is calculated using this value.
Admin_Level_2_district WeekNumber Epid_ID
Jhapa 18 COV-NEP-PR1-SUN-20-00072
Jhapa 19 COV-NEP-PR1-SUN-20-00073
Morang 18 COV-NEP-PR1-SUN-20-00074
Morang 19 COV-NEP-PR1-SUN-20-00075
I want to find the difference in data in two weeks. This is my expected output.
Admin_Level_2_district count_Week_18 count_Week 19 Difference
Jhapa 50 60 10
Morang 60 50 -10
Following is the query I have tried.
SELECT
Admin_Level_2_district,
Week_number,
count(Epid_ID)
FROM `interim-data.casedata.Interim EpiData`
GROUP BY
Admin_Level_2_district,
Week_number
HAVING Week_number = '18'
or Week_number = '19'
Please help!
I think you want conditional aggregation here:
SELECT
Admin_Level_2_district,
COUNT(CASE WHEN WeekNumber = 18 THEN 1 END) AS count_Week_18,
COUNT(CASE WHEN WeekNumber = 19 THEN 1 END) AS count_Week_19,
COUNT(CASE WHEN WeekNumber = 19 THEN 1 END) -
COUNT(CASE WHEN WeekNumber = 18 THEN 1 END) AS Difference
FROM `interim-data.casedata.Interim EpiData`
GROUP BY
Admin_Level_2_district;
You want to use conditional aggregation. In BigQuery, I would recommend countif():
SELECT Admin_Level_2_district,
COUNTIF(week_number = '18') as count_week_18,
COUNTIF(week_number = '19') as count_week_19,
COUNTIF(week_number = '19') - COUNTIF(week_number = '18') as diff
FROM `interim-data.casedata.Interim EpiData`
WHERE Week_number IN ('18', '19')
GROUP BY Admin_Level_2_district;
Note: I would expect week_number to be a number, in which case you would not use single quotes. However, your code treats that as a string, so I left that in.

Group By Creates Duplicate Rows

I am using Oracle sql to create a sample data GridView and run into a very basic issue. So here it's, I've to organize data month-wise, say no of employees in a month based on a status column. So status = 0; Jan1 and status > 0; Jan2. I am not elaborating anything else as it has already a built-in view and that's what I've to use to make it work. So here is the query that I am using and the sample output that works fine except one:
SELECT DISTINCT SYEAR, DEPT_NAME,
--Month-wise data - Starts
DECODE ( upper((MONTHNAMESHORT)), 'JAN', NVL((FirstLetter), 0), NULL) "JAN1" ,
DECODE ( upper((MONTHNAMESHORT)), 'JAN', NVL((SecondLetter), 0), NULL) "JAN2",
DECODE ( upper((MONTHNAMESHORT)), 'FEB', NVL((FirstLetter), 0), NULL) "FEB1" ,
DECODE ( upper((MONTHNAMESHORT)), 'FEB', NVL((SecondLetter), 0), NULL) "FEB2"
--Month-wise data - Ends
FROM
--Sub-query - starts
(SELECT DISTINCT VWWEBLETTERSTATUS2.SYEAR, MONTHRANK.MONTHNAMESHORT,VWWEBLETTERSTATUS2.DEPT_NAME,
nvl(fnfirstletter(DEPT_NAME,upper(MONTHRANK.MONTHNAMESHORT),VWWEBLETTERSTATUS2.SYEAR),0) FirstLetter,
nvl(fnSecondLetter(DEPT_NAME,upper(MONTHRANK.MONTHNAMESHORT),VWWEBLETTERSTATUS2.SYEAR),0) SecondLetter,MONTHRANK.RANK
FROM
MONTHRANK,VWWEBLETTERSTATUS2 where VWWEBLETTERSTATUS2.SYEAR = '2018' AND
nvl(fnfirstletter(DEPT_NAME,upper(MONTHRANK.MONTHNAMESHORT),VWWEBLETTERSTATUS2.SYEAR), 0) <> 0 AND
nvl(fnSecondLetter(DEPT_NAME,upper(MONTHRANK.MONTHNAMESHORT),VWWEBLETTERSTATUS2.SYEAR), 0) <> 0
order by DEPT_NAME, rank) q
--Sub-query - Ends
GROUP BY SYEAR, (MONTHNAMESHORT), DEPT_NAME; --Issue here - For the month-wise group by
Output
Year Dept Jan1 Jan2 Feb1 Feb2
2018 UNIT-I3 93 87
2018 UNIT-I5 62 66
2018 QA 0 0
2018 UNIT-I5 87 66
Here for the GROUP BY (MONTHNAMESHORT) clause, it creates duplicate rows for the department and that specific year. Say when Unit-I5 has data for both the months, it creates separate rows though it should be in a single row.
Any way to overcome the issue keeping the same thing, just an alternate for the GROUP BY?
Update 1: Even tried this one, but didn't work
SUM(CASE WHEN Q.MONTHNAMESHORT = 'JAN' THEN Q.FirstLetter ELSE 0 END) "JAN1",
SUM(CASE WHEN Q.MONTHNAMESHORT = 'JAN' THEN Q.SecondLetter ELSE 0 END) "JAN2"
N.B: FirstLetter and SecondLetter are counted in the view.
SELECT DISTINCT is almost never appropriate with GROUP BY.
Your problem is that you are including (MONTHNAMESHORT) in the GROUP BY.
Your query is very difficult to decipher. But it should look something like this:
SELECT SYEAR, DEPT_NAME,
SUM(CASE WHEN upper(MONTHNAMESHORT) = 'JAN' THEN FirstLetter END) as "JAN1" ,
SUM(CASE WHEN upper(MONTHNAMESHORT) = 'JAN' THEN SecondLetter END) as "JAN2" ,
SUM(CASE WHEN upper(MONTHNAMESHORT) = 'FEB' THEN FirstLetter END) as "FEB1" ,
SUM(CASE WHEN upper(MONTHNAMESHORT) = 'FEB' THEN SecondLetter END) as "FEB2"
FROM . . .
GROUP BY SYEAR, DEPT_NAME;

SQL Pivot 2 Columns

I have the table of the following format
I think my problem is a bit unique than the possible duplicate question, and I'm trying to get repetitive 201601...201652 columns for the two metrics orders and cost.
This is an approach for any database (including SQL Server) that does not rely on a proprietary PIVOT() function. It's a bit weird to do that for 52 weeks in such an example, though (and, to tell you the truth, the 105 resulting columns are not really the best output for the benefit of a human being reading the report).
Having said that, in this example, I do it for quarters of a year rather than weeks, and you'd just have to repeat the expressions 52 times instead of 4 times.
You could use perl or Visual Basic or whatever you prefer to generate the statement, actually.
Here goes:
-- the input table, don't use in real query ...
WITH
input(id,quarter,orders,cost) AS (
SELECT 1,201601,200,1000
UNION ALL SELECT 1,201602,300,1500
UNION ALL SELECT 1,201603,330,1800
UNION ALL SELECT 1,201604,500,2500
)
-- end of input -
SELECT
id
, SUM(CASE quarter WHEN 201601 THEN orders END) AS "orders_201601"
, SUM(CASE quarter WHEN 201602 THEN orders END) AS "orders_201602"
, SUM(CASE quarter WHEN 201603 THEN orders END) AS "orders_201603"
, SUM(CASE quarter WHEN 201604 THEN orders END) AS "orders_201604"
, SUM(CASE quarter WHEN 201601 THEN cost END) AS "cost_201601"
, SUM(CASE quarter WHEN 201602 THEN cost END) AS "cost_201602"
, SUM(CASE quarter WHEN 201603 THEN cost END) AS "cost_201603"
, SUM(CASE quarter WHEN 201604 THEN cost END) AS "cost_201604"
FROM input
GROUP BY id;
id|orders_201601|orders_201602|orders_201603|orders_201604|cost_201601|cost_201602|cost_201603|cost_201604
1| 200| 300| 330| 500| 1,000| 1,500| 1,800| 2,500

How to count every half hour?

I have a query that its counting every hour, using a pivot table.
How would it be possible to get the count for every 30 minutes?
for example 8:00-8:29,8:30-8:59,9:00-9:29 etc. until 5:00
SELECT CONVERT(varchar(8),start_date,1) AS 'Day',
SUM(CASE WHEN DATEPART(hour,start_date) = 8 THEN 1 ELSE 0 END) as eight ,
SUM(CASE WHEN DATEPART(hour,start_date) = 9 THEN 1 ELSE 0 END) AS nine,
SUM(CASE WHEN DATEPART(hour,start_date) = 10 THEN 1 ELSE 0 END) AS ten,
SUM(CASE WHEN DATEPART(hour,start_date) = 11 THEN 1 ELSE 0 END) AS eleven,
SUM(CASE WHEN DATEPART(hour,start_date) = 12 THEN 1 ELSE 0 END) AS twelve,
SUM(CASE WHEN DATEPART(hour,start_date) = 13 THEN 1 ELSE 0 END) AS one_clock,
SUM(CASE WHEN DATEPART(hour,start_date) = 14 THEN 1 ELSE 0 END) AS two_clock,
SUM(CASE WHEN DATEPART(hour,start_date) = 15 THEN 1 ELSE 0 END) AS three_clock,
SUM(CASE WHEN DATEPART(hour,start_date) = 16 THEN 1 ELSE 0 END) AS four_clock
FROM test
where user_id is not null
GROUP BY CONVERT(varchar(8),start_date,1)
ORDER BY CONVERT(varchar(8),start_date,1)
I use sql server 2012 (version Microsoft SQL Server Management Studio 11.0.3128.0)
Try using iif as below:
SELECT CONVERT(varchar(8),start_date,1) AS 'Day', SUM(iif(DATEPART(hour,start_date) = 8 and
DATEPART(minute,start_date) >= 0 and
DATEPART(minute,start_date) =< 29,1,0)) as eight_tirty
FROM test where user_id is not null GROUP BY
CONVERT(varchar(8),start_date,1) ORDER BY
CONVERT(varchar(8),start_date,1)
To get counts by day and half hour, something like this should work.
SELECT day, half_hour, count(1) AS half_hour_count
FROM (
SELECT
CAST(start_date AS date) AS day,
DATEPART(hh, start_date)
+ 0.5*(DATEPART(n,start_date)/30) AS half_hour
FROM test
WHERE user_id IS NOT NULL
) qry
GROUP BY day, half_hour
ORDER BY day, half_hour;
Formatting the result could be done later.
You need a few things, and then this query just falls together.
First, assuming you need multiple dates, you're going to want what's known as a Calendar Table (hands down, probably the most useful analysis table).
Next, you're going to want either an existing Numbers table if you have one, or just generate the first on the fly:
WITH Halfs AS (SELECT CAST(0 AS INT) m
UNION ALL
SELECT m + 1
FROM Halfs
WHERE m < 24 * 2)
SELECT m
FROM Halfs
(recursive CTE - generates a table with a list of numbers starting at 0).
These two tables will provide the basis for a range query based on the timestamps in your main table. This will make it very easy for the optimizer to bucket rows for whatever aggregation you're doing. That's done by CROSS JOINing the two tables together in a subquery, as well as adding a couple of other derived columns:
WITH Halfs AS (SELECT CAST(0 AS INT) m
UNION ALL
SELECT m + 1
FROM Halfs
WHERE m < 24 * 2)
SELECT calendarDate, m, rangeStart, rangeEnd
FROM (SELECT Calendar.calendarDate, Halfs.m rangeGroup,
DATEADD(minutes, m * 30, CAST(Calendar.calendarDate AS DATETIME2) rangeStart,
DATEADD(minutes, (m + 1) * 30, CAST(Calendar.calendarDate AS DATETIME2) rangeEnd
FROM Calendar
CROSS JOIN Halfs
WHERE Calendar.calendarDate >= CAST('20160823' AS DATE)
AND Calendar.calendarDate < CAST('20160830' AS DATE)
-- OR whatever your date range actually is.
) Range
ORDER BY rangeStart
(note that, if the range of dates is sufficiently large, it may be beneficial to save this off as a temporary table with indicies. For small tables and datasets, the performance gain isn't likely to be noticeable)
Now that we have our ranges, it's trivial to get our groups, and pivot the table.
Oh, and SQL Server has a specific operator for PIVOTing.
WITH Halfs AS (SELECT CAST(0 AS INT) m
UNION ALL
SELECT m + 1
FROM Halfs
WHERE m < 3 * 2)
-- Intentionally limiting range for example only
SELECT calendarDate AS day, [0], [1], [2], [3], [4], [5], [6]
-- If you're displaying "nice" names,
-- do it at this point, or in the reporting application
FROM (SELECT Range.calendarDate, Range.rangeGroup
FROM (SELECT Calendar.calendarDate, Halfs.m rangeGroup,
DATEADD(minutes, m * 30, CAST(Calendar.calendarDate AS DATETIME2) rangeStart,
DATEADD(minutes, (m + 1) * 30, CAST(Calendar.calendarDate AS DATETIME2) rangeEnd
FROM Calendar
CROSS JOIN Halfs
WHERE Calendar.calendarDate >= CAST('20160823' AS DATE)
AND Calendar.calendarDate < CAST('20160830' AS DATE)
-- OR whatever your date range actually is.
) Range
LEFT JOIN Test
ON Test.user_id IS NOT NULL
AND Test.start_date >= Range.rangeStart
AND Test.start_date < Range.rangeEnd
) AS DataTable
PIVOT (COUNT(*)
FOR Range.rangeGroup IN ([0], [1], [2], [3], [4], [5], [6])) AS PT
-- Only covers the first 6 groups,
-- or the first three hours.
ORDER BY day
The pivot should take care of the getting individual columns, and COUNT will automatically resolve null rows. Should be all you need.

Help with cross join creating a week view calendar

Hi all i have a table called bookings like this ( Ive bolded the columns to align)
CustID VenueID BookingDt Session
45 44 2010-03-20 00:00:00.000 PM
45 44 2010-03-27 00:00:00.000 PM
45 44 2009-10-18 00:00:00.000 PM
45 44 2009-10-24 00:00:00.000 PM
I have another table called Venues
oID oLocation oPitch
1 Left Park Rugby
2 Right Park Rugby
The tables are inter joined by Venues.oID=bookings .CustID
i want to make a table such as this
X Column = week days
Y column = locations
oID oSun oMon oTue oWed oThu oFri oSat
1 x x x x
2 x x x x x x x
I believe i have to do a cross join with the data from the bookings Database
Eg
select distinct v.olocation , b.BookingDt from oVenue V
cross join tblBookings B
Where B.VenueID=V.oID
and DATEPART( wk, b.BookingDt )='44'
and DATEPART( yy, b.BookingDt )='2009'
But this does oID and Date, i want it to do check to see if that date is there, if so place a x in its place other wise place a '' in its place.
Not sure the best way to proceed.
Any help is muchly appreciated.
Thanks in advance
Since you already restricted the week and year in your query, this is how to display it:
select
v.olocation,
max(case DATEPART(weekday, b.BookingDt) When 1 then 'x' else '' end) Sun,
max(case DATEPART(weekday, b.BookingDt) When 2 then 'x' else '' end) Mon,
max(case DATEPART(weekday, b.BookingDt) When 3 then 'x' else '' end) Tue,
max(case DATEPART(weekday, b.BookingDt) When 4 then 'x' else '' end) Wed,
max(case DATEPART(weekday, b.BookingDt) When 5 then 'x' else '' end) Thu,
max(case DATEPART(weekday, b.BookingDt) When 6 then 'x' else '' end) Fri,
max(case DATEPART(weekday, b.BookingDt) When 7 then 'x' else '' end) Sat
from
(
select distinct v.olocation , b.BookingDt
from oVenue V
LEFT JOIN tblBookings B on B.VenueID=V.oID
and DATEPART( wk, b.BookingDt )='44'
and DATEPART( yy, b.BookingDt )='2009'
) selweek
group by v.olocation
How the data is displayed should be a front-end issue, not a database issue. I wouldn't concentrate on things like putting "x" in a specific spot. Return the data that your application needs to fill in your calendar and have the front-end do that.
That said, in order to create results like what you're looking for, you're missing a set of data - the set of calendar days. You can do this with a temporary table, a CTE, or a permanent table in your database, but you basically need a table that gives you all of the days in question as a resultset. You can then LEFT OUTER JOIN from that table to your bookings table and use CASE to fill in values based on whether or not a matched bookings row was found.