I have two tables with the following (simplified) structures:
table "Factors" which holds data about purchased goods' factors and has these columns:
FactorSerial, PurchaseDate, PurchasedGood
table "Prices" which holds goods prices on different dates
Serial, GoodCode, EvaluationDate, Price
A price is valid until a new row with the same Code but different date is added and thus updates its value
Now, I want to create a table which adds the price to the table 1 according to purchase date.
So if we have:
PurchaseDate PurchasedGood
-----------------------------
05/20/2011 A111
and:
GoodCode EvaluationDate Price
--------------------------------
A111 02/01/2011 100
...
A111 04/01/2011 110
...
A111 06/01/2011 120
the result would be
PurchaseDate PurchasedGood Price
-----------------------------------
05/20/2011 A111 110
Preferred method is creating the view Prices1 as
Serial GoodCode StartDate EndDate Price
and then joining Factors with this view by
PurchasedDate between StartDate AND EndDate
Can anybody show me how to create view1 (or obtaining the final result with any other method)? Thanks in advance!
P.S. sorry for my bad English!
I want to create a table which adds the price to the table 1 according to purchase date.
Here is a query that returns such data. The syntax is pretty standard SQL, I believe, but this was tested on SQL Server (looks like you may be using PostgreSQL with your "serial" naming).
select a.FactorSerial, a.PurchasedGood, a.PurchaseDate
, (select max(Price) from Prices where GoodCode = a.PurchasedGood and EvaluationDate = a.EvaluationDate) as Price
from (
select f.FactorSerial, f.PurchasedGood, f.PurchaseDate, max(p.EvaluationDate) as EvaluationDate
from Factors as f
join Prices as p on f.PurchasedGood = p.GoodCode
where f.PurchaseDate >= p.EvaluationDate
group by f.FactorSerial, f.PurchasedGood, f.PurchaseDate
) as a
This query assumes that there are no Purchases before a Price existed.
Also, considering:
Preferred method is creating the view Prices1 as
Serial GoodCode StartDate EndDate Price
and then joining Factors with this view by
PurchasedDate between StartDate AND EndDate
between is inclusive. Using this method that you've described, you would get duplicates if a PurchaseDate lies on the EndDate of one row and the StartDate of another.
Related
I have a sqlite3 database maintained on an AWS exchange that is regularly updated by a Python script. One of the things it tracks is when any team generates a new post for a given topic. The entries look something like this:
id
client
team
date
industry
city
895
acme industries
blueteam
2022-06-30
construction
springfield
I'm trying to create a table that shows me how many entries for construction occur each day. Right now, the entries with data populate, but they exclude dates with no entries. For example, if I search for just
SELECT date, count(id) as num_records
from mytable
WHERE industry = "construction"
group by date
order by date asc
I'll get results that looks like this:
date
num_records
2022-04-01
3
2022-04-04
1
How can I make sqlite output like this:
date
num_records
2022-04-02
3
2022-04-02
0
2022-04-03
0
2022-04-04
1
I'm trying to generate some graphs from this data and need to be able to include all dates for the target timeframe.
EDIT/UPDATE:
The table does not already include every date; it only includes dates relevant to an entry. If no team posts work on a day, the date column will jump from day 1 (e.g. 2022-04-01) to day 3 (2022-04-03).
Given that your "mytable" table contains all dates you need as an assumption, you can first select all of your dates, then apply a LEFT JOIN to your own query, and map all resulting NULL values for the "num_records" field to "0" using the COALESCE function.
WITH cte AS (
SELECT date,
COUNT(id) AS num_records
FROM mytable
WHERE industry = "construction"
GROUP BY date
ORDER BY date
)
SELECT dates.date,
COALESCE(cte.num_records, 0) AS num_records
FROM (SELECT date FROM mytable) dates
LEFT JOIN cte
ON dates.date = cte.date
Good day
May you kindly assist me, I have a SQL view That has txDate, reference, Amount
like
select TxDate, Reference, Amount
FROM Transactions
Then I have a Table called Period, that has PeriodId, StartDate and EndDate
so the period is by month,
LIKE
PeriodID StartDate EndDate
1 2017/01/01 2017/01/31
2 2017/02/01 2017/02/29
3 2017/03/01 2017/03/31
4 2017/04/01 2017/04/30
And so on up until December which will be period 12,
So i want to have the Query to Search the Period table using the txDate in my SQL View, for Example
if txDate is '2017/02/25'
The Query must return the PeriodID in Which the TxDate is Between StartDate and EndDate, so in this instance it should be 2.
So my result should be something like this
txDate Reference Amount PeriodID
2017/02/25 INVOO1 2000 2
2017/01/04 REC002 30 1
2017/03/05 SALE 5000 3
How do I make that JOIN on Searching that Table to bring the PEriodID onto my SQL View...??
If you're sure the start- and end-date within the Period table never overlap and that there's always a record in the Period table that matches your TxDate, it's quite simple:
SELECT tran.TxDate,
tran.Reference,
tran.Amount,
peri.idPeriod
FROM Transactions tran
JOIN Period peri
ON tran.TxDate BETWEEN peri.PeriodStartDate and peri.PeriodEndDate,
I have a table which has a large number of entries and from which I need only the first in each group.
The table is used to store daily fund prices (1000+ funds) over the last 30 years. I need to find the last price prior to, or on a specific date for each fund existing on that date (so only one row per fund).
In its simplified form, the table has columns Date, FundCode and Price.
The following input
Date FundCode Price
2016/01/05 X123 1.234
2016/01/04 X123 1.233
2016/01/03 X123 1.222
2016/01/05 A456 1.876
2016/01/04 A456 1.822
2016/01/03 A456 1.776
2016/01/03 M234 3.234
...when queried for 2016/01/04, should produce
Date FundCode Price
2016/01/04 X123 1.233
2016/01/04 A456 1.822
2016/01/03 M234 3.234
I have a solution which uses a correlated subquery in the where but no amount of messing with indexes will make it run in a reasonable amount of time.
I'm sure there's a straightforward solution to this but I just can't see it.
Somethink like
SELECT fundCode, price, date FROM your_table WHERE date<='date_you_need' GROUP BY fundCode HAVING MAX(date)
Query like this works in SQLITE, what db do you use?
A single nested query gives me the max date for each fund, then inner join to this on fundCode/Date, thus...
SELECT
Date,
FundCode,
Price
FROM
PriceHistory H
INNER JOIN
/* this nested query gives the max date for each fund*/
(SELECT
FundCode,
max(Date) AS MaxDate
FROM
PriceHistory H2
WHERE
Date<=#DateToSearchFor
GROUP BY
FundCode) AS RowSelector
ON H.FundCode=RowSelector.FundCode AND H.Date=RowSelector.MaxDate
I have tried and failed to solve this puzzle, and now im looking for some nice help if anyone is out there. Explanation:
First table consists of the original price on a item.
Table1 (Sales_Data):
+---------+----------+-------+------------+
| Item_Id | Store_Id | Price | Sales_Date |
+---------+----------+-------+------------+
Second table consists of two prices
one is if a store has another price on the item and this has a 0 value in To_Date
because this price should last forever.(Lets call this forever price)
one is if a store has another price on a item for just a period (02.03.2014-10.03.2014) lets call this discount
both prices is stored in Price, but the dates are the big difference.
Table2 (Discount_Data):
+---------+----------+-------+------------+
| Item_Id | Store_Id | Price | Sales_Date |
+---------+----------+-------+------------+
Now to the big Q:
Forever price should always overwrite original price
Discount price should always overwrite original/or forever price for the exact period
Item_Id, and Store_Id has to be the same.
How can i go forward to solve this? Can anyone help me on the way?
I hate to make an assumption, but it appears your second table should include two more columns, From_Date and To_Date. I will alias Discount_Data forever price as ddf and Discount_Data period price as ddp
select sd.Item_Id,
sd.Store_Id,
sd.Price as OriginalPrice,
coalesce(ddd.Price,ddf.Price,sd.Price) as UpdatedPrice
from Sales_Data sd
left join Discount_Data ddf
on sd.Item_Id=ddf.Item_Id
and sd.Store_Id=ddf.Store_Id
and sd.Sales_Date >= ddf.From_Date
and ddf.To_Date=0
left join Discount_Data ddp
on sd.Item_Id=ddp.Item_Id
and sd.Store_Id=ddp.Store_Id
and sd.Sales_Date between ddp.From_Date and ddp.To_Date
Hope that helps.
So I have a table with a date column. I want to be able to group these dates by Item field of some type. For example I might have a column called Item and within the Item field there may be 500 entries. Item 12345 might have 5 entires, each with a price. I want to be able to pull out all of the Items, grouped by the newest date.
031126-2M1 8/10/2011 12:00:00 AM 7.8678
031126-2M1 7/22/2011 12:00:00 AM 9.5620
031126-2M1 7/15/2011 12:00:00 AM 8.8090
In this example, I want to show the item with the closest date, 7/15/2011 so I can use that Price of 8.8090. The list would then show the other items, some may have one entry, others might have many, but I want to show all of them with the closets date. Need Help!
Thanks
A MS SQL Server version...
WITH
sorted_data AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY item_id ORDER BY item_date DESC) AS row_id,
*
FROM
item_data
WHERE
item_date <= getDate()
)
SELECT * FROM sorted_data WHERE row_id = 1
selct * from table
where This = that
Group by something having This.
order by date desc.
having "THIS" should be in the query itself.
hope this helps..
Cheers