SQL store results table with month name - sql

I have several CSV's stored to query against. Each CSV represents a month of data. I would like to count all the records in each CSV and save that data to a table as a row in the table. For instance, the table that represents May should return something that looks like this with June following. The data starts in Feb 2018 and continues to Feb 2019 so year value would be needed as well.
Month Results
----------------
May 18 1170
June 18 1167
I want to run the same query against all the tables for purposes of efficiency. I also want the query to work with all future updates eg. a March 19 table gets added, and the query will still work.
So far, I have this query.
SELECT COUNT(*)
FROM `months_data.*`
I am querying in Google Big Query using Standard SQL.

It sounds like you just want an aggregation that counts rows for each month:
SELECT
DATE_TRUNC(DATE(timestamp), MONTH) AS Month,
COUNT(*) AS Results
FROM `dataset.*`
GROUP BY month
ORDER BY month
You can use the DATE_FORMAT function if you want to control the formatting.

You seem to need union all:
select 2018 as yyyy, 2 as mm, count(*) as num
from feb2018
union all
select 2018 as yyyy, 3 as mm, count(*)
from mar2018
union all
. . .
Note that you have a poor data model. You should be storing all the data in a single table with a date column.

Related

Get multiple columns for different WHERE clause on the same column in SQL

I have a table and column containing date (say in string). I need to get two columns such as countin(2020) and countin(2019) from the same date column such that date is like '2020%' or '2019%' and I want them as separate columns.
My query is kind of like this.
select C.pin_code, count(distinct(C.customer_code)) as 2020
from table
group by C.pin_code
My output is this
For me there should be another column beside 2020 called 2019 which give same data as 2020 in year 2019.
If I have under emphasized something, please let me know in the comments.
Select
Count(Year(year_col)) as year,
Pin_code
From T
Group_by pin_code
Order by pin_code desc;
You can use pivot in case you need those values in year column
For me there should be another column beside 2020 called 2019 which give same data as 2020 in year 2019.
If your data has a date in it, then I would expect a query like this:
select C.pin_code,
count(distinct case when year(C.date) = 2020 then C.customer_code end) as cnt_2020,
count(distinct case when year(C.date) = 2019 then C.customer_code end) as cnt_2019
from C
group by C.pin_code;
Date/time functions are notoriously database dependent. But year() is pretty common and all databases have this functionality somehow.

How do I average the last 6 months of sales within SQL based on period AND year?

How do I average the last 6 months of sales within SQL?
Here are my tables and fields:
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR,
And I need to average these fields
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
The hard part I'm having is understanding how to average the last whole 6 months, ie. fsicalcalperiod 2-6(inside fiscalcalyear 2017).
I'm hoping for some help on what the SQL command text should look like since I'm very new to manipulating SQL outside of the UI.
Sample Data
My Existing SQL String:
SELECT IM_ItemWhseHistoryByPeriod.ITEMCODE,
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR
FROM MAS_AME.dbo.IM_ItemWhseHistoryByPeriod
IM_ItemWhseHistoryByPeriod
ScaisEdge Attempt #1
if fiscalyear and fiscalperiod are number you could use
select avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
or for each item code
select itemcode, avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
group by itemcode
Try the following solution and see if it works for you:
select avg(DOLLARSSOLD) as AvgDollarSod,
avg(QUANTITYSOLD) as AvgQtySold
from IM_ItemWhseHistoryByPeriod
where FISCALCALYEAR = '2017
and FISCALCALPERIOD between 2 and 6

Order an SQL table by year and ranges of years

I would like to be able to order the results in the following way.
There are two columns, one stores years and the other year ranges, and, sometimes, dates, like this:
2017
2016
2014–2016
1980-ongoing
2013
2000 28-27 March
1970
At the moment, I concatenate them and order by DESC, Getting this (showing the concatenated temporary column):
order by CONCAT(IFNULL(CAST(Year_Pub AS VARCHAR(16)) THEN '' ELSE CAST(Year_Pub AS VARCHAR(16))), IFNULL(Date_Freeform THEN '' ELSE Date_Freeform)) DESC
The result is:
2017
2016
2014-2016
2013
200028-27 March
1980-ongoing
1970
However, what I would like to get is this (imagine that this is a list of activities for a CV or similar):
1980-ongoing
2017
2016
2014-2016
2013
2000
1970
That is if there is a span of years, I would like to have the ongoing engagements to appear first, ordered by the start year, then have spans of years ordered by last year and mixed with single years. Dates only occur when Year_Pub is NULL and will have to be removed before concatenation, I imagine.
The separator is an ndash, so I need to split those strings by that somehow as I see from examples that show how to order by the family names in tables that have first name and family name in one column but this is a more complicated situation and I am not really familiar with SQL.
Also, this operation will be performed on a table that comes with an application so I do not want to insert data or columns into their database in case something gets broken.
Using SQL on an ElevateDB database (SQL 2003 standard (ANSI ISO/IEC 9075:2003), but a generic solution will do, I can look up the syntax).
Thank you for the advice.
This is one way to achieve your result:
SELECT
year_pub,
date_freeform,
COALESCE(CAST(year_pub AS VARCHAR(4)), date_freeform) AS year_list
FROM
table_name
ORDER BY
COALESCE(CAST(year_pub AS VARCHAR(4)),
CASE WHEN RIGHT(date_freeform, 4) = 'oing'
THEN '9999'
ELSE RIGHT(date_freeform, 4)
END
) DESC,
date_freeform DESC;

SQL: Can GROUP BY contain an expression as a field?

I want to group a set of dated records by year, when the date is to the day. Something like:
SELECT venue, YEAR(date) AS yr, SUM(guests) AS yr_guests
FROM Events
...
GROUP BY venue, YEAR(date);
The above is giving me results instead of an error, but the results are not grouping by year and venue; they do not appear to be grouping at all.
My brute force solution would be a nested subquery: add the YEAR() AS yr as an extra column in the subquery, then do the grouping on yr in the outer query. I'm just trying to learn to do as much as possible without nesting, because nesting usually seems horribly inefficient.
I would tell you the exact SQL implementation I'm using, but I've had trouble discovering it. (I'm working through the problems on http://www.sql-ex.ru/ and if you can tell what they're using, I'd love to know.) Edited to add: Per test in comments, it is probably not SQL Server.
Edited to add the results I am getting (note the first two should be summed):
venue | yr | yr_guests
1 2012 15
1 2012 35
2 2012 12
1 2008 15
I expect those first two lines to instead be summed as
1 2012 50
Works Fine in SQL Server 2008.
See working Example here: http://sqlfiddle.com/#!3/3b0f9/6
Code pasted Below.
Create The Events Table
CREATE TABLE [Events]
( Venue INT NOT NULL,
[Date] DATETIME NOT NULL,
Guests INT NOT NULL
)
Insert the Rows.
INSERT INTO [Events] VALUES
(1,convert(datetime,'2012'),15),
(1,convert(datetime,'2012'),35),
(2,convert(datetime,'2012'),12),
(1,convert(datetime,'2008'),15);
GO
-- Testing, select newly inserted rows.
--SELECT * FROM [Events]
--GO
Run the GROUP BY Sql.
SELECT Venue, YEAR(date) AS yr, SUM(guests) AS yr_guests
FROM Events
GROUP BY venue, YEAR(date);
See the Output Results.
VENUE YR YR_GUESTS
1 2008 15
1 2012 50
2 2012 12
it depends of your database engine (or SQL)
to be sure (over different DB Systems & Versions), make a subquery
SELECT venue, theyear, SUM(guests) from (
SELECT venue, YEAR(date) AS theyear, guest
FROM Events
)
GROUP BY theyear
you make a subtable of
venue, date as theyear, guest
aaaa, 2001, brother
aaaa, 2001, bbrother
bbbb, 2001, nobody
... and so on
and then
count them

Storing/Quering multiple value in SQLite

I'm trying to store a blooming season in month for each tree in SQLite3. Currently I had the field "month" then I store the month name in the field. For example
Tree Name Month
Tree1 Jan,Feb,Mar
Tree2 Nov,Dec,Jan
Tree3 Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
Tree4 Mar,Apr,Nov,Dec
Tree5 Jan,Feb,Mar,Apr
I'm not sure if this is the best way to store it, Any recommendation is appreciate.
Secondly, I need to perform a query where I enter in the month and it should return me the tree name that match the search criteria. For example
If I search for
"Jan"
the result should be Tree1,Tree2,Tree3,Tree5
"Jan,Feb,Mar"
the result should be Tree1,Tree3,Tree5
"Jan,Feb,Mar,Apr"
the result should be Tree5
"Sep,Oct,Nov,Dec"
the result should be none
Which SQL query do I have to use in order to obtain the above result?
Thanks
No. This is not the best way to store it.
You should normalise your data and store one month/tree per row
Tree1 Jan
Tree1 Feb
Tree1 Mar
Tree2 Nov
Tree2 Dec
Tree2 Jan
....
(or perhaps store the months as numbers 1-12)
then you can
select treename from TreeBlooming where month = 'jan'
and
select treename from TreeBlooming
where month in ('jan','feb')
group by treename
having count(distinct month)=2
You can use Group_concat in following way:
SELECT Group_concat(name) AS [tree name]
FROM tree
WHERE month LIKE '%Jan,Feb,Mar%'
Check out SQLFIDDLE