Hi I'm struggling with the following problem.
I have two tables one with employee info name and id's the second is a time-sheet table.
Name date_entered durhr part_no TotalHours
Ajay Singh 2012-02-06 09:58:02.440 0.25 ADM-DESKTOP 43
Ajay Singh 2012-02-08 07:47:50.720 0.25 ADM-DESKTOP 43
Ajay Singh 2012-02-08 11:15:17.110 0.25 ADM-DESKTOP 43
Ajay Singh 2012-02-09 09:35:20.360 0.25 ADM-DESKTOP 43
Ajay Singh 2012-02-06 09:57:43.373 0.5 ADM-DESKTOP 43
Ajay Singh 2012-02-06 09:57:52.407 0.5 ADM-DESKTOP 43
Ajay Singh 2012-02-09 09:35:09.193 0.5 ADM-DESKTOP 43
Ajay Singh 2012-02-10 14:23:38.893 0.5 ADM-DESKTOP 43
Ajay Singh 2012-02-09 09:16:36.327 0.75 AD-CREATE 43
Ajay Singh 2012-02-09 09:24:05.170 1 AD-CREATE 43
Ajay Singh 2012-02-10 11:49:16.670 1 ADM-DESKTOP 43
Ajay Singh 2012-02-10 14:23:27.577 1 ADM-DESKTOP 43
Ajay Singh 2012-02-08 11:14:02.247 1 ADM-DESKTOP 43
What I need is to total the day up not the week my query totals the week.. here is what I have so far
select (e.Firstname + ' ' + e.Lastname) as name, p.date_entered, p.durhr, p.part_no, th.TotalHours
from ProdUseExt p
JOIN Employee as e on e.employeeid = p.employeeid
JOIN (select employeeid, sum(durhr) as totalhours from ProdUseExt where date_Entered between '2/5/2012' and '2/11/2012' group by employeeid) as th on th.employeeid = p.employeeid
where e.OSVendor ='myVendor'
and p.date_Entered between '2/5/2012' and '2/11/2012'
group by e.Firstname,e.Lastname, p.date_entered, p.durhr, p.part_no, th.TotalHours
order by e.Firstname,e.lastname, p.durhr
Try this,
CONVERT(DATE, ) will convert it to the date data type removing the time.
The subquery join is also unnecessary
select (e.Firstname + ' ' + e.Lastname) as name, CONVERT(DATE, p.date_entered) p_date_entered, p.part_no, sum(P.durhr) as TotalHours
from ProdUseExt p
JOIN Employee as e on e.employeeid = p.employeeid
where e.OSVendor ='myVendor'
and p.date_Entered between '2/5/2012' and '2/11/2012'
group by e.Firstname,e.Lastname, CONVERT(DATE, p.date_entered), p.part_no
order by e.Firstname,e.lastname, p.durhr
Related
Could someone help me with cte expresion? I have a table:
old_card
new_card
dt
111
555
2020-01-09
222
223
2020-02-10
333
334
2020-03-11
444
222
2020-04-12
555
666
2020-05-12
666
777
2020-06-13
777
888
2020-07-14
888
0
2020-08-15
999
333
2020-09-16
223
111
2020-10-16
I need to get all the changes of old_card to a new_card, since old_card number 111 to a new_card number 0. So I must get 5 records from this table having only a new_card = 0 as input parameter
old_card
new_card
dt
111
555
2020-01-09
555
666
2020-05-12
666
777
2020-06-13
777
888
2020-07-14
888
0
2020-08-15
I think of to do it using cte, but I get all the records from the source table and can't understand why. Here is my cte:
;with cte as(
select
old_card,
new_card,
dt
from
cards_transfer
where
new_card = 0
union all
select
t1.old_card,
t1.new_card,
t1.dt
from
cards_transfer t1
inner join
cte on cte.old_card = t1.new_card)
But I get 8 rows instead. Can someone tell me please what I did wrong?
You said you wanted from 111 onwards. So you need to add that "stop" condition
where cte.old_card <> 111
;with cte as(
select
old_card,
new_card,
dt
from
cards_transfer
where
new_card = 0
union all
select
t1.old_card,
t1.new_card,
t1.dt
from
cards_transfer t1
inner join
cte on cte.old_card = t1.new_card
where cte.old_card <> 111
)
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 a table that has data of user_id and the timestamp they joined.
If I need to display the data month-wise I could just use:
select
count(user_id),
date_trunc('month',(to_timestamp(users.timestamp))::timestamp)::date
from
users
group by 2
The date_trunc code allows to use 'second', 'day', 'week' etc. Hence I could get data grouped by such periods.
How do I get data grouped by "n-day" period say 45 days ?
Basically I need to display number users per 45 day period.
Any suggestion or guidance appreciated!
Currently I get:
Date Users
2015-03-01 47
2015-04-01 72
2015-05-01 123
2015-06-01 132
2015-07-01 136
2015-08-01 166
2015-09-01 129
2015-10-01 189
I would like the data to come in 45 days interval. Something like :-
Date Users
2015-03-01 85
2015-04-15 157
2015-05-30 192
2015-07-14 229
2015-08-28 210
2015-10-12 294
UPDATE:
I used the following to get the output, but one problem remains. I'm getting values that are offset.
with
new_window as (
select
generate_series as cohort
, lag(generate_series, 1) over () as cohort_lag
from
(
select
*
from
generate_series('2015-03-01'::date, '2016-01-01', '45 day')
)
t
)
select
--cohort
cohort_lag -- This worked. !!!
, count(*)
from
new_window
join users on
user_timestamp <= cohort
and user_timestamp > cohort_lag
group by 1
order by 1
But the output I am getting is:
Date Users
2015-04-15 85
2015-05-30 157
2015-07-14 193
2015-08-28 225
2015-10-12 210
Basically The users displayed at 2015-03-01 should be the users between 2015-03-01 and 2015-04-15 and so on.
But I seem to be getting values of users upto a date. ie: upto 2015-04-15 users 85. which is not the results I want.
Any help here ?
Try this query :
SELECT to_char(i::date,'YYYY-MM-DD') as date, 0 as users
FROM generate_series('2015-03-01', '2015-11-30','45 day'::interval) as i;
OUTPUT :
date users
2015-03-01 0
2015-04-15 0
2015-05-30 0
2015-07-14 0
2015-08-28 0
2015-10-12 0
2015-11-26 0
This looks like a hot mess, and it might be better wrapped in a function where you could use some variables, but would something like this work?
with number_of_intervals as (
select
min (timestamp)::date as first_date,
ceiling (extract (days from max (timestamp) - min (timestamp)) / 45)::int as num
from users
),
intervals as (
select
generate_series(0, num - 1, 1) int_start,
generate_series(1, num, 1) int_end
from number_of_intervals
),
date_spans as (
select
n.first_date + 45 * i.int_start as interval_start,
n.first_date + 45 * i.int_end as interval_end
from
number_of_intervals n
cross join intervals i
)
select
d.interval_start, count (*) as user_count
from
users u
join date_spans d on
u.timestamp >= d.interval_start and
u.timestamp < d.interval_end
group by
d.interval_start
order by
d.interval_start
With this sample data:
User Id timestamp derived range count
1 3/1/2015 3/1-4/15
2 3/26/2015 "
3 4/4/2015 "
4 4/6/2015 " (4)
5 5/6/2015 4/16-5/30
6 5/19/2015 " (2)
7 6/16/2015 5/31-7/14
8 6/27/2015 "
9 7/9/2015 " (3)
10 7/15/2015 7/15-8/28
11 8/8/2015 "
12 8/9/2015 "
13 8/22/2015 "
14 8/27/2015 " (5)
Here is the output:
2015-03-01 4
2015-04-15 2
2015-05-30 3
2015-07-14 5
I want to find out how many days are left until "End_date" is reached in postgres. What will be equivalent for following in postgres?
Days_Left = Column in table - Today's date
GREATEST(INT4(CEIL(("NUMERIC"(DATE_PART('EPOCH'::"VARCHAR", (T1.End_date - "TIMESTAMP"(DATE('now'::"VARCHAR"))))) / '86400'::"NUMERIC"))), 0) AS DAYS_LEFT
--Thanks I tried your suggestion but did not get expected result.
Expected Result -- if use GREATEST(INT4(CEIL(("NUMERIC"(DATE_PART('EPOCH'::"VARCHAR", (CA.END_DATE - "TIMESTAMP"(DATE('now'::"VARCHAR"))))) / '86400'::"NUMERIC"))), 0)
End_date Days_left
2014-11-01 03:59:00 47
2016-01-01 04:59:59 473
2017-01-01 06:59:59 839
2014-12-31 22:59:00 107
Result - date(end_date) - date(current_date)
End_date Days_Left
2014-11-01 03:59:00 46
2016-01-01 04:59:59 472
2017-01-01 06:59:59 838
2014-12-31 22:59:00 106
Result - if use (end_date - current_date)
End_date Days_Left
2014-11-01 03:59:00 46 days 03:59
2016-01-01 04:59:59 472 days 04:59:59
2017-01-01 06:59:59 838 days 06:59:59
2014-12-31 22:59:00 106 days 22:59
Thanks
Sandy
If column_in_table is defined as a DATE you can use this:
select column_in_table - current_date as days_left
from the_table
Edit
As end_date is a timestamp the above expression will return an interval not an integer.
If you don't care about the hours and minutes left, casting the timestamp to a date should work:
select end_date::date - current_date as days_left
from the_table
MySQL client version: 5.0.24a
Hey Folks,
I have a table WorkOrders_errors that looks like this:
ID CO CAR NAME CAN BLN INDATE MODDATE EX
66897 461 57 KKLU KKLUSH9862088 AKLU6013312 1/27/2014 1:00 1/27/2014 1:00 -1
60782 461 57 KKLU KKLUHB21629300 AKLU6501153 1/26/2014 22:00 1/26/2014 22:00 1
74188 461 57 KKLU KKLUHB21629300 AKLU6501153 1/27/2014 10:00 1/27/2014 10:00 1
66645 461 57 KKLU KKLUSH8222080 AKLU6501744 1/26/2014 21:45 1/26/2014 21:45 1
63307 461 126 ZIMU ZIMUGOA321986 AMFU3037671 1/27/2014 1:15 1/27/2014 1:15 1
65081 461 24 CMDU CMDUAU1337382 AMFU3043761 1/26/2014 21:30 1/26/2014 21:30 1
72660 461 24 CMDU CMDUAU1337382 AMFU3043761 1/27/2014 9:30 1/27/2014 9:30 1
I need only the records with the most recent MODDATE, ie ID Record 74188, not 60782.
I have tried this a few ways, but without success. Most recently tried
SELECT * FROM (
SELECT * FROM WorkOrders_errors ORDER BY ModDate DESC) as tmp
GROUP BY can
ORDER BY can
'ALSO TRIED
SELECT t1.*
FROM WorkOrders_errors t1
WHERE t1.Can = (SELECT t2.Can
FROM WorkOrders_errors t2
WHERE t2.Can = t1.Can
ORDER BY t2.Can DESC
LIMIT 1)
These both seem to take a Huge amount of resources/time. The table only has about 80,000 rows.
Thanks anyone!