ms access max count - sql

Here is my query:
SELECT Count(*) AS CountOfJoker, tblPrediction.GameID
FROM (tblUser INNER JOIN (tblPool INNER JOIN tblUserPool ON tblPool.PoolID = tblUserPool.PoolID) ON tblUser.UserID = tblUserPool.UserID) INNER JOIN (tblResult INNER JOIN tblPrediction ON tblResult.GameID = tblPrediction.GameID) ON tblUser.UserID = tblPrediction.UserID
GROUP BY tblPrediction.GameID, tblPrediction.Joker, tblResult.GroupName, tblPool.PoolID
HAVING (((tblPrediction.Joker)=True) AND ((tblResult.GroupName)='A') AND ((tblPool.PoolID)=314));
Here is the output:
CountOfJoker GameID
2 1
2 2
2 9
1 10
1 17
3 18
I would like the MAX of CountOfJoker with the following output:
CountOfJoker GameID
3 18
Thanks
Drew

You could use select top and order by ... desc
SELECT TOP 1 Count(*) AS CountOfJoker, tblPrediction.GameID
FROM (tblUser INNER JOIN (tblPool INNER JOIN tblUserPool ON tblPool.PoolID = tblUserPool.PoolID) ON tblUser.UserID = tblUserPool.UserID) INNER JOIN (tblResult INNER JOIN tblPrediction ON tblResult.GameID = tblPrediction.GameID) ON tblUser.UserID = tblPrediction.UserID
GROUP BY tblPrediction.GameID, tblPrediction.Joker, tblResult.GroupName, tblPool.PoolID
HAVING (((tblPrediction.Joker)=True) AND ((tblResult.GroupName)='A') AND ((tblPool.PoolID)=314));
ORDER BY Count(*) DESC

Related

how to generate a report in sql server from a previous select

Periodically I need to generate a report based on some data from database.
The first select IS:
SELECT DISTINCT
A.PRO_C_NOME,
B.ETS_C_NOME
FROM WETI_ETAPA_ITEM F
INNER JOIN WETE_ETAPA_ITEM_EDITORS E
ON E.ETE_N_ETI_N_CODIGO = F.ETI_N_CODIGO
INNER JOIN WETA_ETAPA_PROCESSO H
ON H.ETA_N_CODIGO = F.ETI_N_ETA_N_CODIGO
INNER JOIN WIPR_ITEM_PROCESSO C
ON C.IPR_N_CODIGO = F.ETI_N_IPR_N_CODIGO
INNER JOIN WIWF_ITEM_WORKFLOW D
ON D.IWF_N_CODIGO = C.IPR_N_IWF_N_CODIGO
INNER JOIN WPRO_PROCESSO A
ON A.PRO_N_CODIGO = C.IPR_N_PRO_N_CODIGO AND PRO_N_DELETED = 0
LEFT OUTER JOIN WISP_ITEM_SUBPROCESS G
ON G.ISP_N_ID = F.ETI_ISP_N_ID
INNER JOIN WETS_ETAPA_SLA B
ON F.ETI_N_ETS_N_CODIGO = B.ETS_N_CODIGO
Returns something like this:
C1
C2
A
1
A
1
A
3
B
2
B
2
B
2
B
3
B
3
B
3
C
1
C
2
I need a report from that first select returning something like this:
S.1
S.2
S.3
S.4
A
2
0
1
B
0
3
3
C
1
1
0
that is:
S.1 - distinct values from C1
S.2 - count 1 values in C2 for the S.1 value
S.3 - count 2 values in C2 for the S.1 value
S.4 - count 3 values in C2 for the S.1 value
Can someone help how do I solve this?
I tried many solutions, such as using temporary tables and selecting from another select but doesn't work at all for different reasons.
;with cte(PRO_C_NOME, ETS_C_NOME) as (
SELECT DISTINCT
A.PRO_C_NOME,
B.ETS_C_NOME
FROM WETI_ETAPA_ITEM F
INNER JOIN WETE_ETAPA_ITEM_EDITORS E
ON E.ETE_N_ETI_N_CODIGO = F.ETI_N_CODIGO
INNER JOIN WETA_ETAPA_PROCESSO H
ON H.ETA_N_CODIGO = F.ETI_N_ETA_N_CODIGO
INNER JOIN WIPR_ITEM_PROCESSO C
ON C.IPR_N_CODIGO = F.ETI_N_IPR_N_CODIGO
INNER JOIN WIWF_ITEM_WORKFLOW D
ON D.IWF_N_CODIGO = C.IPR_N_IWF_N_CODIGO
INNER JOIN WPRO_PROCESSO A
ON A.PRO_N_CODIGO = C.IPR_N_PRO_N_CODIGO AND PRO_N_DELETED = 0
LEFT OUTER JOIN WISP_ITEM_SUBPROCESS G
ON G.ISP_N_ID = F.ETI_ISP_N_ID
INNER JOIN WETS_ETAPA_SLA B
ON F.ETI_N_ETS_N_CODIGO = B.ETS_N_CODIGO
)
SELECT PRO_C_NOME AS 'S.1'
,SUM(CASE WHEN ETS_C_NOME=1 THEN 1 ELSE 0 END) AS 'S.2'
,SUM(CASE WHEN ETS_C_NOME=2 THEN 1 ELSE 0 END) AS 'S.3'
,SUM(CASE WHEN ETS_C_NOME=3 THEN 1 ELSE 0 END) AS 'S.4'
FROM cte
GROUP BY PRO_C_NOME

Most efficient way to SELECT and JOIN these tables

I have 4 tables:
Table1(Groups):
id_group name_group
1 abc
2 def
3 ghi
Table2(Color):
id_color name_color
1 blue
2 red
3 green
Table3(Variety):
id_variety id_color id_group name_variety
1 1 1 light
2 3 1 dark
3 6 2 dark
Table4(Tone):
id_tone id_color id_group name_tone
1 1 1 ocean
2 5 1 sea
3 9 3 clay
Given an id_group like 1 I want to make a selection resulting something like this:
id_group id_color name_variety name_tone
1 1 light ocean
1 3 dark NULL
1 5 NULL sea
I was able to solve the problem with an union and changing the order of the JOINS but I think that there must be another solution
SELECT
GRO.id_group, COL.id_color, VARI.name_variety, TONE.name_tone
FROM
groups GRO
LEFT OUTER JOIN
variety VARI ON (VARI.id_group = GRO.id_group)
LEFT OUTER JOIN
tone TONE ON (TONE.id_group = GRO.id_group)
AND (TONE.id_color = VARI.id_color)
LEFT OUTER JOIN
color COL ON (COL.id_color = VARI.id_color)
OR (COL.id_color = TONE.id_color)
WHERE
GRO.id_group = 1
UNION
SELECT
GRO.id_group, COL.id_color, VARI.name_variety, TONE.name_tone
FROM
groups GRO
LEFT OUTER JOIN
tone TONE ON (TONE.id_group = GRO.id_group)
LEFT OUTER JOIN
variety VARI ON (VARI.id_group = GRO.id_group)
AND (VARI .id_color = TONE.id_color)
LEFT OUTER JOIN
color COL ON (COL.id_color = VARI.id_color)
OR (COL.id_color = TONE.id_color)
WHERE
GRO.id_group = 1
I think what you want is something more like:
SELECT
id_group,
id_color,
MAX(max_variety),
MAX(max_tone)
FROM
(SELECT
v.id_group,
v.id_color,
MAX(name_variety) AS max_variety,
MAX(name_tone) AS max_tone
FROM
variety V INNER JOIN
color c ON
V.id_color = c.id_color LEFT OUTER JOIN
tone t ON
t.id_group = v.id_group AND
t.id_color = V.id_color
WHERE
v.id_group = 1
GROUP BY
v.id_group,
v.id_color
UNION ALL
SELECT
t.id_group,
t.id_color,
MAX(name_variety) AS max_variety,
MAX(name_tone) AS max_tone
FROM
tone t INNER JOIN
color c ON
t.id_color = c.id_color LEFT OUTER JOIN
variety V ON
v.id_group = t.id_group AND
v.id_color = t.id_color
WHERE
t.id_group = 1
GROUP BY
t.id_group,
t.id_color) u
GROUP BY
id_group,
id_color
instead of using union you could use full join , as full join return all result from first table and also all results from the second tables.
SELECT
GRO.id_group, COL.id_color, VARI.name_variety, Tone.name_tone from Variety VARI
join Color COL on col.id_color=VARI col.id
join Groups GRO on GRO.id_group=VARI.id_group
full join Tone on Tone.id_group=GRO.id_group
where GRO.id_group = 1

How to join with two tables simultaneously?

Table Product
Id name t1 t2
1 A 1 4
2 B 5 2
3 C 3 1
4 D 4 5
Table Tan
id tan
1 tanA
2 tanB
3 tanC
4 tanD
5 tanE
I have two above table and i want the result as below in expecting result how it is possible.
Expecting result
A tanA tanD
B tanE tanB
C tanC tanA
D tanD tanE
You can join on multiple tables:
SELECT p.Name AS ProductName,
t1.tan AS Tan1,
t2.tan AS Tan2
FROM dbo.Product p
INNER JOIN Tan t1
ON p.t1 = t1.id
INNER JOIN Tan t2
ON p.t2 = t2.id
ORDER BY ProductName ASC
Demo
You can select from the tan table twice by using a table alias:
FROM Product INNER JOIN tan tan1 ON tan1.id = product.t1
INNER JOIN tan tan2 ON tan2.id = product.t2
select the appropriate columns:
SELECT Product.name AS name, tan1.tan AS Tan_1, tan2.tan AS Tan_2
to give:
SELECT product.name AS name, tan1.tan AS Tan_1, tan2.tan AS Tan_2
FROM Product INNER JOIN tan tan1 ON tan1.id = product.t1
INNER JOIN tan tan2 ON tan2.id = product.t2

SQL tricky query

I have this query
select lab.IDLAB,
lab.NOMLAB,
lab.CAPACIDAD
from laboratorio lab
inner join DETALLESOLICITUD det on det.IDLAB = lab.IDLAB
inner join dia on dia.iddia = det.iddia
inner join bloque blo on blo.idbloque = det.idbloque
inner join solicitud sol on sol.IDSOLICITUD=det.IDSOLICITUD
where blo.idbloque = 1
and dia.iddia = 1
and sol.estado in (1,2)
which returns:
IDLAB | NOMLAB | CAPACIDAD
----------------------------
1 | LCOMP1 | 22
And it does exactly what I want it to do. Now I want to get all the records from the table laboratorios that doesn't appear in this query. For example my laboratorios table has contents:
IDLAB | NOMLAB | CAPACIDAD
----------------------------
1 | LCOMP1 | 22
2 | LCOMP2 | 31
3 | LCOMP3 | 17
4 | LCOMP4 | 26
And I want the following output:
IDLAB | NOMLAB | CAPACIDAD
----------------------------
2 | LCOMP2 | 31
3 | LCOMP3 | 17
4 | LCOMP4 | 26
I tried with a not exists statement this way:
select *
from laboratorio
where not exists(
select lab.idlab, lab.nomlab, lab.CAPACIDAD
from laboratorio lab
inner join DETALLESOLICITUD det on det.idlab = lab.idlab
inner join dia on dia.iddia = det.iddia
inner join bloque blo on blo.idbloque = det.idbloque
inner join solicitud sol on sol.IDSOLICITUD = det.IDSOLICITUD
where blo.idbloque = 1
and dia.iddia = 1
and sol.estado in(1,2)
);
and this way:
select *
from laboratorio
where not exists(
select det.IDLAB
from DETALLESOLICITUD det
inner join dia on dia.iddia = det.iddia
inner join bloque blo on blo.idbloque = det.idbloque
inner join solicitud sol on sol.IDSOLICITUD = det.IDSOLICITUD
where blo.idbloque = 1
and dia.iddia = 1
and sol.estado in(1,2)
);
but both returns nothing. Any help will be really appreciate.
Your sub-queries return rows. You know that because of the first query. But where not exists is only true when the sub-query returns no rows. Check it out:
SQL> select * from dual
2 /
D
-
X
SQL> select * from dual
2 where not exists (select * from dual
3 where dummy = 'X')
4 /
no rows selected
SQL> select * from dual
2 where not exists (select * from dual
3 where dummy = 'Y')
4 /
D
-
X
SQL>
So what you need to do is correlate the outer query with the sub-query. Easiest way to do this:
select * from laboratorio
where (idlab, nomlab, CAPACIDAD)
not in (select lab.idlab, lab.nomlab, lab.CAPACIDAD
from laboratorio lab
inner join DETALLESOLICITUD det on det.idlab = lab.idlab
inner join dia on dia.iddia = det.iddia
inner join bloque blo on blo.idbloque = det.idbloque
inner join solicitud sol on sol.IDSOLICITUD=det.IDSOLICITUD
where blo.idbloque = 1
and dia.iddia = 1
and sol.estado in(1,2)
)
select * from laboratorio lab1 WHERE NOT EXISTS (
select 1 from laboratorio lab
inner join DETALLESOLICITUD det on det.IDLAB = lab.IDLAB
inner join dia on dia.iddia = det.iddia inner join bloque blo on blo.idbloque = det.idbloque
inner join solicitud sol on sol.IDSOLICITUD=det.IDSOLICITUD
where blo.idbloque = 1 and dia.iddia = 1 and sol.estado in (1,2)
AND lab1.rowid = lab.rowid)
Check this out. I think you did not join with the outer table.

Fetch Max Value

TABLE Laptop_Shift_Departments
id Laptop_ID Curr_department Pre_Department
-----|----------|-----------------|----------------
9 71 4 3
10 68 4 3
11 71 5 4
12 68 5 4
User only search PO Number and against this PO_Num there are 2 laptops (Laptop_ID)
Above is my table now i want to get MAX value Against this laptop_ID ID
I have tried this:
SELECT LD.ID AS Laptop_ID,ld.GulfITBarcode,po.ID as PO_ID,PO.PO_Number as PO_Number,D.Department,
D.ID as Crr_DepID1,d2.Department,D2.ID as Pre_DepID2
FROM PO_PURCHASEORDER PO
INNER JOIN PO_Laptop_Master LM
ON LM.PO_ID = PO.ID
INNER JOIN PO_LaptopDetail LD
ON LD.LapTop_Master_ID = LM.ID
INNER JOIN Laptop_Shift_Departments DP
ON DP.Laptop_Detail_ID = LD.ID
inner join Laptop_Departments d
on d.ID = DP.Current_Dep_ID
inner join Laptop_Departments d2
on d2.ID = DP.Previous_Dep_ID
WHERE PO_NUMBER = '5258'
AND
LD.ID IN (select Max(cSh.id) from Laptop_Shift_Departments cSh)
But it is not working
MY output should be like this:
id Laptop_ID Curr_department Pre_Department
-----|----------|-----------------|----------------
11 71 5 4
12 68 5 4
You'll want to use the 'Group By' function at the end of the fields you want to group by and the 'Max' function in the Select on the columns you want the 'Max' (or 'Min' or 'Avg'). Should look something like this:
SELECT
LD.ID AS Laptop_ID
,ld.GulfITBarcode
,po.ID as PO_ID
,PO.PO_Number as PO_Number
,D.Department
,MAX(D.ID) as Crr_DepID1
,d2.Department
,MAX(D2.ID) as Pre_DepID2
FROM PO_PURCHASEORDER PO
INNER JOIN PO_Laptop_Master LM
ON LM.PO_ID = PO.ID
INNER JOIN PO_LaptopDetail LD
ON LD.LapTop_Master_ID = LM.ID
INNER JOIN Laptop_Shift_Departments DP
ON DP.Laptop_Detail_ID = LD.ID
inner join Laptop_Departments d
on d.ID = DP.Current_Dep_ID
inner join Laptop_Departments d2
on d2.ID = DP.Previous_Dep_ID
WHERE PO_NUMBER = '5258'
AND LD.ID IN (select Max(cSh.id) from Laptop_Shift_Departments cSh)
AND DP.Current_Dep_ID = 5
GROUP BY
LD.ID
,ld.GulfITBarcode
,po.ID
,PO.PO_Number
,D.Department
,d2.Department
(Having not seen the table structure I'm not sure as to the rest of the query. Hope this helps.)