Count 2 consecutive years of Members in DB SQL Server Management Studio - 2008 R2 - sql-server-2008-r2

Microsoft SQL Server Management Studio - 2008 R2
Hi,
I have a table called [VotingStatusHistory] in this table I have 5 columns named:
UID
MemberID
AffilliationDate
VotingStatus
Inserted
This table has [MemberID's] and holds [AffilliationDates] for each year a member is Affilliated.
I would like to know what [MemberID] has [AffilliationDate's] for 2 consecutive years with a [VotingStatus] of 1 (example below for 2017, 2018)
UID MemberID AffilliationDate VotingStatus Inserted
535436 153393 2017-04-24 09:46:13.000 1 2018-03-14 00:00:00.000
582084 153393 2018-04-30 09:46:13.000 1 2019-01-29 00:00:00.000
Please some help how to approach this, I’ve tried using SUM and CASE but got nowhere so far.
Thanks in advance...

First, you should know that the 2008 r2 version has ended it's extended support this July. This means that it will no longer be receiving updates (not even security updates!) from Microsoft.
This is one reason why you should seriously consider upgrading to a later version of SQL Server.
The other reason is that starting with 2012 (which is the very next version), SQL Server is supporting the lag and lead window functions, which can be very helpful in queries like this.
Having said all that, and assuming you can't upgrade to a supported version, here's a query that should get you the desired results with 2008 r2 version:
SELECT vsh.UID, vsh.MemberID, vsh.AffilliationDate, vsh.VotingStatus, vsh.Inserted
FROM [dbo].[VotingStatusHistory] As vsh
WHERE vsh.VotingStatus = 1
AND EXISTS
(
SELECT 1
FROM [dbo].[VotingStatusHistory] As i
WHERE vsh.MemberID = i.MemberID
AND i.VotingStatus = 1
AND YEAR(i.AffilliationDate) = YEAR(vsh.AffilliationDate) + 1
)

Related

Inaccurate COUNT DISTINCT Aggregation with Date dimension in Google Data Studio

When I aggregate values in Google Data Studio with a date dimension on a PostgreSQL Connector, I see buggy behaviour. The symptom is that performing COUNT(DISTINCT) returns the same value as COUNT():
My theory is that it has something to do with the aggregation on the data occurring after the count has already happened. If I attempt the exact same aggregation on the same data in an exported CSV instead of directly from a PostgreSQL Connector Data Source, the issue does not reproduce:
My PostgreSQL Connector is connecting to Amazon Redshift (jdbc:postgresql://*******.eu-west-1.redshift.amazonaws.com) with the following custom query:
SELECT
userid,
submissionid,
date
FROM mytable
Workaround
If I stop using the default date field for the Date Dimension and aggregate my own dates directly in within the SQL query (date_byweek), the COUNT(DISTINCT) aggregation works as expected:
SELECT
userid,
submissionid,
to_char(date,'YYYY-IW') as date_byweek
FROM mytable
While this workaround solves my immediate problem, it sucks because I miss out on all the date functionality provided by Data Studio (Hierarchy Drill Down, Date Range filtering, etc.). Not to mention reducing my confidence at what else may be "buggy" within the product 😞
How to Reproduce
If you'd like to re-create the issue, using the following data as a PostgreSQL Data Source should suffice:
> SELECT * FROM mytable
userid submissionid
-------- -------------
1 1
2 2
1 3
1 4
3 5
> COUNT(DISTINCT userid) -- ERROR: Returns 5 when data source is PostgreSQL
> COUNT(DISTINCT userid) -- EXPECTED: Returns 3 when data source is CSV (exported from same PostgreSQL query above)
I'm happy to report that as of Sep 17 2020, there's a workaround.
DataStudio added the DATETIME_TRUNC function (see here https://support.google.com/datastudio/answer/9729685?), that allows you to add a custom field that truncs the original date to whatever granularity you want, without causing the distinct bug.
Attempting to set the display granularity in the report still causes the bug (i.e., you'll still set Oct 1 2020 12:00:00 instead of Oct 2020).
This can be solved by creating a SECOND custom field, which just returns the first, and then you can add IT to the report, change the display granularity, and everything will work OK.
I have the same issue with MySQL Connector. But my problem is solved, when I change date field format in DB from DATETIME (YYYY-MM-DD HH:MM:SS) to INT (Unixtimestamp). After connection this table to the Googe Datastudio I set type for this field as Date (YYYYMMDD) and all works, as expected. Hope, this may help you :)
In this Google forum there is a curious solution by Damien Choizit that involves combining your data source with itself. It works well for me.
https://support.google.com/datastudio/thread/13600719?hl=en&msgid=39060607
It says:
I figured out a solution in my case: I used a Blend Data joining twice the same data source with corresponding join key(s), then I specified a data range dimension only on the left side and selected the columns I wanted to CTD aggregate as "dimensions" (and not metric!) on the right side.

Crystal Reports Viewer showing thousands of pages

3 page document repeating to 48,387 pages.
I have a NextGen driven form that uses Crystal Reports to generate a document. I'm not familiar with Crystal Reports, but am trying to help troubleshoot this issue.
When we try to view this 2 page document (3 if you count the blank page), it shows as 48,387 pages. I reviewed the document in the Cystal Reports Viewer and it shows the 3 pages repeating over and over again. Does anyone have any insight into why this would occur? It seems to be looping the 3 pages over and over.
It started to happen yesterday. The only thing that may have changed on the Server, is that Hurricane Irma is about to visit us in Orlando, and we know the server team has initiated some disaster recovery plans. Thank you for any advice.
This most likely is happening because of a join. If you have a sql like this
select ... from tableA inner join tableB on X=Y
and both tables contain 1 record the statement will return 1 record. If tableB contains 5 records , which satisfy the condition (X=Y) then the statement will return 5 records. Most likely somebody generated 50 thousands new records. It is possible also that the records were generated in different tables for example 10 records in tableA and 5 records in tableB may cause the statement above to return 50 records. You may also check the join clause, may be it is incorrect.

ssrs/ssrb end user picks a date to filter by

I'm using sql server 2008 r2 and i'm new to building reports with ssrs. I have the reports built, but i'm trying to make it so when the end user, goes to the report on the site, they are able to pick a start and end date. So the data gets filtered by those, to look at it each quarter.
I've been looking through the interwebs for a few hours and it looks like people are able to do this, but i haven't seen anything on how yet.
Also i see there is a date picker thats greyed out, but I've read that it can only be used in web projects, is that what I'm looking for?
I don't have 2008 but in 2012 you would simply define 2 parameters as date/time and amend your dataset to include them. For example
SELECT SalesOrderID, OrderDate, Status, TotalDue
FROM Sales.SalesOrderHeader
where orderdate between #lodate and #hidate
The datasource in this case is advicentureworks2012.
SSRS automatically adds the parameters to the report and includes a date picker.

sql server partitioned table

I am preparing for 70-451 exam. There is a question I got:
You are a database developer. You plan to design a database solution by using SQL Server 2008. The database will contain a table named Claims. The Claims table will contain a large amount of data. You plan to partition the data into following categories:
Open claims
Claims closed before January 1, 2005
Claims closed between January 1, 2005 and December 31, 2007
Claims closed from January 1, 2008 till date
The close_date field in the Claims table is a date data type and is populated only if the claim has been closed. You need to design a partition function to segregate records into the defined categories.
what should you do?
A Create a RANGE RIGHT partition function by using the values 20051231, 20071231, and 20080101.
B Create a RANGE RIGHT partition function by using the values 20051231, 20071231, and NULL.
C Create a RANGE LEFT partition function by using the values 20051231, 20071231, and 20080101.
D Create a RANGE LEFT partition function by using the values 20051231, 20071231, and NULL.
Can someone answer this?
I've looked at this a few times, and I can't see any of them being right.
The partition for claimes before Jan 1 2005 partition is not generated by any of them, since the first partition value on any answer is 20051231. Whether LEFT / RIGHT is used is then immaterial, every value up until 31st Dec 2005 is in a single partition, and the LEFT / RIGHT is just about whether that date is included.
I would of expected a left with 20041231, or a right with 20050101 to be in the mix somewhere.
If the answers all started with 20041231 instead of 20051231, then I would take answer D as correct. Either question has a typo, or the test does.
I had the exam this week and this question came up. I commented this question with the non related date 20051231.

Progress 8, CURRENT_DATE select

I´d like to execute a statement on my Progress DB (Major Version 8) which would look like this in SQL Server:
Select GETDATE()
or
Select Current_Date from DUAL
with Oralce
With Progress8, the date regisiter is, CURRENT_DATE.
My question is now: does a table like dual exist for Progress8 ? Or is this statement, that returns just a Date not possible ?
(I could create a Table like the DUAL Table, but i don´t have permission to it)
Progress is not SQL. Progress version 8 is also ancient, obsolete and unsupported. It was released in the 90s. The current version of Progress is OpenEdge 11 (the "OpenEdge" branding was introduced with version 9).
FWIW Progress 8 does have some very limited SQL-89 features but using them is generally considered crazy. They were added to the language in the dark ages for marketing purposes. You will be much more successful writing your query using the 4GL.
If you are actually trying to do this from within the 4GL then you are looking for the TODAY keyword.
Modern versions of Progress do support SQL-92 access via ODBC or JDBC. But you need to upgrade to get that.