Count records against filters - sql

I have following 4 tables in sql with some records in them.
Users
UserId UserName EmploymentId
------ -------- -------------
1 User1 1
2 John 1
3 Doe 2
Country
CountryId countryName
------ --------
1 USA
2 UK
EmpStatus
EmploymentId EmpName
------ --------
1 Employed
2 Un-Employed
UserNationality (many to many )
UserId CountryId
------ --------
1 1
1 1
3 1
Now what I am trying to achieve is get show counts with filter criteria.i.e. I have view which should display filter criteria as shown below.
**Search Records**
Search by country
USA (3)
UK (0)
Search by employment
Employed (2)
Un-Employed (1)
Note that in UserNationality table there is a many to many relation ship between UserId and CountryId. So there may or may not be a record against a user.
I've tried counting by applying joins on the tables but it doesn't give desired results.
Also tried to used PARTITION but I've so far failed to generate the results. What approach should I use to solve this issue ?

Related

Count values separately until certain amount of duplicates SQL

I need a Statement that selects all patients and the amount of their appointments and when there are 3 or more appointments that are taking place on the same date they should be counted as one appointment
That is what my Statement looks so far
SELECT PATSuchname, Count(DISTINCT AKTDATUM) AS AKTAnz
FROM tblAktivitaeten
LEFT OUTER JOIN tblPatienten ON (tblPatienten.PATID=tblAktivitaeten.PATID)
WHERE (AKTDeleted<>'J' OR AKTDeleted IS Null)
GROUP BY PATSuchname
ORDER BY AKTAnz DESC
The result should look like this
PATSuchname Appointments
----------------------------------------
Joey Patner 13
Billy Jean 15
Example Name 13
As you can see Joey Patner has 13 Appointments, in the real table though he has 15 appointments but three of them have the same Date and because of that they are only counted as 1
So how can i write a Statement that does exactly that?
(I am new to Stack Overflow, sorry if the format I use is wrong and tell me if it is.
In the table it looks like this.
tblPatienten
----------
PATSuchname PATID
------------------------
Joey Patner 1
Billy Jean 2
Example Name 3
tblAktivitaeten
----------
AKTDatum PATID AKTID
-----------------------------------------
08.02.2021 1 1000 ----
08.02.2021 1 1001 ---- So these 3 should counted as 1
08.02.2021 1 1002 ----
09.05.2021 1 1003
09.07.2021 2 1004 -- these 2 shouldn't be counted as 1
09.07.2021 2 1005 --
Two GROUP BY should do it:
SELECT
x.PATID, PATSuchname, SUM(ApptCount)
FROM (
SELECT
PATID, AKTDatum, CASE WHEN COUNT(*) < 3 THEN COUNT(*) ELSE 1 END AS ApptCount
FROM tblAktivitaeten
GROUP BY
PATID, AKTDatum
) AS x
LEFT JOIN tblPatienten ON tblPatienten.PATID = x.PATID
GROUP BY
x.PATID, PATSuchname

Self JOIN to find the parent detail which matches with the row data -

I am trying to query in MS SQL and I can not resolve it. I have a table employees:
Id Name Surname FatherName MotherName WifeName Pincode isChild
-- ------- ------- ---------- ---------- -------- ------- -------
1 John Green James Sue null 101011 1
2 Michael Sloan Barry Lilly null 101011 1
3 Sally Green Andrew Molly Jemi 101011 1
4 Barry Sloan Soul Paul Lilly 101011 0
5 James Green Ned White Sue 101011 0
I want a query that selects rows where the father name and mother name of child matches with name and wife name. For the example table, where I want to return the result of rows where father and mother name matches the name and wife name column. For eg. id=1, where John's father name James and mother name Sue matches with id 5 which returns James as first name and Sue as wife name. So my query should return (this is my expected result)
Id Name Surname FatherName MotherName WifeName Pincode isChild
-- ------- ------- ---------- ---------- -------- ------- -------
5 James Green Ned White Sue 101011 0
4 Barry Sloan Soul Paul Lilly 101011 0
I tried with the below query but it checks for James only. How to change my query so that it checks all the names and returns the expected result.
select * FROM employees
where first_name like '%James%'
and wife_name like '%Sue%'
and pincode=101011;
Any tips on this will be really helpful. I am new to joins, need help on writing self join to get the result.
…
select *
from thetable as p -- the parent/father
where exists -- with one child at least
(
select *
from thetable as c
where c.fathername = p.name
and c.mothername = p.wifename
-- lastname?
)
Too long for a comment, but also not intended as a slam against what you are working with. Please take as constructive criticism.
Aside from VERY POOR DESIGN on the table content, getting that corrected before you get too deep into whatever you are working should be done first. A more typical design might be having a table of people. Now, to get the relationships you could do a couple ways. One is that on each individual person's record, you add 2 additional IDs. FatherID, MotherID. These IDs would join directly back to the child vs hard strings to match against. Take a surname like Smith or Jones. Then, look at the many instances of a "John Smith" may exist, yes a lot, and lower probability of finding a matching wife's name of Sue, Mary or whatever else name. But even that could lead to multiple possibilities. Yes, you are adding a PIN, but even a computer can generate a random pin of 1234.
By having the IDs, there is NO ambiguity of who the relationship is with.
If the data were slightly altered to something like
Id Name Surname FatherID MotherID SpouseID
-- ------- ------- ---------- ---------- --------
1 John Green 5 6 null
2 Michael Sloan 4 3 null
3 Lilly Sloan null null 4
4 Barry Sloan null null 3
5 James Green 9 10 6
6 Sue Green 7 8 5
7 Bill Jones null null 8
8 Martha Jones null null 7
9 Brian Green null null 10
10 Beth Smith-Green null null 9
So, in this modified example, you can see right away that ID#1 John Green has parents of Father (ID#5) is James and Mother (ID#6) is Sue. But even from this, James is a child to Father (ID#9) Brian and Mother (ID#10) Beth. This scenario is showing to a grand-parent level capacity and that each of James and Sue are also children but to their respective parents. Sue's parents of the Jones surname.
For Michael Sloan, parents of #4 Barry, and #3 Lilly.
And I additionally added a spouse ID. This prevents redundancy of people's names copied all over. Then you can query based on the child's parent's respective IDs to find out vs a hopeful name LIKE guess.
So, even though not solving a relatively simple query, fixing the underlying foundation of your database and is relations will, long-term, help ease your querying in the future.
Try this:
SELECT
T2.*
FROM Employee T1
JOIN Employee T2 ON T2.Name = T1.FatherName
AND T2.WifeName = T1.MotherName

Oracle SQL -- selecting specific data from multiple rows into one row

I'm trying to select data across multiple rows into one row.
For example, with this data set:
NAME THING DATE
----- ------ ------
JACK 1 EARLY
JACK 2 LATER
JACK 3 NOW
JANE 1 LATER
JANE 2 EARLY
JANE 3 NOW
I want to produce the following result:
NAME THING DATE
---- ---- -----
JACK 1, 2, 3 NOW
JANE 1, 2, 3 NOW
And so, I know i can use the LISTAGG function to combine the "Thing" rows, but my biggest question is how to select across multiple rows to get the "NOW" values in the date field.
Any help would be appreciated. Thanks!
It isn't clear if you want the latest date for any thing (ordering the aggregated things either by date or by their own values):
select name,
listagg(thing, ',') within group (order by date_col) as things,
max(date_col) as now
from your_table
group by name
order by name;
or the date corresponding to the highest value of thing:
select name,
listagg(thing, ',') within group (order by thing) as things,
max(date_col) keep (dense_rank last order by thing) as now
from your_table
group by name
order by name;
As you said those are actually dates, with your sample data with added date values configured slightly differently for two names:
NAME THING DATE_COL
---- ---------- ----------
JACK 1 2019-01-01
JACK 2 2019-03-15
JACK 3 2019-04-30
JANE 1 2019-02-01
JANE 2 2019-05-03
JANE 3 2019-04-02
the first query gets:
NAME THINGS NOW
---- --------------- ----------
JACK 1,2,3 2019-04-30
JANE 1,3,2 2019-05-03
and the second query gets:
NAME THINGS NOW
---- --------------- ----------
JACK 1,2,3 2019-04-30
JANE 1,2,3 2019-04-02
db<>fiddle

SQL: Identify distinct blocks of treatment over multiple start and end date ranges for each member

Objective: Identify distinct episodes of continuous treatment for each member in a table. Each member has a diagnosis and a service date, and an episode is defined as all services where the time between each consecutive service is less than some number (let's say 90 days for this example). The query will need to loop through each row and calculate the difference between dates, and return the first and last date associated with each episode. The goal is to group results by member and episode start/end date.
A very similar question has been asked before, and was somewhat helpful. The problem is that in customizing the code, the returned tables are excluding first and last records. I'm not sure how to proceed.
My data currently looks like this:
MemberCode Diagnosis ServiceDate
1001 ----- ABC ----- 2010-02-04
1001 ----- ABC ----- 2010-03-20
1001 ----- ABC ----- 2010-04-18
1001 ----- ABC ----- 2010-05-22
1001 ----- ABC ----- 2010-09-26
1001 ----- ABC ----- 2010-10-11
1001 ----- ABC ----- 2010-10-19
2002 ----- XYZ ----- 2010-07-10
2002 ----- XYZ ----- 2010-07-21
2002 ----- XYZ ----- 2010-11-08
2002 ----- ABC ----- 2010-06-03
2002 ----- ABC ----- 2010-08-13
In the above data, the first record for Member 1001 is 2010-02-04, and there is not a difference of more than 90 days between consecutive services until 2010-09-26 (the date at which a new episode starts). So Member 1001 has two distinct episodes: (1) Diagnosis ABC, which goes from 2010-02-04 to 2010-05-22, and (2) Diagnosis ABC, which goes from 2010-09-26 to 2010-10-19.
Similarly, Member 2002 has three distinct episodes: (1) Diagnosis XYZ, which goes from 2010-07-10 to 2010-07-21, (2) Diagnosis XYZ, which begins and ends on 2010-11-08, and (3) Diagnosis ABC, which goes from 2010-06-03 to 2010-08-13.
Desired output:
MemberCode Diagnosis EpisodeStartDate EpisodeEndDate
1001 ----- ABC ----- 2010-02-04 ----- 2010-05-22
1001 ----- ABC ----- 2010-09-26 ----- 2010-10-19
2002 ----- XYZ ----- 2010-07-10 ----- 2010-07-21
2002 ----- XYZ ----- 2010-11-08 ----- 2010-11-08
2002 ----- ABC ----- 2010-06-03 ----- 2010-08-13
I've been working on this query for too long, and still can't get exactly what I need. Any help would be appreciated. Thanks in advance!
SQL Server 2012 has the lag() and cumulative sum functions, which makes it easier to write such a query. The idea is to find the first in each sequence. Then take the cumulative sum of the first flag to identify each group. Here is the code:
select MemberId, Diagnosis, min(ServiceDate) as EpisodeStartDate,
max(ServiceStartDate) as EpisodeEndDate
from (select t.*, sum(ServiceStartFlag) over (partition by MemberId, Diagnosis order by ServiceDate) as grp
from (select t.*,
(case when datediff(day,
lag(ServiceDate) over (partition by MemberId, Diagnosis
order by ServiceDate),
ServiceDate) < 90
then 0
else 1 -- handles both NULL and >= 90
end) as ServiceStartFlag
from table t
) t
group by grp, MemberId, Diagnosis;
You can do this in earlier versions of SQL Server but the code is more cumbersome.
For versions of SQL Server prior to 2012, here's some code snippets that should work.
First, you'll need a temp table (as opposed to a CTE, as the lookup of the edge event will fire the newid() function again, rather than retriving the value for that row)
DECLARE #Edges TABLE (MemberCode INT, Diagnosis VARCHAR(3), ServiceDate DATE, GroupID VARCHAR(40))
INSERT INTO #Edges
SELECT *
FROM Treatments E
CROSS APPLY (
SELECT
CASE
WHEN EXISTS (
SELECT TOP 1 E2.ServiceDate
FROM Treatments E2
WHERE E.MemberCode = E2.MemberCode
AND E.Diagnosis = E2.Diagnosis
AND E.ServiceDate > E2.ServiceDate
AND DATEDIFF(dd,E2.ServiceDate,E.ServiceDate) BETWEEN 1 AND 90
ORDER BY E2.ServiceDate DESC
) THEN 'Group'
ELSE CAST(NEWID() AS VARCHAR(40))
END AS GroupID
) z
The EXISTS operator contains a query that looks into the past for a date between 1 and 90 days ago. Once the Edge cases are gathered, this query will provide the results you posted as desired from the test data you posted.
SELECT MemberCode, Diagnosis, MIN(ServiceDate) AS StartDate, MAX(ServiceDate) AS EndDate
FROM (
SELECT
MemberCode
, Diagnosis
, ServiceDate
, CASE GroupID
WHEN 'Group' THEN (
SELECT TOP 1 GroupID
FROM #Edges E2
WHERE E.MemberCode = E2.MemberCode
AND E.Diagnosis = E2.Diagnosis
AND E.ServiceDate > E2.ServiceDate
AND GroupID != 'Group'
ORDER BY ServiceDate DESC
)
ELSE GroupID END AS GroupID
FROM #Edges E
) Z
GROUP BY MemberCode, Diagnosis, GroupID
ORDER BY MemberCode, Diagnosis, MIN(ServiceDate)
Like Gordon said, more cumbersome, but it can be done if your server is not SQL 2012 or greater.

How can I create multiple rows from a single row (sql server 2008)

In our organisation we have a central purchasing company (CPC) who then sells to our retail company (Company_X) via an intercompany PO, who then sells on to the customer.
What I need to do is link our retail sale back to the original purchase order.
For example I have a table which contains the following (and a multitude of other columns):
Company_X_Sales:
InterCO_PO_no Sales_Order_No Part_No Qty
------------- -------------- ------- ---
12345 98765 ABCD 10
I then have a table which has the following:
CPC_Sales:
PO_Number InterCO_SO_No Part_No Qty
--------- ------------- ------- ---
00015 12345 ABCD 5
00012 12345 ABCD 2
00009 12345 ABCD 4
00007 12345 ABCD 3
So you can see that the final sale of 10 items was made up of parts which came from more than 1 external POs in the central company.
What I need to be able to do is replicate the rows in Company_X_Sales, include the Original PO Number and set the quantities as in CPC_Sales.
I need to end up with something like this:
Company_X_Sales_EXTD:
PO_Number InterCO_PO_no Sales_Order_No Part_No Qty
--------- ------------- -------------- ------- ---
00007 12345 98765 ABCD 3
00009 12345 98765 ABCD 4
00012 12345 98765 ABCD 2
00015 12345 98765 ABCD 1
I have to use the Company_X_Sales as my driving table - the CPC_Sales is simply as a lookup to derive the original PO Number.
Hoping you can help I am working through the weekend on this as it is part of a piece of work which has a very aggressive timescale.
I do not mind if the solution requires more than one pass of the table or creation of views if needed. I am just really really struggling.
I'm a little confused by your question, but it sounds like you're trying to make your Company_X_Sales table have 3 rows instead of 1, just with varying quantities? If so, something like this should work:
SELECT S.PO_Number, C.InterCO_PO_no, C.Sales_Order_No, C.Part_No, S.Qty
FROM Company_X_Sales C
JOIN CPC_Sales S ON C.InterCO_PO_no = S.InterCO_SO_No
Here is the SQL Fiddle.
That will give you the 4 rows with the correct quantities. Then you can delete and reinsert accordingly.
To get those rows into the table, you have a few options, but something like this should work:
--Flag the rows for deletion
UPDATE Company_X_Sales SET Qty = -1 -- Or some arbitrary value that does not exist in the table
--Insert new correct rows
INSERT INTO Company_X_Sales
SELECT C.InterCO_PO_no, C.Sales_Order_No, C.Part_No, S.Qty
FROM Company_X_Sales C
JOIN CPC_Sales S ON C.InterCO_PO_no = S.InterCO_SO_No
--Cleanup flagged rows for deletion
DELETE FROM Company_X_Sales WHERE Qty = -1
Good luck.
select [PO_Number],[InterCO_SO_No], Company_X_Sales.Sales_Order_No, [Part_No],[Qty] from CPC_Sales inner join Company_X_Sales on Company_X_Sales.InterCO_PO_no = CPC_Sales.InterCO_SO_no
Simple inner join on two tables will get you the required result IMHO.