mysql count from same table and data from the other table - sql

I want to display the name of the registered users with the count of regid by supplying replyid, I don't know what will be the correct query to get the results
Here are the tables.
details_table
id regid replyid
-------------------
1 1 2
2 1 3
6 2 4
5 3 4
8 2 5
9 3 5
10 4 5
11 5 5
12 2 6
13 6 6
14 4 6
15 7 7
16 8 7
17 9 7
18 10 8
19 2 9
20 2 10
21 11 10
22 12 10
reg_table
id regname
---------------
1 Sam
2 Ash
3 Tina
4 Rohny
5 Martin
6 Natasha
7 Natalia
8 Kim
9 Alex
10 John
11 Neil
12 Peter
So if replyid i.e. (10) is select from details_table by where clause, it's suppose to display the 2,11,12 i.e. (Ash,Neil,Peter) from reg_table with the count of Ash=5,Neil=1,Peter=1

SELECT a.id, a.regname, COUNT(1)
FROM reg_table a, details_table b,
details_table c
WHERE b.replyid=10
AND b.regid = a.id
AND c.regid = a.id
GROUP BY a.id, a.regname

SELECT r.regid, r.regname, count(*)
FROM (
SELECT DISTINCT regid
FROM details_table
WHERE replyid = 10
) rg
JOIN reg_table r ON rg.regid = r.regid
JOIN details_table d ON r.regid = d.regid
GROUP BY r.regid

try this
select count(dt.regid) as cnt, regname from details_table dt, reg_table rt where dt.regid = rt.Regid and dt.replyid = 10 group by rt.Regid

SELECT reg_table.regname, count(*) from reg_table, details_table where details_table.regid = reg_table.id GROUP BY reg_table.id

Related

How to implement this requirement?

I have table
** Table:**
MTID code boxnumber
1 10-01 10
1 10-02 10
1 10-03 10
1 10-04 10
1 10-05 10
1 11-01 11
1 11-02 11
1 11-03 11
1 11-04 11
1 11-05 11
1 12-01 12
1 12-02 12
1 12-03 12
2 13 NULL
2 14 NULL
2 15 NULL
2 16 NULL
2 17 NULL
2 18 NULL
2 19 NULL
Requirement:
In return result, MTID 2 all rows and
MTID 1 conatins full box (count(boxnumber) = 5 where MTID = 1)
In the expected result, code = 12-xx WHERE MTID = 1 not return, because it not full box.
The Expected Result
MTID code boxnumber
1 10-01 10
1 10-02 10
1 10-03 10
1 10-04 10
1 10-05 10
1 11-01 11
1 11-02 11
1 11-03 11
1 11-04 11
1 11-05 11
2 13 NULL
2 14 NULL
2 15 NULL
2 16 NULL
2 17 NULL
2 18 NULL
2 19 NULL
Anyone can Help on this?
Thanks.
We can try using COUNT as an analytic function here to assert the count of 5 requirement. Matching rows either belong to MTID = 1 and having a count of 5 boxnumber, or some other value for MTID.
SELECT MTID, code, boxnumber
FROM
(
SELECT *, COUNT(*) OVER (PARTITION BY MTID, boxnumber) cnt
FROM yourTable
) t
WHERE (MTID = 1 AND cnt = 5) OR MTID <> 1;
Demo

Fill in sequence numbers in sql oracle

I have a table that includes two columns, these columns have ranges i.e
Batch from _serial_no to_serial_no
a 1 5
b 2 7
I want to create another column to fill in the gaps for a abd b separately
Something like this
Batch from _serial_no to_serial_no seq_number
a 1 5 1
a 1 5 2
a 1 5 3
a 1 5 4
a 1 5 5
b 2 7 2
b 2 7 3
b 2 7 4
b 2 7 5
b 2 7 6
b 2 7 7
Is there an sql I could use?
I tried something like this but it didn't work
select *
from (
select a.*,rownum n
from my_table a connect by level <= TO_SERIAL_NO
)
where n >= FROM_SERIAL_NO;
SQL> with
2 data as (select 'a' batch, 1 from_serial_number, 5 to_serial_number from dual
3 union all
4 select 'b' batch, 2 from_serial_number, 7 to_serial_number from dual),
5 seq as (select rownum n# from dual connect by level <= (select max(to_serial_number) from data))
6 select
7 data.*,
8 seq.n#
9 from
10 data,
11 seq
12 where
13 seq.n# between data.from_serial_number and data.to_serial_number
14 order by
15 1, 2, 4;
BATCH FROM_SERIAL_NUMBER TO_SERIAL_NUMBER N#
----- ------------------ ---------------- ----------
a 1 5 1
a 1 5 2
a 1 5 3
a 1 5 4
a 1 5 5
b 2 7 2
b 2 7 3
b 2 7 4
b 2 7 5
b 2 7 6
b 2 7 7
11 rows selected
Yet another option:
SQL> with test (batch, from_serial_no, to_serial_no) as
2 (select 'a', 1, 5 from dual union
3 select 'b', 2, 7 from dual
4 )
5 select
6 batch,
7 from_serial_no,
8 to_serial_no,
9 froM_serial_no + column_value - 1 seq_number
10 from test,
11 table(cast(multiset(select level from dual
12 connect by level <= to_serial_no - from_serial_no + 1
13 ) as sys.odcinumberlist))
14 order by batch, seq_number;
B FROM_SERIAL_NO TO_SERIAL_NO SEQ_NUMBER
- -------------- ------------ ----------
a 1 5 1
a 1 5 2
a 1 5 3
a 1 5 4
a 1 5 5
b 2 7 2
b 2 7 3
b 2 7 4
b 2 7 5
b 2 7 6
b 2 7 7
11 rows selected.
SQL>
Using a join
select d.*, t.seq_number
from data d
join
(
SELECT from_serial_no + level - 1 seq_number
FROM (select min(from_serial_no) from_serial_no,
max(to_serial_no) to_serial_no
from data) t
CONNECT BY from_serial_no + level - 1 <= to_serial_no
) t on d.from_serial_no <= t.seq_number and
d.to_serial_no >= t.seq_number
order by d.batch, t.seq_number;
dbfiddle demo

summarising a 3 months sales report across 2 branches into top 3 product for each month

I have the following REPORT table
m = month,
pid = product_id,
bid = branch_id,
s = sales
m pid bid s
--------------------------
1 1 1 20
1 3 1 11
1 2 1 14
1 4 1 16
1 5 1 31
1 1 2 30
1 3 2 10
1 2 2 24
1 4 2 17
1 5 2 41
2 3 1 43
2 5 1 21
2 4 1 10
2 1 1 5
2 2 1 12
2 3 2 22
2 5 2 10
2 4 2 5
2 1 2 4
2 2 2 10
3 3 1 21
3 5 1 10
3 4 1 44
3 1 1 4
3 2 1 14
3 3 2 10
3 5 2 5
3 4 2 6
3 1 2 7
3 2 2 10
I'd like to have a summary of this sales table
by showing the top 3 sales among the products across all branches.
something like this:
m pid total
---------------------
1 5 72
1 1 50
1 4 33
2 3 65
2 5 31
2 2 22
3 4 50
3 3 31
3 2 24
so on month 1, product #5 has the highest total sales with 72, followed by product #1 is 50.. and so on. if i could separate them into different table for each month would be better
so far what i can do is make a summary for 1 month and shows the entire thing and not top 3.
select pid, sum(s)
from report
where m = 1
group by pid
order by sum(s);
thanks a lot!
Most databases support the ANSI standard window functions. You can do what you want with row_number():
select m, pid, s
from (select r.m, r.pid, sum(s) as s,
row_number() over (partition by m order by sum(s) desc) as seqnum
from report r
group by r.m, r.pid
) r
where seqnum <= 3
order by m, s desc;

SQL(Oracle) select the most common value (multiple tables)

I want to know which seat was the most sold by individual halls?
TICKETS
IDTICKET MOVIE_IDMOVIE HALL_IDHALL PRICE SEAT ROW
1 10 2 4 10 6
2 5 2 4 10 5
3 5 2 4 10 4
4 8 5 4 3 1
5 7 5 4 4 15
6 10 7 4 7 9
7 6 2 4 14 3
HALLS
IDHALL PLACE_IDPLACE NAME NUMSEATS EQUIPMENT
1 5 A1 250 high
2 5 B1 200 medium
3 5 B2 200 medium
4 5 C2 180 medium
5 5 C2 180 medium
6 9 old hall 120 low
Display should look like
B1 10
C2 3
...
SELECT b.Name, a.Seat
FROM (SELECT Hall, Seat, COUNT(1) AS SeatCount, RANK() OVER (PARTITION BY Hall ORDER BY COUNT(1) DESC) AS SeatRank
FROM SEAT
GROUP BY Hall, Seat ) a
INNER JOIN
HALL b
ON a.HALL_IDHALL = b.IDHALL
WHERE a.SeatRank = 1
select h.name,t.hall_idhall,h.idhall, max(t.seat) from tickets t, hall h where t.hall_idhall=h.idhall
group by h.name,t.hall_idhall,h.idhall
Try the above query

Find multiple records with same Id in SQL server

Find multiple records with same Id in SQL server
I have following value in my table
PK Id Value1 Value2
1 1 5 10
2 1 10 10
3 2 5 20
4 3 20 25
5 3 5 5
6 4 10 10
7 1 5 20
8 4 5 10
9 2 25 30
10 5 25 30
11 5 5 20
12 5 5 5
I want to get 'Id' column which have two record with some values like (First Record) Value1 = 5, Value2 = 20 and (Second Record) Value1 =25, Value2 = 30.
In Above table my expected result is
PIds Id Value1 Value2
3 2 5 20
9 2 25 30
10 5 25 30
11 5 5 20
Thanks in Advance.
Please try:
select * from YourTable
where
(Value1=5 and Value2=20) OR
(Value1 =25 and Value2 = 30)
All other query shows 1, 2 and 5 Ids
I have found this query for my problem
select T1.Id,* from TestData T1 inner join TestData T2
on T1.Id = T2.Id
where
(T2.Value1=5 and T2.Value2=20) and
(T1.Value1=25 and T1.Value2 = 30)