Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple column values in where clause

I need to display the Single user schedule for every week like Timetable ,

Scenario : A faculty is assigned to Multiple batches in a single week (E.g : BBA,Maths and Forenoon for Hour 1 and 2) & (MBA, Maths, Forenoon for Hour 3&4) in a same date say (30-06-2015).I row of gridview will store as 1 and 2 row as stores as 2 and so on.........

My Table Definition :

CREATE TABLE [dbo].[test] (
    [datedif]     NVARCHAR (50)  NOT NULL,
    [hour]         INT              NULL,
    [subject]    NVARCHAR (MAX) NULL,
    [faculty]    NVARCHAR (MAX) NULL,
    [attendence] BIT            NULL,
    [dayweek]     NVARCHAR (50)  NULL,
    [weekmonth]   NVARCHAR (MAX) NULL,
    [batch]       NVARCHAR (MAX) NULL,
    [section]     NVARCHAR (MAX) NULL,
    [session]     NVARCHAR (MAX) NULL
);

Table Looks Like :

Datefdiff | hour | subject | faulty| batch
----------+-------+----------+---------+-------+----------+--------+-------+----------+---------+-------
30-06-2015| 1| Maths    | Kevin   | BBA
30-06-2015| 2| Science  | Amal    | MBA
30-06-2015|3 | chemistry|Jaya     |BBA
30-06-2015|4 | chemistry|Jaya     |BBA 
30-06-2015|5 | chemistry|Jaya     |BBA
31-06-2015 |1| science  | Amal    |BBA
31-06-2015 |2| Maths    | kevin   |BBA 
31-06-2015 |3| Science  | Amal    |BBA 
31-06-2015 |4 | chemistry|Jaya     |BBA
31-06-2015 |5| science  | Amal    |BBA

Expected Output to be given for only the Faculty: Amal

Datefdiff |hour|subject| batch |hour|subject | batch |faculty|hour | subject | batch | hour | subject | batch| hour | subject | batch | 
----------+-------+----------+-------+-------+----------+-------+-------+----------+
30-06-2015| 1    | Maths| BBA| 2| Science  | MBA   | 3| Science  |   BBA| 4| chemistry| BBA | 5 |Physics |MBA
31-06-2015| 1    | Maths| BBA| 2| Science  | MBA   | 3| Science  |   BBA| 4| chemistry| BBA | 5 |Physics |MBA

enter image description here

like image 245
Prhem Avatar asked Nov 01 '22 01:11

Prhem


2 Answers

This might be a starting point for your table design:

declare @tbFaculty table (
      FacultyID int --identity(1,1) primary key
    , Name varchar(50)
)

insert into @tbFaculty ( FacultyID, Name )
values    ( 1, 'Kevin' )
        , ( 2, 'Amal' )

declare @tbBatch table(
      BatchID int --identity(1,1) primary key
    , Name char(3)
)

insert into @tbBatch ( BatchID, Name )
values    ( 1, 'BBA' )
        , ( 2, 'MBA' )

declare @tbClass table (
      [Hour] tinyint
    , [Subject] nvarchar (128)
    , [FacultyID] int
    , [Attendence] bit
    , [BatchID] char(3)
    , [ClassDate] date
)

insert into @tbClass ( [Hour], [Subject], FacultyID, Attendence, BatchID, ClassDate )
values    ( 1, 'Maths', 1, 1, 1, '2015-06-30' )
        , ( 2, 'Maths', 1, 1, 1, '2015-06-30' )
        , ( 3, 'Science', 2, 1, 1, '2015-06-30' )
        , ( 1, 'Science', 2, 1, 2, '2015-06-30' )
        , ( 2, 'Science', 2, 1, 2, '2015-06-30' )
        , ( 3, 'Maths', 1, 1, 2, '2015-06-30' )

select
    cl.ClassDate
    , cl.[Hour]
    , cl.[Subject]
    , ba.Name as BatchName
    , fa.Name as FacultyName
from
    @tbClass cl
    inner join @tbBatch ba on ba.BatchID = cl.BatchID
    inner join @tbFaculty fa on fa.FacultyID = cl.FacultyID
where
    fa.Name = 'Amal'

You could normalize the Subject as well.

like image 105
Addy G Avatar answered Nov 15 '22 05:11

Addy G


I'm just going to add this a s a separate answer. Try this out:

--INSERT INTO dbo.test(datedif,[hour],[subject],faculty,batch)
--VALUES
--('30-06-2015',1,'Maths','Kevin','BBA'),
--('30-06-2015',2,'Science','Amal','MBA'),
--('30-06-2015',3,'chemistry','Jaya','BBA'),
--('30-06-2015',4,'chemistry','Jaya','BBA'),
--('30-06-2015',5,'chemistry','Jaya','BBA'),
--('31-06-2015',1,'science','Amal','BBA'),
--('31-06-2015',2,'Maths','kevin','BBA'),
--('31-06-2015',3,'Science','Amal','BBA'),
--('31-06-2015',4,'chemistry','Jaya','BBA'),
--('31-06-2015',5,'science','Amal','BBA');

WITH CTE_Hours
AS
(
    SELECT
            1 AS hour1,
            MAX(CASE WHEN [Hour] = 1 THEN [subject] END)    AS subject1,
            MAX(CASE WHEN [Hour] = 1 THEN [batch] END)      AS batch1 ,

            2 AS hour2,
            MAX(CASE WHEN [Hour] = 2 THEN [subject] END)    AS subject2,
            MAX(CASE WHEN [Hour] = 2 THEN [batch] END)      AS batch2

            --etc...
    FROM dbo.test
    WHERE faculty = 'Amal'
)

SELECT *
FROM    (
            SELECT DISTINCT datedif
            FROM dbo.test
        ) A
CROSS JOIN CTE_Hours
like image 45
Stephan Avatar answered Nov 15 '22 06:11

Stephan