Reorganize Postgrest Query - sql

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.

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.

How do I perform this kind of month-over-month comparison in SQL Server?

Say I have data in a table like this:
| DateTimePurchased | Amount |
|----------------------|--------|
| 1/1/2017 3:23:15 PM | 657 |
| 1/1/2017 3:38:29 PM | 730 |
And I want to run a query that outputs like this:
| DayOfMonth | Feb 2017 | Mar 2017 |
|------------|----------|----------|
| 1 | 2344 | 4342 |
| 2 | 3435 | 4564 |
| 3 | 5675 | 6787 |
etc...
How would I write the query for SQL Server?
Oh, that is what you want. You just do:
select day(DateTimePurchased) as dy,
sum(case when DateTimePurchased >= '2017-02-01' and DateTimePurchased < '2017-03-01'
then amount
end) as amount_201702,
sum(case when DateTimePurchased >= '2017-03-01' and DateTimePurchased < '2017-04-01'
then amount
end) as amount_201703
from t
group by day(DateTimePurchased)
order by dy;

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

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:

SQLite: Multiple aggregate columns

I'm a little new to SQL world and still learning the ins and outs of the language.
I have a table with an id, dayOfWeek, and a count for each day. So any given id might appear in the table up to seven times, with a count of events for each day for each id. I'd like to restructure the table to have a single row for each id with a column for each day of the week, something like the following obviously incorrect query:
SELECT id, sum(numEvents where dayOfWeek = 0), sum(numEvents where dayOfWeek = 1) ... from t;
Is there a solid way to approach this?
EDIT:
I'm worried I may not have been very clear. The table would ideally be structured something like this:
id | Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday
0 | 13 | 45 | 142 | 3 | 36 | 63 | 15336
1 | 17 | 25 | 45 | 364 | 37 | 540 | 0
So event 0 occurred 13 times on Sunday, 45 on Monday, etc... My current table looks like this:
id | dayOfWeek | count
0 | 0 | 13
0 | 1 | 45
0 | 2 | 142
0 | 3 | 3
0 | 4 | 36
0 | 5 | 63
0 | 6 | 15336
1 | 0 | 17
1 | 1 | 25
...
Hope that helps clear up what I'm after.
The following is verbose, but should work (generic ideone sql demo unfortunately SqlLite on SqlFiddle is down at the moment):
SELECT id,
SUM(case when dayofweek = 1 then numevents else 0 end) as Day1Events,
SUM(case when dayofweek = 2 then numevents else 0 end) as Day2Events,
SUM(case when dayofweek = 3 then numevents else 0 end) as Day3Events
--, etc...
FROM EventTable
GROUP BY ID;
SELECT dayOfWeek, sum(numEvents) as numberOfEvents
FROM t
GROUP BY dayOfWeek;