Join 2 tables: one data table and one table of statut and get statut with no entrie - sql

I have this query:
SELECT c.Show_Code, req.Statut_ID, COUNT(req.Statut_ID) 'Count'
FROM [Case] c
JOIN Request req ON req.Case_Number = c.Number
GROUP BY c.Show_Code, req.Statut_ID
The result is:
Show_Code Statut_ID Count
564900 2 1
568127 2 1
And I have this statut table (Ref_RequestStatut)
ID Name
1 Test
2 Test2
How can I get this result:
Show_Code Statut_ID Count
564900 1 0
564900 2 1
568127 1 0
568127 2 1
I want all the statut, even those which have no value?
Thank

If you are using SQL Server 2005 or later:
WITH counted AS (
SELECT c.Show_Code, req.Statut_ID, COUNT(req.Statut_ID) 'Count'
FROM [Case] c
JOIN Request req ON req.Case_Number = c.Number
GROUP BY c.Show_Code, req.Statut_ID
),
showcodes AS (
SELECT DISTINCT Show_Code
FROM counted
)
SELECT
s.Show_Code,
r.ID AS Statut_ID,
Count = ISNULL(c.Count, 0)
FROM showcodes s
CROSS JOIN Ref_RequestStatut r
LEFT JOIN counted c ON s.Show_Code = c.Show_Code AND r.ID = c.Statut_ID
ORDER BY
s.Show_Code,
r.ID

Related

sqlite return same value on left join

in below my sqlite command i want to get count of barcoeds when that's equals with sessions.id
select sessions.id, sessions.session_name, sessions.session_type,sessions.date_time, count(barcodes.id) as barcode_count
from sessions left join barcodes
on sessions.id = barcodes.session_id
group by barcodes.id
order by sessions.id desc
this command works, but that return more data with same value, for example if data is one, that return more than 3, but really i have one row
0 = {_List} size = 5
0 = 11
1 = "111"
2 = 2
3 = "1398/05/14 ساعت: 08:43"
4 = 1
1 = {_List} size = 5
0 = 11
1 = "111"
2 = 2
3 = "1398/05/14 ساعت: 08:43"
4 = 1
2 = {_List} size = 5
0 = 11
1 = "111"
2 = 2
3 = "1398/05/14 ساعت: 08:43"
4 = 1
I think you want one row per session. So, your query is aggregating by the wrong column:
select s.id, s.session_name, s.session_type,
s.date_time, count(b.id) as barcode_count
from sessions s left join
barcodes b
on s.id = b.session_id
group by s.id
---------^ sessions not barcode
order by s.id desc;
You might find this also easy to do with a correlated subquery:
select s.*,
(select count(*) from barcodes b where b.session_id = s.id)
from sessions s;
The use of table aliases in these queries makes them easier to write and to read.
First count the ids for each session_id in table barcodes and then join to sessions:
select
s.id, s.session_name, s.session_type, s.date_time,
coalesce(b.barcode_count, 0) barcode_count
from sessions s left join (
select session_id, count(id) barcode_count
from barcodes
group by session_id
) b on s.id = b.session_id
order by s.id desc
I guess id is unique in the table barcodes so there is no need for count(distinct id).

Select Total and If Read

I have the following tables:
Book
Id Title
1 Test
BookPage (BookId corresponds to Id from Book table)
Id BookId Page
1 1 1
1 1 2
BookUserPage (BookPageId corresponds to Id from BookPage table)
UserId BookPageId
1 1
2 2
3 1
My select query is as follows:
SELECT B.[Id], B.[Title], BP.[Id], BP.[BookId], BP.[Page], COUNT(BUP.[BookpageId]) AS Total
FROM [Book] B
LEFT OUTER JOIN [BookPage] BP ON BP.[BookId] = B.[Id]
LEFT OUTER JOIN [BookUserPage] BUP ON BUP.[BookPageId] = BP.[Id]
WHERE B.[Id] = 1
GROUP BY B.[Id], B.[Title], BP.[Id], BP.[BookId], BP.[Page]
The result I get is the following:
Id, Title, Id, BookId, Page, Total
1 Test 1, 1, 1, 2
1 Test 2, 1, 1, 1
I'm trying to modify the query so that it will also tell me which of the 2 pages were read by the user.
I have tried the following:
SELECT B.[Id], B.[Title], BP.[Id], BP.[BookId], BP.[Page], COUNT(BUP.[BookpageId]) AS Total,
CASE WHEN EXISTS (
SELECT BUP2.[UserId]
FROM [PollUserAnswer] BUP2
WHERE BUP2.[UserId] = '98ad813b-cd0e-4a63-b40a-e09ee84f4b96')
THEN 1
ELSE 0
END AS Voted
FROM [Book] B
LEFT OUTER JOIN [BookPage] BP ON BP.[BookId] = B.[Id]
LEFT OUTER JOIN [BookUserPage] BUP ON BUP.[BookPageId] = BP.[Id]
WHERE B.[Id] = 1
GROUP BY B.[Id], B.[Title], BP.[Id], BP.[BookId], BP.[Page]
But the above puts a 1 on both rows of my result. I have also tried adding in the Case statement a condition:
AND BUP2.[BookPageId] = BUP.[PageId]
But that can't work due to group by and i can't list it in the group by as its a subquery.
My Desired Output for user 1 and 3 is this:
Id, Title, Id, BookId, Page, Total, Read
1 Test 1, 1, 1, 2, 1
1 Test 2, 1, 1, 1, 0
My Desired Output for user 2 is this:
Id, Title, Id, BookId, Page, Total, Read
1 Test 1, 1, 1, 2, 0
1 Test 2, 1, 1, 1, 1
Note: Please ignore the fact that there are 2 Id columns in the query output.
I would join to separate subquery to find the total number of users who read a given page. Then, you only need to tag on another left join to BookUserPage to generate the Read column:
SELECT b.Id, b.Title, bp.Id, bp.BookId, bp.Page, bup1.total,
CASE WHEN bup2.UserId IS NULL THEN 0 ELSE 1 END AS [Read]
FROM Book b
LEFT JOIN BookPage bp
ON bp.BookId = b.Id
LEFT JOIN
(
SELECT BookPageId, COUNT(*) AS total
FROM BookUserPage
GROUP BY BookPageId
) bup1
ON bup1.BookPageId = bp.Id
LEFT JOIN BookUserPage bup2
ON bup2.BookPageId = bp.Id AND bup2.UserId = 1
WHERE b.Id = 1;
Demo
This is not generating the results you expect, but it seems logically correct given your actual sample data and the join logic.

Select one row that is a duplicate and also select the other rows that are not duplicates

here is my code
select i.RefNo,i.Premium,i.Description from (select d.Description,c.IsActiveRecord,c.RefNo,c.MovementID, c.Premium,ROW_NUMBER()
over(partition by c.premium order by c.refno) n from lif_mgm_t_contract c
inner join SDT_LJG_T_MovementDescription d with (nolock) on c.MovementID = d.MovementID ) i
where i.n = 1 and i.MovementID <> 0
so for instance
table
a
a
b
I want the query to return
a
b and not just a hope this makes sense --only a beginner
try using a group by clause
SELECT OrderNumber
FROM AccountOrder
group by OrderNumber
so if my table has the following order numbers
1
2
2
3
the query would return
1
2
3

Display Summary Result in SQL Server

I have the following table structure also I have mention my expected output please help me with query as I don't know much about SQL query
Table 1 : Category
Name CatId
A 1
B 2
C 3
Table 2 : Emp Details
FName Id Dob CatId
Pratik 1 1958-04-06 2
Praveen 3 1972-05-12 1
Nilesh 2 1990-12-12 2
So far I have tried to get all result with:
SELECT A.Code,A.EmpName,A.DOB,B.cname
FROM EMPMASTER A
JOIN CATMASTER B ON A.cCode = B.ccode AND A.Compcode = B.CompCode
WHERE A.compcode = 'C0001' AND month(A.DOB) >= 1
AND MONTH(A.DOB) <= 12 AND A.termflag='L'
ORDER BY A.DOB
But my problem is, I also want summary results to be displayed
Expected Summary Output :
Grouping No Of Employees
A 1
B 2
C 0
I think you can use LEFT JOIN, GROUP BY and COUNT as follows:
SELECT [Grouping] = c.Name,
[No Of Employees] = COUNT(e.ID)
FROM Category AS c
LEFT JOIN EmpDetails AS e
ON e.CatId = c.CatId
GROUP BY c.Name;
TRY THIS:
SELECT A.NAME,
(SELECT COUNT(*) FROM #EMP B WHERE A.CATID = B.CATID) AS COUNT
FROM #TEMP A

How to find rows that have one equal value and one different value from the table

I have the following table:
ID Number Revision
x y 0
x y 1
z w 0
a w 0
a w 1
b m 0
b m 0
I need to return rows that for the same Number thare are more then one ID with the same Revision.Number can be "Null" and I don't need those values.
The output should be:
z w 0
a w 0
I have tried the following query:
SELECT a.id,a.number,a.revision,
FROM table a INNER JOIN
(SELECT id, number, revision FROM table where number > '0'
GROUP BY number HAVING COUNT(*) > 1
) b ON a.revision = b.revision AND a.id != b.id
A little addition- I have rows in my table with the same Number, ID and Revision- I don't need those rows in my query to be displayed!
It is not working! Please help me to figure out how to fix it.
Thanks.
Select t.Id,s.number,t.revision
from (Select number,count(*) 'c'
from table t1
where revision=0
group by number
having count(*) > 1
) s join table t on t.number= s.number
where revision = 0
Another simple approach:
SELECT DISTINCT b.id, b.Number, b.Revision
FROM tbl a
INNER JOIN tbl b
ON a.ID != b.ID AND a.Number = b.Number AND a.Revision = b.Revision;
This is tested in MySql 5, syntax might differ slightly.
You are not that far away with your query:
SELECT a.id,a.number,a.revision
FROM table a
JOIN (
-- multiple id for the same number and revision
SELECT number, revision
FROM table
GROUP BY number, revision
HAVING COUNT(*) > 1
) b
ON a.revision = b.revision
AND a.number = b.number
Untested, but you should get the idea. If your sql-server is a resent version you can solve this with OLAP functions as well.
To filter out rows where the whole row is duplicated we can select only unique rows via group by and having:
SELECT a.id,a.number,a.revision
FROM table a
JOIN (
-- multiple id for the same number and revision
SELECT number, revision
FROM table
GROUP BY number, revision
HAVING COUNT(*) > 1
) b
ON a.revision = b.revision
AND a.number = b.number
GROUP BY a.id,a.number,a.revision
HAVING COUNT(1) = 1