I'm working on multiple rows with the same Date and don't know how to combine and count for the amount customers.
The raw Data
Here is my expected result
You need to use "group by" like:
select [Date], count(customer_id)
from tablename
group by [Date]
Related
Can you filter a SQL table based on an aggregated value, but still show column values that weren't in the aggregate statement?
My table has only 3 columns: "Composer_Tune", "_Year", and "_Rank".
I want to use SQL to find which "Composer_Tune" values are repeated in each annual list, as well as which ranks the duplicated items had.
Since I am grouping by "Composer_Tune" & "Year", I can't list "_Rank" with my current code.
The image shows the results of my original "find the duplicates" query vs what I want:
Current vs Desired Results
I tried applying the concepts in this Aggregate Subquery StackOverflow post but am still getting "_Rank is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause" from this code:
WITH DUPE_DB AS (SELECT * FROM DB.dbo.[NAME] GROUP BY Composer_Tune, _Year HAVING COUNT(*)>1)
SELECT Composer_Tune, _Year, _Rank
FROM DUPE_DB
You need to explicitly declare the columns used in the Group By expression in the select columns.
You can use the following documentation if you are using transact sql for the proper use of Group By.
Simply join the aggregated resultset to original unit level table:
WITH DUPE_DB AS (
SELECT Composer_Tune, _Year
FROM DB.dbo.[NAME]
GROUP BY Composer_Tune, _Year
HAVING COUNT(*) > 1
)
SELECT n.Composer_Tune, n._Year, n._Rank
FROM DB.dbo.[NAME] n
INNER JOIN DUPE_DB
ON n.Compuser_Tune = DUPE_DB.Composer_Tune
AND n._Year = DUPE_DB._Year
ORDER n.Composer_Tune, n._Year
I need to get the count for each distinct combination of two columns. Here is the query to get the distinct combinations.
SELECT distinct Sales_Cat, Sub_cat
FROM rtx_Sales
It returns all combinations as seen below. I need to add a count field that also shows the number of occurrences for each of the combinations. Is SQL capable of this or should I write a python script to query for each combo?
Use COUNT()
SELECT sales_cat, sub_cat, COUNT(*)
FROM rtx_sales
GROUP BY sales_cat, sub_cat
I've spent an inordinate amount of time this morning trying to Google what I thought would be a simple thing. I need to set up an SQL query that selects multiple columns, but only returns one instance if one of the columns (let's call it case_number) returns duplicate rows.
select case_number, name, date_entered from ticket order by date_entered
There are rows in the ticket table that have duplicate case_number, so I want to eliminate those duplicate rows from the results and only show one instance of them. If I use "select distinct case_number, name, date_entered" it applies the distinct operator to all three fields, instead of just the case_number field. I need that logic to apply to only the case_number field and not all three. If I use "group by case_number having count (*)>1" then it returns only the duplicates, which I don't want.
Any ideas on what to do here are appreciated, thank you so much!
You can use ROW_NUMBER(). For example
select *
from (
select *,
row_number() over(partition by case_number) as rn
) x
where rn = 1
The query above will pseudo-randomly pick one row for each case_number. If you want a better selection criteria you can add ORDER BY or window frames to the OVER clause.
I tried adding two numbers that are present in two different columns but it's not adding up when there are no numbers present in the second column(B). Please find the screenshot of the table and the query I was using to achieve this.
Not getting the value present in COLUMN A in total sales.
The query which I ran but wasn't successful.
SELECT Date,
SUM(sales a) as "total_a",
SUM(sales b) as "total_b",
("total_a"+"total_b") as "total_sales"
FROM data_table
GROUP BY Date;
I would suggest:
SELECT Date,
SUM(sales_a) as "total_a",
SUM(sales_b) as "total_b",
COALESCE(SUM(sales_a, 0) + COALESCE(SUM(sales_b, 0)) as "total_sales"
FROM data_table
GROUP BY Date;
I do know that Amazon Redshift allows the re-use of table aliases -- contravening the SQL standard. However, I find it awkward to depend on that functionality; and it can lead to hard-to-find-errors if your column aliases match existing column names.
You can't reuse column aliases in the same scope, so your query should error. You need to repeat the SUM() expressions.
Then: if one of the sums returns NULL, it propagates the the results of the addition. You can use coalesce() to avoid that:
SELECT Date,
SUM(sales_a) as total_a,
SUM(sales_b) as total_b,
COALESCE(SUM(sales_a), 0) + COALESCE(SUM(sales_b), 0) as total_sales
FROM data_table
GROUP BY Date;
I am very new to Oracle. I am writing a SQL statement against an Oracle 10g database. My table has a date field, DATA_DT, with multiple entries for each date. I want to get SUM of the number field, BQWP, for each of these dates. To get the sum of BQWP for a specific date, my select statement would be:
SELECT SUM(BQWP)
FROM tasks
WHERE TRUNC(DATA_DT) = TO_DATE('07/19/2013', 'mm/dd/yyyy');
Now, how can I loop through all dates and get the SUM for each in a single SQL Query?
You have to aggregate the entries by date:
SELECT TRUNC(DATA_DT),SUM(BQWP)
FROM tasks
GROUP BY TRUNC(DATA_DT)
Look at this document for further information:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions003.htm
You can use GROUP BY
SELECT TRUNC(DATA_DT) as data_dt, SUM(BQWP) as sumBqwp
FROM tasks
GROUP BY TRUNC(DATA_DT)