I am new to Postgresql/Python, so please bear with me!
Assuming we have two tables:
item table having a itemid, price, time.
user table having colums userid, itemid, timecreated, quantity, firstprice, lastprice, difference.
Table examples like :
item table:
itemid price time
RBK 92 1546408800
LBV 51 1546408800
ZBT 49 1546408800
GLS 22 1546408800
DBC 17 1546408800
RBK 91 1546495200
LBV 55 1546495200
ZBT 51 1546495200
GLS 24 1546495200
DBC 28 1546581600
RBK 108 1546581600
LBV 46 1546581600
ZBT 49 1546581600
GLS 21 1546581600
DBC 107 1546581600
In item table all those values comes up with api.
and user table:
userid itemid timecreated quantitty firstprice currentprice difference
1 RBK 1546408800 20
2 RBK 1546408800 15
3 RBK 1546408800 35
3 GLS 1546408800 101
3 DBC 1546495200 140
1 RBV 1546495200 141
2 RBK 1546495200 25
2 RBV 1546581600 31
User table is djangobased table which is user can register\add new items to follow prices.
My struggle access the item table to fetch first price which is having a same timestamp. In that example userid 1 RBK First price (1546408800) must be filling with 92
I did some trick with postgresql with (lag) But this does not seems to be working:
update user
set firstprice = tt.prev_price
from (select item.*,
lag(price) over (partition by itemid order by time) as prev_price
from item
) tt
where tt.id = item.id and
tt.prev_close is distinct from item.price;
I can call current price from the api but didnt find out the way to filling firstprice from the item table. I will be making for a trigger for this query. I searched on google and on stackoverflow but I couldn't find anything that could help me. Thanks in advance.
I can advice next approach (may be not fastest):
update "user" set firstprice = (
select price from "item" i
where i.itemid = "user".itemid and i.time >= "user".timecreated order by i.time limit 1
);
It calculate firstprice using sub-query. Test this SQL here
I created a view which is presently giving the data like this:
practice_name message_type message_count
CHC ALOG_SYNC 1
CHC BULKNT 0
CHC PIE_SYNC 1
CHC PPRV_SYNC 1
CHC SYNC_PRACT 3
CHC SYNC_PROV 9
CHC SYNC_WTXT 3
CHC SYNC_XYZ 0
Midtown ALOG_SYNC 0
Midtown BULKNT 0
Midtown PIE_SYNC 0
Midtown PPRV_SYNC 0
Midtown SYNC_PRACT 3
Midtown SYNC_PROV 0
Midtown SYNC_WTXT 3
Midtown SYNC_XYZ 0
NextGen MedicalPractice ALOG_SYNC 0
NextGen MedicalPractice BULKNT 1
NextGen MedicalPractice PIE_SYNC 0
NextGen MedicalPractice PPRV_SYNC 0
NextGen MedicalPractice SYNC_PRACT 3
NextGen MedicalPractice SYNC_PROV 591
NextGen MedicalPractice SYNC_WTXT 3
NextGen MedicalPractice SYNC_XYZ 0
My View:
CREATE OR REPLACE VIEW sha.sha_export_queue_view AS
SELECT q3.practice_name,
q3.message_type,
q3.share_site_org_key,
COALESCE(q2.message_count, '0'::text) AS message_count
FROM ( SELECT q1.practice_name,
mt.message_type,
q1.share_site_org_key
FROM sha.message_types mt,
( SELECT DISTINCT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
ai.share_site_org_key
FROM sha.sha_share_site_view ssv
LEFT JOIN ( SELECT mytable2.assessment_id,
mytable2.result_json,
mytable2.share_site_org_key,
mytable2.rnk
FROM ( SELECT assessment_info.assessment_id,
assessment_info.result_json,
assessment_info.share_site_org_key,
dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
FROM sha.assessment_info
WHERE assessment_info.assessment_id = 8::numeric) mytable2
WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q1) q3
LEFT JOIN ( SELECT jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Practice Name'::text AS practice_name,
jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Type'::text AS message_type,
jsonb_array_elements((ai.result_json -> 'Patient Portal Operational Information'::text) -> 'nxmd_export contents by message type'::text) ->> 'Message Count'::text AS message_count
FROM sha.sha_share_site_view ssv
LEFT JOIN ( SELECT mytable2.assessment_id,
mytable2.result_json,
mytable2.share_site_org_key,
mytable2.rnk
FROM ( SELECT assessment_info.assessment_id,
assessment_info.result_json,
assessment_info.share_site_org_key,
dense_rank() OVER (PARTITION BY assessment_info.share_site_org_key ORDER BY assessment_info.modified_datetime DESC) AS rnk
FROM sha.assessment_info
WHERE assessment_info.assessment_id = 8::numeric) mytable2
WHERE mytable2.rnk = 1) ai ON ssv.share_site_org_key = ai.share_site_org_key) q2 ON q3.message_type::text = q2.message_type AND q3.practice_name = q2.practice_name
ORDER BY q3.practice_name;
I want the second column to be flattened:
Practice Time Stamp <<message type 1>> <<message type 2>> <<message type 3>> <<message type 4 >> <<message type 5>> <<message type 6>> <<message type 7>> <<message type 8>>
Practice Name 1 21-12-2016 10:00 23 25 27 29 31 33 35 37
Practice Name 2 21-12-2016 10:00 24 26 28 30 32 34 36 38
Practice Name 3 21-12-2016 13:00 25 27 29 31 33 35 37 39
Practice Name 4 21-12-2016 13:00 26 28 30 32 34 36 38 40
Practice Name 5 24-12-2016 13:00 27 29 31 33 35 37 39 41
Practice Name 6 27-12-2016 13:00 28 30 32 34 36 38 40 42
Practice Name 7 30-12-2016 13:00 29 31 33 35 37 39 41 43
Practice Name 8 02-01-2017 13:00 30 32 34 36 38 40 42 44
Practice Name 1 05-01-2017 13:00 31 33 35 37 39 41 43 45
Practice Name 2 08-01-2017 13:00 32 34 36 38 40 42 44 46
Practice Name 3 11-01-2017 13:00 33 35 37 39 41 43 45 47
Is there any way I can achieve that?
Sorry for the little alignment issue.
The values are corresponding message type values
Sample for query (idea in comments to question):
SELECT
practice_name,
sum("ALOG_SYNC") AS "ALOG_SYNC",
sum("BULKNT") AS "BULKNT",
...
FROM (
SELECT
practice_name,
CASE WHEN q3.message_type = 'ALOG_SYNC' THEN sum(message_count) END AS "ALOG_SYNC",
CASE WHEN q3.message_type = 'BULKNT' THEN sum(message_count) END AS "BULKNT"
FROM
<your from + where clause>
) AS A
GROUP BY 1
Probably your query might be optimised.
Or you can use crosstab function (https://www.postgresql.org/docs/current/static/tablefunc.html)
I have the following T-SQL function: https://gist.github.com/cwattengard/11365802
This returns data in a breadth-first traversal. Is there a simple way to make this function return its data in a depth-first traversal? I have a treeview-component that excpects this (legacy system).
I already have a similar stored procedure that returns the tree in a depth-first traversal, but it's using cursors and is really slow. (6-7 seconds as opposed to this function that takes less than a second on the same data).
I think I just had a eureka moment. If I add the Path variable already supplied by the CTE, and sort by that, I get what I want. The OrgID is a unique ID. So ordering by it would make it sort by the expected output for the user (chronologically) and be depth-first for the treeview.
http://sqlanywhere.blogspot.in/2012/10/example-recursive-union-tree-traversal.html
Here's a diagram showing the primary keys for a tree-structured table:
1
|
---------------------------------------
2 93 4 5
| | | |
-------------- ------------ -------- ------
6 7 8 9 10 11 12 13 14 15 16 17 18 19
| | | |
----- ----- ----- -----
27 26 25 24 23 22 21 20
Here's what the breadth-first and depth-first queries should return:
Breadth-First Depth-First
1 1
2 2
93 6
4 7
5 27
6 26
7 8
8 9
9 10
10 93
11 11
12 12
13 13
14 25
15 24
16 14
17 4
18 15
19 16
27 17
26 23
25 22
24 5
23 18
22 21
21 20
20 19
If you order the output by tekst will that do it?
First populate a table variable #unsorted inside the function; then finally return Select * from #unsorted order by tekst?
I know this is very late to the game, but seems like you could use hierarchyid to get a nice depth first search...
The github file referenced in the op appears to have gone missing, but the basic formula is
Put hierarchyid::GetRoot() as Foo in your CTE anchor query
Put cast (cte.Foo.ToString() + cast(row_number() over(order by ) as varchar) + '/' as hierarchyid) as Foo in your recursive query
order By Foo when you invoke the CTE
and the results come out depth first
Alright, I've been trying to conceptualize this for a better part of the afternoon and still cannot figure out how to structure this subselect.
The data that I need to report are ages for a given student major grouped by the past 3 fiscal years. Each fiscal year has 3 semesters (summer, fall, spring). I need to have my query grouped on the fiscalyear and agerange fields and then count the distinct student id's.
I currently have this for my SQL statement:
Select COUNT(distinct StuID), AgeRange, FiscalYear
from tblStatic
where Campus like 'World%' and (enrl_act like 'REG%' or enrl_act like 'SCH%')
and StuMaj = 'LAWSC' and FiscalYear IN ('09/10', '10/11', '11/12')
group by FiscalYear, AgeRange
order by FiscalYear, AgeRange
So this is all fine and dandy except it doesn't match my headcount of students for the fiscalyear. The reason being, that people may cross over in the age ranges during the fiscal year and is adding them to my count twice.
How can I use a subselect to resolve this duplicate entry? The field I have been trying to get working is my semester field and using a max to find the max semester during a fiscalyear for a given student.
Data Sample:
Count AgeRange FiscalYear
3 1 to 19 09/10
20 20 to 23 09/10
60 24 to 29 09/10
96 30 to 39 09/10
34 40 to 49 09/10
14 50 to 59 09/10
3 60+ 09/10
2 1 to 19 10/11
24 20 to 23 10/11
73 24 to 29 10/11
109 30 to 39 10/11
43 40 to 49 10/11
11 50 to 59 10/11
2 60+ 10/11
1 1 to 19 11/12
17 20 to 23 11/12
75 24 to 29 11/12
123 30 to 39 11/12
44 40 to 49 11/12
14 50 to 59 11/12
2 60+ 11/12
Solution: (Just got this working and produced my headcounts that match what they are suppose to be)
Select COUNT(distinct S.StuID), AR.AgeRange, S.FiscalYear
from tblStatic S
INNER JOIN
( Select S.StuID, MIN(AgeRange) as AgeRange
From tblStatic S
Group By S.StuID) AR on S.StuID=AR.StuID
where Campus like 'World%' and (enrl_act like 'REG%' or
enrl_act like 'SCH%')
and StuMaj = 'LAWSC' and FiscalYear IN ('09/10', '10/11', '11/12')
group by S.FiscalYear, AR.AgeRange
order by S.FiscalYear, AR.AgeRange
Replace each student's age range with its maximum (or minimum, if you like) age range that fiscal year, then count them:
;
WITH sourceData AS (
SELECT
StudID,
MaxAgeRangeThisFiscalYear = MAX(AgeRange) OVER
(PARTITION BY StudID, FiscalYear),
FiscalYear
FROM tblStatic
WHERE Campus LIKE 'World%'
AND (enrl_act LIKE 'REG%' OR enrl_act LIKE 'SCH%')
AND StuMaj = 'LAWSC'
AND FiscalYear IN ('09/10', '10/11', '11/12')
)
SELECT
FiscalYear,
AgeRange = MaxAgeRangeThisFiscalYear,
Count = COUNT(DISTINCT StudID)
FROM sourceData
GROUP BY
FiscalYear,
MaxAgeRangeThisFiscalYear
ORDER BY
FiscalYear,
MaxAgeRangeThisFiscalYear
In SQL server 2008, I have below table Score. I want to show Score1 and Score2 together for same student. Name and Email combined uniquely identifies a student (Name or Email may be missing too, like Jack and Maya#moon.com). The expected output shown as T_Combined.
Score
Name Email Score1 Score2
John 'John#pluto.com' 75
Peter 'Peter#pluto.com' 34
Nina 'Nina#pluto.com' 45
Joseph 'Joseph#pluto.com' 76
Tom 'Tom#pluto.com' 43
Sam 'Sam#pluto.com' 76
Nancy 'Nancy#pluto.com' 12
Tina 'Tina#pluto.com' 56
John 'John#mars.com' 98
Peter 'Peter#mars.com' 12
Nina 'Nina#mars.com' 45
Joseph 'Joseph#mars.com' 87
Tom 'Tom#mars.com' 67
Sam 'Sam#mars.com' 99
Nancy 'Nancy#mars.com' 33
Tina 'Tina#mars.com' 23
John 'John#pluto.com' 86
Peter 'Peter#pluto.com' 56
Nina 'Nina#pluto.com' 98
Joseph 'Joseph#pluto.com' 78
Tom 'Tom#pluto.com' 12
Sam 'Sam#pluto.com' 45
Nancy 'Nancy#pluto.com' 76
Tina 'Tina#pluto.com' 78
John 'John#mars.com' 98
Peter 'Peter#mars.com' 45
Nina 'Nina#mars.com' 76
Joseph 'Joseph#mars.com' 12
Tom 'Tom#mars.com' 84
Sam 'Sam#mars.com' 27
Nancy 'Nancy#mars.com' 54
Tina 'Tina#mars.com' 50
Jack 10
'Maya#moon.com' 20
T_Combined
Name Email Score1 Score2
John 'John#pluto.com' 86
Peter 'Peter#pluto.com' 56
Nina 'Nina#pluto.com' 98
Joseph 'Joseph#pluto.com' 78
Tom 'Tom#pluto.com' 43 12
Sam 'Sam#pluto.com' 76 45
Nancy 'Nancy#pluto.com' 12
Tina 'Tina#pluto.com' 56
John 'John#mars.com' 98
Peter 'Peter#mars.com' 12
Nina 'Nina#mars.com' 45 76
Joseph 'Joseph#mars.com' 87 12
Tom 'Tom#mars.com' 67 84
Sam 'Sam#mars.com' 99 27
Nancy 'Nancy#mars.com' 33 54
Tina 'Tina#mars.com' 23 50
Jack 10
'Maya#moon.com' 20
Many thanks
Create table Score (Name varchar(20),Email varchar(20),Score1 int,Score2 int)
insert into Score (Name,Email,Score1)values('John','John#pluto.com',75)
insert into Score (Name,Email,Score1)values('Peter','Peter#pluto.com',34)
insert into Score (Name,Email,Score1)values('Nina','Nina#pluto.com',45)
insert into Score (Name,Email,Score1)values('Joseph','Joseph#pluto.com',76)
insert into Score (Name,Email,Score1)values('Tom','Tom#pluto.com',43)
insert into Score (Name,Email,Score1)values('Sam','Sam#pluto.com',76)
insert into Score (Name,Email,Score1)values('Nancy','Nancy#pluto.com',12)
insert into Score (Name,Email,Score1)values('Tina','Tina#pluto.com',56)
insert into Score (Name,Email,Score1)values('John','John#mars.com',98)
insert into Score (Name,Email,Score1)values('Peter','Peter#mars.com',12)
insert into Score (Name,Email,Score1)values('Nina','Nina#mars.com',45)
insert into Score (Name,Email,Score1)values('Joseph','Joseph#mars.com',87)
insert into Score (Name,Email,Score1)values('Tom','Tom#mars.com',67)
insert into Score (Name,Email,Score1)values('Sam','Sam#mars.com',99)
insert into Score (Name,Email,Score1)values('Nancy','Nancy#mars.com',33)
insert into Score (Name,Email,Score1)values('Tina','Tina#mars.com',23)
insert into Score (Name,Email,Score2)values('John','John#pluto.com',86)
insert into Score (Name,Email,Score2)values('Peter','Peter#pluto.com',56)
insert into Score (Name,Email,Score2)values('Nina','Nina#pluto.com',98)
insert into Score (Name,Email,Score2)values('Joseph','Joseph#pluto.com',78)
insert into Score (Name,Email,Score2)values('Tom','Tom#pluto.com',12)
insert into Score (Name,Email,Score2)values('Sam','Sam#pluto.com',45)
insert into Score (Name,Email,Score2)values('Nancy','Nancy#pluto.com',76)
insert into Score (Name,Email,Score2)values('Tina','Tina#pluto.com',78)
insert into Score (Name,Email,Score2)values('John','John#mars.com',98)
insert into Score (Name,Email,Score2)values('Peter','Peter#mars.com',45)
insert into Score (Name,Email,Score2)values('Nina','Nina#mars.com',76)
insert into Score (Name,Email,Score2)values('Joseph','Joseph#mars.com',12)
insert into Score (Name,Email,Score2)values('Tom','Tom#mars.com',84)
insert into Score (Name,Email,Score2)values('Sam','Sam#mars.com',27)
insert into Score (Name,Email,Score2)values('Nancy','Nancy#mars.com',54)
insert into Score (Name,Email,Score2)values('Tina','Tina#mars.com',50)
insert into Score (Name,Score1)values('Jack',10)
insert into Score (Email,Score2)values('Maya#moon.com',20)
select Name, Email, isnull(SUM(Score1),'') as Score1, isnull(SUM(Score2),'') as Score2
from Score
group by Name, Email