MDX query with TOPCOUNT between date range returning a top with some empty values - mdx

I'm using a MDX query to get the top 10 liked Products between two dates. Oddly, the result is composed by some Products with likes within that date range, and some without any likes at all. Here is the query:
SELECT
{ [Measures].[Likes] } ON COLUMNS,
{ TOPCOUNT([Products].[Name].Members, 10, [Measures].[Likes]) } ON ROWS
FROM [Likes]
WHERE ( [Date].[2014].[3].[29]:[Date].[2014].[4].[5] )
Here are the results:
[Measures].[Likes]
[Product].[XX]
[Product].[XX]
[Product].[XX] 139
[Product].[XX]
[Product].[XX] 1
[Product].[XX]
[Product].[XX]
[Product].[XX] 125
[Product].[XX] 111
[Product].[XX] 1
If I change the top limit to 20, for example, the results will have more products with likes but also with more empty ones, and not ordered (like a top usually is).
Using NON EMPTY makes the query return only 5 results instead of 10, and still not ordered.
Thanks!

Try this query
SELECT
{ [Measures].[Likes] } ON 0,
Head(ORDER([Products].[Name].Members,[Measures].[Likes], DESC), 10) ON 1
FROM [Likes]
WHERE ( [Date].[2014].[3].[29]:[Date].[2014].[4].[5] )

Related

Select All rows and it's count in a single query

I have some table called items and want to get some rows from it and count(all) in a single query, right now I'm trying something like this:
SELECT
COUNT(*) as count,
(SELECT * FROM items WHERE ...) as items
FROM items
But I got subquery must return only one column error
Items table:
Id| Comment
1 | "comment1"
2 | "comment2"
3 | "comment3"
4 | "comment4"
5 | "comment5"
I want to have something like this in a result:
[{
count: 5,
items: [
{
id: 1,
comment: "comment1"
}
...other 4 comments
]
}]
What is s good way to do this?
You can try below query(Postgres 9.5 and Above):
select
json_agg(d)
from
(SELECT count(*) as "Count", json_agg(t) as "Items"
FROM (select * from items) t
) d
DB-Fiddle Example
If you want rows from items along with the count, you can use window functions:
select i.*, count(*) over () as num_items
from items i;

SELECT statement to Return a Count for Each Row in Sybase Table

While working on a MongoDB/Node back-end environment I am needing to run some ETLs to pull in data from a previous Sybase environment. I am far less familiar with SQL than I am with Mongo/Node.
The following SELECT statement finds the count for a particular record (row of data) in the bl_serv_staffmember_xref table:
SELECT Count()
FROM "bl_serv_staffmember_xref"
WHERE ( "bl_serv_staffmember_xref"."staff_member_id" = 129 ) AND
( "bl_serve_staffmember_xref"."isactive" = 1 );
What I need to do is generate a SELECT statement that will return data with that count for EACH row in the table. How would I go about doing that?
I tried this:
SELECT Count()
FROM "bl_serv_staffmember_xref"
WHERE ( "bl_serv_staffmember_xref"."staff_member_id" is NOT NULL ) AND
( "bl_serv_staffmember_xref"."isactive" = 1 ) ;
But this just returns one total count:
Count()
1 2,452
rather than a count for each staff_member_id, which is what I need.
This is my desired result:
staff_member_id assignment_count
1 23
2 12
3 16
You could use count(*) and group by eg:
SELECT bl_serv_staffmember_xref.staff_member_id, Count(*)
FROM "bl_serv_staffmember_xref"
WHERE ( "bl_serve_staffmember_xref"."isactive" = 1 )
GROUP BY bl_serv_staffmember_xref.staff_member_id

Is it possible to have some kind of a group query that groups on the number in a column?

I have a very simple table:
CREATE TABLE "Score"(
"Id" varchar primary key not null ,
"EnglishCount" int ,
"RomajiCount" int )
Is there a type of query that I could run that would show me:
how many rows have EnglishCount = 0,
how many rows have EnglishCount = 1,
how many rows have EnglishCount = 2,
how many rows have EnglishCount = 3,
how many rows have EnglishCount = 4,
etc ...
Here's the kind of output I am hoping to get:
Count Instances
0 1
1 2
3 1
4 5
5 2
You can use a group by clause to separate the result per distinct value of EnglishCount and then apply count(*) to each group:
SELECT EnglishCount, COUNT(*)
FROM Score
GROUP BY EnglishCount

SQL display duplicate field but not nulled field

I have a simple problem i think. I have in sql Server one table with this :
Name : Sum : CNP
Andrey 100 120
Marius 20 100
George 20 200
Popescu Nulled 300
Antal Nulled 100
I use this comand to show duplicate :
SELECT SUM, Name,CNP
FROM dbo.database
where SUM IN ( Select SUM from dbo.asigpag group by SUM HAVING Count(*)> 1)
Everything work ok.
In this Case show :
Name : Sum : CNP
Marius 20 100
George 20 200
Popescu Nulled 300
Antal Nulled 100
This is the problem . I want to display duplicate but with not Nulled.
I want to display this with all the other field not only Sum.
Name : Sum : CNP
Marius 20 100
George 20 200
You need to add another condition to exclude records that have a null value in the sum field:
SELECT SUM, Name,CNP
FROM dbo.database
where SUM IN ( Select SUM from dbo.asigpag group by SUM HAVING Count(*)> 1)
AND SUM is not NULL
SQL Server treats NULLS differently from values, because they have no value at all. They're special case that need to be selected using [Field] IS NULL or [Field] = NULL, or their reverse, as in this case.
In SQL NULL <> NULL always. You can use IS NULL or IS NOT NULL or predefined function ISNULL(SUM, 0) - with last statement you will prepare NULL values to default 0 value. For example:
SELECT SUM, Name,CNP
FROM dbo.database
where ISNULL(SUM, 0) IN ( Select ISNULL(SUM, 0) from dbo.asigpag group by SUM HAVING Count(*) > 1)
Update:
Sorry, I misunderstood what you want. In order to eliminate NULL from the list you need to update sub-query as:
Select SUM from dbo.asigpag where SUM IS NOT NULL group by SUM HAVING Count(*) > 1
Whole query will be:
SELECT SUM, Name, CNP
FROM dbo.database
where SUM IN ( Select SUM from dbo.asigpag Where SUM IS NOT NULL group by SUM HAVING Count(*) > 1)

SQL Query Help: Returning distinct values from Count subquery

I've been stuck for quite a while now trying to get this query to work.
Here's the setup:
I have a [Notes] table that contains a nonunique (Number) column and a nonunique (Result) column. I'm looking to create a SELECT statement that will display each distinct (Number) value where the count of the {(Number), (Result)} tuple where Result = 'NA' is > 25.
Number | Result
100 | 'NA'
100 | 'TT'
101 | 'NA'
102 | 'AM'
100 | 'TT'
200 | 'NA'
200 | 'NA'
201 | 'NA'
Basically, have an autodialer that calls a number and returns a code depending on the results of the call. We want to ignore numbers that have had an 'NA'(no answer) code returned more than 25 times.
My basic attempts so far have been similar to:
SELECT DISTINCT n1.Number
FROM Notes n1
WHERE (SELECT COUNT(*) FROM Notes n2
WHERE n1.Number = n2.Number and n1.Result = 'NA') > 25
I know this query isn't correct, but in general I'm not sure how to relate the DISTINCT n1.Number from the initial select to the Number used in the subquery COUNT. Most examples I see aren't actually doing this by adding a condition to the COUNT returned. I haven't had to touch too much SQL in the past half decade, so I'm quite rusty.
you can do it like this :
SELECT Number
FROM Notes
WHERE Result = 'NA'
GROUP BY Number
HAVING COUNT(Result) > 25
Try this:
SELECT Number
FROM (
SELECT Number, Count(Result) as CountNA
FROM Notes
WHERE Result = 'NA'
GROUP BY Number
)
WHERE CountNA > 25
EDIT: depending on SQL product, you may need to give the derived table a table correlation name e.g.
SELECT DT1.Number
FROM (
SELECT Number, Count(Result) as CountNA
FROM Notes
WHERE Result = 'NA'
GROUP
BY Number
) AS DT1 (Number, CountNA)
WHERE DT1.CountNA > 25;