Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
There are 2 tables
CUSTOMERS(ID, FIRSTNAME, LASTNAME, ADDRESS);
ORDERS (ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER, ID_CUSTOMER, AMOUNT);
The task is to show all orders (all fields), that have been made before year 2015 and sort the data by ID.
This my code:
SELECT * FROM ORDERS
WHERE YEAR(DATE_ORDER) < 2015
ORDER BY ID
Error when executed:
no such function YEAR
I tried to do that like this:
SELECT * FROM ORDERS
WHERE strfttime('%Y', DATE_ORDER) < 2015
ORDER BY ID
and still doesn't work
I would advise you to write the code using date logic:
where date_order < date('2015-01-01') -> "I tried this one and it worked" - author of the post
Standard SQL uses the function extract():
where extract(year from date_order) < 2015
However, the exact syntax for both depends on the database you are using.
This smells like SQLite. Assuming that your dates be in the format YYYY-MM-DD you could try:
SELECT *
FROM ORDERS
WHERE CAST(SUBSTR(DATE_ORDER, 1, 4) AS integer) < 2015
ORDER BY ID;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 days ago.
Improve this question
i want my code to show month, categ and max(items)
SELECT extract(month from ot.delivered_at) as month,
p.category as categ, count(*) as items
FROM bigquery-public-data.thelook_ecommerce.products p
INNER JOIN bigquery-public-data.thelook_ecommerce.order_items ot
ON p.id = ot.product_id
where status = "Complete"
and delivered_at >= "2022-01-01"
and delivered_at < "2022-10-01"
GROUP BY month, category
data that show
enter image description here
month | categ | items
1 intimate 111
etc
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I'm cracking my head why the WHERE is not returning the right results
(it is not returning properly the < and > results)
could someone help please? Thank you!
SELECT
country_txt AS Country
,DATE_FROM_PARTS(iyear, imonth, iday) AS Date
,count(eventid) AS Number of events
,SUM(Col1-Col2) AS Number of men
,SUM(Col3) AS Numbe of women
,SUM(Col4) AS Number of kids
FROM xxx
WHERE iyear=('2022') AND Country in ('USA') AND eventid >= 10 AND (Col1-Col2) >= 8
GROUP BY Country, Date
ORDER BY Country ASC, Date ASC
;
Where you've put 'AND eventid >= 10 AND (Col1-Col2) >= 8' in your conditions, are you looking for cases with ten or more events and eight or more men? If so, get rid of that and stick the below under the 'group by' line:
HAVING COUNT(eventid) >= 10 AND SUM(Col1-Col2) >= 8
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
this is what I tried-
select distinct studid
from registration
where to_char(doj,'MM')='june'
order by studid;
but I did not get the desired results.
TO_CHAR() with MM parameter will give you 06, instead of JUNE. You may try below query instead -
select distinct studid
from registration
where to_char(doj,'MM')='06'
order by studid;
Rather than using TO_CHAR, You should use oracle specific function EXTRACT -
select distinct studid
from registration
where extract(month from doj) = 6
order by studid;
I assume that you are running Oracle, as the use of to_char() suggests.
If you want students that joined in june of a given year, say 2019, then I would recommend to check doj against a half-open interval (this is more efficient than applying a date function on the column):
select distinct studid
from registration
where doj >= date '2019-06-01' and doj < date '2019-07-01'
order by studid
On the other hand, if you want sudents that joined in any month of june:
select distinct studid
from registration
where extract(month from doj) = 6
order by studid
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How to write a query to get the following output:
Main Table/ source table:
Here is one method that uses union all and aggregation:
select weeknumber, sum(opened) as opened, sum(closed) as closed
from ((select OpenWeekNumber as weeknumber, 1 as Opened, 0 as Closed
from maintable
) union all
(select ClosedWeekNumber as weeknumber, 0 as Opened, 1 as Closed
from maintable
where ClosedWeekNumber > 0
)
) t
group by weeknumber
order by weeknumber;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to show data created after 01.01.2014 - data created before should not be shown in the output.
I got this so far:
AND TO_CHAR(table1.datum,'IW') = 17 (number of the week - just a start from me)
AND TO_CHAR(table1.datum,'YYYY') = 2014 (shows only data from 2014)
Can you help me?
I would like to show data created after 01.01.2014 - data created before should not be shown in the output.
Then just put the date condition in the filter predicate.
WHERE table1.datum > TO_DATE('01-01-2014','DD-MM-YYYY')
Note that this will not include the rows which were created on '01-01-2014', to include them as well you need greater than or equal to:
WHERE table1.datum >= TO_DATE('01-01-2014','DD-MM-YYYY')
For example,
SQL> WITH table1 AS(
2 SELECT to_date('20-12-2013','DD-MM-YYYY') datum FROM dual UNION ALL
3 SELECT to_date('01-04-2014','DD-MM-YYYY') datum FROM dual UNION ALL
4 SELECT to_date('20-10-2015','DD-MM-YYYY') datum FROM dual
5 )
6 SELECT * FROM table1
7 WHERE table1.datum >= TO_DATE('01-01-2014','DD-MM-YYYY')
8 /
DATUM
---------
01-APR-14
20-OCT-15
SQL>
So, '20-12-2013' is filtered out and you have rows created only created after '01.01.2014'.
Select * from table
Where the_date_field < to_date('01-01-2014','dd-mm-yyyy')
Order by ..... /* If needed */