select data that has at least P and R - sql

I have a table named Table1 as shown below:
ID AccountNo Trn_cd
1 123456 P
2 123456 R
3 123456 P
4 12345 P
5 111 R
6 111 R
7 5625 P
I would like to display those records that accountNo appears more than one time (duplicate) and trn_cd has at least both P and R.
In this case the output should be at this way:
ID AccountNo Trn_cd
1 123456 P
2 123456 R
3 123456 P
I have done this sql but not the result i want:
select * from Table1
where AccountNo IN
(select accountno from table1
where trn_cd = 'P' or trn_cd = 'R'
group by AccountNo having count(*) > 1)
Result as below which AccountNo 111 shouldn't appear because there is no trn_cd P for 111:
ID AccountNo Trn_cd
1 123456 P
2 123456 R
3 123456 P
5 111 R
6 111 R
Any idea?

Use aggregation for this. To get the account numbers:
select accountNo
from table1
having count(*) > 1 and
sum(case when trn_cd = 'P' then 1 else 0 end) > 0 and
sum(case when trn_cd = 'N' then 1 else 0 end) > 0
To get the account information, use a join or in statement:
select t.*
from table1 t
where t.accountno in (select accountNo
from table1
having count(*) > 1 and
sum(case when trn_cd = 'P' then 1 else 0 end) > 0 and
sum(case when trn_cd = 'N' then 1 else 0 end) > 0
)

This problem is called Relational Division.
This can be solved by filtering the records which contains P and R and counting the records for every AccountNo returned, and filtering it again using COUNT(DISTINCT Trn_CD) = 2.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT AccountNo
FROM TableName
WHERE Trn_CD IN ('P','R')
GROUP BY AccountNo
HAVING COUNT(DISTINCT Trn_CD) = 2
) b ON a.AccountNO = b.AccountNo
SQLFiddle Demo
SQL of Relational Division
OUTPUT
╔════╦═══════════╦════════╗
║ ID ║ ACCOUNTNO ║ TRN_CD ║
╠════╬═══════════╬════════╣
║ 1 ║ 123456 ║ P ║
║ 2 ║ 123456 ║ R ║
║ 3 ║ 123456 ║ P ║
╚════╩═══════════╩════════╝
For faster performance, add an INDEX on column AccountNo.

Related

Merge 1st and Second Row, 3rd and 4th Row and so on

Lets say I have table with rows,
Id Value
----------
1 a
1 b
1 c
1 d
1 e
1 f
and the expected result should be,
Id Value1 Value2
-------------------
1 a b
1 c d
1 e f
I am very confused here.
Ok, there's definitely a simpler way to do this, but this works:
WITH CTE AS
(
SELECT *,
RN = ROW_NUMBER() OVER(PARTITION BY Id ORDER BY Value)
FROM dbo.YourTable
)
SELECT Id,
MIN(CASE WHEN RN % 2 = 1 THEN Value END) Value1,
MIN(CASE WHEN RN % 2 = 0 THEN Value END) Value2
FROM CTE
GROUP BY Id,
RN - ((RN - 1) % 2);
This is the result:
╔════╦════════╦════════╗
║ Id ║ Value1 ║ Value2 ║
╠════╬════════╬════════╣
║ 1 ║ a ║ b ║
║ 1 ║ c ║ d ║
║ 1 ║ e ║ f ║
╚════╩════════╩════════╝
;WITH cte AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY value) AS RowNum
FROM YourTable
)
SELECT
c1.id
, c1.value as Value1
, c2.value as Value2
FROM cte c1
LEFT JOIN cte c2 ON c1.rownum = c2.rownum - 1
WHERE c1.RowNum % 2 = 1
select Id
,min(Value) as Value1
,max(Value) as Value2
from (select Id,Value
,(row_number () over
(partition by Id order by Value)+1)/2 as group_id
from mytable as t
) t
group by Id
,group_id

Getting Results from inner join differences on same row

Currently have sql returning a result set as below
WORKFLOWID UNMATCHEDVALUE MATCHEDADDRESS EXCEPTIONREASON
1001 UNIQUE ADDRESS1 (null)
1001 UNIQUE ADDRESS2 Some Value
What I am looking for is a result like this
WORKFLOWID UNMATCHEDVALUE MATCHEDADDRESS EXCEPTIONREASON MATCHEDADDRESS2 EXCEPTIONREASON2
1001 UNIQUE ADDRESS1 (null) ADDRESS2 Some Value
So the "variant" columns are MatchedAddress and Exception Reason, the other columns will be the same for each record. Note that for each workflow_id, will always have 2 rows coming back.
I have also created a fiddle to show the schema.
http://sqlfiddle.com/#!6/f7cde/3
Try this:
;WITH CTE AS
(
SELECT ws.id as WorkflowStepId,
ws.workflow_id as WorkflowId,
sg.unmatchValue as UnmatchedValue,
geo_address as MatchedAddress,
ws.exception_Value as ExceptionReason,
ROW_NUMBER() OVER(PARTITION BY ws.workflow_id ORDER BY ws.id) as RN
FROM workflow_step as ws
INNER JOIN workflow as gw
ON ws.workflow_id = gw.id
INNER JOIN super_group as sg
ON gw.super_group_id = sg.id
INNER JOIN alias on
ws.id = alias.workflow_step_id
)
SELECT WorkflowId,
UnmatchedValue,
MIN(CASE WHEN RN = 1 THEN MatchedAddress END) MatchedAddress,
MIN(CASE WHEN RN = 1 THEN ExceptionReason END) ExceptionReason,
MIN(CASE WHEN RN = 2 THEN MatchedAddress END) MatchedAddress2,
MIN(CASE WHEN RN = 2 THEN ExceptionReason END) ExceptionReason2
FROM CTE
GROUP BY WorkflowId,
UnmatchedValue
ORDER BY workflowId
Here is the modified sqlfiddle.
The results are:
╔════════════╦════════════════╦════════════════╦═════════════════╦═════════════════╦══════════════════╗
║ WORKFLOWID ║ UNMATCHEDVALUE ║ MATCHEDADDRESS ║ EXCEPTIONREASON ║ MATCHEDADDRESS2 ║ EXCEPTIONREASON2 ║
╠════════════╬════════════════╬════════════════╬═════════════════╬═════════════════╬══════════════════╣
║ 1001 ║ UNIQUE ║ ADDRESS1 ║ (null) ║ ADDRESS2 ║ Some Value ║
╚════════════╩════════════════╩════════════════╩═════════════════╩═════════════════╩══════════════════╝
Try this:
SELECT ws.workflow_id as WorkflowId, sg.unmatchValue as UnmatchedValue,
MAX(CASE WHEN ws.id = 1 THEN geo_address END) as MatchedAddress1,
MAX(CASE WHEN ws.id = 2 THEN geo_address END) as MatchedAddress2,
MAX(CASE WHEN ws.id = 1 THEN ws.exception_Value END) as ExceptionReason1,
MAX(CASE WHEN ws.id = 2 THEN ws.exception_Value END) as ExceptionReason2
FROM workflow_step as ws
INNER JOIN workflow as gw on ws.workflow_id = gw.id
INNER JOIN super_group as sg on gw.super_group_id = sg.id
inner JOIN alias on ws.id = alias.workflow_step_id
GROUP BY ws.workflow_id, sg.unmatchValue
SQL FIDDLE DEMO
Since I can't comment, I just wanted to point out that the answer given by Lamak is using a Common Table Expression. These are generally your best option for solving a recursion problem in sql.
This assumes you only have 2 address types. If you have more I would recommend creating a pivot table.
select a.*, MATCHEDADDRESS2,EXCEPTIONREASON2
from
(Select WORKFLOWID,UNIQUEVALUE,MATCHEDADDRESS,EXCEPTIONREASON
from "Your Table"
where MATCHEDADDRESS='ADDRESS1') a
join
(Select WORKFLOWID,UNIQUEVALUE,MATCHEDADDRESS as MATCHEDADDRESS2,EXCEPTIONREASON as XCEPTIONREASON2
from "Your Table"
where MATCHEDADDRESS='ADDRESS2') b
on a.WORKFLOWID=b.WORKFLOWID
and a.UNMATCHEDVALUE = b.UNMATCHEDVALUE

sql Pivot how can I do this

I have 3 tables
------------------------
users
1 -> mark
2 -> adel
3 -> khali
4 -> piter
5 -> adam
------------------------
groups
1 -> group 1
2 -> group 2
3 -> group 3
4 -> group 4
----------------------
usersGroups
1 -> 4
3 -> 2
4 -> 3
1 -> 2
I want to display
if username has a group then 1 if it hasn't then 0
like this whith pivot but I dont know how ???
-- username group1 group2 group3 group4
----------------------------------------------------
-- mark 0 1 0 1
-- adel 0 1 0 0
-- adam 0 0 1 0
I try this please help me
SELECT username, [group1] AS 'group1', [group2] AS 'group2', [group3] AS 'group3', [group4] AS 'group4'
FROM
(
SELECT ug.groupid, ug.userid, g.description, u.username FROM users u
INNER JOIN usersgroups ug ON u.userid = ug.userid
INNER JOIN groups g ON ug.groupid = g.groupid
)AS q
PIVOT
(COUNT(groupid) FOR [description] IN ([group1],[group2],[group3],[group4])) AS pvt
Test Data
DECLARE #users TABLE (userid INT, username NVARCHAR(100))
INSERT INTO #users
VALUES (1,'mark'),(2,'adel'),(3,'khali'),(4,'piter'),(5,'adam')
DECLARE #groups TABLE (groupid INT, [description] NVARCHAR(100))
INSERT INTO #groups
VALUES
(1,'group 1'),(2,'group 2'),(3,'group 3'),(4,'group 4')
DECLARE #usersGroups TABLE (userid INT, groupid INT)
INSERT INTO #usersGroups
VALUES (1,4),(3,2),(4,3),(1,2)
Query
SELECT username
, CASE WHEN [group 1] IS NOT NULL THEN 1 ELSE 0 END AS 'group1'
, CASE WHEN [group 2] IS NOT NULL THEN 1 ELSE 0 END AS 'group2'
, CASE WHEN [group 3] IS NOT NULL THEN 1 ELSE 0 END AS 'group3'
, CASE WHEN [group 4] IS NOT NULL THEN 1 ELSE 0 END AS 'group4'
FROM
(
SELECT ug.groupid, ug.userid, g.description, u.username
FROM #users u INNER JOIN #usersGroups ug
ON u.userid = ug.userid
INNER JOIN #groups g
ON ug.groupid = g.groupid
)AS q
PIVOT
(MIN(groupid)
FOR [description]
IN ([group 1],[group 2],[group 3],[group 4])
) AS pvt
Result Set
╔══════════╦════════╦════════╦════════╦════════╗
║ username ║ group1 ║ group2 ║ group3 ║ group4 ║
╠══════════╬════════╬════════╬════════╬════════╣
║ mark ║ 0 ║ 1 ║ 0 ║ 1 ║
║ khali ║ 0 ║ 1 ║ 0 ║ 0 ║
║ piter ║ 0 ║ 0 ║ 1 ║ 0 ║
╚══════════╩════════╩════════╩════════╩════════╝
SELECT case when ug.groupid is null then 0 else 1 end as groupid,
ug.userid, g.description, u.username FROM users u
LEFT JOIN usersgroups ug ON u.userid = ug.userid
INNER JOIN groups g ON ug.groupid = g.groupid
and then change aggregate COUNT(groupid) to MAX(groupid)

Group by does not show all the rows

I have a table tblPersonaldata and tblStudentsadmitted
tblPersonalData
UID Name Gender
------------------------
E1 xyz M
E2 pqr M
E3 mno M
tblStudentsadmitted
UID Status Stage
----------------------
E1 Y 1
E2 Y 2
E3 Y 1
Now I want the data like this:
Gender Stage1 Stage2
M 2 1
But in this case I dont get the data for female gender. I want the data for female gender even if it is null
I have tried this:
select
case
when gender='M' then 'Male'
when gender='F' then 'Female'
end as Gender,
sum(case when Stage=1 then 1 else 0) end as Stage1,
sum(case when Stage=2 then 1 else 0) end as Stage2
from tblPersonaldata A inner join
tblStudentsadmitted B on A.UID=B.UID
where B.Status='Y'
group by Gender
SELECT CASE WHEN a.Gender = 'M' THEN 'Male' ELSE 'FEMALE' END Gender,
SUM(CASE WHEN Stage = 1 THEN 1 ELSE 0 END) Stage1,
SUM(CASE WHEN Stage = 2 THEN 1 ELSE 0 END) Stage2
FROM personal a
LEFT JOIN studentadmitted b
ON a.UID = b.UID AND b.Status = 'Y'
GROUP BY a.Gender
SQLFiddle Demo
SELECT CASE WHEN c.Gender = 'M' THEN 'Male' ELSE 'Female' END Gender,
SUM(CASE WHEN Stage = 1 THEN 1 ELSE 0 END) Stage1,
SUM(CASE WHEN Stage = 2 THEN 1 ELSE 0 END) Stage2
FROM (SELECT 'F' Gender UNION SELECT 'M' Gender) c
LEFT JOIN personal a
ON a.Gender = c.Gender
LEFT JOIN studentadmitted b
ON a.UID = b.UID AND b.Status = 'Y'
GROUP BY c.Gender
SQLFiddle Demo
OUTPUT
╔════════╦════════╦════════╗
║ GENDER ║ STAGE1 ║ STAGE2 ║
╠════════╬════════╬════════╣
║ Female ║ 0 ║ 0 ║
║ Male ║ 2 ║ 1 ║
╚════════╩════════╩════════╝
In SQL Server, you can use the PIVOT function to generate the result:
select gender,
Stage1,
Stage2
from
(
select
c.gender,
'Stage'+cast(stage as varchar(10)) Stage
from (values ('F'),('M')) c (gender)
left join tblpersonaldata p
on c.gender = p.gender
left join tblStudentsadmitted s
on p.uid = s.uid
and s.Status='Y'
)src
pivot
(
count(stage)
for stage in (Stage1, Stage2)
) piv
See SQL Fiddle with Demo.
Since you are using SQL Server 2008 this query uses the VALUES to generate the list of the genders that you want in the final result set
from (values ('F'),('M')) c (gender)
Then by using a LEFT JOIN on the other tables the final result will return a row for both the M and F values.
This can also be written using a UNION ALL to generate the list of genders:
select gender,
Stage1,
Stage2
from
(
select
c.gender,
'Stage'+cast(stage as varchar(10)) Stage
from
(
select 'F' gender union all
select 'M' gender
) c
left join tblpersonaldata p
on c.gender = p.gender
left join tblStudentsadmitted s
on p.uid = s.uid
and s.Status='Y'
)src
pivot
(
count(stage)
for stage in (Stage1, Stage2)
) piv
See SQL Fiddle with Demo
The result of both is:
| GENDER | STAGE1 | STAGE2 |
----------------------------
| F | 0 | 0 |
| M | 2 | 1 |
This is also working. Using Left joins with a new table (a table with two records for genders M & F).
Fiddle demo
select t.g Gender,
isnull(sum(case when Stage = 1 then 1 end),0) Stage1,
isnull(sum(case when Stage = 2 then 1 end),0) Stage2
from (values ('M'),('F')) t(g)
left join personal a on t.g = a.gender
left join studentadmitted b on a.uid = b.uid and b.Status = 'Y'
group by t.g
order by t.g
| GENDER | STAGE1 | STAGE2 |
----------------------------
| F | 0 | 0 |
| M | 2 | 1 |
SELECT GENDER, 0 AS 'STAGE 0', 1 AS 'STAGE 1', 2 AS 'STAGE 2'
FROM
(
SELECT P.ID, GENDER,CASE WHEN STAGE IS NULL THEN 0 ELSE STAGE END STAGE
FROM tblPersonaldata P
LEFT JOIN tblStudentsadmitted S ON P.UID = S.UID
) AS A
PIVOT
(
COUNT (ID) FOR STAGE IN ([0],[1],[2])
)P

Using GROUP BY and ORDER BY on an INNER JOIN SQL query

I am using the following query to group work times and expenses for clients from three tables, one for clients, one for work times and one for expenses:
SELECT a.*,
COALESCE(b.totalCount, 0) AS CountWork,
COALESCE(b.totalAmount, 0) AS WorkTotal,
COALESCE(c.totalCount, 0) AS CountExpense,
COALESCE(c.totalAmount, 0) AS ExpenseTotal
FROM clients A
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount
FROM work_times
WHERE DATE BETWEEN '2013-01-01' AND '2013-02-01'
GROUP BY Client
) b ON a.Client = b.Client
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount
FROM expenses
WHERE DATE BETWEEN '2013-01-01' AND '2013-02-01'
GROUP BY Client
) c ON a.Client = c.Client
WHERE b.Client IS NOT NULL OR
c.Client IS NOT NULL
You can see the query working in a fiddle here.
I am trying to amend this query so that there is a row for each client for each month sorted by month and then client. I am trying to do so with the following amended query:
SELECT a.*,
COALESCE(b.totalCount, 0) AS CountWork,
COALESCE(b.totalAmount, 0) AS WorkTotal,
COALESCE(c.totalCount, 0) AS CountExpense,
COALESCE(c.totalAmount, 0) AS ExpenseTotal
FROM clients A
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount,
SUBSTR(Date, 1, 7) as Month
FROM work_times
GROUP BY Month,Client
ORDER BY Month
) b ON a.Client = b.Client
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount,
SUBSTR(Date, 1, 7) as Month
FROM expenses
GROUP BY Month,Client
ORDER BY Month,Client
) c ON a.Client = c.Client
WHERE b.Client IS NOT NULL OR
c.Client IS NOT NULL
You can see the amended query in action here.
It's not working quite right though. Only one row is returned for Client B even though there is a work time in January 2013 and an expense in February 2013 (so there should be 2 rows) and it appears that the rows are being ordered by Client as opposed to Month. Could someone suggest how to amend the query to get the desired output which for the example on the second fiddle would be:
╔════════╦═══════════╦═══════════╦══════════════╦══════════════╗
║ CLIENT ║ COUNTWORK ║ WORKTOTAL ║ COUNTEXPENSE ║ EXPENSETOTAL ║
╠════════╬═══════════╬═══════════╬══════════════╬══════════════╣
║ A ║ 1 ║ 10 ║ 1 ║ 10 ║
║ B ║ 1 ║ 20 ║ 0 ║ 0 ║
║ A ║ 1 ║ 15 ║ 0 ║ 0 ║
║ B ║ 0 ║ 0 ║ 1 ║ 10 ║
║ C ║ 1 ║ 10 ║ 0 ║ 0 ║
╚════════╩═══════════╩═══════════╩══════════════╩══════════════╝
Unless I am missing something in the requirements, what you need to do is get a list of the clients and the dates and then join that to your subqueries. So your query will be:
SELECT a.*,
COALESCE(b.totalCount, 0) AS CountWork,
COALESCE(b.totalAmount, 0) AS WorkTotal,
COALESCE(c.totalCount, 0) AS CountExpense,
COALESCE(c.totalAmount, 0) AS ExpenseTotal
FROM
(
select distinct c.Client, d.Month
from clients c
cross join
(
select SUBSTR(Date, 1, 7) as Month
from work_times
union
select SUBSTR(Date, 1, 7) as Month
from expenses
) d
) A
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount,
SUBSTR(Date, 1, 7) as Month
FROM work_times
GROUP BY Month,Client
ORDER BY Month,Client
) b
ON a.Client = b.Client
and a.month = b.month
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount,
SUBSTR(Date, 1, 7) as Month
FROM expenses
GROUP BY Month,Client
ORDER BY Month,Client
) c
ON a.Client = c.Client
and a.month = c.month
WHERE b.Client IS NOT NULL OR
c.Client IS NOT NULL
order by a.month, a.client
See SQL Fiddle with Demo.
The result is:
| CLIENT | MONTH | COUNTWORK | WORKTOTAL | COUNTEXPENSE | EXPENSETOTAL |
--------------------------------------------------------------------------
| A | 2013-01 | 1 | 10 | 1 | 10 |
| B | 2013-01 | 1 | 20 | 0 | 0 |
| A | 2013-02 | 1 | 15 | 0 | 0 |
| B | 2013-02 | 0 | 0 | 1 | 20 |
| C | 2013-02 | 1 | 10 | 0 | 0 |
If you do an order by in a sub-query, it doesn't matter, because the outer query may (and may need to) re-order the results. You want to add an order by to the outer query.
Your problem is that you are trying to order by the month and client of the B table, and also order by the month and client of the C table. You need to define the order of B.month, B.client, and C.month and put it into an order by for the outer query.
BTW, if you only group by month in the sub-query for the C table, then client is not meaningful. Some databases, like DB2, do not allow you to put an unaggregated field in a select if it is not in the group by.