SQL TSQL Query to get the count of equipment on weekly basis - sql

ID | Equipment | HireDate | HireTodate | ActualOffhireDate
---------------------------------------------------------------------
01 | Printer | 01/01/2013 | 31/12/2016 |
02 | Printer | 01/05/2015 | 31/12/2016 |
03 | Laptop | 17/01/2016 | 31/12/2016 |
04 | Laptop | 01/01/2015 | 31/12/2016 | 28/01/2016
I have like the above table and would like to get the count based on weekly (from friday to thurday) for month of january 2016 as per the below
Equipment | January count | Week 1| Week 2| Week 3| Week 4
------------------------------------------------------------------
Printer | 02 | 02 | 02 | 02 | 02
Laptop | 02 | 01 | 01 | 02 | 01

You need to have a calendar table which helps in processing this kind of queries very fast and with minimal effort.
Here is one way which i used to generate calendar table..and it looks like below in my environment...
Once you have calendar table,all you have to do is join date which you want to count which is as simple as below
select
equipment,
count(*) as 'Jancount',
sum(case when wkno =1 then 1 else 0 end) 'Week 1',
sum(case when wkno =2 then 1 else 0 end) 'Week 2',
sum(case when wkno =3 then 1 else 0 end) 'Week 3',
sum(case when wkno =4 then 1 else 0 end) 'Week 4'
from
calendar c
join testtable p
on p.hiretodate=c.date
group by equipment
Output:

Related

Sum case from previous month

I couldn't find the answer to this on here or on google.
This is part of the main table
+---+-------+----------------+--------------+
| | Acct | Last_trans_date|Last_transpay |
+---+-------+----------------+--------------+
| 1 | ABC | July 31 | Nov 5 |
| 2 | DEF | Mar 1 | Aug 8 |
| 3 | GFH | Mar 9 | Feb 7 |
+---+------+-----------------+--------------+
I want the total account for the previous month that includes last_trans_date and Last_transpay = previous month as count.
I used this
Select
year(open)
sum(case when month(last_trans_date) = month(current date - 1) and month(last_transpay) = month(current_date - 1) then 1 else 0 end) as activity
from table
group by 1.
I don't think it's outputting the correct amount
SELECT Count(*)
FROM [table]
WHERE
CHARINDEX(#PrevMonth, Last_trans_date) = 1
AND CHARINDEX(#PrevMonth, Last_transpay) = 1

SQL Query to Display Daily Count Results in Columns from 1st to last day of month

Need a Query to Display daily count of each item bought by customers in columns from 1st day of month to last day
Sample data table "Item"
+--------+--------+----------+---------------+
| Purchase Date | Item Code| Item Name| Price|
|--------+--------+----------+--------------+
| 01-JAN-20 | 11 | Apple | 1 |
| 01-JAN-20 | 11 | Apple | 1 |
| 02-JAN-20 | 12 | Orange | 2 |
| 02-JAN-20 | 11 | Apple | 1 |
| 03-JAN-20 | 12 | Orange | 2 |
| 03-JAN-20 | 12 | Orange | 2 |
| 04-JAN-20 | 12 | Orange | 2 |
| 04-JAN-20 | 11 | Apple | 1 |
+--------+--------+----------+--------------+
SQL Query should Display Daily Count using Item code and Result to be displayed as below table .
Count daily with each day displayed in column base on the day e.g If today is 4th of Jan then count tomorrow will create new column with count result and continues until last day of month or something similar.
+--------+--------+----------+---------------+
| Items | Jan 01| Jan 02| Jan 03|Jan 04| etc
+--------+--------+----------+--------------+
| Apple | 2 | 1 | 2 | 1 |
| Orange | 0 | 1 | 0 | 1 |
+--------+--------+----------+--------------+
If you know what dates you want, you can use conditional aggregation:
select item,
sum(case when purchase_date = '2020-01-01' then 1 else 0 end) as jan_1,
sum(case when purchase_date = '2020-01-02' then 1 else 0 end) as jan_2,
sum(case when purchase_date = '2020-01-03' then 1 else 0 end) as jan_3,
sum(case when purchase_date = '2020-01-04' then 1 else 0 end) as jan_4,
. . .
from items
group by item;
Note that this assumes that purchase_date is really stored as an internal date format. So the comparison is a date constant -- however, that might differ among databases.
If you do not have a specific set of dates in mind, then you will need to use dynamic SQL.

Sql Query issue and error regarding groupby cause

I am trying to calculate the total number of Projects in every year. and also how many projects are active, how many of them are canceled.
I tried to group by cause for PRojects dates so we have a total number of project in every year but I am not sure where to start and what to do
Select ts.Id as projectid ,
--a.ParentObjectId,
ts.RequestName as ProjectDates,
ts.Type,
ts.Category,
ts.SubType,
ts.status as projectstatus,
Count (ts.ReceptionDate),
cast (ts.ReceptionDate as Date) as ReceptionDate,
from [rpt].[TransmissionServicesRpt] ts
left join [dbo].[AuditHistory] a on a.ParentObjectId = ts.Id
Left join [dbo].[User] u on a.CreatedById = u.id
Group by ts.id, ts.ReceptionDate
+ -------------+--------+-----------+------------+----------+-----------------+
| New Projects | Active | Cancelled | Terminated | Inactive | Carried Forward |
+ -------------+--------+-----------+------------+----------+-----------------+
| 2013 | 32 | 45 | 4 | 11 | 30 |
| 2014 | 45 | 75 | 17 | 14 | 44 |
| 2015 | 46 | 90 | 25 | 21 | 44 |
| 2016 | 30 | 74 | 27 | 10 | 37 |
| 2017 | 82 | 119 | 11 | 26 | 82 |
| 2018 | 86 | 168 | 29 | 24 | 115 |
| 2019 | 23 | 138 | 9 | 4 | 125 |
+ -------------+--------+-----------+------------+----------+-----------------+
You want one result row per year. So group by year. You get it via YEAR or DATEPART. Then count conditionally:
select
year(receptiondate) as year,
count(*) as total,
count(case when status = 'Active' then 1 end) as active,
count(case when status = 'Cancelled' then 1 end) as cancelled,
count(case when status = 'Terminated' then 1 end) as terminated,
count(case when status = 'Inactive' then 1 end) as inactive,
count(case when status = 'Carried Forward' then 1 end) as carried_forward
from rpt.transmissionservicesrpt
group by year(receptiondate)
order by year(receptiondate);

Reorganize Postgrest Query

Im using a query on postgresql that return a result like this.
schedule | day | subject | grade |
===============================================
06 - 08 | Monday | Biology | Second |
-----------------------------------------------
08 - 10 | Monday | Chemistry | Third |
-----------------------------------------------
06 - 08 | Tuesday | Math | Second |
-----------------------------------------------
10 - 12 | Wednesday | English | Second |
-----------------------------------------------
10 - 12 | Friday | Language | Second |
-----------------------------------------------
I need a result or reorganize this query like this.
schedule | Monday | Tuesday| Wednesday | Friday | grade
==============================================================
06 - 08 | Biology | Math | null | null | second
--------------------------------------------------------------
10 - 12 | null | null | English |Language | second
--------------------------------------------------------------
I appreciate any help or suggestion.
Thnks.
One method is conditional aggregation:
select schedule,
max(case when day = 'Monday' then subject end) as Monday,
max(case when day = 'Tuesday' then subject end) as Tuesday,
max(case when day = 'Wednesday' then subject end) as Wednesday,
max(case when day = 'Friday' then subject end) as Friday,
min(grade) as grade,
from t
group by schedule;
I'm not really sure where the grade comes from; the above is just a guess.

postgresql : extract(epoch function over multiple rows

I am using postgresql 8.3 (no choice in the version at this time)
My raw data is as follows:
ID | From | To | Time
01 | n/a | open | 06:56
01 | open | pt1 | 07:56
01 | pt1 | pt2 | 07:59
01 | pt2 | pt3 | 08:36
01 | pt3 | pt4 | 08:56
01 | pt4 | close | 09:58
What I want to end up with is:
ID | Open_Time | Close_Time
01 | 06:56 | 09:58
I don't care about the time intervals between the individual parts. I have many ID numbers and each can have this or more are part intervals to it. I'm fairly new to sql so I am pretty lost here. I'm stuck on how to merge the two end and beginning rows into one row in a new view.
select
id ID,
min(case rd.to when 'open' then "time" end) Open_Time,
min(case rd.to when 'close' then "time" end) Close_Time
from raw_data rd
group by id