Show data that is only from a specific quarter - sql

I need to show the data that is only from the 2nd quarter of any year. I am trying to use to_date in the where clause but it is giving me an error. Not sure what I am doing wrong. (Using Oracle 10g)
Code:
SELECT product_name, name
FROM a_product p JOIN a_item i
ON p.product_id=i.product_id
JOIN a_sales_order so ON i.order_id=so.order_id
JOIN a_customer c ON so.customer_id=c.customer_id
WHERE regexp_like(product_name, 'Transducer') AND order_date=to_date(2, 'Q')
The AND portion is where I am having trouble, it is giving me a "format code cannot appear in date input format" order_date is the field within the table that I use to find out what sales were done in quarter 2 of any year.

You have the arguments backwards. You want that to be:
AND to_char(order_date, 'Q') = '2'
The function to_char() (in this context) takes a date and formats it as a string. What string? A string with the quarter in it. You then want to compare it to the value you care about.

Related

im trying to use & in a where condition, but this condition is a date, i want to ask for the month and i dont know how to do that

select cod_prestamo, cod_ejemplar, cod_libro,
trunc (extract (month from fec_prestamo)/3) as "trimestre"
**where fec_prestamo &????**
from prestamo;
This is the code, I don't know how to ask for the month.
I tried with this but it obviously doesn´t work:
select cod_prestamo, cod_ejemplar, cod_libro,
trunc (extract (month from fec_prestamo)/3) as "trimestre"
where fec_prestamo like '/&month/ '
from prestamo;
I need to answer with a month and then Oracle show the books that are registered in that month.
Assuming that fec_prestamo is a DATE data type and that you are trying to use a substitution variable to pass in a month name and want to return three columns plus which quarter contains fec_prestamo then you can use:
SELECT cod_prestamo,
cod_ejemplar,
cod_libro,
TO_CHAR(fec_prestamo, 'Q') as "trimestre"
FROM prestamo
WHERE TO_CHAR(fec_prestamo, 'fmMonth') = &month;
Then when you are prompted for the substitution variable you can enter: 'January' (including the surrounding quotes).

Select and group multiple range of dates in oracle SQL

I want to Select the same range of dates and group them by year. In this case I want the total of the bills of a customer per year from September and October.
My Code right now:
SELECT c.name, c.telefonnumber, TO_CHAR(b.billdate, 'YYYY'), sum(b.sume)
FROM customer c, bill b
WHERE b.customerId = c.id
AND b.datum BETWEEN '01.10.2016' AND '30.09.2016'
GROUP BY c.name, c.telefonnumber, TO_CHAR(b.billdate, 'YYYY');
This Query works perfectly but when I want to add another year the result is an endless loop:
AND b.datum BETWEEN '01.10.2016' AND '30.09.2016'
OR b.datum BETWEEN '01.10.2015' AND '30.09.2015'
I also tried to do it in a String way. In this case when I only look for the results of September it works perfectly, but again as soon as I add the OR it becomes again an endless loop:
AND TO_CHAR(kp.tmstmp) LIKE '%.09.%'
OR TO_CHAR(kp.tmstmp) LIKE '%.10.%'
I know I am somehow missing something in the OR operator, but since this is only happening me with dates I am a bit confused.
This query works fine and return 0 rows, I guess - exactly the number of dates between 01.10.2016 and 30.09.2016 :)
If you wish to check several ranges, you should enclose them into braces:
... and
(
b.datum between date '2016-10-01' and date '2017-09-30' or
b.datum between date '2015-10-01' and date '2016-09-30' or
...
) and
...
It looks like your date criteria in the WHERE clause are being phrased incorrectly. Try this instead:
SELECT c.name, c.telefonnumber, TO_CHAR(b.billdate, 'YYYY'), sum(b.sume)
FROM customer c, bill b
WHERE b.customerId = c.id AND
(b.datum BETWEEN TO_DATE('01.10.2016', 'DD.MM.YYYY') AND
TO_DATE('30.09.2016', 'DD.MM.YYYY') OR
b.datum BETWEEN TO_DATE('01.10.2015', 'DD.MM.YYYY') AND
TO_DATE('30.09.2015', 'DD.MM.YYYY'));
I made two changes, namely making certain that the date checks occur in one logical place, surrounded by outer parentheses, and that we use bona fide dates when comparing against the datum column.

How do I convert Months from Numeral to String Form in SQL Query Containing GROUP BY?

So, I'm creating a View that pulls a bunch of data (with a SUM aggregate on one field). The query contains GROUP BY clause and when I do the month conversion in the query as follows, I get an %invalid character% error.
SELECT Code, Name, Products, SUM(Costs) AS TOTAL,
Year, to_char(to_date(PurchaseMonth),'DD-Mon-yyyy') AS MonthProductPurchased
FROM ProductCatalog
<BUNCH OF JOINS HERE>
WHERE ABC.Code=DEF.Code AND Products IS NOT null
GROUP BY Code, Name, Products, Year, MonthProductPurchased;
So, for instance, I want to convert '1' in the Month field to 'January', '2' to February, so on and so forth.
I see, you have something called PurchaseMonth and it takes the values 1, 2, 3, etc. You want to convert these to month names. How about this?
to_char(to_date('2000-' || PurchaseMonth || '-01'), 'YYYY-MM-DD'), 'MONTH')

View data by date after Format 'mmyy'

I'm trying to answer questions like, how many POs per month do we have? Or, how many lines are there in every PO by month, etc. The original PO dates are all formatted #1/1/2013#. So my first step was to Format each PO record date into 'mmyy' so I could group and COUNT them.
This worked well but, now I cannot view the data by date... For example, I cannot ask 'How many POs after December did we get?' I think this is because SQL does not recognize mm/yy as a comparable date.
Any ideas how I could restructure this?
There are 2 queries I wrote. This is the query to format the dates. This is also the query I was trying to add the date filter to (ex: >#3/14#)
SELECT qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy") AS [Date]
FROM qryALL_PO
GROUP BY qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy");
My group and counting query is:
SELECT qryALL_PO.POLN, Sum(qryALL_PO.[LINE QUANTITY]) AS SUM_QTY_PO
FROM qryALL_PO
GROUP BY qryALL_PO.POLN;
You can still count and group dates, as long as you have a way to determine the part of the date you are looking for.
In Access you can use year and month for example to get the year and month part of the date:
select year(mydate)
, month(mydate)
, count(*)
from tableX
group
by year(mydate)
, month(mydate)
You can format it 'YYYY-MM' , and then use '>' for 'after' clause

can I use the to_char function in where clause in a query with PL/SQL?

I am trying to find out average of electricity volume of certain day in a week with the following query:
SELECT avg(volume)
FROM v_nem_rm16
WHERE to_char(day, 'day') = 'monday';
where the v_nem_rm16 is a table and volume, day are its columns and my query is returning null whatever I change the day value 'monday', 'tuesday',....
is this query wrong?
Actually 'DAY' is returned with padding spaces on the right side.
If you use 'RTRIM' then you can avoid the null values.
SELECT avg(volume)
FROM v_nem_rm16
WHERE RTRIM(to_char(day, 'day')) = 'monday';
I would rather use different date format to_char DAY is nls-dependent that is bad (for instance your software will fail in Spain). D returns number so in your case the query should look like
SELECT avg(volume)
FROM v_nem_rm16
WHERE RTRIM(to_char(day, 'd')) = 1;