I have a Table with the below information.
Name status
Test1 Running
Test1 Running
Test1 Running
Test1 Running
Test1 Down
Test2 Running
Test2 Down
SQL Query should return me the results like
Name Availaibility
Test1 80%
Test2 50%
Any suggestions ?
Try this one:
SELECT
Tests2.Name,
(Count(Name)* 100) / (SELECT Count(*) FROM Tests WHERE Tests.Name=Tests2.Name)
FROM Tests AS Tests2
WHERE Status='Running'
GROUP BY Name
Here is the SQL Fiddle, where I tested it.
select Name,
(sum(case when status='Running' then 1 else 0 end)/sum(case when 1=1 then 1 end))*100 as Availabilty
from table
group by Name
Related
Need your help in getting the SQL Query.
1. I have one table which is having following columns
Name Null? Type
------------ -------- ------------
EMP_ID NOT NULL NUMBER(2)
DEP_ID NUMBER(2)
SALARY NUMBER(14,3)
NAME1 VARCHAR2(50)
NAME2 VARCHAR2(50)
JOINING_DATE DATE
Now I want the result - COUNT(1) based on DEP_ID without using GROUP BY .
EXAMPLE :
select DEP_ID,COUNT(1) from unipartemp group by DEP_ID;
DEP_ID COUNT(1)
1 2
2 2
3 1
What is the Query where we should get the same result but we should not use group by ...
Please suggest .
I am assuming that the result u r looking for is the count of the distinct dept_id. Try using the distinct(dept_id) to get the result.
I am trying to sum output of 2 different sql in Redshift.
SQL1:
select count (*) from table1; -- Output : 10
SQL2:
select count (*) from table2; -- Output : 14
I am trying to build a query that would show the total of both these queries. Expected output : 24
Figured the solution:
select ((select count(*) from table1) +
(select count(*) from table2)) as count
I have a UDTF that will always return 1 row of 6 columns
The UDTF has one parameter
I want the 5 columns included in the result set of a query. The table I'm querying has a column that I want to use as the parameter for each row
I have not been able to figure out the correct syntax.
Any suggestions?
The UDTF
create function xxxx.UF_yyyyyy(USERID CHAR(10))
returns table (
p2User char(10),
STATUS CHAR(3),
USED DEC(7, 0),
CREATED DEC(7, 0),
SIGNON DEC(7, 0),
EXCLUDE DEC(7, 0))
language RPGLE
NOT DETERMINISTIC
NO SQL
DISALLOW PARALLEL
NOT FENCED
EXTERNAL NAME 'xxxx/UF_yyyyyy'
PARAMETER STYLE DB2SQL
example
select * from table(xxxx/UF_yyyyyy(CHAR('CMFIRST '))) a
result
P2USER STATUS USED CREATED SIGNON EXCLUDE
---------- ------ ------- ------- ------- -------
CMFIRST ACT 1170926 1150826 1170926 0
Here is an example of a select I tried
SELECT T1.AQABVN, T1.AQA8TX,
(SELECT COUNT(*) FROM fffff T4 WHERE T4.BDABVN = T1.AQABVN) AS SACCMS,
t2.p2User, t2.used
FROM
zzzzz T1
full join table(xxxx.UF_yyyyyy(T1.AQABVN)) t2 on T1.AQABVN = t2.p2User
Result
[SQL0205] Column P2USER not in table T2 in *N.
Got it working
SELECT T1.AQABVN, T1.AQA8TX,
(SELECT COUNT(*) FROM fffff T4 WHERE T4.BDABVN = T1.AQABVN) AS SACCMS,
t2.status, t2.used, t2.created, t2.signon, t2.exclude
FROM
zzzzz T1
join
table(SMLFQA.UF_XAJKUPR(T1.AQABVN)) t2 on T1.AQABVN = t2.p2User
I have a table say tbl_test. In this table I have 2.4 million records. I run the following three queries which are very very slow and frustrating.
select count(*) from tbl_test;
-- 2.4 mil records in ~9 seconds
select count(*) from tbl_test where status = 'active';
-- 2.4 mil records in ~9 seconds
select count(*) from tbl_test where status = 'inactive';
-- 0 records in ~0 seconds
I have created a view say view_tbl_test using the following query:
create view view_tbl_test as
select * from
(select count(*) count_active from tbl_test where status = 'active' ) x,
(select count(*) count_inactive from tbl_test where status = 'inactive' ) y,
(select count(*) count_total from tbl_test) z
Now, I am picking only the single row from the view and its taking the same amount of time like previous.
select * from view_tbl_test limit 1;
Am I doing something wrong here? Is there any way which can make the view to return data in ~0 seconds?
Your statement runs three selects on the table. This can be done with a single statement:
create view view_tbl_test
as
select count(case when status = 'active' then 1 end) as count_active
count(case when status = 'inactive' then 1 end) as count_inactive,
count(*) as count_total
from tbl_test;
This should run in approx. 9seconds as it essentially does the same as your first statement.
The last statement is probably that fast because you have an index on status but as you did not provide the execution plans, this is nearly impossible to tell.
Suppose I have a table as follows: (on DB2 9.7.2)
COL1 COL2 COL3
----------- ---------- ----------
3 4 xyz
3 4 xyz
Now I want to write a query such that only one from these two identical records will be deleted. How can I achieve this?
I can think of :
delete from ;
or
delete from where col1=3;
but both of the above queries will delete both records whereas I want to keep one of them.
If LIMIT doesn't work, this will:
DELETE FROM (SELECT * FROM tbl WHERE col = 3 FETCH FIRST ROW ONLY)
Can't you use a limit clause?
DELETE FROM <table> WHERE <column>=3 LIMIT 1
This is something that served my purpose:
DELETE FROM tabA M
WHERE M.tabAky IN (SELECT tabAky
FROM (SELECT tabAky,
ROW_NUMBER() OVER (PARTITION BY tabAcol1,
tabAcol2,
tabAcoln)
FROM tabA a) AS X (tabAky, ROWNUM)
WHERE ROWNUM> 1) ;
Try This
delete from table A (select row_number() over (partition by col1 order by col1 ) count,* from table) where A.count> 1