MDX filter level member - mysql

My question is how to filter valid field by another field in MDX.
I have one table: samples;
if I use sql to solve problem , just like this:
select patient_id from samples where calc_test_type_id = 1;
I created dimension for this: Patient Characteristic
Hierarchy: id
level : id (patient id in samples table) , including property, named 'testA', 'testA' linked to calc_test_type_id.
So I tried MDX like this :
SELECT
{[Measures].[num_samples]} ON COLUMNS,
{
filter
(
distinct([Patient characteristic.id].[id].members),
[Patient characteristic.id].CurrentMember.Properties("calc_type") = 1
)
} ON ROWS
FROM [EIDCube]
[Measures].[num_samples] is to calculate how many rows for calc_test_type_id = 1.
But I found some data lost. So how to find all suitable patient id ?

Your code looks fine. Try creating the set first. Also I don't think you need distinct:
WITH SET [calc_type1] AS
FILTER
(
[Patient characteristic.id].[id].members)
,[Patient characteristic.id].CurrentMember.Properties("calc_type") = 1
)
SELECT
{[Measures].[num_samples]} ON 0,
[calc_type1] ON 1
FROM [EIDCube];
To check the set returned I'd be tempted to run this script without that measure first, like this:
WITH SET [calc_type1] AS
FILTER
(
[Patient characteristic.id].[id].members)
,[Patient characteristic.id].CurrentMember.Properties("calc_type") = 1
)
SELECT
{} ON 0,
[calc_type1] ON 1
FROM [EIDCube];

Related

Mysql Query for fetching records using single Query from three tables

We have three table
table 1- app ( id , name )
table 2- appPlayer ( id , name )
table 3- appPlayerSession ( id , appId , appPlayerId , version)
my Current query is:
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,count(version) FROM appPlayerSession aps GROUP BY appId,version,appName
we need to count the session users for each game with same version, and also woth the object of all users data using single mysql query.
Current Result using my query, but we also need players for each app..
As you havent given your expected result and on basis of your requirement you can do something this.it may be enhanced as per your requirement.
SELECT (select name from app k where k.id= aps.appId) AS appName,version,appId,(select P.name from appPlayer P where P.id=aps.appPlayerid) as appPlayerName, count(version) FROM appPlayerSession aps GROUP BY appId,version,appName,appPlayerName
Also check fiddle as per your requirement created as you havent given any data set and its on my assumption.
http://sqlfiddle.com/#!9/30fe4f/1
New Sql as per your new added requirement-
select X.appname,X.version,X.appid,GROUP_CONCAT(distinct X.appPlayerName order by X.appPlayerName) as Users ,
sum(X.vercount)
from (SELECT (select name from app k where k.id= aps.appId)
AS appName,version,appId,
(select P.name from appPlayer P where P.id=aps.appPlayerid)
as appPlayerName, count(version)as vercount
FROM appPlayerSession aps
GROUP BY appId,version,appName,appPlayerName) X
group by X.appname,X.version,X.appid
New fiddle -http://sqlfiddle.com/#!9/13646c/5
You can use JOIN in sql to connect with multiple tables and fetch result
Below is the format :
SELECT t1.col, 
       t3.col 
FROM   table1 
       JOIN table2 
         ON table1.primarykey = table2.foreignkey 
       JOIN table3 
         ON table2.primarykey = table3.foreignkey 
In your case :
SELECT app.col, 
       appPlayer.col,
appPlayerSession.col 
FROM   app 
       JOIN appPlayer 
         ON app.id = appPlayer.appId
       JOIN appPlayerSession 
         ON appPlayer.id = appPlayerSession.appPlayerId
Hope this is helpful.
One suggestion . It is not a standard to use camelCase for table and column names. snake_case is preferred widely.

How to use select with update query ? mysql

I want to count points for each business for having deals or gallery :
so if the business has un empty deal, or has at least one gallery , business_data_count should be 2.
this what i've tried :
UPDATE `business` businessTable SET
business_data_count
=
sum(
(
SELECT
CASE
WHEN count(*)>= 1 then count(*)
ELSE 0
END as points
FROM gallery WHERE bussId=businessTable.bussId
)
+
(
SELECT
case
WHEN deal!='' then 1
ELSE 0
end
FROM business WHERE bussId=businessTable.bussId
)
where 1
but i got this error :
you cant specify table business for update
How to fix this ?
There's no need to do a separate select from the update table. Try this instead (untested):
UPDATE business
SET business_data_count = (deal != '')
+ (SELECT COUNT(*)
FROM gallery
WHERE bussId = business.bussId);
On a separate note, it's generally bad practice to store data that can be easily extracted with a query, such as the above.
I think you have a typo.
I think you should have:
UPDATE `business`.businessTable SET
instead of
UPDATE `business` businessTable SET
If you use the schema when defining your table in your query, you need to separate them with a dot (.).
Maybe it is not the only problem, but that's the first that comes to mind.

Reduce MySQL Code down or combine SELECT Statements

I have made a few relations to do with a banking database system.
this is my current code. The table has
SELECT COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
and SortCode = 00000001;
SELECT COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
and SortCode = 00000002;
SELECT COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
and SortCode = 00000003;
the rest of the code is a duplicate of this part with the next type of 'Account' and looping back through sortcode's 1-3 again.
I was wondering if there was a more elegant way of producing this. I need to count the number of student, current and saver accounts for each bank.
Or is there a way to combine lots of selects together to make a neat table?
That's what GROUP BY is for!
SELECT SortCode,COUNT(AccountType) AS Student_Total FROM Account
WHERE AccountType ='Student'
GROUP BY SortCode;
UPDATE:
You can also GROUP BY with multiple grouping fields:
SELECT SortCode,AccountType,COUNT(AccountType) AS Student_Total FROM Account
GROUP BY SortCode,AccountType;
You could also apply a PIVOT approach to this query to always return a single row and know the fixed-final columns of the result set. However, applying a group by allows for more flexibility of returned rows, especially if you have a large amount of individual things you are trying to tally up.
select
A.AccountType,
SUM( IF( A.SortCode = 1, 1, 0 )) as SortCode1Cnt,
SUM( IF( A.SortCode = 2, 1, 0 )) as SortCode2Cnt,
SUM( IF( A.SortCode = 3, 1, 0 )) as SortCode3Cnt
from
Account A
where
A.AccountType = 'Student'
AND A.SortCode IN ( 1, 2, 3 )
group by
A.AccountType
Note... it appears your sort code is a numeric as you have no quotes around indicating a character string. So, all the leading zeros are irrelevant. And if you were only doing based on a single Account Type, you don't even need the leading Account Type column and can remove the group by too.

Return zero when records not found

Im making a table generator as a school project.
In MySQL I have 3 tables namely process,operation,score. Everything looked fine until i tested out my "ADD column" button in the web app.
Previous saved data should be read properly but also include the new column in the format, problem is the previous data queried does not include any values for the new table, so I intended it to return a score of 0 if no records were found, tried IFNULL & COALESCE but nothing happens(maybe im just using it wrong)
process - processID, processName
operation - operationID, operationName
score - scoreID, score, processID, operationID, scoreType (score
types are SELF,GL,FINAL)
ps = (PreparedStatement)dbconn.prepareStatement("SELECT score FROM score WHERE processID=? and operationID=? and type=?ORDER BY processid");
here's a pic of a small sample http://i50.tinypic.com/2yv3rf9.jpg
The reason that IFNULL doesn't work is that it only has an effect on values. A result set with no rows has no values, so it does nothing.
First, it's probably better to do this on the client than on the server. But if you have to do it on the server, there's a couple of approaches I can think of.
Try this:
SELECT IFNULL(SUM(score), 0) AS score
FROM score
WHERE processID=? and operationID=? and type=?
ORDER BY processid
The SUM ensures that exactly one row will be returned.
If you need to return multiple rows when the table contains multiple matching rows then you can use this (omitting the ORDER BY for simplicity):
SELECT score
FROM score
WHERE processID = ? and operationID = ? and type = ?
UNION ALL
SELECT 0
FROM (SELECT 0) T1
WHERE NOT EXISTS
(
SELECT *
FROM score
WHERE processID = ? and operationID = ? and type = ?
)

How to get a filtered list

I'm trying to do something like this :
listeTagGr.DataContext = From a In oo.articles
Where a.producttype.id.Equals(1)
Select a.tagstrings Distinct
Error : tagstrings is a list.
I wan't to retrieve the list of distincts tagstrings for articles where producttype is 1
listeTagGr.DataContext = (From a In oo.articles
Where a.producttype.id.Equals(1)
From ts in a.tagstrings
Select ts).Distinct()