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
Related
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.
I've create 3 computed columns as alias and then used the aliased columns to calculate the total cost. This is the query:
SELECT TOP 1000 [Id]
,[QuantityOfProduct]
,[Redundant_ProductName]
,[Order_Id]
,(CASE
WHEN [PriceForUnitOverride] is NULL
THEN [Redundant_PriceForUnit]
ELSE
[PriceForUnitOverride]
END
) AS [FinalPriceForUnit]
,(CASE
WHEN [QuantityUnit_Override] is NULL
THEN [Redundant_QuantityUnit]
ELSE
[QuantityUnit_Override]
END
) AS [FinalQuantityUnit]
,(CASE
WHEN [QuantityAtomic_Override] is NULL
THEN [Redundant_QuantityAtomic]
ELSE
[QuantityAtomic_Override]
END
) AS [Final_QuantityAtomic]
--***THIS IS WHERE THE QUERY CREATES AN ERROR***--
,([QuantityOfProduct]*[FinalPriceForUnit]*
([Final_QuantityAtomic]/[FinalQuantityUnit])) AS [Final_TotalPrice]
FROM [dbo].[ItemInOrder]
WHERE [IsSoftDeleted] = 0
ORDER BY [Order_Id]
The console returns this ERROR message:
Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalPriceForUnit'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'Final_QuantityAtomic'.
Msg 207, Level 16, State 1, Line 55
Invalid column name 'FinalQuantityUnit'.
If I remove the "AS [Final_TotalPrice]" alias computed column, no error occurs, but I need the total price. How can I solve this issue? It seems as the other aliases have not been created when the Final_TotalPrice is reached.
You can't use table aliases in the same select. The normal solution is CTEs or subqueries. But, SQL Server also offers APPLY. (Oracle also supports APPLY and other databases such as Postgres support lateral joins using the LATERAL keyword.)
I like this solution, because you can create arbitrarily nested expressions and don't have to worry about indenting:
SELECT TOP 1000 io.Id, io.QuantityOfProduct, io.Redundant_ProductName,
io.Order_Id,
x.FinalPriceForUnit, x.FinalQuantityUnit, x.Final_QuantityAtomic,
(x.QuantityOfProduct * x.FinalPriceForUnit * x.Final_QuantityAtomic / x.FinalQuantityUnit
) as Final_TotalPrice
FROM dbo.ItemInOrder io OUTER APPLY
(SELECT COALESCE(PriceForUnitOverride, Redundant_PriceForUnit) as FinalPriceForUnit,
COALESCE(QuantityUnit_Override, Redundant_QuantityUnit) as FinalQuantityUnit
COALESCE(QuantityAtomic_Override, Redundant_QuantityAtomic) as Final_QuantityAtomic
) x
WHERE io.IsSoftDeleted = 0
ORDER BY io.Order_Id ;
Notes:
I don't find that [ and ] help me read or write queries at all.
COALESCE() is much simpler than your CASE statements.
With COALESCE() you might consider just putting the COALESCE() expression in the final calculation.
You can't use an alias in the same select. What you can do is find the value in subquery and then use it outside in the expression (or may be repeated the whole case statement in your expression). Also, use COALESCE to instead of CASE.
select t.*,
([QuantityOfProduct] * [FinalPriceForUnit] * ([Final_QuantityAtomic] / [FinalQuantityUnit])) as [Final_TotalPrice]
from (
select top 1000 [Id],
[QuantityOfProduct],
[Redundant_ProductName],
[Order_Id],
coalesce([PriceForUnitOverride], [Redundant_PriceForUnit]) as [FinalPriceForUnit],
coalesce([QuantityUnit_Override], [Redundant_QuantityUnit]) as [FinalQuantityUnit],
coalesce([QuantityAtomic_Override], [Redundant_QuantityAtomic]) as [Final_QuantityAtomic]
from [dbo].[ItemInOrder]
where [IsSoftDeleted] = 0
order by [Order_Id]
) t;
When i execute below query in sql server i got below error
Incorrect syntax near the keyword 'in'
Query:
select projectid
from projects
where iif(1!=1,
projects.projectid in (1,16,17,18,19,20,21,22,23),
1);
This will work.
Explanation: you cannot use IN operator in THEN part of CASE Statement, that's why use nested CASE statements. Now, this query will give you no record because "1 != 1" always returns false and THEN part will not execute.
SELECT projectid
FROM projects
WHERE
projectid = CASE
WHEN 1 != 1
THEN
CASE
WHEN projectid IN (1,16,17,18,19,20,21,22,23)
THEN projectid
ELSE 1 END
END;
Now, there is no syntax error near IN keyword. You can modify this query according to your requirement.
The equivalent logic is:
select p.projectid
from projects p
where (1 = 1) or p.projectid in (1, 16, 17, 18, 19, 20, 21, 22, 23);
However, this is overkill, because the first expression involves only constants and is always true. More commonly, you would have something like:
where (#TakeAllFlag = 1) or p.projectid in (1, 16, 17, 18, 19, 20, 21, 22, 23);
Also, if you are learning SQL -- and not using MS Access -- learn the CASE statement, not IIF(). CASE is standard SQL and available in basically all databases.
This should be as simple as:
select projectid
from projects
where projects.projectid in (1,16,17,18,19,20,21,22,23)
Don't see any sense of putting an IIF function in the where clause.
iif(1!=1, projects.projectid in (1,16,17,18,19,20,21,22,23), 1)
In simple English the above line interoperates as if 1 is not equal to 1 then use the IN expression else return 1
1 is always equal to 1 and the rest doesn't make any sense .
In the query below, why do we use As foo (geom) and not As geom? What does foo() do?
SQL Query
SELECT ST_SRID(geom) AS srid, ST_SRID(ST_SetSRID(geom, 4326)) as srid_new
FROM (
VALUES (
ST_GeomFromText('POLYGON((70 20, 71 21, 71 19, 70 20))', 4269)),
(ST_Point(1,2)
)
) As foo (geom);
Using As geom gives the error:
ERROR: function st_srid(record) does not exist
LINE 1: SELECT ST_SRID(geom) AS srid, ST_SRID(ST_SetSRID(geom, 4326)...
As foo is an alias for the subquery, and the (geom) attached to it is the column name in it. SQL requires an alias for every subquery. Find an example in the manual in the chapter Subqueries.
The same goes for a VALUES expression used in this place. I quote the manual here:
Syntactically, VALUES followed by expression lists is treated as equivalent to:
SELECT select_list FROM table_expression
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.