how to do math on results from two querys - sql

i have to results that came from each query
first query:
SELECT [xcv], COUNT( * ) AS Total
FROM [my_table]
GROUP BY [xcv]
second query:
SELECT [xcv], COUNT( * ) AS Total
FROM [my_table]
WHERE=[result]='ok'
GROUP BY [xcv]
what i want to do is to get the value of -> [ (first_query/second query) *100 ]
And i want results for each [xcv] in my table...
any idea how i can do it ?
thank you all

SELECT t1.[xcv],
(t1.total/t2.total)*100
FROM (SELECT [xcv],
COUNT( * ) AS Total
FROM [my_table]
GROUP BY
[xcv]
) t1 JOIN (
SELECT [xcv],
COUNT( * ) AS Total
FROM [my_table]
WHERE [result]='ok'
GROUP BY
[xcv]
) t2
ON t1.[xcv]=t2.[xcv]
I also removed the = sign from WHERE=[result]='ok' - I think you mistyped it in.

One query:
SELECT [xcv],
(SUM(CASE
WHEN [result] = 'ok'
THEN 1
ELSE 0
END
)/COUNT( * )
) * 100
FROM [my_table]
GROUP BY [xcv]

Related

How pivot a sum of values

i have a question, How can i pivot an aggregation result to look like ..
I'm trying to pivot a simple aggregation using this query first:
select sync_action action, count(sync_action) total
FROM my_table
group by sync_action
and to pivot the table i'm using:
select * from (
select sync_action , count(sync_action) total
FROM my_table
group by sync_action )
pivot
(
count(sync_action)
for sync_action in ('delete','create')
)
;
and i don't know where is the error, because the result is:
the idea is have the same as the first image.
Can somebody help me?
Best regards
I would just use conditional aggregation:
select
sum(case when sync_action = 'delete' then total else 0 end) sum_delete,
sum(case when sync_action = 'create' then total else 0 end) sum_create
from mytable
where sync_action in ('delete', 'create')
You don't need to do group by, just do like
SELECT *
FROM mytable
pivot
( COUNT(sync_action)
FOR sync_action IN('delete','create')
);
In your query you need "SUM of total" instead of "Count of sync_action" in pivot section. Others are ok. If you use count, in your case you will always get 1.
select * from (
select sync_action , count(sync_action) total
FROM my_table
group by sync_action ) as p
pivot
(
sum(p.total)
for p.sync_action in ("delete","create")
)pt
I think you just want:
select sum(case when sync_action = 'delete' then 1 else 0 end) as delete,
sum(case when sync_action = 'create' then 1 else 0 end) as create
from my_table;
I don't see how pivot helps at all with what you want to do.

How to divide the count() of two seperate queries in DB2

So I have
select count(*) from ( "query1")
select count(*) from ( "query2")
I want to divide the two and get the floating point result.
I was told to use something like this
SELECT (COUNT(smtg) * 1.0) / COUNT(smtg)
But Im not sure
You can just do:
select q1.cnt * 1.0 / q2.cnt
from (select count(*) as cnt from ( "query1") ) q1 cross join
(select count(*) as cnt from ( "query2") ) q2;
Or, if you prefer:
select ( (select count(*) from ( "query1")) * 1.0 /
(select count(*) from ( "query2"))
)
from sysibm.sysdummy1;
other solution (be carefull to not divide by 0)
with
query1 as (select count(*) as nb1 from ( "query1")),
query2 as (select count(*) as nb2 from ( "query2"))
select case when nb2=0 then null else nb1* 1.0/nb2 end as Result
from query1, query2

How to cast to varchar and add % sign to SQL

Ive been pulling my hair out to get around this..how can i convert this to a varchar and add the % symbol to the results?
I have placed the code below of the query and have tried to cast it but keep getting errors that i don't know how to fix. Can someone pleae make some suggestions on how to either simplify and prevent divide by zero issues also.
select 'Conversion Rate' as Type,
(
SELECT
(
COALESCE
(
CAST(CAST(nullif(t.ConvertedTrials,0) AS NUMERIC(18,2)) / (CAST(nullif(t.UnconvertedTrials,0) AS NUMERIC(18,2))) * 100 as decimal(10,2))
,
0
)
) AS Percentage
FROM (
SELECT
UnconvertedTrials = (
SELECT count(*) FROM
(
select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64' Group by membership.memberid
) as a
),
ConvertedTrials = (
SELECT count(*) FROM
(
SELECT membership.memberid
FROM Membership, Package
WHERE membership.PackageId = Package.Id
AND Package.PackageTypeId != 1
AND membership.memberid in (select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64')
Group by membership.memberid
) as b
)
) as t
) as Total
select 'Conversion Rate' as Type,
RTRIM(CAST((
SELECT
(
COALESCE
(
CAST(CAST(nullif(t.ConvertedTrials,0) AS NUMERIC(18,2)) / (CAST(nullif(t.UnconvertedTrials,0) AS NUMERIC(18,2))) * 100 as decimal(10,2))
,
0
)
) AS Percentage
FROM (
SELECT
UnconvertedTrials = (
SELECT count(*) FROM
(
select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64' Group by membership.memberid
) as a
),
ConvertedTrials = (
SELECT count(*) FROM
(
SELECT membership.memberid
FROM Membership, Package
WHERE membership.PackageId = Package.Id
AND Package.PackageTypeId != 1
AND membership.memberid in (select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64')
Group by membership.memberid
) as b
)
) as t
) AS NVARCHAR(50))) + '%' as Total
your actual percentage is quite a large sub-query, it's hard to see how to enclose it in brackets - I had about 10 goes - is it any good yet?
maybe it's easier to wrap the whole query in another select e.g
SELECT A,B FROM (<your query>) AS AQUERY
(which should give exactly same results as your query) - THEN work on formatting B in the outer query
Here is my second way of doing it
SELECT ORIGINAL.Type, RTRIM(CAST(ORIGINAL.Total as varchar(80))) + '%' AS Total
FROM
(
select 'Conversion Rate' as Type,
(
SELECT
(
COALESCE
(
CAST(CAST(nullif(t.ConvertedTrials,0) AS NUMERIC(18,2)) / (CAST(nullif(t.UnconvertedTrials,0) AS NUMERIC(18,2))) * 100 as decimal(10,2))
,
0
)
) AS Percentage
FROM (
SELECT
UnconvertedTrials = (
SELECT count(*) FROM
(
select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64' Group by membership.memberid
) as a
),
ConvertedTrials = (
SELECT count(*) FROM
(
SELECT membership.memberid
FROM Membership, Package
WHERE membership.PackageId = Package.Id
AND Package.PackageTypeId != 1
AND membership.memberid in (select memberid from membership where discountcodeid = '79a7fd7c-9ebe-4ec0-8ac0-95ea274f1f64')
Group by membership.memberid
) as b
)
) as t
) as Total
) ORIGINAL

sql Calculating a percentage for a group

iam trying to get this
1.select all the rows that [result] != Success
2.count each unique [result]
3.tell me how much % each [result] take from all [result]
seems like the first two works fine but the last column is always '0'
this is my query:
SELECT [result] , COUNT( * ) AS Total,(
COUNT( * ) /
(
SELECT COUNT( * )
FROM[my_table]
)
) * 100 AS '%'
FROM [my_table]
WHERE ([result]!='Success')
GROUP BY [result]
this is the result:
tank you all!
You need to convert the numbers to decimals or floating point numbers, or you will be operating on integers.
SELECT [result] , COUNT( * ) AS Total,
(1.0 * COUNT( * ) / (
SELECT COUNT( * )
FROM[my_table]
)
) * 100 AS '%'
FROM [my_table]
WHERE ([result]!='Success')
GROUP BY [result]
I would try this:
select [result], count(1) over (partition by [result]),
(count(1) over (partition by [result]) * 100/count(*))
FROM [my_table]
WHERE ([result]!='Success')
I think your code works though, I think you need to * 100 before you divide, I think you are finding an issue with using ints and having a value < 1, so it rounds to 0.
This should do it, as long as the first number is a float the result will be a float
SELECT [result]
,COUNT(*) AS Total
,CAST((COUNT(*)AS FLOAT) / (SELECT COUNT(*) FROM [my_table])) AS '%'
FROM [my_table]
WHERE ([result]!='Success')
GROUP BY [result]

How can i select all other records without top five records?

Location TotalRevenue LocationID
Orugodawatta 10059135.78 OR
Kohuwala 7058537.73 KH
Koswaththa 6717136.02 KW
Havelock Town 5748932.59 HT
Negombo 5193678.33 NG
Induruwa 3017552.74 IA
Absdhku 2254281.21 AB
I have a table in sql server 2008. how can i select all other rows without top 5 records?
if my table had 100 records i can selece all other 95 records without top 5 records. please help me
Try this.
SELECT * FROM MyTable WHERE LOCATION NOT IN (SELECT TOP 5 LOCATION FROM MyTable)
SELECT TOP (SELECT Count(*) - 5 FROM tableName WHERE YOUR_WHERE_CLAUSE) *
FROM tableName
WHERE YOUR_WHERE_CLAUSE
ORDER BY COLUMN_NAME DESC
suppose you get Top 5 records by Query
select Top 5 *
from Table_name
order by Location desc
So to get your 95 records
select Top 100 *
from Table_name
order by Location desc
except
select Top 5 *
from Table_name
order by Location desc
Select all in first CTE, select top 5 in second, and just use EXCEPT :
WITH CTE_ALL AS
(
SELECT
Location ,
SUM(SellingPrice) AS [Total Revenue] ,
LocationID
FROM BI_LocWiseTopItems
WHERE ( GRNDate BETWEEN '' AND GETDATE() )
GROUP BY Location ,
LocationID
)
, CTE_TOP5 AS
(
SELECT TOP 5 * FROM CTE_ALL
ORDER BY [Total Revenue]
)
SELECT * FROM CTE_ALL
EXCEPT
SELECT * FROM CTE_TOP5
SQLFiddle DEMO - Simplified for CTE
; WITH top5 AS (
SELECT TOP 5
Location
, TotalRevenue
, LocationID
FROM your_table
ORDER
BY TotalRevenue DESC
)
SELECT Location
, TotalRevenue
, LocationID
FROM your_table
EXCEPT
SELECT Location
, TotalRevenue
, LocationID
FROM top5
SELECT * FROM tableName
EXCEPT (SELECT TOP(5)* FROM tableName )
Your entire query. Have a try
SELECT Location,[Total Revenue],LocationID
FROM
(
SELECT Location, SUM(SellingPrice) AS 'Total Revenue', LocationID
FROM BI_LocWiseTopItems
WHERE (GRNDate BETWEEN '' AND GETDATE())
GROUP BY Location, LocationID ORDER BY 'Total Revenue' desc
) AS temp
EXCEPT (
SELECT top(5)Location, SUM(SellingPrice) AS 'Total Revenue', LocationID
FROM BI_LocWiseTopItems
WHERE (GRNDate BETWEEN '' AND GETDATE())
GROUP BY Location, LocationID ORDER BY 'Total Revenue' desc
)