I'm trying to get the minimum value of open, across multiple rows of year. This is from app.mode.com and the site only says SQL, not sure which version
SELECT year, open
FROM tutorial.aapl_historical_stock_price
WHERE open =
(
select MIN(open)
FROM tutorial.aapl_historical_stock_price
)
When I use the code above, the result is
Table result vs actual output
Year
Open
2000
0
2000
0
2000
0
What I'm trying to get is
Year
Open
2002
0
2001
0
2000
0
Can someone help point me what I'm doing wrong?
select year and get the min by grouping each year as following:
select
year
, min(open) as <desired_alias>
from your_table
group by 1
order by 1 desc;
Related
First off, I am a beginner in SQL just learning and I am stuck on one problem looked everywhere but not able to find an answer for it.
SCHEMA: time_ts TIMESTAMP,id BYTES,sale_amount FLOAT,client STRING.
The report I am trying to export is the clients who are newly acquired within the last 12 months that has made 2 and 3 purchase over the last 12 months as well.
DATA SAMPLE:
Row time_ts id sale_amount client
1 2011-12-02 16:17:01.280 UTC James 97.67 104795
2 2010-03-29 19:43:07.723 UTC Mark 90.0 106186
EXPECTED RES
Number_of_Orders Revenue_Total Year Total_Num_of_orders
1, 100$ 2010 60
2, 150$ 2010 65
What I have so far ( Which returns 0 results):
SELECT client, COUNT(id) AS sales, MIN(time_ts),
FROM [bigquery-public-data:hacker_news.comments]
WHERE time_ts >= TIMESTAMP(time_ts) > DATE_ADD(USEC_TO_TIMESTAMP(NOW()), -12, 'MONTH')
GROUP BY client
HAVING COUNT(id) = 2;
You are close. But the condition in the where needs to be in the having:
SELECT client, COUNT(id) AS sales, MIN(time_ts),
FROM [bigquery-public-data:hacker_news.comments]
GROUP BY client
HAVING COUNT(id) = 2 AND
MIN(time_ts) > DATE_ADD(USEC_TO_TIMESTAMP(NOW()), -12, 'MONTH');
I am assuming that the date arithmetic is right. I stopped using legacy SQL a while ago and you should use standard SQL as well.
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
Is there any way to generate a custom sequential number like the following?
I want the Number to be incremented with grouping by the Code and Year.
Code Year Number
A 2016 1
A 2016 2
A 2016 3
B 2016 1
B 2016 2
C 2016 1
A 2017 1
A 2017 2
Any suggestion would be appreciated.
EDIT
Sorry, I was too ambiguous what I want. I want to generate the unique number when I query, so if I ask a new number in the above data context with Code:A and Year:2017, I want the Number to be 3. I guess to get the Number properly in a future I need to save the Code and Year with the Number.
Use ROW_NUMBER to assign Number per Code,Year grouping.
SELECT *,
Number = ROW_NUMBER() OVER(PARTITION BY Code, [Year] ORDER BY (SELECT NULL))
FROM tbl
Replace SELECT NULL with the column you want the order to be based from.
This query returns the results below.
Select BookTitle,TotalNumberInStock
From CurrentStock
Where (Year<2000 OR Year >2010);
Table 1
BookTitle TotalNumberInStock
The Tower 2
Orange Goblins 1
The future of Metal 3
Chronicles of the Banjo 2
Opera 4
Advanced SQL 5
The GAA 4
I want to write a subquery that sums the TotalNumber In Stock so I used this statement:
Select Sum(TotalNumberInstock)
From
(
Select BookTitle,TotalNumberInStock
From CurrentStock
Where (Year<2000 OR Year >2010)
);
I get an error saying:
Incorrect syntax near ';'.
What is wrong with this code?
you need to give an alias name to your sub query (aka derived table)
select Sum(TotalNumberInstock)
From (Select BookTitle,TotalNumberInStock From CurrentStock
Where (Year<2000 OR Year >2010)) x ;
this correction makes your query work, but you don't need such complexity to have your desired result. you can simply get what you want by this query
select sum(totalNumberInStock)
from CurrentStock
where year < 2000 or year > 2010
I'm not sure why you need a sub query.
select sum(totalNumberInStock)
from CurrentStock
where year < 2000 or year > 2010
This should do the trick:
Select BookTitle,SUM(TotalNumberInStock)
From CurrentStock
Where (Year < 2000 and YEAR > 2010)
GROUP BY BookTitle
I have a question regarding Oracle SQL.
My data looks like this:
id year
-- ----
1 2000
1 2001
1 2002
1 2003
1 2006
1 2000
2 2001
2 2002
2 2003
3 2003
3 2005
4 2012
4 2013
I want the id's which have the years 2001, 2002, 2003.
My result set:
id
--
1
2
Please help me with this. I actually tried searching this, but couldn't figure a way to search about my particular problem.
SQL
SELECT t.id
FROM TABLE t
WHERE t.year in(2001,2002,2003)
GROUP BY t.id
Sample SqlFiddle
http://sqlfiddle.com/#!2/4ec9f/2/0
Explanation
You want to filter your data set to only show rows with certain years, so that is what you put in the where clause WHERE t.year in(2001,2002,2003).
Since a single id can be in multiple years, your result set would contain duplicates. To remove the duplicates you could GROUP BY the ID or use the DISTINCT statement to only show unique elements.
UPDATE
Based on comments, here's a version that will only display id's that have all three years. We use DISTINCT t.YEAR to avoid counting id's that perhaps would have a single year repeated multiple times. The HAVING COUNT(DISTINCT t.YEAR) = 3 part ensures that we only include id's that have all three years.
SELECT t.id
FROM years t
WHERE t.year in(2001,2002,2003)
GROUP BY t.id
HAVING COUNT(DISTINCT t.YEAR) = 3
Updated sqlFiddle, which includes a data set where id of 3 has two rows for 2003 to show off the logic that only counts unique years for an ID.
select distinct id
from table
where year in(2001,2002,2003)