I am currently doing to get the count,
Select count(*) from MyTable WHERE ATTEMPT_DATE='01/JUNE/19';
Select count(*) from MyTable WHERE ATTEMPT_DATE='02/JUNE/19';
.
.
Select count(*) from MyTable WHERE ATTEMPT_DATE='30/JUNE/19';
Is there any way to get all the SUM of ( count(*) all dates)
You can use aggregation:
select ATTEMPT_DATE, count(*)
from MyTable
group by ATTEMPT_DATE;
If you want to counts dates within a range, use where:
select count(*)
from MyTable
where ATTEMPT_DATE in ('01/JUNE/19', . . . '30/JUNE/19');
You are using a non-standard date format. If this were a standard date format (or if the column really is a date/datetime value), then I would suggest inequalities rather than in.
Why don't you use direct count(*)
Select count(*) from MyTable WHERE ATTEMPT_DATE BETWEEN '01/JUNE/19' AND '30/JUNE/19'
for datewise count use group by
Select ATTEMPT_DATE, count(*) from MyTable
WHERE ATTEMPT_DATE BETWEEN '01/JUNE/19' AND '30/JUNE/19' group by ATTEMPT_DATE
Cheers!!
Related
Is it possible to order when the data comes from many select and union it together? Such as
In this statement, the vouchers data is not showing in the same sequence as I saved on the database, I also tried it with "ORDER BY v_payments.payment_id ASC" but won't be worked
( SELECT order_id as id, order_date as date, ... , time FROM orders WHERE client_code = '$searchId' AND order_status = 1 AND order_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
UNION
( SELECT vouchers.voucher_id as id, vouchers.payment_date as date, v_payments.account_name as name, ac_balance as oldBalance, v_payments.debit as debitAmount, v_payments.description as descriptions,
vouchers.v_no as v_no, vouchers.v_type as v_type, v_payments.credit as creditAmount, time, zero as tax, zero as freightAmount FROM vouchers INNER JOIN v_payments
ON vouchers.voucher_id = v_payments.voucher_id WHERE v_payments.client_code = '$searchId' AND voucher_status = 1 AND vouchers.payment_date BETWEEN '$start_date' AND '$end_date' ORDER BY v_payments.payment_id ASC , time )
UNION
( SELECT return_id as id, return_date as date, ... , time FROM w_return WHERE client_code = '$searchId' AND w_return_status = 1 AND return_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
Wrap the sub-select queries in the union within a SELECT
SELECT id, name
FROM
(
SELECT id, name FROM fruits
UNION
SELECT id, name FROM vegetables
)
foods
ORDER BY name
If you want the order to only apply to one of the sub-selects, use parentheses as you are doing.
Note that depending on your DB, the syntax may differ here. And if that's the case, you may get better help by specifying what DB server (MySQL, SQL Server, etc.) you are using and any error messages that result.
You need to put the ORDER BY at the end of the statement i.e. you are ordering the final resultset after union-ing the 3 intermediate resultsets
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. See link below:
ORDER BY and LIMIT in Unions
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
I have a simple query but it keeps throwing "Invalid use of group function". It works fine when I remove "count(*)". How can I get the count without using it in lag?
select CreateDate as date, count(*) as count,
lag(count(*), 1) over(order by CreateDate) as previous
from contacts
group by createdate
Hmmm . . . MySQL should allow the use of aggregation functions with window functions. Maybe there is a bug in the parser though.
I think this will work:
select d.*, lag(cnt) over (order by cnt) as previous
from (select CreateDate as date, count(*) as cnt
from contacts
group by CreateDate
) d;
I've been reading through the solutions of similar problems posted here, but none seem to resolve my particular issue.
I currently have a table (CT_JOINED) that includes three columns: an identifer column (TUMOURID), a date column (AVCT_DATE) and another date column (OPDATE).
As an example, the columns for two IDs look as follows:
ID, AVCT_DATE, OPDATE
1, 06-APR-13, 06-APR-13
1, 06-APR-13, 14-JUN-13
1, 06-APR-13, 22-JUN-13
2, 03-APR-14, 10-DEC-15
2, 03-APR-14, 31-DEC-15
What I'm attempting to do is create a column that is equal to the number of unique dates per ID. So the result for ID 1 would be 3 and the result for ID 2 would be 3.
I have attempted a count of distinct values across the two columns, but this does not provide the answers above (it instead reports values of 3 and 2 respectively):
select TUMOURID, COUNT(DISTINCT(AVCT_DATE || OPDATE)) AS COUNT
FROM CT_JOINED
GROUP BY TUMOURID;
The same thing happens if I try and do the same in a new table:
CREATE TABLE CT_DISTINCT AS (
SELECT TUMOURID, COUNT(*) AS COUNT
FROM (
SELECT DISTINCT TUMOURID, AVCT_DATE, OPDATE
FROM CT_JOINED)
GROUP BY TUMOURID
);
I'm at a loss. Is it possible?
You could use:
SELECT TUMOURID, COUNT(DISTINCT d) AS cnt
FROM (select TUMOURID, AVCT_DATE AS d
FROM CT_JOINED
UNION ALL
SELECT TUMOURID, OPDATE AS d) sub
GROUP BY TUMOURID;
Unpivot the data and then use count(distinct) or remove duplicates along the way:
select tumourid, count(*)
from ((select tumourid, avct_date as dte
from ct_joined
) union -- intentional to remove duplicates
(select tumourid, opdate as dte
from ct_joined
)
) t
group by tumourid;
Use UNION to avoid duplicate date & just use count(*) :
SELECT tumourid, COUNT(date)
FROM ((SELECT tumourid, avct_date AS date
FROM ct_joined
) UNION
(SELECT tumourid, opdate
FROM ct_joined
)
) t
GROUP BY tumourid;
All of the answers below work like a charm with a few tweaks to also account for rows with null values. For instance:
SELECT TUMOURID, COUNT(*)
FROM ((SELECT TUMOURID, AVCT_DATE AS DTE
FROM CT_JOINED
WHERE AVCT_DATE IS NOT NULL
) UNION
(SELECT TUMOURID, OPDATE AS DTE
FROM CT_JOINED
WHERE OPDATE IS NOT NULL
)
) T
GROUP BY TUMOURID;
Many thanks.
I have this table in SQL Server:
I want a result like this
I am going to write SQL queries to count the transaction and consolidate every month.
Thank you in advance.
First, you need to edit the column you want to group.
SELECT
A.YYYYMM,
COUNT(*) TxnCount
FROM
(
SELECT
*,
LEFT(TXN_DATE, 6) YYYYMM
FROM
Tbl
) A
GROUP BY
A.YYYYMM
Use Group By and Substring :
SELECT
SUBSTRING(CAST(TXNDate AS VARCHAR(12)),0,9) AS TXNDate,COUNT(*) AS 'TXN Count'
FROM
#tblTest
GROUP BY SUBSTRING(CAST(TXNDate AS VARCHAR(12)),0,9)
You can use GROUP BY clause to count the transaction.
Assuming your TXN Date is of date type, you can use following query:
SELECT CONVERT(VARCHAR(6), TXN_DATE, 112) AS YYYYMM, COUNT(*) AS TXN_COUNT
FROM MyTable
GROUP BY CONVERT(VARCHAR(6), TXN_DATE, 112)
ORDER BY CONVERT(VARCHAR(6), TXN_DATE, 112)
EDIT: since your TXN_DATE is int type, you can use the following
SELECT LEFT(CONVERT(VARCHAR, TXN_DATE), 6) AS YYYYMM, COUNT(*) AS TXN_COUNT
FROM MyTable
GROUP BY LEFT(CONVERT(VARCHAR, TXN_DATE), 6)
ORDER BY LEFT(CONVERT(VARCHAR, TXN_DATE), 6)
I have a simple group by query:
SELECT timestamp, COUNT(users)
FROM my_table
GROUP BY users
How do I add a sum_each_day column that will sum the users count of each row and will aggregate it forward to the next row and so on
The output should be like this:
timestamp | users | sum_each_day
2015-11-27 1 1
2015-11-28 5 6
2015-11-29 3 9
2015-11-30 7 16
Thanks in advance
You could use a sub-query, like this:
SELECT timestamp,
num_users,
(SELECT COUNT(users)
FROM my_table
WHERE timestamp <= main.timestamp) sum_users
FROM (
SELECT timestamp,
COUNT(users) num_users
FROM my_table
GROUP BY timestamp
) main
If you really need this in mysql it'll cost some performance but i believe a sub query with a count will solve it:
SELECT t1.timestamp, count (), select count () from my_table t2 where t2.timestamp <= t1.timestamp From my_table t1 Group by users
If you display this data through a scripting language like PHP it would be easier to keep a counter and display the aggregate per row.
I would do this using variable:
SET #total := 0;
SELECT timestamp, DayCount, (#total := #total + DayCount) AS Total
FROM
(SELECT timestamp, COUNT(users) AS DayCount
FROM my_table
GROUP BY timestamp) AS t1
Fiddler: I am not using your table structure here, but you can get idea
If I understand correclty, this will work:
set #c=0;
SELECT `timestamp`,sum(`users`),(select #c:=#c+sum(`users`))
FROM `my_table`
group by `timestamp`;