I have a table which contains summary totals by date by location for sales,qty,trans and bud_sales
Is it possible in 1 query to select data for the current date and the week to date and the month to date
location,trans_date,sales,qty,trans,budget,wkSales,wkQty,wktrans,wkBud,mtdSales,mtdQty,mtdtrans,mtdBud
tried with the following
SELECT
stockhst.TRANS_DATE,
stockhst.LOCATION,
stockhst.SALE_QTY,
stockhst.SALE_VALUE,
stockhst.TAX_VALUE,
stockhst.ecomm_value,
stockhst.TRANS,
stockhst.COST_VALUE,
stockhst.BUD_SALES,
location.NAME,
(SELECT SUM(stockhst.SALE_VALUE) AS wkSales
SUM(stockhst.qty) AS wkqty FROM stockhst
WHERE TRANS_DATE > CURDATE()-7 AND LOCATION=location GROUP BY location)
FROM
stockhst
INNER JOIN location ON stockhst.LOCATION = location.LOCATION
WHERE TRANS_DATE = CURDATE()
It errors, not sure what I am doing wrong.
Related
with all_data as (
select agent_id ,date(created_at) , , count(delivered)as number_of_orders
from orders_order
where date(created_at) between {{start_date}} and {{end_date}}
group by date,agent_id
order by date
)
select ag.first_name, ag.last_name, ad.*
from all_data as ad
left join agents_agent as ag on ad.agent_id=ag.id
i have a query which i need to run for every day in an interval, like for each day, for the last 2 years, i don't have the day info in the table, so i need to do it in a loop i think:
' select distinct on (osu.order_id) osu.order_id, osu.order_state, osu.created_at
from stock_management.order_state_updates osu
where osu.created_at < '2021-01-26 22:00:00'
order by osu.order_id desc, osu.created_at desc) temp
where temp.order_state = 'Filter1';'
in which the date '2021-01-26 22:00:00' would go through each day of the interval. thank you
https://docs.google.com/spreadsheets/d/1B2xx-c3wWZYaEN76LxjYhHnlrPRUx4TG8vsAkZ1X_Vs/edit?usp=sharing
error
You can generate a calendar and join it to your query. I'm not sure this will retrieve the right datas because I don't have sample data and expected result.
with d as (select * from generate_series ('20210101','20220427',interval '1 day') as date)
select distinct on (osu.order_id) osu.order_id, osu.order_state, osu.created_at::date
from stock_management.order_state_updates osu right join d on osu.created_at::date = d.date
order by osu.order_id desc, osu.created_at desc) temp
where temp.order_state = 'Filter1';
I am trying to pull unique active users before a date.
So specifically, I have a date range (let's say August - November) where I want to know the cumulative unique active users on or before a day within a month.
So, the pseudocode would look something like this:
SELECT COUNT(DISTINCT USERS) FROM USER_DB
WHERE
Month = [loop through months 8-11]
AND
DAY <= [day in loop of 1:31]
The output I desire is something Like this
step-by-step demo: db<>fiddle
SELECT
mydate,
SUM( -- 3
COUNT(DISTINCT username) -- 1, 2
) OVER (ORDER BY mydate) -- 3
FROM t
GROUP BY mydate -- 2
GROUP BY your date and count the users
Because you don't want to count ALL user accesses, but only one access per user and day, you need to add the DISTINCT
This is a window function. This one aggregates all counts which where previously done cumulatively.
If you want to get unique user over ALL days (count a user only on its first access) you can filter the users with a DISTINCT ON clause first:
demo: db<>fiddle
SELECT DISTINCT ON (username)
*
FROM t
ORDER BY username, mydate
This yields:
SELECT
mydate,
SUM(
COUNT(*)
) OVER (ORDER BY mydate)
FROM (
SELECT DISTINCT ON (username)
*
FROM t
ORDER BY username, mydate
) s
GROUP BY mydate
Need to get order qty of the minimum ADATE
Im using below query and getting 12 records. Now I want to select orderqty of minimum ADATE which is 06-NOV-2018(2018-11-06).
For every customer(will get multiple records), i need to get the Order_Qty of minimum ADATE column.
select
Customer ,
OrderID ,
LocationID ,
Order_Qty,Sent_date ,ADATE
from
(
select
OrderID ,
LocationID ,
Sent_date ,
Order_Qty ,
Customer ,
TimeStampA
from ARC_TBL
)
obn
inner join
(
select
ADATE ,TimeStampA
from trackTBL snt
)snt
on obn.TimeStampA = snt.TimeStampA
where Customer='ABC' and OrderID='XYZ100' and Sent_date='2018-11-18' and LocationID='250';
SELECT QTY, ADATE
FROM table
ORDER BY ADATE
FETCH FIRST 1 ROW ONLY
Explain your question in more detail and you will get better answers.
I need to select the rows for which the difference between max(date) and the date just before max(date) is smaller than 366 days. I know about SELECT MAX(date) FROM table to get the last date from now, but how could I get the date before?
I would need a query of this kind:
SELECT code, MAX(date) - before_date FROM troncon WHERE MAX(date) - before_date < 366 ;
NB : before_date does not refer to anything and is to be replaced by a functionnal stuff.
Edit : Example of the table I'm testing it on:
CREATE TABLE troncon (code INTEGER, ope_date DATE) ;
INSERT INTO troncon (code, ope_date) VALUES
('C086000-T10001', '2014-11-11'),
('C086000-T10001', '2014-11-11'),
('C086000-T10002', '2014-12-03'),
('C086000-T10002', '2014-01-03'),
('C086000-T10003', '2014-08-11'),
('C086000-T10003', '2014-03-03'),
('C086000-T10003', '2012-02-27'),
('C086000-T10004', '2014-08-11'),
('C086000-T10004', '2013-12-30'),
('C086000-T10004', '2013-06-01'),
('C086000-T10004', '2012-07-31'),
('C086000-T10005', '2013-10-01'),
('C086000-T10005', '2012-11-01'),
('C086000-T10006', '2014-04-01'),
('C086000-T10006', '2014-05-15'),
('C086000-T10001', '2014-07-05'),
('C086000-T10003', '2014-03-03');
Many thanks!
The sub query contains all rows joined with the unique max date, and you select only ones which there differente with the max date is smaller than 366 days:
select * from
(
SELECT id, date, max(date) over(partition by code) max_date FROM your_table
) A
where max_date - date < interval '366 day'
PS: As #a_horse_with_no_name said, you can partition by code to get maximum_date for each code.