How to Add one row to another - sql

I have a set of data which is shown in picture:
I've tried adding January to February using the lead function which was correct shown in the picture below again:
And I thought it will add and add to the next row like a for loop but it did not.
I am asking for help on how to add the data again and again like a for loop would do. I've pasted the code I used below.
;WITH MonthActiveUsers AS (
SELECT MonthLogin, COUNT(MonthLogin) AS ActiveUsers
FROM #TEMPDistinctActiveUsersPerMonth DAUPM
GROUP BY MonthLogin
)
SELECT MAU.MonthLogin, (LEAD(MAU.ActiveUsers,0) OVER (ORDER BY MAU.MonthLogin ASC) + LEAD(MAU.ActiveUsers,1) OVER (ORDER BY MAU.MonthLogin ASC)) AS [Count of Active Users]
FROM
( SELECT CASE
WHEN MonthLogin = 'January'
THEN '2020-01-01'
WHEN MonthLogin = 'February'
THEN '2020-02-01'
WHEN MonthLogin = 'March'
THEN '2020-03-01'
WHEN MonthLogin = 'April'
THEN '2020-04-01'
WHEN MonthLogin = 'May'
THEN '2020-05-01'
WHEN MonthLogin = 'June'
THEN '2020-06-01'
ELSE NULL
END AS [MonthLogin]
,ActiveUsers
FROM MonthActiveUsers MAU
) MAU
The desired result would be:
January = 3313
February = 3349
March = 3398
April = 3421
May = 3437
June = 3444

I think that you want a window sum:
WITH ...
SELECT
MAU.MonthLogin,
SUM(MAU.ActiveUsers) OVER (ORDER BY MAU.MonthLogin) [Count of Active Users]
FROM ...
Note that the query can be simplified a little by using the short-circuit form of CASE, and mixing aggregation and window functions:
SELECT
MonthLogin,
COUNT(*) ActiveUsers,
SUM(COUNT(*)) OVER(ORDER BY MonthLogin) [Count of Active Users]
FROM (
SELECT
CASE MonthLogin
WHEN'January' THEN '2020-01-01'
WHEN'February' THEN '2020-02-01'
WHEN'March' THEN '2020-03-01'
WHEN'April' THEN '2020-04-01'
WHEN'May' THEN '2020-05-01'
WHEN'June' THEN '2020-06-01'
END AS MonthLogin
FROM #TEMPDistinctActiveUsersPerMonth
) t
GROUP BY MonthLogin
ORDER BY MonthLogin

Related

Count sequence selected columns

I have query below, I want sequence result like the value of 'feb' will sum by jan and feb, value of 'mar' will sum by jan, feb and mar,... . Is there any way to get the result like that?
select A.location as location
, count(Case When SUBSTRING(A.base_date,5,2)='01' Then A.customer_no else null end) as "jan"
, count(Case When SUBSTRING(A.base_date,5,2)='02' Then A.customer_no else null end) as "feb"
....
, count(Case When SUBSTRING(A.base_date,5,2)='12' Then A.customer_no else null end) as "dec"
from table_income A group by A.location;
SQL is a much more effective language when you think in rows rather than columns (normalisation).
For example, having one row per month is much simpler...
SELECT
location,
SUBSTRING(base_date,5,2) AS base_month,
SUM(COUNT(customer_no))
OVER (
PARTITION BY location
ORDER BY SUBSTRING(base_date,5,2)
)
AS count_cust
FROM
table_income
GROUP BY
location,
SUBSTRING(base_date,5,2)
Side notes:
If your base_date is a string, it shouldn't be, use data-types relevant to the data
If your base_date is a date or timestamp, you should really use date/timestamp functions, such as EXTRACT(month FROM base_date).
You probably should also account for different years...
SELECT
location,
DATE_TRUNC('month', base_date) AS base_month,
SUM(COUNT(customer_no))
OVER (
PARTITION BY location, DATE_TRUNC('year', base_date)
ORDER BY DATE_TRUNC('month', base_date)
)
AS count_cust
FROM
table_income
GROUP BY
location,
DATE_TRUNC('month', base_date)
Try this :
SELECT A.location as location
, count(Case When SUBSTRING(A.base_date,5,2) in ('01') Then A.customer_no else null end) as "jan"
, count(Case When SUBSTRING(A.base_date,5,2) in ('01','02') Then A.customer_no else null end) as "feb"
....
, count(Case When SUBSTRING(A.base_date,5,2) in ('01','02',...'12') Then A.customer_no else null end) as "dec"
from table_income A group by A.location;

SQL concat few select into one big table

I have table with data:
YYYY/MM/DD HH:MM:SS - Number of views, number of clicks, etc..
YYYY/MM/DD HH:MM:SS - ...
I want sum all those columns into sum value.
Like:
1. YYYY/MM/DD - sum of views, sum of clicks
What I have done:
1. Query foreach column.
For views;
select
cast([EventTime] as date) as 'Date',
Count([Id]) as 'Views'
from [TelemetryData]
where [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5'
and [EventName] = 'DiscountView'
group by cast([EventTime] as date)
order by cast([EventTime] as date) asc
Number of clicks per day:
select
cast([EventTime] as date) as 'Date',
Count([Id]) as 'Clicks'
from [TelemetryData]
where [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5'
and [EventName] = 'DiscountClick'
group by cast([EventTime] as date)
order by cast([EventTime] as date) asc
How I can sum all of them into one row per day?
Try it with checks and sums:
SELECT cast([EventTime] as date) as 'Date', SUM(case when [EventName]='DiscountClick' then 1 else 0 end) as 'Clicks', SUM(case when [EventName]='DiscountView' then 1 else 0 end) as 'Views'
from [TelemetryData]
where [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5'
and ([EventName] = 'DiscountView' or [EventName] = 'DiscountClick')
group by cast([EventTime] as date)
order by cast([EventTime] as date) asc
What has been changed:
We first were maded where column to filter both Views and Clicks.
Then we used SUM but with CASE when EVENT=SOMETHING we are adding 1, otherwise 0.
With your group, you will get both by days at one query.
use SUM with CASE..
sum(case when [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5'
and [EventName] = 'DiscountClick' then noofclicks column else 0 end
) as clicks
sum(case when [DiscountId] = '8773fd1b-f0c0-41fd-b0a0-ab8238f227f5'
and [EventName] = 'DiscountView' then view column else 0 end
) as views
and so on for other ....
from
table
group by cast([EventTime] as date)
order by cast([EventTime] as date)

SQL: Group SUM values by date ranges and show in multiple columns by date

I have a SQL table showing charge amounts per person and an associated date. I'm looking to create a printout of each person's charges per month. I have the following code which will show me everyone's data for ONE month but I'd like to put this in one report without having to rerun this for every month's date range. Is there a way to pull this data all at once? Essentially I'd like the columns to show:
Last Name January February March Etc
name amt amt amt
Here is my code to pull this data for April as an example. You'll see the dates are codes as YYYYMMDD. This code works perfectly for one month at a time.
select pm.last_name, SUM(amt) as Total_Charge from charges c
inner join provider_mstr pm ON c.rendering_id = pm.provider_id
where begin_date_of_service >= '20150401' and begin_date_of_service <= '20150431'
group by pm.last_name
A generic solution uses SUM over CASEs:
select pm.last_name,
SUM(case when begin_date_of_service >= '20150101' and begin_date_of_service < '20150201' then amt else 0 end) as Total_Charge_Jan,
SUM(case when begin_date_of_service >= '20150201' and begin_date_of_service < '20150301' then amt else 0 end) as Total_Charge_Feb,
SUM(case when begin_date_of_service >= '20150301' and begin_date_of_service < '20150401' then amt else 0 end) as Total_Charge_Mar,
...
from charges c
inner join provider_mstr pm ON c.rendering_id = pm.provider_id
where begin_date_of_service >= '20150101' and begin_date_of_service < '20160101'
group by pm.last_name
Depending on your DBMS you might have a PIVOT function or similar...
Change SUM(amt) as Total_Charge to the following for each period
SUM(CASE WHEN begin_date_of_service BETWEEN '20150401' AND '20150430' THEN amt ELSE 0 END) AS April_Amt
SUM(CASE WHEN begin_date_of_service BETWEEN '20150301' AND '20150331' THEN amt ELSE 0 END) AS March_Amt
And update WHERE clause to include all date ranges for pull.
A condition will evaluate to 1 or 0 in a product, so you could use the following approach to do a selective sum:
select
pm.last_name,
SUM(amt * (begin_date_of_service >= '20150401' and begin_date_of_service <= '20150431')) AS 'april',
SUM(amt * (begin_date_of_service >= '20150501' and begin_date_of_service <= '20150530')) AS 'may'
...
from charges c
inner join provider_mstr pm ON c.rendering_id = pm.provider_id
group by pm.last_name

How to group data by different date period in sql?

How to group data by different date period in sql?
For example, I want the data to be grouped from 1/2/2015 to 6/2/2015, 7/2/2015 to 12/2/2015 etc. So far I could only group them according to 1 date range by using the WHERE condition.
SELECT type, count(*)
from table1
WHERE Day(datefield) <=6
Group by type
table1:
type, datefield
typeA, '2015-2-1'
typeB, '2015-2-2'
typeB, '2015-2-9'
typeA, '2015-2-18'
typeB, '2015-2-28'
desired result:
type, no. for day 1-6, no. for day 7-12, no. for day 13-18, no. for day 19-24, no. for day 25-31
type A, 1, 0, 1, 0, 0
type B, 1, 1, 0, 0, 1
First create a derived table where each datefield is replaced by a label ('1-6','7-12',etc.).
Then group that table by its label and type and get the counts. Finally pivot the labels into columns
using conditional aggregation (max(case when ...).
select type,
max(case when date_label = '1-6' then date_count end) '1-6',
max(case when date_label = '7-12' then date_count end) '7-12',
max(case when date_label = '13-18' then date_count end) '13-18',
max(case when date_label = '19-24' then date_count end) '19-24',
max(case when date_label = '25-31' then date_count end) '25-31',
from (
select
type,
date_label,
count(*) date_count
from
(select type,
case when day(datefield) <=6 then '1-6'
when day(datefield) <= 12 then '7-12'
when day(datefield) <= 18 then '13-18'
when day(datefield) <= 24 then '19-24'
when day(datefield) <= 31 then '25-31'
else 'n/a' end as date_label
from table1) t1
group by type, date_label
) t1 group by type

Using Case statement in sqlite, trouble getting the results I'm looking for

I am having difficulty with a SQL query. I'm using sqlite.
I have a table expenses. I'm trying to use a CASEstatement to select and sum values. Here's some sample data:
This is the query which is not getting what I'd like...
SELECT e.category,
CASE
WHEN e.schedule ='None' AND e.created_at > '2014-02-01 00:00:00 +0000' THEN SUM(e.amount)
WHEN e.schedule = 'Daily' THEN SUM(e.amount)*28.000000
WHEN e.schedule = 'Mon - Fri' THEN SUM(e.amount)*20
WHEN e.schedule = 'Weekly' THEN SUM(e.amount)*4.000000
WHEN e.schedule = 'Every 4 Weeks' THEN SUM(e.amount)*1.083333333333
WHEN e.schedule = 'Monthly' THEN SUM(e.amount)*1
ELSE 0
END
AS totalValue FROM expenses e GROUP BY e.category ORDER BY e.category
Below is a pic of what the query returns.
What I would like to see in total value for Housing 420, this is because there is are three records with the category "Housing", one of which has a schedule of "None" (record id 2) and was created in January. The other two (records 10 and 16) have a schedule of Monthly. In my CASE I try to only select the schedules with a created_at greater than the 1st of Feb:
WHEN e.schedule ='None' AND e.created_at > '2014-02-01 00:00:00 +0000' THEN SUM(e.amount)
Or with a schedule value other than "None", in this case:
WHEN e.schedule = 'Monthly' THEN SUM(e.amount)
Unfortunately I'm getting a result of 495 (300 + 120 + 75). It's summing all the records with a particular category if it finds a record that fits the case statement!
Please can you help me rewrite the case so that I get the results I'm looking for.
One problem with the query is that you are using e.schedule in the select list but it is not in a group by. This is usually the sign of a problem.
I think you want to move the entire case statement inside the sum():
SELECT e.category,
SUM(CASE WHEN e.schedule ='None' AND e.created_at > '2014-02-01 00:00:00 +0000' THEN e.amount
WHEN e.schedule = 'Daily' THEN e.amount*28.000000
WHEN e.schedule = 'Mon - Fri' THEN e.amount*20
WHEN e.schedule = 'Weekly' THEN e.amount*4.000000
WHEN e.schedule = 'Every 4 Weeks' THEN e.amount*1.083333333333
WHEN e.schedule = 'Monthly' THEN e.amount*1
ELSE 0
END) as totalValue
FROM expenses e
GROUP BY e.category
ORDER BY e.category;
Otherwise, you might want to move e.schedule to the group by clause to have a separate row for each schedule within each category.