group by with 2 conditions total and expiry count in postgres [closed] - postgresql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I need the total and expiry date group by taluk name and taluk_code. kindly help me to group by taluk name and taluk_code.
With Total and Expiry Date:
SELECT
l.taluk_code, t.taluk_name,
count (serial_number) as Total
FROM
license_register l, taluk t where l.taluk_code=t.taluk_code and rev_district_code='17' and
l.validity_to <= now()
group by l.taluk_code, t.taluk_name
without Expiry date
SELECT
l.taluk_code, t.taluk_name,
count (serial_number) as Total
FROM
license_register l, taluk t where l.taluk_code=t.taluk_code and rev_district_code='17'
group by l.taluk_code, t.taluk_name
kindly combine and give the solution
"taluk_code" "taluk_name" "total"
"01 " "Ariyalur" 2
"04 " "Sendurai" 1
"taluk_code" "taluk_name" "ExpiryCount"
"01 " "Ariyalur" 2
"04 " "Sendurai" 1
I need the below output:
"taluk_code" "taluk_name" "total" "ExpiryCount"
"01 " "Ariyalur" 2 1
"04 " "sendurai" 1 4

SELECT
l.taluk_code,
t.taluk_name,
count(serial_number) AS total,
count(serial_number) FILTER (WHERE l.validity_to <= now()) AS expiry_count
FROM
license_register l,
taluk t
WHERE
l.taluk_code = t.taluk_code
AND rev_district_code = '17'
GROUP BY
l.taluk_code,
t.taluk_name;
filter tutorial: https://modern-sql.com/feature/filter#footnote-0

Related

Mixing DISTINCT with GROUP_BY Postgres

I am trying to get a list of:
all months in a specified year that,
have at least 2 unique rows based on their date
and ignore specific column values
where I got to is:
SELECT DATE_PART('month', "orderDate") AS month, count(*)
FROM public."Orders"
WHERE "companyId" = 00001 AND "orderNumber" != 1 and DATE_PART('year', ("orderDate")) = '2020' AND "orderNumber" != NULL
GROUP BY month
HAVING COUNT ("orderDate") > 2
The HAVING_COUNT sort of works in place of DISTINCT insofar as I can be reasonably sure that condition filters the condition of data required.
However, being able to use DISTINCT based on a given date within a month would return a more reliable result. Is this possible with Postgres?
A sample line of data from the table:
Sample Input
"2018-12-17 20:32:00+00"
"2019-02-26 14:38:00+00"
"2020-07-26 10:19:00+00"
"2020-10-13 19:15:00+00"
"2020-10-26 16:42:00+00"
"2020-10-26 19:41:00+00"
"2020-11-19 20:21:00+00"
"2020-11-19 21:22:00+00"
"2020-11-23 21:10:00+00"
"2021-01-02 12:51:00+00"
without the HAVING_COUNT this produces
month
count
7
1
10
2
11
3
Month 7 can be discarded easily as only 1 record.
Month 10 is the issue: we have two records. But from the data above, those records are from the same day. Similarly, month 11 only has 2 distinct records by day.
The output should therefore be ideally:
month
count
11
2
We have only two distinct dates from the 2020 data, and they are from month 11 (November)
I think you just want to take the distinct count of dates for each month:
SELECT
DATE_PART('month', orderDate) AS month,
COUNT(DISTINCT orderDate::date) AS count
FROM Orders
WHERE
companyId = 1 AND
orderNumber != 1 AND
DATE_PART('year', orderDate) = '2020'
GROUP BY
DATE_PART('month', orderDate)
HAVING
COUNT(DISTINCT orderDate::date) > 2;

need auto increment select query without sequence in oracle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Kindly help,my question is in attached screenshot.
Given your table and sample data:
CREATE TABLE x ( y ) AS
SELECT 10 * LEVEL FROM DUAL CONNECT BY LEVEL <= 3;
You can update the column to add 1 five times:
UPDATE x
SET y = y + 1 + 1 + 1 + 1 + 1;
(You could simplify it by just adding 5.)
Then:
SELECT * FROM x;
Outputs:
| Y |
| -: |
| 15 |
| 25 |
| 35 |
db<>fiddle here

How to write Joins in loop in SQL server? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a below kind of data in my table and i need to get the below kind of output.
U.Id Current_Id Previous_Id Date reason
01 aa null 21 xyz
01 bb aa 24 yxz
01 cc bb 24 out
01 dd cc 25 tot
01 aaa null 11 yyz
01 bbb aaa 12 zyy
First four records are one set and next two records are one set. we can identify this by current_id and Previous_ID columns. I need below kind of Output.
Output :
O1 - aa - 21 - 25 - tot
01 - aaa - 11 - 12 -zyy
For each set i need first and last record dates. How can i achieve this in ms sql?
You can use a recursive Common Table Expression (rCTE) to recurse through the data, and then get the respective MIN and MAX:
WITH YourTable AS(
SELECT *
FROM (VALUES('01','aa',NULL,21),
('01','bb','aa',24),
('01','cc','bb',24),
('01','dd','cc',25),
('01','aaa',NULL,11),
('01','bbb','aaa',12))V([U.Id],Current_Id,Previous_Id,[Date])), --A column with a . in the name is a bad idea.
--Date is an odd name for something that is clearly an int
--Solution
rCTe AS(
SELECT YT.[U.Id],
YT.Current_Id,
YT.Previous_Id,
YT.[Date],
YT.Current_Id AS Start_Id
FROM YourTable YT
WHERE Previous_ID IS NULL
UNION ALL
SELECT YT.[U.Id],
YT.Current_Id,
YT.Previous_Id,
YT.[Date],
r.Start_Id
FROM YourTable YT
JOIN rCTE r ON YT.Previous_Id = r.Current_Id)
SELECT r.[U.Id],
r.Start_Id AS Current_Id,
MIN(r.[Date]) AS StartDate,
MAX(r.[Date]) AS EndDate
FROM rCTE r
GROUP BY r.[U.Id],
r.Start_Id;

Postgresql : Average over a limit of Date with group by

I have a table like this
item_id date number
1 2000-01-01 100
1 2003-03-08 50
1 2004-04-21 10
1 2004-12-11 10
1 2010-03-03 10
2 2000-06-29 1
2 2002-05-22 2
2 2002-07-06 3
2 2008-10-20 4
I'm trying to get the average for each uniq Item_id over the last 3 dates.
It's difficult because there are missing date in between so a range of hardcoded dates doesn't always work.
I expect a result like :
item_id MyAverage
1 10
2 3
I don't really know how to do this. Currently i manage to do it for one item but i have trouble extending it to multiples items :
SELECT AVG(MyAverage.number) FROM (
SELECT date,number
FROM item_list
where item_id = 1
ORDER BY date DESC limit 3
) as MyAverage;
My main problem is with generalising the "DESC limit 3" over a group by id.
attempt :
SELECT item_id,AVG(MyAverage.number)
FROM (
SELECT item_id,date,number
FROM item_list
ORDER BY date DESC limit 3) as MyAverage
GROUP BY item_id;
The limit is messing things up there.
I have made it " work " using between date and date but it's not working as i want because i need a limit and not an hardcoded date..
Can anybody help
You can use row_number() to assign 1 to 3 for the records with the last date for an ID an then filter for that.
SELECT x.item_id,
avg(x.number)
FROM (SELECT il.item_id,
il.number,
row_number() OVER (PARTITION BY il.item_id
ORDER BY il.date DESC) rn
FROM item_list il) x
WHERE x.rn BETWEEN 1 AND 3
GROUP BY x.item_id;

T-SQL: How to count records that fall within given date range? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a table that has among other columns, effective and expiration date. Given the date range, I need to count how many are active for each month within that date range. Any ideas how to do this?
Edit:
It seems that many are missing the point of the question. It is not a simple where query. I have records that have different effective and expiration dates. I want to run a query, that given the date range, it returns me the count, for each month within the given range, number of records. So if I give it date range from august 2012 - august 2013, it needs to give me for each month, record that where active based on therr effective and expiration date.
An example of the data
EffDt ExpDt Id
08/01/2012 10/01/2012 1
08/01/2012 09/31/2012 2
10/01/2012 01/01/2013 3
11/01/2012 08/01/2013 4
01/01/2013 09/01/2013 5
if the date range is 07/01/2012 - 05/01/2013 I should get
Date Count
07-2012 0 - there are no active records in 07-2012
08-2012 2 - id 1 and 2 are active in 08-2012
09-2012 2 - id 1,2 are active in 09-2012
10-2012 2 - id 1,3 are active but 2 is no longer active in 10-2012
11-2012 3 - id 1,3,4 are now active, 2 is not longer active
12-2012 3
01-2013 4
02-2013 3 - id 3 is now no longer active
03-2013 3
04-2013 3
05-2013 3
The easiest way for you to get this result would be to create a calendar table with dates, then you can join the calendar table to your current table to determine what falls in the date range.
If you don't have a calendar table, then you can use a recursive CTE to generate the list of dates:
;with cte (dt) as
(
select cast('2012-07-01' as date)
union all
select dateadd(m, 1, dt)
from cte
where dateadd(m, 1, dt) <= '2013-05-01'
)
select dt
from cte
See SQL Fiddle with Demo.
You can use this CTE an join to your table and get the count for each row in the range:
;with cte (dt) as
(
select cast('2012-07-01' as date)
union all
select dateadd(m, 1, dt)
from cte
where dateadd(m, 1, dt) <= '2013-05-01'
)
select c.dt, count(t.id) TotalCount
from cte c
left join yourtable t
on c.dt >= t.effdt
and c.dt <= t.expdt
group by c.dt
See SQL Fiddle with Demo