I have a following query:
SELECT '-SELECT MONTH' NAME, -1 VALUE
UNION ALL SELECT 'JANUARY' NAME,1 VALUE
UNION ALL SELECT 'FEBRUARY' NAME,2 VALUE
UNION ALL SELECT 'MARCH' NAME,3 VALUE
UNION ALL SELECT 'APRIL' NAME,4 VALUE
UNION ALL SELECT 'MAY' NAME,5 VALUE
UNION ALL SELECT 'JUNE' NAME,6 VALUE
UNION ALL SELECT 'JULY' NAME,7 VALUE
UNION ALL SELECT 'AUGUST' NAME,8 VALUE
UNION ALL SELECT 'SEPTEMBER' NAME,9 VALUE
UNION ALL SELECT 'OCTOBER' NAME,10 VALUE
UNION ALL SELECT 'NOVEMBER' NAME,11 VALUE
UNION ALL SELECT 'DECEMBER' NAME,12 VALUE
which I bind to a dropdown.
I have another query
SELECT MONTH FROM HRMONTHYEARMASTER
WHERE LOCKINGSTATUS IS NULL OR LOCKINGSTATUS <> '05'
which gives me only particular months.
My problem is that i want to combine the two queries and get only the name of the month and its value in my resultset.
I dont know how to go about it. Please help..
For union you must have the same coulmn name :
SELECT '-SELECT MONTH' NAME, -1 VALUE
UNION ALL SELECT 'JANUARY' NAME,1 VALUE
UNION ALL SELECT 'FEBRUARY' NAME,2 VALUE
UNION ALL SELECT 'MARCH' NAME,3 VALUE
UNION ALL SELECT 'APRIL' NAME,4 VALUE
UNION ALL SELECT 'MAY' NAME,5 VALUE
UNION ALL SELECT 'JUNE' NAME,6 VALUE
UNION ALL SELECT 'JULY' NAME,7 VALUE
UNION ALL SELECT 'AUGUST' NAME,8 VALUE
UNION ALL SELECT 'SEPTEMBER' NAME,9 VALUE
UNION ALL SELECT 'OCTOBER' NAME,10 VALUE
UNION ALL SELECT 'NOVEMBER' NAME,11 VALUE
UNION ALL SELECT 'DECEMBER' NAME,12 VALUE
UNION ALL
SELECT MONTH AS NAME, 100 AS VALUE FROM HRMONTHYEARMASTER
WHERE LOCKINGSTATUS IS NULL OR LOCKINGSTATUS <> '05'
SELECT MONTH VALUE,DATENAME(month, DATEADD(month, MONTH-1, CAST('2008-01-01' AS datetime))) NAME
FROM HRMONTHYEARMASTER
WHERE LOCKINGSTATUS IS NULL OR LOCKINGSTATUS <> '05'
This solved my problem. Now I get the Name as well as the int value of a month.
Thanks Guys!!!!
Related
This question already has answers here:
The new PIVOT function in BigQuery
(1 answer)
GBQ Transpose Table in SQL
(1 answer)
Pivot table in SQL with multiple columns
(1 answer)
Closed 1 year ago.
Following this question, I was trying to create a dynamic SQL statement with a pivot table and a CTE in Google BigQuery.
This worked straight of the box (with CTE and pivot):
with Produce AS (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4')
SELECT * FROM
(SELECT product, sales, quarter FROM Produce)
PIVOT(SUM(sales) FOR quarter IN ('Q1', 'Q2', 'Q3', 'Q4'))
Then I tried to modify it to include the dynamic SQL using execute immediate:
execute immediate(
"""
with Produce as (
SELECT 'Kale' as product, 51 as sales, 'Q1' as quarter UNION ALL
SELECT 'Kale', 23, 'Q2' UNION ALL
SELECT 'Kale', 45, 'Q3' UNION ALL
SELECT 'Kale', 3, 'Q4' UNION ALL
SELECT 'Apple', 77, 'Q1' UNION ALL
SELECT 'Apple', 0, 'Q2' UNION ALL
SELECT 'Apple', 25, 'Q3' UNION ALL
SELECT 'Apple', 2, 'Q4'
)
select '''
select *
from (select product, sales, quarter from Produce)
pivot(sum(sales) for quarter in ("''' || string_agg(distinct quarter, '", "' order by quarter) || '''"))
'''
from Produce
"""
);
My issue is, this above prints out the query instead of executing it.
How can I modify it to actually get it to execute and get me the sample results using the data in the CTE?
Assume I have a few columns in a database: id, date_added, tag
`20134` | `February 07, 2019` | `New`
`40323` | `February 09, 2019` | `New`
I want to run a query with a filter based on date_added:
SELECT * FROM table
WHERE date_added > 'February 08, 2019'
How would I do this? I saw you can convert strings to date objects, but wasn't sure if that is possible inside a WHERE filter.
Any help is appreciated!
Edit: I am using SQLite
You chose a format for the date_added column, that is not comparable.
SQLite is not that flexible with dates, which are in fact Text.
So in this case you need to extract the date parts piece by piece and create a comparable date:
select *
from tablename
where
substr(date_added, instr(date_added, ',') + 2) ||
case substr(date_added, 1, instr(date_added, ' ') - 1)
when 'January' then '01'
when 'February' then '02'
when 'March' then '03'
when 'April' then '04'
when 'May' then '05'
when 'June' then '06'
when 'July' then '07'
when 'August' then '08'
when 'September' then '09'
when 'October' then '10'
when 'November' then '11'
when 'December' then '12'
end ||
substr(date_added, instr(date_added, ' ') + 1, 2) > '20190208'
See the demo
Look, I do not know what database engine you're working with, but in case it's an Sql server, you can do the following:
in the following query it is not necessary to cast the date_added column if it is of the date type, in case it is of the string type, you can also cast it cast (date_add as date).
SELECT * FROM table
WHERE date_added > cast('2019-01-01' as date)
NOTE: it is very important that if they are going to handle dates as string type, they must have a valid format, in Sql Server the dates are saved by default in yyyy-MM-dd -2019-02-10.
If you want to convert the date in another format you can use the convert function
So I am trying to convert some Day of the Year dates to Date format. The problem is some day of the year dates are in DDYYYY format for days under 100 and DDDYYYY format for days 100 and over. I have tried the following but still receive a "day of year must be between 1 and 365 (366 for leap year)" error:
select CASE when data_Date >= 999999
then to_date(data_date, 'DDDYYYY')
when data_Date >= 99999
then to_date(data_Date, 'DDYYYY')
else to_date(data_date, 'DYYYY')
END as DATA_DATE_CONVERTED
from table;
Thanks in advance
Sample Data is as follows:
Data_date (being passed in as a varchar2)
1072015
12017
612014
672013
72017
1112018
The last 4 digit is the year. Use LPAD to put leading zeroes on DAY
select to_date( lpad(dayyear, 7, '0'),'DDDYYYY')
from table;
sqlfiddle
How about this:
with demo (data_date) as
( select 1072015 from dual union all
select 12017 from dual union all
select 612014 from dual union all
select 672013 from dual union all
select 72017 from dual union all
select 1112018 from dual )
select data_date
, to_char(data_date,'0000000')
, to_date(to_char(data_date,'0000000'),'DDDYYYY') as data_date_converted
from demo
You can also put a dash (-) between.
with demo (data_date) as
( select 1072015 from dual union all
select 12017 from dual union all
select 612014 from dual union all
select 672013 from dual union all
select 72017 from dual union all
select 1112018 from dual )
select data_date, TO_DATE(SUBSTR(data_date, 1, length(data_date)-4)||'-'||SUBSTR(data_date, -4, 4), 'DDD-YYYY')
from demo;
i've got a DB with invoices and datetime that they were created, i want to have a New columm with the name of The month according to The date of each invoice. I mean if The date is 2013-01-15, i would like to have " january" on The New columm.
Thanks in advance, i've few knowledge about sql.
If your database is MySQL, try:
DATE_FORMAT(date, '%M')
For MS SQL Server use
SELECT DATENAME(MONTH,invoiceDate)
In Oracle:
TO_CHAR(date, 'Month')
Extract the month from the datetime object and then use it in a CASE statement.
Build upon this
select
case strftime('%m', date('now'))
when '01' then 'January'
when '02' then 'Febuary'
when '03' then 'March'
when '04' then 'April'
when '05' then 'May'
when '06' then 'June'
when '07' then 'July'
when '08' then 'August'
when '09' then 'September'
when '10' then 'October'
when '11' then 'November'
when '12' then 'December' else '' end
as month
I would suggest copying the schema of your table, adding another column for the month. Then using the following statement.
INSERT INTO TABLE newTable (col1, col2, col3, ..., colLast, colMonth)
SELECT col1, col2, col3, ..., colLast,
case strftime('%m', date('now'))
when '01' then 'January'
when '02' then 'Febuary'
when '03' then 'March'
when '04' then 'April'
when '05' then 'May'
when '06' then 'June'
when '07' then 'July'
when '08' then 'August'
when '09' then 'September'
when '10' then 'October'
when '11' then 'November'
when '12' then 'December' else '' end
as colMonth
FROM oldTable;
Then
drop table oldTable;
Then
Some alter to change the name of the new table to the name of the old table.
I have a view which contains the data seen in the image below.
The view is showing me how many working days are available in each month for the current financial year taking away any school/bank holidays.
As the month of August has zero days available it has excluded this month from the view.
As the total number of days available will always be zero for the month of August, then it seems acceptable to hardcode the SQL to always have 0 for August, and also an April-August record which will be the same as April-July.
What would be the best way to add these 2 records, and where about in the code should it be placed see example of code layout:
see link (answered question) for layout of code:
SQL populate total working days per month minus bank holidays for current financial year
For my answer, I will assume you have a view vDays with columns that match your screen shot: period, availabledays, year.
To append any zero-day periods to your results whatever month may have zero (which will cater for August and any other month that happens to have zero days), you can extend your view like this:
WITH Mths (Mth) AS (
SELECT 'January'
UNION SELECT 'February'
UNION SELECT 'March'
UNION SELECT 'April'
UNION SELECT 'May'
UNION SELECT 'June'
UNION SELECT 'July'
UNION SELECT 'August'
UNION SELECT 'September'
UNION SELECT 'October'
UNION SELECT 'November'
UNION SELECT 'December'
UNION SELECT 'April - January'
UNION SELECT 'April - February'
UNION SELECT 'April - March'
UNION SELECT 'April - May'
UNION SELECT 'April - June'
UNION SELECT 'April - July'
UNION SELECT 'April - August'
UNION SELECT 'April - September'
UNION SELECT 'April - October'
UNION SELECT 'April - November'
UNION SELECT 'April - December'
), Years (Year) AS (
SELECT DISTINCT year
FROM vDays
), ZeroPeriods (Mth, Years) AS (
SELECT Mth, Year
FROM Mths, Years
), JoinedData (Mth, AvailableDays, Year) AS (
SELECT Mth, 0, Years
FROM ZeroPeriods
UNION ALL
SELECT period, availabledays, year
FROM vDays
), GroupedData (Mth, AvailableDays, Year) AS (
SELECT Mth, SUM(AvailableDays), Year
FROM JoinedData
GROUP BY Mth, Year
)
SELECT *
FROM GroupedData
ORDER BY Year, CASE UPPER(LEFT(Mth, 3))
WHEN 'JAN' THEN 1 WHEN 'FEB' THEN 2 WHEN 'MAR' THEN 3
WHEN 'APR' THEN 4 WHEN 'MAY' THEN 5 WHEN 'JUN' THEN 6
WHEN 'JUL' THEN 7 WHEN 'AUG' THEN 8 WHEN 'SEP' THEN 9
WHEN 'OCT' THEN 10 WHEN 'NOV' THEN 11 ELSE 12 END;
I have split this out into lots of separate queries, although some could be merged into sub queries, but doing it like this makes it a lot clearer to understand.
Would this give you your desired result set?
SELECT Period, DaysAvailable, Year FROM YOURVIEW
UNION ALL
SELECT DISTINCT 'April-August', DaysAvailable, Year FROM YOURVIEW where Period = 'April-July'
UNION ALL
SELECT DISTINCT 'August', 0, YEAR FROM YOURVIEW