SQL for multiple sums and divides under the same query - sql

We want to extract through a sql query the results from the division of each one of multiple sums divided by their counterpart divider, which is also a sum coming from another column under the same DB.
e.g: sum (cross_border_approved_volume)/ sum (cross_border_transaction_count), then sum (domestic_approved_volume)/ sum (domestic_transaction_count), etc.
I'm trying something like the below, but it doesn't seem to have the correct syntax to do this and I'm not sure what to change.
select((select sum(cross_border_approved_volume) as cross_border_approved_volume,
sum(domestic_approved_volume) as domestic_approved_volume,
sum(consumer_approved_volume) as consumer_approved_volume
from rmd_owner.ica_dimension_aggregate
where issuer_id in (41, 42, 43)
and bin8 = -1
/ (select sum(cross_border_transaction_count) as cross_border_transaction_count,
sum(domestic_transaction_count) as domestic_transaction_count,
sum(consumer_transaction_count) as consumer_transaction_count
from rmd_owner.ica_dimension_aggregate
where issuer_id in (41, 42, 43)
and bin8 = -1))) as result;
Thank you.

You could do something like this (SQL Server syntax):
;with cte as (select
sum(cross_border_approved_volume) as cross_border_approved_volume,
sum(domestic_approved_volume) as domestic_approved_volume,
sum(consumer_approved_volume) as consumer_approved_volume,
sum(cross_border_transaction_count) as cross_border_transaction_count,
sum(domestic_transaction_count) as domestic_transaction_count,
sum(consumer_transaction_count) as consumer_transaction_count
from rmd_owner.ica_dimension_aggregate
where issuer_id in (41, 42, 43) and bin8 = -1
)
select
cross_border_approved_volume / cross_border_transaction_count as cross_border_ratio,
domestic_approved_volume / domestic_transaction_count as domestic_approved_ratio,
consumer_approved_volume / consumer_transaction_count as consumer_approved_ratio
from cte
The same can be done with subquery:
select
cross_border_approved_volume / cross_border_transaction_count as cross_border_ratio,
domestic_approved_volume / domestic_transaction_count as domestic_approved_ratio,
consumer_approved_volume / consumer_transaction_count as consumer_approved_ratio
from (select
sum(cross_border_approved_volume) as cross_border_approved_volume,
sum(domestic_approved_volume) as domestic_approved_volume,
sum(consumer_approved_volume) as consumer_approved_volume,
sum(cross_border_transaction_count) as cross_border_transaction_count,
sum(domestic_transaction_count) as domestic_transaction_count,
sum(consumer_transaction_count) as consumer_transaction_count
from rmd_owner.ica_dimension_aggregate
where issuer_id in (41, 42, 43) and bin8 = -1
) x
You can also add the group by clause if you need these values calculated for groups, e.g. per issuer_id.

Related

How to Fill SQL query's parameters automatically?

I have a query like below:
select ct.ID,
ct.B_CODE,
ct.G_F_ID,
ct.T_CD,
ct.STATUS,
ct.E,
? as TYPE_CODE
from ln.CUSTOMER ct
where 1 = 1
AND B_CODE = ?
AND G_F_ID = ?
AND INSERT_DATE > ?
AND INSERT_DATE < ?
AND STATUS in (?, ?, ?, ?);
and value of its parameters are given in an array separated by comma like this:
[50, 1001, 1734508, 2019-01-13, 2019-01-13, 10, 20, 30, 40]
For testing this query all the time I have to put array parameters one by one sequentially in their corresponding place in query instead of ? characters. Final query will be like this:
select ct.ID,
ct.B_CODE,
ct.G_F_ID,
ct.T_CD,
ct.STATUS,
ct.E,
50 as TYPE_CODE
from ln.CUSTOMER ct
where 1 = 1
AND B_CODE = 1001
AND G_F_ID = 1734508
AND INSERT_DATE > 2019-01-13
AND INSERT_DATE < 2019-01-13
AND STATUS in (10, 20, 30, 40);
Sometimes this array is too long (100 items in it) and it is bothersome and doing it without tool is error prone too. Is there any tool or plugin in Intellij IDEA to overcome this issue? Or I have to write a plugin by my own?
Use User parameters to substitute them with actual values at the moment of execution:

How to sort a column in sql based on value B where values in fields are in format A-B-C. I w

I am trying to sort a column in a table where the value in the column is in format 00000197-001-00001 and so on.
I have tried with query
select * from <table name> where <condition> order by column name.
Values are sorted till 00000197-099-001. But after this, the value I am getting is 00000197-1000-001 in my list instead 00000197-100-001.
The result I am getting:
00000197-097-000001
00000197-098-000001
00000197-099-000001
00000197-1000-000001
00000197-100-000001
00000197-1001-000001
00000197-1002-000001
00000197-1003-000001
00000197-1004-000001
00000197-1005-000001
00000197-1006-000001
00000197-1007-000001
00000197-1008-000001
00000197-1009-000001
00000197-1010-000001
00000197-101-000001
00000197-1011-000001
00000197-1012-000001
Expected Result:
00000197-097-000001
00000197-098-000001
00000197-099-000001
00000197-100-000001
00000197-101-000001
.
.
.
00000197-999-000001
00000197-1000-000001
00000197-1001-000001
00000197-1002-000001
00000197-1003-000001
.
.
.
Please suggest the solution.
Many Thanks in advance.
Try this
SELECT *
FROM MyTable1
ORDER BY CAST(SUBSTRING(SUBSTRING(MyCol1, CHARINDEX('-', MyCol1, 0) + 1, 100), 0, CHARINDEX('-', substring(MyCol1, CHARINDEX('-', MyCol1, 0) + 1, 100), 0)) as int)
This works for every string of any length in format X-Y-Z.
Given your sample data, you can do:
order by len(column1), column1
This idea will work in any database, although the length function might be length() rather than len().

How can I achieve in Filemaker results similar to GROUP_CONCAT?

I am doing some work in a program called Filemaker which has a watered down sql function. It does not have the GROUP_CONCAT function and the GROUP BY clause requires all fields mentioned in the SELECT.
Is there another away to get the same results of:
SELECT id_candidate, GROUP_CONCAT(id_award SEPARATOR ', ')
FROM nomination
GROUP BY id_candidate
result
5 113, 116
6 109, 113, 114, 117, 120
7 104, 113
8 113
9 101, 104, 113, 118
10 100, 114, 118
Note: if it needs to done over multiple selects I can store the results of one SELECT and use it in another. So I could use the results of SELECT DISTINCT (id_candidate) and then call it in a …WHERE IN ({var})
You've answered your own question - you can't do it in FileMaker SQL in a single statement - it has very limited support for subqueries.
I could use the results of SELECT DISTINCT (id_candidate) and then
call it in a …WHERE IN ({var})
I would suggest doing the opposite. Store calculated query in FileMaker at the record level e.g. a calculated field called "awards":
ExecuteSQL ( "SELECT id_award
FROM nomination
WHERE id_candidate = ?" ;
"" ; ", " ; nomination::id_candidate )
Then you can run your report:
ExecuteSQL ( "SELECT id_candidate, awards
FROM nomination
GROUP BY id_candidate" ;
"" ; "" )
This should allow you to have the desired output

Understanding a complex SQL code with multiple joins

I have a lengthy query which I am trying to understand. I learned in this forum that the best way to understand complex queries is to split them, check their results separately and then combine. I am pasting a part of code here.
Please help me understand why its giving me an error: Unable to parse query text. Incorrect syntax near the keyword 'AS'.
(SELECT AppID, AppDetailID, AppDetailSavePointID, FieldID, Value AS Acct_Renewed
FROM v_RptAppDetailField_v001
WHERE (FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (AppDetailSavePointID = 0) AND (Value IS NOT NULL)) AS Acct_Renewed
ON
v_RptFlatAppDetail_v001.AppDetailID = Acct_Renewed.AppDetailID AND
v_RptFlatAppDetail_v001.AppID = Acct_Renewed.AppID
When I deleted this part of code from the above code. I got the results. Please help me understand the ON clause here. I am confused as it doesn't mention any join as well. Also please be aware that there was a left outer join right after this code.
AS Acct_Renewed
ON
v_RptFlatAppDetail_v001.AppDetailID = Acct_Renewed.AppDetailID AND
v_RptFlatAppDetail_v001.AppID = Acct_Renewed.AppID
Appreciate your help. Thank you in advance!
Your first block of SQL isn't a complete query. The part you delete is the extra piece that is part of the larger query.
What's happening is that this sql...
SELECT AppID, AppDetailID, AppDetailSavePointID, FieldID, Value AS Acct_Renewed
FROM v_RptAppDetailField_v001
WHERE (FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (AppDetailSavePointID = 0) AND (Value IS NOT NULL)
...is being used as a subquery. You can get more details here: http://technet.microsoft.com/en-us/library/ms189575(v=sql.105).aspx
As a subquery, it is used just like a table. That's why it has AS Acct_Renewed followed by an ON clause. This subquery is being joined to the previous table that you didn't copy using that ON clause.
The following part of the query, which gives you results, independently is called an inline view. You can give a name (alias) for inline views. Here, it is given the name Acct_Renewed.
(SELECT AppID, AppDetailID, AppDetailSavePointID, FieldID, Value AS Acct_Renewed
FROM v_RptAppDetailField_v001
WHERE (FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (AppDetailSavePointID = 0) AND (Value IS NOT NULL)
The following conditions indicate that the inline view is joined with another table / view / inline view named as v_RptFlatAppDetail_v001.
ON
v_RptFlatAppDetail_v001.AppDetailID = Acct_Renewed.AppDetailID AND
v_RptFlatAppDetail_v001.AppID = Acct_Renewed.AppID
References:
Inline views (Oracle) on MacLochlainns Weblog
A Visual Explanation of SQL Joins on Coding Horror
Select AppID,
AppDetailID,
AppDetailSavePointID,
FieldID,
Value AS Acct_Renew
(SELECT
AppID,
AppDetailID,
AppDetailSavePointID,
FieldID,
Value AS Acct_Renewed
FROM v_RptAppDetailField_v001 V
WHERE (V.FieldID IN (- 87, - 88, - 152, - 179, - 258, - 263))
AND (VAppDetailSavePointID = 0) AND (V.Value IS NOT NULL )
FROM v_RptAppDetailField_v001 VV
INNER JOIN v_RptAppDetailField_v001 V
ON
v.AppDetailID = vv.AppDetailID AND
v.AppID = vv.AppID

Strange Behaviour: SQL And operator with multiple IN operators

I am using multiple IN operators with AND in my sql query where clause as given below...
---
where ID in (1, 3, 234, 2332, 2123, 989) AND tag in ('wow', 'wonderful')
But surprisingly behaviour of result seems to be of OR type rather then AND type. What I mean is it is ignoring AND operator...
Can you please explain me why?
I couldn't reproduce the result using SQL Server 2008.
SELECT * FROM
(
SELECT 0 AS ID, 'wow' as Tag
) X
WHERE ID in (1, 3, 234, 2332, 2123, 989) AND tag in ('wow', 'wonderful')
Result:
No records
SELECT * FROM
(
SELECT 1 AS ID, 'wow' as Tag
) X
WHERE ID in (1, 3, 234, 2332, 2123, 989) AND tag in ('wow', 'wonderful')
Result:
ID Tag
1 wow
Check your code again.