Union of string and Avg function in mysql showing some junk value - mysql

Mysql SQL Quires and Output
Query 1:
Query 2:
Union of above two queries:
Expected output:

The main problem is in the call of the sql function in the query, Try with this:
SELECT 'MEAN','STANDARD_DEVATION','MAX','MIN','VARIANCE'
UNION
SELECT * FROM ( SELECT AVG('MetricValue'),STDDEV_SAMP('MetricValue'),MAX('MetricValue'),
MIN('MetricValue'),VAR_SAMP('MetricValue')
FROM 'XR_METRIC_ANALYSIS' ) as temp
This is a possible solution

Related

My MariaDB MYSQL query is returning results when it shouldn't

here is my query -
SELECT column_one FROM generation1 UNION SELECT column_one FROM generation2 WHERE column_one = value;
I am trying to query both tables for the value and i want PHP to execute a block of code if no result is found but the value in the where condition returns a result even when the value doesn't exist on both tables.
Pls how can I make this query work and return the value of the "where" condition?
I think you want the WHERE logic to apply to the entire result set generated by the union query. If so, then you should subquery and apply the filter there:
SELECT column_one
FROM
(
SELECT column_one FROM generation1
UNION ALL
SELECT column_one FROM generation2
) t
WHERE column_one = value;

How to SELECT COUNT(DISTINCT) and WHERE conditional on Ms.Access Query

I just wanted to simply count using where conditional and yet this query ask me for parameter instead of automatically execute the query
SELECT COUNT(ActDiscDischargingPort) from(
SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable) WHERE SchLoadVessel LIKE "XB24 - MV. MEMPHIS" AND SchLoadVoyageNo LIKE "0019";
What is the proper way of writing this query?
Found the answer, turn out the query has to be like this
SELECT COUNT(ActDiscDischargingPort) from ( SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable WHERE SchLoadVoyageNo LIKE "XB24 - MV. MEMPHIS" AND SchLoadVessel LIKE "0019" )
Every derived table must have its own alias.
The correct syntax would be
SELECT
COUNT(ActDiscDischargingPort)
from(
SELECT
DISTINCT ActDiscDischargingPort
FROM
SelisihLoadVSActualLoadTable
) AS T
WHERE
SchLoadVessel LIKE "XB24 - MV. MEMPHIS"
AND SchLoadVoyageNo LIKE "0019";
You can further speed up your query by optimizing it a bit.

Union Query In Separate Databases

I have 2 databases. In database1, I want to run a query from database2 and UNION ALL the results. This is the syntax I tried, but I get an error of
Syntax error in from clause
Here is the syntax I tried --- where is my error?
SELECT * FROM query1
UNION ALL
SELECT * FROM query1 IN C:\Database\production.mdb
Add quotes around the path of the external database.
I would also use aliases to distinguish between the 2 instances of query1.
SELECT q1Local.* FROM query1 AS q1Local
UNION ALL
SELECT q1Remote.* FROM query1 AS q1Remote IN 'C:\Database\production.mdb'

Select from array in MySQL

Is it possible in MySQL to select from a static set of integers? Given an array of integers like 1 and 2, what is the correct syntax in MySQL to do something like:
select
*
from
(values(1),(2))
Which should return one row containing the number 1 and one row containing the number 2.
In other SQL than MySQL (e.g. MSSQL) this is possible. Is there a way to do this in MySQL?
I think you mean something like this?
SELECT 1 UNION SELECT 2 UNION SELECT 3
The only way to create a virtual set in MySQL is using a subquery with UNION. The subquery in the FROM clause creates 3 rows which can be joined with another table:
SELECT foo.*
FROM (
SELECT 1 AS num UNION
SELECT 2 AS num UNION
SELECT 3 AS num
) virtual
LEFT JOIN foo ON foo.num = virtual.num
When you want to use your list of values as a condition for WHERE or ON clause, then the IN() construct may be the right way to go:
SELECT foo.* FROM foo WHERE foo.num IN (1,2,3)
sorry for my english
you can use (IN) like this
SELECT * FROM Table WHERE id IN (1,2,3....)

Query to invoke a function - Access 2003 vba

I have a function in a module (the function combines 6 tables into a new one, then looks up another table to update one of the field values in the new table) and all is working perfectly well. Now I would like to call this function from an access 2003 query so that when i run the query the New Table should be opened.
I tried using Expr: in the query but although the new table is being created in the 'Tables' Section (by invoking my function), all I can see when i run the query is a blank table with one column named 'Expr'.
Can anyone please guide me to the right direction?
Any help would be greatly appreciated.
You can use in your select ..
SELECT * INTO TBL_ALLTABLES
FROM (
--Here your select union all tables for example
SELECT * from tbl1
UNION ALL
SELECT * from tbl2
UNION ALL
SELECT * from tbl3
UNION ALL
SELECT * from tbl5
UNION ALL
)