Postgresql select distinct for every unique quarter - sql

I have a query
select * from table_name where sales=0
from which i got the following data
Now, for every quarter I want to select distinct rows so that final data will be like this
I am able to get data for individual quarter with
select distinct quarter, id from some_view where quarter='2020-Q2'
but I am unable to get a single query which can accommodate all quarters data.
please suggest how to proceed

I might be oversimplifying this... But you do seem to want:
select distinct quarter, id from some_view where sales = 0
This gives you all distinct querter/id tuples, out of rows that satisfy the where clause.

Related

how to sum result of count in sql query from one table and one column

I need to sum the result of count of a column in one query.
Is it possible to have like this query?
SELECT sum(count(pro_id)) from jalasat group by pro_id
You have not mentioned which SQL database you are using so you may modify this slightly to fit it to what you are using:
SELECT SUM(cnt) FROM (SELECT COUNT(pro_id) as cnt
FROM jalasat
GROUP BY continent) as t1

BigQuery how to group by after flattening a collection of tables over timerange

I'm trying to do the following:
combine tables over a timerange using FROM TABLE_DATE_RANGE
FLATTEN that set of data
GROUP BY ColumnX
SELECT ColumnX, SUM(ColumnY), SUM(ColumnZ) over only unique ColumnX values.
here's the gist of my query:
SELECT
r.ColumnX
,SUM(r.ColumnY)
,SUM(r.ColumnZ)
FROM
(
SELECT *
FROM FLATTEN(
(
SELECT
ColumnX
,ColumnY
,ColumnZ
FROM TABLE_DATE_RANGE(projectx.events_,
TIMESTAMP('2015-09-01'), TIMESTAMP('2015-09-08'))), my_funky_object
)
WHERE ColumnY > 10
) r
GROUP BY
r.ColumnX
The problem is, I get a number of rows WAY GREATER than the count of unique values of ColumnX should. So I took a step back and simply outputted the GROUP BY - COUNT of ColumnX in order to debug, and I get the following output!
and I get what looks like an intermediate result.
What is happening and how do I ensure that my outer select only aggregates over unique values of ColumnX?
You're getting the count of each distinct value of ColumnX, but you're only showing the count, not the value.
If your goal is to get an accurate count for the number of distinct values, try something like this:
SELECT
COUNT(*) ct
FROM (
SELECT
1
FROM
... rest of your query ...
GROUP BY r.ColumnX
)
That inner query will give you exactly one row (each with the value 1) for each distinct value of ColumnX. The outer select statement will count the number of such rows.
Another alternative is to use EXACT_COUNT_DISTINCT to get the exact count of rows. That's simpler but less scalable than using GROUP BY.

How to count unique rows in Oracle

I have an oracle database table with a lot of columns. I'd like to count the number of fully unique rows. The only thing I could find is:
SELECT COUNT(DISTINCT col_name) FROM table;
This however would require me listing all the columns and I haven't been able to come up with syntax that will do that for me. I'm guessing the reason for that is that this query would be very low performance? Is there a recommended way of doing this?
How about
SELECT COUNT(*)
FROM (SELECT DISTINCT * FROM Table)
It depends on what you are trying to accomplish.
To get a count of the distinct rows by specific column, so that you know what data exists, and how many of that distinct data there are:
SELECT DISTINCT
A_CODE, COUNT(*)
FROM MY_ARCHV
GROUP BY A_CODE
--This informs me there are 93 unique codes, and how many of each of those codes there are.
Another method
--How to count how many of a type value exists in an oracle table:
select A_CDE, --the value you need to count
count(*) as numInstances --how many of each value
from A_ARCH -- the table where it resides
group by A_CDE -- sorting method
Either way, you get something that looks like this:
A_CODE Count(*)
1603 32
1600 2
1605 14
I think you want a count of all distinct rows from a table like this
select count(1) as c
from (
select distinct *
from tbl
) distinct_tbl;
SELECT DISTINCT **col_name**, count(*) FROM **table_name** group by **col_name**

Group by one column and select more than one column Sql query

I need to group by more than one columns but in special case:
I have a table of (Payment_Type,Year,TotalMoney)
I need to get sum of total grouping by payment_type(cash or credit) only and I need to select year in my query how can I do it? my query is:
select Payment_Type,Year,SUM(TotalMoney)
from table
where 1=1
group by Payment_Type,Year
I get an error message as:
Year is not contained in either an aggregate function or the GROUP BY clause
select Payment_Type,Year(YourDateColumn),SUM(TotalMoney)
from table
group by Payment_Type,Year(YourDateColumn)
if your column is named year then
select Payment_Type,[Year],SUM(TotalMoney)
from table
group by Payment_Type,[Year]

SELECT *, COUNT(*) in SQLite

If i perform a standard query in SQLite:
SELECT * FROM my_table
I get all records in my table as expected. If i perform following query:
SELECT *, 1 FROM my_table
I get all records as expected with rightmost column holding '1' in all records. But if i perform the query:
SELECT *, COUNT(*) FROM my_table
I get only ONE row (with rightmost column is a correct count).
Why is such results? I'm not very good in SQL, maybe such behavior is expected? It seems very strange and unlogical to me :(.
SELECT *, COUNT(*) FROM my_table is not what you want, and it's not really valid SQL, you have to group by all the columns that's not an aggregate.
You'd want something like
SELECT somecolumn,someothercolumn, COUNT(*)
FROM my_table
GROUP BY somecolumn,someothercolumn
If you want to count the number of records in your table, simply run:
SELECT COUNT(*) FROM your_table;
count(*) is an aggregate function. Aggregate functions need to be grouped for a meaningful results. You can read: count columns group by
If what you want is the total number of records in the table appended to each row you can do something like
SELECT *
FROM my_table
CROSS JOIN (SELECT COUNT(*) AS COUNT_OF_RECS_IN_MY_TABLE
FROM MY_TABLE)