The fact table is quite simple :
Date Product Area
========== ======= ====
2010-01-01 P1 A
2010-01-01 P2 B
2010-01-02 P1 B
...
There is a large number of areas that we can't know in advance
The desired result is :
Date A B ...
========== == == ==
2010-01-01 1 1 ...
2010-01-02 0 2 ...
...
I'm looking for a query to do this job.
I would write a query:
WITH MEMBER X AS
COUNT([DimProduct].[Product].[Product])
SELECT
{[DimProduct].[Product area].[product area]} ON COLUMNS,
[Dim date].[date].[date]
ON ROWS
FROM [Cube name]
WHERE X
Jiri
SELECT
[Dim product area].[Prod area].children ON 0,
[Dim periods].[Dates].children ON 1
FROM [cubename]
Related
At the end of an enormous stored procedure (in SQL Server), I've created two CTE. One with some date ranges (with 6 month intervals) and one with some records.
Let's assume i have date ranges on table B from 2020-01-01 to 2010-01-01 (with 6 months intervals)
Start End
----------------------
2020-01-01 | 2020-07-01
... ...
other years here
... ...
2010-01-01 | 2010-07-01
and on table A this situation:
Name Date
-----------------
John 2020-01-01
John 2019-01-01
John 2018-07-01
... ...
Rob 2020-01-01
Rob 2019-07-01
Rob 2018-07-01
... ...
I'm trying to generate a recordset like this:
Name MissingDate
-----------------
John 2019-07-01
... ...
John 2010-01-01
Rob 2019-01-01
... ...
Rob 2010-01-01
I've got the flu and I barely know who I am at this moment, I hope it was clear and if anyone could help me with this I would really appreciate it.
If you want missing dates (which appear to be by month), then generate all available dates and take out the ones you have.
with cte as (
select start, end
from dateranges
union all
select dateadd(month, 1, start), end
from cte
where start < end
)
select n.name, cte.start
from cte cross join
(select distinct name from tablea) n left join
tablea a
on a.date = cte.start and a.name = n.name
where a.date is null;
Problem: Monthly distinct count of members from the first date of the gene reading, till the member is cancelled.
Members can have more than one reading per month. They can continue to have as many readings as they want.
Example:
member_id date gene_a_measurement_done gene_b_measurement_done
5557153 1/1/2010 y
5557153 2/1/2010 y
222458 2/1/2010 y y
222458 1/1/2011 y
707222 1/1/2011 y
Another table has members cancellation date:
member_id status date
5557153 Cancelled 5/1/2011
222458 Cancelled 12/1/9999
707222 Cancelled 12/1/9999
Expected result :
month distinct_count_of_member_with_gene_a_measurement distinct_count_of_member_with_gene_b_measurement
1/1/10 1 0
2/1/10 2 2
3/1/10 2 2
4/1/10 2 2
5/1/10 1 1
6/1/10 1 1
7/1/10 1 1
8/1/10 1 1
9/1/10 1 1
10/1/10 1 1
11/1/10 1 1
12/1/10 1 1
1/1/11 2 1
Query tried:
SELECT
sub.last_day,
sum(sub.distinct_count_of_member_with_gene_a_measurement) as distinct_count_of_member_with_gene_a_measurement,
sum(sub.distinct_count_of_member_with_gene_b_measurement) as distinct_count_of_member_with_gene_b_measurement,
FROM
(SELECT last_day(date),
COUNT(DISTINCT member_id) as distinct_count_of_member_with_gene_a_measurement,
null as distinct_count_of_member_with_gene_b_measurement,
FROM measurement
WHERE gene_a_measurement_done is not null
GROUP BY last_day(date)
UNION ALL
SELECT last_day(date),
null as distinct_count_of_member_with_gene_a_measurement,
COUNT(DISTINCT member_id) as distinct_count_of_member_with_gene_b_measurement,
FROM measurement
WHERE gene_b_measurement_done is not null
GROUP BY last_day(date)) as sub
GROUP BY sub.last_day(date)
Above query only gives distinct count of member for the month for which measurement was done and I am not sure how to best consider cancellation date? (inner join with member_status table on member_id and have condition to filter out cancelled member?)
I have a database table called Fees. In that table, I have FeeTypeEnum, EffectiveDate, and Amount columns.
Example of Table:
ID FeeTypeEnum EffectiveDate Amount
-----------------------------------------
1 1 1/1/2013 13.00
2 1 10/1/2013 98.36
3 2 4/1/2013 53.00
4 2 6/1/2013 51.00
5 3 7/1/2013 53.00
6 1 12/1/2012 12.00
How would I return only the results that would considered to be active based on the a datetime parameter date. For example: If today was 5/1/2013, only the following would be returned:
ID FeeTypeEnum EffectiveDate Amount
-----------------------------------------
1 1 1/1/2013 13.00
3 2 4/1/2013 53.00
I was thinking that I could just group them by FileTypeEnum and then remove the ones that are before the current date. Would that work?
Edit
Basically, I'm trying to return all the results where the EffectiveDate is BEFORE the datetime parameter and NOT the results where the EffectiveDate is AFTER the parameter.
Edit 2
When using the following query:
SELECT *
FROM [Fees]
WHERE [EffectiveDate] <= GETDATE()
ORDER BY [FeeEnum]
It also returns results for records that are no longer considered active. How would I select only 1 from each [FeeEnum] group?
Something like this?
SELECT *
FROM Fees
WHERE EffectiveDate <= #asOfDate
How you are calling the SQL (e.g. a stored procedure or directly form an application) will determine how to set the #asOfDate parameter.
New at this but I belive you want to bring back anything < systdate.
Try something like this:
SELECT *
FROM tablename
WHERE EffectiveDate<SYSDATE
I have a table like this : (note that id_pack is not auto incremented)
id_pack start_date end_date is_parent id_contract
1 2011-11-01 2012-01-18 1 5547
2 2012-01-18 2050-01-01 1 5547
3 2009-02-02 2050-01-01 0 5547
where id_pack = 3 is the child of the two parents. I want to make a query to select the parents and the child for the month 2012-01 but the child needs to be doubled (because his first parent finished on 2012-01-18). So the result needs to look like this :
id_pack start_date end_date id_parent
1 2012-01-01 2012-01-18 0
2 2012-01-18 2012-01-31 0
3 2012-01-01 2012-01-18 1
3 2012-01-18 2012-01-31 2
I have tried in every way and I can't figure it out. I'm doing this because parents are assigned a price rate in another table, and for the current month the child had two parents with different price rates, so I need to charge from 2012-01-01 : 2012-01-18 using a rate plan and from 2012-01-18 : 2012-01-31 using another rate plan.
Is this even possible with one query ?
Thank you
PS: I have something like this :
select c.id_pack,
case when c.start_date < '2012-01-01' then '2012-01-01'
else c.start_date
end as start_date,
case end date ...... the same as start_date as end_date,
from client a
join contract b on b.id_client = a.id_client
join package c on c.id_contract = b.id_contract
and c.start_date < dateadd(mm,1,'2012-01-01')
and c.end_date >= '2012-01-01'
where a.id_clinet = '12345'
Try:
select c.id_pack, p.start_date, p.end_date, coalesce(p.id_pack,0) id_parent
from package c
left join package p
on c.contract_id = p.contract_id and p.is_parent = 1 and c.is_parent = 0
I have a table like this:
Item Qty Price A Price B
abc 5 36.00 0
qwe 8 0 48.00
zxc 6 12.00 0
poi 4 10.00 0
lkj 9 12.00 0
mnb 3 0 14.00
vfr 7 0 6.00
How can I sum the value using SQL ie. if Price A is zero, it will pick Price B. The expected results will be as follows :-
Item Value
abc 180.00
qwe 384.00
zxc 72.00
poi 40.00
lkj 36.00
mnb 42.00
vfr 42.00
SELECT
ITEM
, (Qty * [Price A]) + (Qty * [Price B]) AS Value
FROM
TableName
Use the following if A and B could be non-zero, and you only want to use price A when this occurs:
SELECT Item, CASE WHEN `Price A` != 0 THEN `Price A` * Qty
ELSE `Price B` * Qty
END AS Value
FROM table;
Why not do something like
SELECT SUM(Price A) FROM table; SELECT SUM(Price B) FROM table WHERE Price A = 0;
And then add the results together...
from the example above you could really just select priceA + priceB, since one or the other is always zero, otherwise... I am not quite sure this can be done in pure SQL...
Assuming both can be not null:
SELECT item, QTY * IF(PriceA!=0, PriceA, PriceB) AS VALUE from tableName;