sql statement how to keep null value - sql

This query here is executed on 3 tables(students) one which makes a zero on the exam so his q.correctanswer != ur.response, how can I keep the 0 value in my output. When i execute this statement it only gives me two values instead of 3 for each student.
select count(*) as Test1
from users u, question q, userresponse ur
where q.eno = '1'
and q.eno = ur.eno
and q.qno = ur.qno
and ur.response = q.correctanswer
and u.uno = ur.uno
group by u.uno
sample data:
Note:
- "||" is used to concatenate strings
- each string is between a pair of single quotes.
- each record is between "(" and ")".
- fields are ordered according to fields in tables
- "\n" means a new line
======exams data==========
(1,'Java Programming',60,1)
(3,'Elementary History',10,3)
======questions data===========
(1,1,'Choose the operations that can be performed on String
objects:(A) +\n(B) + =\n(C) -\n(D) %\n(E) ^','B')
(1,2,'What will be the result of the expression\na % b\nwhen a and b
are of type int and their values are a = -17 and b = -6?','A')
(1,3,'What will be the result of the expression\na % b\nwhen a and b
are of type int and their values are a = 10 and b = 6?','B')
(1,4,'Consider the following code:\nint x, y, z\ny = 1\nz = 5\nx = 0
- (++y) + z++\nAfter execution of this, what will be the values of x, y and z?','C')
(1,5,'Identify the statements that are correct:\n(A) int a = 13,
a>>2 = 3\n(B) int b = -8, b>>1 = -4\n(C) int a = 13, a>>>2 = 3\n(D)
int b = -8, b>>>1 = -4','E')
(1,6,'If not assigned a value, a variable of type char has the
following default value:','A')
(1,7,'What will be the value of a after execution of the following
statements:\nint a = 23, b = 34\na = ((a < b) ? (b + a) : (b -
a)','B')
(1,8,'Which of the following is not a hexadecimal number?\n(A)
999\n(B) (hex)23\n(C) 0x556\n(D) 0x1F2','C')
(1,9,'if(check4Biz(storeNum) != null) {}\nReferring to the above,
what datatype could be returned by method check4Biz()?','D')
(1,10,'Select the invalid assignment statements from the
following:\n(A) float x = 238.88\n(B) double y = 0x443\n(C) int n =
(int) true\n(D) long m =778645','A')
(1,11,'int j\nfor(int i=0i<14i++) {\nif(i<10) {\nj = 2 +
i\n}\nSystem.out.println("j: " + j + " i: " + i)\n}\nWhat is WRONG
with the above code?','E')
(1,12,'Consider the following code:\nclass ClassA\n{\npublic static
void main(String args [ ])\n{\nClassB b = classB()\n}\nClassA(int x) {
}\n}\nclass ClassB extends ClassA\n{\n}\nWhat will happen when we
compile and run this code?','A')
(1,13,'The keywords reserved but not used in the initial version of
Java are:\n(A) union\n(B) const\n(C) inner\n(D) goto\n(E) boolean\n(F)
synchronized','C')
(1,14,'We would like to make a member of a class visible in all
subclasses regardless of what package they are in. Which one of the
following keywords would achieve this?','D')
(1,15,'Which of the following are not keywords?\n(A) NULL\n(B)
implements\n(C) protected\n(D) extended\n(E) string','B')
(3,1,'The Battle of Gettysburg was fought during which war?','C')
(3,2,'Neil Armstrong and Buzz Aldrin walked how many \n' ||
'minutes on the moon in 1696?','B')
(3,3,'Which Presidents held office during World War II?','D')
(3,4,'In a communist economic system, people:','B')
(3,5,'Which president did not die while in office?','D')
(3,6,'Which state refused to attend the Constitutional Convention
\n' ||
'in 1787 because it didn''t want the United States government \n' ||
'to interfere with already established state affairs?','A')
(3,7,'Who founded Buddhism?','A')
(3,8,'Where is India?','D')
(3,9,'What is the dominant religion in India?','B')
(3,10,'Near which river did archaeologists find India''s \n' ||
'first civilization?','B')
========== answerOption data ============
(3,8,'A','Australia') (3,8,'B','America') (3,8,'C','Africa')
(3,8,'D','Asia') (3,1,'A','World War II') (3,1,'B','The
Revolutionary War') (3,1,'C','The Civil War') (3,1,'D','World War I')
(3,2,'A','123') (3,2,'B','None') (3,2,'C','10') (3,2,'D','51')
(3,3,'A','Franklin D. Roosevelt') (3,3,'B','Dwight D. Eisenhower')
(3,3,'C','Harry Truman') (3,3,'D','Both A and C') (3,4,'A','Are
forced to work as slaves') (3,4,'B','Work for the common good')
(3,4,'C','Work from home computers') (3,4,'D','Don''t work')
(3,5,'A','John F. Kennedy') (3,5,'B','Franklin D. Roosevelt')
(3,5,'C','Abraham Lincoln') (3,5,'D','Ronald Reagan')
(3,5,'E','James A. Garfield') (3,7,'A','Siddharta Gautama')
(3,7,'B','Jesus Christ') (3,7,'C','Mahatma Gandhi')
(3,7,'D','Muhammad') (3,6,'A','Rhode Island') (3,6,'B','New
Hampshire') (3,6,'C','New Jersey') (3,6,'D','New York')
(3,9,'A','Islam') (3,9,'B','Hinduism') (3,9,'C','Christianity')
(3,9,'D','Buddhism') (3,10,'A','The Tiber River') (3,10,'B','The
Indus River') (3,10,'C','The Yellow River') (3,10,'D','The Nile
River') (1,1,'A','(D) & (E)') (1,1,'B',' (A) & (B)') (1,1,'C','(D)')
(1,1,'D','(A)') (1,1,'E','None of these') (1,2,'A','-17')
(1,2,'B','5') (1,2,'C','3') (1,2,'D','-5') (1,2,'E','None of these')
(1,3,'A','1.66') (1,3,'B','4') (1,3,'C','2') (1,3,'D','1')
(1,3,'E','None of these') (1,4,'A','x = 4, y = 1, z = 5') (1,4,'B',' x
= -7, y = 1, z = 5') (1,4,'C','x = 3, y = 2, z = 6') (1,4,'D',' x = 4, y = 2, z = 6') (1,4,'E',' x = 8, y = 2, z = 1') (1,5,'A','(C) & (D) ')
(1,5,'B',' (A), (B) & (C) ') (1,5,'C','(A), (B), (C) & (D)')
(1,5,'D','(A) & (B) ') (1,5,'E','None of the above')
(1,6,'A','\uffff') (1,6,'B','\u0000') (1,6,'C','" " (space)')
(1,6,'D','\u0001') (1,6,'E','None of the above') (1,7,'A','23')
(1,7,'B','Error. Cannot be executed.') (1,7,'C','57') (1,7,'D','11')
(1,7,'E','34') (1,8,'A','(A), (B) & (C)') (1,8,'B','(C)')
(1,8,'C','(A) & (B) ') (1,8,'D','(A)') (1,8,'E','(D)')
(1,9,'A','Boolean') (1,9,'B','Int') (1,9,'C','String')
(1,9,'D','Char') (1,9,'E','Byte') (1,10,'A','(A) & (C)')
(1,10,'B','(A) & (B)') (1,10,'C','(B) ') (1,10,'D','(B) & (D) ')
(1,10,'E','(D)') (1,11,'A','Integer "j" is not initialized.')
(1,11,'B','Nothing.') (1,11,'C','You cannot declare integer i inside
the for-loop declaration.') (1,11,'D','The syntax of the "if"
statement is incorrect.') (1,11,'E','You cannot print integer values
without converting them to strings.') (1,12,'A','Will compile and run
successfully ') (1,12,'B','Error. ClassA does not define a no-argument
constructor ') (1,12,'C','Error. There is no code in the constructor
ClassA(int x) ') (1,12,'D','Error. ClassB does not define a
no-argument constructor ') (1,12,'E','Error. There is no code in the
class ClassB ') (1,13,'A','(C) & (E) ') (1,13,'B','(B),(C) & (D)')
(1,13,'C','(A), (C) & (E)') (1,13,'D','All of these. ')
(1,13,'E','None of these. ') (1,14,'A','private ') (1,14,'B','public')
(1,14,'C','protected') (1,14,'D','private OR protected')
(1,14,'E','All of the above') (1,15,'A','(C), (D) & (E)')
(1,15,'B','(D) ') (1,15,'C','(A), (D) & (E)') (1,15,'D','(D) & (E) ')
(1,15,'E','(A)')
===== users data ====
(1,'1#gmail.com','','George','Ronald','','','Atlanta','GA','30303')
(2,'2#gmail.com','','Mary','Erikson','','','San Diego','CA','91901')
(3,'3#gmail.com','','John','Washington','','','Atlanta','GA','30314')
====enrolls data=====
(1,1,to_date('2012/02/15 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/22 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(2,1,to_date('2012/02/15 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/22 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(3,1,to_date('2012/02/17 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/24 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(1,3,to_date('2012/02/17 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/24 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(2,3,to_date('2012/02/20 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/27 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(3,3,to_date('2012/02/20 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/27 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
===userResponse data====
(1,1,1,'A') (1,1,2,'B') (1,1,3,'A') (1,1,4,'B') (1,1,5,'N')
(1,1,6,'C') (1,1,7,'D') (1,1,8,'A') (1,1,9,'B') (1,1,10,'B')
(1,1,11,'A') (1,1,12,'B') (1,1,13,'A') (1,1,14,'A') (1,1,15,'A')
(2,1,1,'B') (2,1,2,'B') (2,1,3,'E') (2,1,4,'C') (2,1,5,'D')
(2,1,6,'A') (2,1,7,'C') (2,1,8,'B') (2,1,9,'B') (2,1,10,'A')
(2,1,11,'A') (2,1,12,'B') (2,1,13,'B') (2,1,14,'A') (2,1,15,'A')
(3,1,1,'C') (3,1,2,'C') (3,1,3,'D') (3,1,4,'D') (3,1,5,'A')
(3,1,6,'A') (3,1,7,'D') (3,1,8,'D') (3,1,9,'A') (3,1,10,'A')
(3,1,11,'B') (3,1,12,'B') (3,1,13,'A') (3,1,14,'A') (3,1,15,'C')
(1,3,1,'N') (1,3,2,'A') (1,3,3,'A') (1,3,4,'B') (1,3,5,'B')
(1,3,6,'C') (1,3,7,'D') (1,3,8,'A') (1,3,9,'A') (1,3,10,'B')
(2,3,1,'B') (2,3,2,'C') (2,3,3,'A') (2,3,4,'C') (2,3,5,'B')
(2,3,6,'D') (2,3,7,'D') (2,3,8,'D') (2,3,9,'A') (2,3,10,'A')
(3,3,1,'B') (3,3,2,'A') (3,3,3,'A') (3,3,4,'B') (3,3,5,'B')
(3,3,6,'C') (3,3,7,'D') (3,3,8,'A') (3,3,9,'A') (3,3,10,'B')
Thanks,

What I think you're asking is "how many questions did any user get right?" If so, based on the query you have - and the fact that there's no direct relation between users and questions - I've come up with the following:
select u.uno, sum(case ur.response when null then 0 else 1 end) as Test1
from question q
cross join users u
left join userresponse ur
on q.eno = ur.eno
and q.qno = ur.qno
and q.correctanswer = ur.response
and u.uno = ur.uno
where q.eno = '1'
group by u.uno
So we do a cartesion join between users and questions, because we don't have a better way to join them. We then do an left outer join against the userresponse table; unless everything matches correctly, the user either didn't answer the question or answered it wrong. We cast whether or not that value's null to a 0 if null and 1 if if it's not, so the sum of those is the number of correct answers per user.

If I've understood your query correctly, the following ought to work. It uses the old Oracle join syntax:
SELECT u.uno, COUNT(*)
FROM users u, userresponse ur, question q
WHERE u.uno = ur.uno
AND ur.response = q.correctanswer(+)
AND ur.eno = q.eno(+)
AND ur.qno = q.qno(+)
AND q.eno(+) = 1
GROUP BY u.uno

I think that you are trying to get all users and the associated quiz score. If I understood correctly, then using appropriate joins clauses, you can easily get the expected results:
select u.uno, count (ur.response) as NumberAnswered, count (q.correctanswer) as NumberCorrect
from users u
join userresponse ur --inner join means we limit user rows to users who have responses
on u.uno = ur.uno
left outer join question q --outer join means we look for row data, but don't exclude users based on the fact that the row doesn't match the join conditions
on ur.response = q.correctanswer
and q.eno = 1 --only include rows where the question.eno value is 1(?)
and q.eno = ur.eno --only include rows where question.eno and userresponse.eno are the same (the answer was correct)
group by u.uno

Related

How to query Oracle grouping?

I have such a problem and I don't know how to solve it, can you help me? t
The query returns a result that is shown on the photo and I want to get it to be shown in one line instead of many based on type of age.
https://imgur.com/a/OA6CBpa
with x as (
select ai.invoice_id, ai.invoice_num, ai.invoice_amount, ai.amount_paid,
trial.entity_id, trial.acctd_amount, trial.entered_amount, trial.gl_date,
aps.amount_remaining, aps.gross_amount, aps.due_date, aps.payment_status_flag,
trial.gl_date - aps.due_date dni_opoznienia
from ap_invoices_all ai,
xla.xla_transaction_entities xte,
(
select nvl (tr.applied_to_entity_id, tr.source_entity_id) entity_id,
tr.source_application_id application_id,
sum (nvl (tr.acctd_unrounded_cr, 0)) - sum (nvl (tr.acctd_unrounded_dr, 0)) acctd_amount,
sum (nvl (tr.entered_unrounded_cr, 0)) - sum (nvl (tr.entered_unrounded_dr, 0)) entered_amount,
max(tr.gl_date) gl_date
from xla.xla_trial_balances tr
where 1=1
and tr.definition_code = 'AP_200_1001'
and tr.source_application_id = 200
and tr.gl_date <= fnd_date.canonical_to_date('2019-12-13') -- Data KG
group by nvl (tr.applied_to_entity_id, tr.source_entity_id),
tr.source_application_id
) trial,
ap_payment_schedules_all aps
where 1=1
and ai.invoice_id = 3568325
and nvl(xte.source_id_int_1, -99) = ai.invoice_id
and xte.ledger_id = 1001
and xte.entity_code = 'AP_INVOICES'
and xte.entity_id = trial.entity_id
and xte.application_id = trial.application_id
and ai.invoice_id = aps.invoice_id
)
select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id
Based on your comment I guess you need the below
select * from (select x.invoice_id, x.invoice_num, x.entity_id, x.acctd_amount, x.gl_date,
x.amount_remaining, x.gross_amount, x.due_date, x.payment_status_flag,
x.dni_opoznienia, aapl.days_start, aapl.days_to,
case
when x.dni_opoznienia between aapl.days_start and aapl.days_to then x.acctd_amount
else 0
end przedzial
from x,
ap_aging_periods aap,
ap_aging_period_lines aapl
where 1=1
and aap.period_name = 'TEST 5 okresow'
and aap.aging_period_id = aapl.aging_period_id)
where przedzial > 0;

SQL adding multiple clauses on the JOIN

I've got the query below which almost works. It's returning the correct number of records and the way I want the records formatted. The thing is I need to return data based on a family so I want the returned data to include the father and mother on a single row of data if they belong to that family or two rows if they have a different familyID.
I've got the following query, the only issue I have is it is returning father and mother on the same line even if they are separated. I've looked at the code and I know why its happening, due to the way I have joined the father and mother to relations table. To handle separations I have a table called family and my relation table contains the familyID key. I need to somehow incorporate to check whether mothers familyID = father familyID somewhere where I make my join on the relation table for both parents, but I'm a little stuck.
I started this query with INNER JOINS, but changed to LEFT JOINS as some of the pupil data didn't have a both parents, so the data returned incorrect number of rows - however seems to work OK with LEFT JOINS.
SELECT p.pk_PupilID, p.Title, p.Forename, p.Surname,
father.pk_PersonID, father.Title, father.Forename, father.Surname,
mother.pk_PersonID, mother.Title, mother.Forename, mother.Surname,
ff.Salutation
FROM Pupil AS p
LEFT JOIN RELATION AS father_r
ON p.pk_PupilID = father_r.fk_PersonID
AND father_r.Relationship IN ('Father','Father')
LEFT JOIN PERSON AS father
ON father_r.fk_RelatedPersonID = father.pk_PersonID
LEFT JOIN RELATION AS mother_r
ON p.pk_PupilID = mother_r.fk_PersonID
AND mother_r.Relationship IN ('Mother','Stepmother')
LEFT JOIN PERSON AS mother
ON mother_r.fk_RelatedPersonID = mother.pk_PersonID
LEFT JOIN Family AS ff
father_r.fk_FamilyID = ff.pk_FamilyID
OR
mother_r.fk_FamilyID = ff.pk_FamilyID
This post is under the assumption the database is MySQL and I can use #variables within the query
I would start with a query of all possible "Parent" types of father, mother or other and sort their respective priority to a position type for all families and group by the person ID in the relationship. Since you appear to be pulling for ALL pupils, we can pre-gather all possible parents per person. Using SQLVariables, we can help pre-group possible entities regardless of parental type relation.
SELECT
Family.fk_PersonID,
Family.fk_FamilyID,
#nextParent := if( #lastPerson = Family.fk_PersonID
AND #lastFamily = Family.fk_FamilyID, #nextParent, 1 ) as parentSeq,
#lastPerson := Family.fk_PersonID as lastPersonSeq,
#lastFamily := Family.fk_FamilyID as lastFamilySeq,
if( #nextParent == 1, Family.pk_PersonID, " " ) as Parent1PersonID,
if( #nextParent == 1, Family.Relationship, " " ) as Parent1Type,
if( #nextParent == 1, Family.Title, " " ) as Parent1Title,
if( #nextParent == 1, Family.Forename, " " ) as Parent1Forename,
if( #nextParent == 1, Family.Surname, " " ) as Parent1Surname,
if( #nextParent == 2, Family.pk_PersonID, " " ) as Parent2PersonID,
if( #nextParent == 2, Family.Relationship, " " ) as Parent2Type,
if( #nextParent == 2, Family.Title, " " ) as Parent2Title,
if( #nextParent == 2, Family.Forename, " " ) as Parent2Forename,
if( #nextParent == 2, Family.Surname, " " ) as Parent2Surname
from
( select
#lastPerson := 0,
#lastFamily := 0,
#nextParent := 0 ) sqlvars,
( SELECT
fr.fk_PersonID,
fk_FamilyID,
fr.fk_RelatedPersonID,
fr.relationship,
whatParent.pk_PersonID,
whatParent.Title,
whatParent.Forename,
whatParent.Surname,
CASE when fr.relationship in ('Father', 'Stepfather' ) then 1
when fr.relationship in ('Mother', 'Stepmother' ) then 2 end as reltype
from
Relation fr
JOIN PERSON AS whatParent
ON fr.fk_RelatedPersonID = whatParent.pk_PersonID
where
fr.relationship in ('Father', 'Stepfather', 'Mother', 'Stepmother' )
order by
fr.fk_PersonID,
fr.fk_FamilyID,
CASE when fr.relationship in ('Father', 'Stepfather' ) then 1
when fr.relationship in ('Mother', 'Stepmother' ) then 2 end ) as Family
group by
Family.fk_PersonID,
Family.fk_FamilyID
The inner order by clause will force any Father / Stepfather in the first position and mother / step mother in second. This now qualifies for all possible conditions of things like f=father, sf=step, m=mother, sm=step (f/m, f/sm, sf/m, f/f, m/m, m/sm, etc). If you want other parental considerations, add them to the inner where clause and relation ship type sequence for priority positioning.
Due to the group by of per person, it SHOULD be able to ignore issues about not having aggregate columns in the group by clause... of which, you could always wrap each of the OTHER non aggreates to MAX() such as
MAX( #nextParent := if( #lastPerson = Family.fk_PersonID, #nextParent, 1 )) as parentSeq,
MAX( #lastPerson := Family.fk_PersonID ) as lastPersonSeq,
MAX( if( #nextParent == 1, Family.Relationship, " " )) as Parent1Type,
MAX( if( #nextParent == 1, Family.Title, " " )) as Parent1Title,
MAX( if( #nextParent == 1, Family.Forename, " " )) as Parent1Forename,
MAX( if( #nextParent == 1, Family.Surname, " " )) as Parent1Surname,
MAX( if( #nextParent == 2, Family.Relationship, " " )) as Parent2Type,
MAX( if( #nextParent == 2, Family.Title, " " )) as Parent2Title,
MAX( if( #nextParent == 2, Family.Forename, " " )) as Parent2Forename,
MAX( if( #nextParent == 2, Family.Surname, " " )) as Parent2Surname
So now, all the parental relationships are all rolled up to one row based on the fk_PersonID AND FAMILY. Now, just join that to the pupil's table, something like..
SELECT
p.pk_PupilID,
parents.fk_FamilyID,
p.Title,
p.Forename,
p.Surname,
Parents.Parent1PersonID,
Parents.Parent1Type,
Parents.Parent1Title,
Parents.Parent1Forename,
Parents.Parent1Surname,
Parents.Parent2PersonID,
Parents.Parent2Type,
Parents.Parent2Title,
Parents.Parent2Forename,
Parents.Parent2Surname,
ff.Salutation
FROM
Pupil AS p
LEFT JOIN ( entire query from above ) as Parents
ON p.pk_PupilID = Parents.fk_PersonID
LEFT JOIN Family AS ff
ON parents.fk_FamilyID = ff.pk_FamilyID
ORDER BY
p.pk_PupilID,
parents.fk_FamilyID

Linq - SQL query conversion

Can some one help me to convert this query to linq? I dont know how to use union and then sum in linq.
SELECT Patientname, SUM(A)AS Trec
FROM (SELECT Pm.Patientname, COUNT(*)AS A
FROM Facilitycheckinholdorder Fcho
INNER JOIN Medordertype Mot ON Fcho.Orderseq = Mot.Orderseq
JOIN Patientmaster Pm ON Mot.Patientseq = Pm.Patientseq
AND Fcho.Filleddate BETWEEN '2011-09-01 00:00:00:000' AND '2012-10-16 00:00:00:000'
AND Mot.Facilityid = 139
GROUP BY Pm.Patientname
UNION ALL
SELECT Pm.Patientname, COUNT(*)AS A
FROM Rxqdeliveryholdorder Rdho
INNER JOIN Medordertype Mot ON Rdho.Orderseq = Mot.Orderseq
JOIN Patientmaster Pm ON Mot.Patientseq = Pm.Patientseq
AND Rdho.Filleddate BETWEEN '2011-09-01 00:00:00:000' AND '2012-10-16 00:00:00:000'
AND Mot.Facilityid = 139
GROUP BY Pm.Patientname
) AS Trec
GROUP BY Patientname;
In fact this is not a real question, but I've tried my best...
var lst1 =
from fhco in facilitycheckinholdorder
join mot in meordertype on mot.orderseq equals fhco.orderseq
join pm in patientmaster on mot.patientseq equals pm.patientseq
where
fcho.FilledDate >= new DateTime(2011, 9, 1)
fcho.FilledDate <= new DateTime(2012, 10, 16)
select pm;
var lst2 =
from rdho in rxqdeliveryholdorder
join mot in meordertype on rdho.orderseq equals mot.orderseq
join pm in patientmaster on moit.patientseq equals pm.patientseq
where
fcho.FilledDate >= new DateTime(2011, 9, 1)
fcho.FilledDate <= new DateTime(2012, 10, 16)
select pm;
var res =
from item in lst1.Union(lst2)
select new { Name = item.patientname, Count = lst1.Count() + lst2.Count() };
Key points:
Your outermost query seems redundant.
Your names are not common C# style, and it makes them difficult to read.
UNION ALL corresponds to Concat, not Union.
Getting the Concat out of the way early lets you streamline the query. You could do this in the SQL.
.
var result =
from order in Queryable.Concat(
FacilityCheckInHoldOrder.Select(o => new { o.OrderSeq, o.FilledDate }),
RxqDeliveryHoldOrder .Select(o => new { o.OrderSeq, o.FilledDate }))
where order.FilledDate >= new DateTime(2011, 9, 1) &&
order.FilledDate < new DateTime(2012, 10, 16)
join type in MedOrderType.Where(t => t.FacilityID == 139)
on order.OrderSeq equals type.OrderSeq
join patient in PatientMaster
on type.PatientSeq equals patient.PatientSeq
group by patient.PatientName into grp
select new
{
PatientName = grp.Key,
Trec = grp.Count()
};

sql returning no rows selected

how can I make this sql query return zero for the count function instead of no rows selected?
select ur.uno, count(*)*5
from userresponse ur, question q
where ur.eno = q.eno
and q.correctanswer = ur.response
and q.qno = ur.qno
and ur.uno = '1'
and q.eno = '1'
group by ur.uno
q.correctanswer = ur.response is never true for this query that's what causes the no row selected, however I want to return zero.
sample data
Note:
- "||" is used to concatenate strings
- each string is between a pair of single quotes.
- each record is between "(" and ")".
- fields are ordered according to fields in tables
- "\n" means a new line
======exams data==========
(1,'Java Programming',60,1)
(3,'Elementary History',10,3)
======questions data===========
(1,1,'Choose the operations that can be performed on String
objects:(A) +\n(B) + =\n(C) -\n(D) %\n(E) ^','B')
(1,2,'What will be the result of the expression\na % b\nwhen a and b
are of type int and their values are a = -17 and b = -6?','A')
(1,3,'What will be the result of the expression\na % b\nwhen a and b
are of type int and their values are a = 10 and b = 6?','B')
(1,4,'Consider the following code:\nint x, y, z\ny = 1\nz = 5\nx = 0
- (++y) + z++\nAfter execution of this, what will be the values of x, y and z?','C')
(1,5,'Identify the statements that are correct:\n(A) int a = 13,
a>>2 = 3\n(B) int b = -8, b>>1 = -4\n(C) int a = 13, a>>>2 = 3\n(D)
int b = -8, b>>>1 = -4','E')
(1,6,'If not assigned a value, a variable of type char has the
following default value:','A')
(1,7,'What will be the value of a after execution of the following
statements:\nint a = 23, b = 34\na = ((a < b) ? (b + a) : (b -
a)','B')
(1,8,'Which of the following is not a hexadecimal number?\n(A)
999\n(B) (hex)23\n(C) 0x556\n(D) 0x1F2','C')
(1,9,'if(check4Biz(storeNum) != null) {}\nReferring to the above,
what datatype could be returned by method check4Biz()?','D')
(1,10,'Select the invalid assignment statements from the
following:\n(A) float x = 238.88\n(B) double y = 0x443\n(C) int n =
(int) true\n(D) long m =778645','A')
(1,11,'int j\nfor(int i=0i<14i++) {\nif(i<10) {\nj = 2 +
i\n}\nSystem.out.println("j: " + j + " i: " + i)\n}\nWhat is WRONG
with the above code?','E')
(1,12,'Consider the following code:\nclass ClassA\n{\npublic static
void main(String args [ ])\n{\nClassB b = classB()\n}\nClassA(int x) {
}\n}\nclass ClassB extends ClassA\n{\n}\nWhat will happen when we
compile and run this code?','A')
(1,13,'The keywords reserved but not used in the initial version of
Java are:\n(A) union\n(B) const\n(C) inner\n(D) goto\n(E) boolean\n(F)
synchronized','C')
(1,14,'We would like to make a member of a class visible in all
subclasses regardless of what package they are in. Which one of the
following keywords would achieve this?','D')
(1,15,'Which of the following are not keywords?\n(A) NULL\n(B)
implements\n(C) protected\n(D) extended\n(E) string','B')
(3,1,'The Battle of Gettysburg was fought during which war?','C')
(3,2,'Neil Armstrong and Buzz Aldrin walked how many \n' ||
'minutes on the moon in 1696?','B')
(3,3,'Which Presidents held office during World War II?','D')
(3,4,'In a communist economic system, people:','B')
(3,5,'Which president did not die while in office?','D')
(3,6,'Which state refused to attend the Constitutional Convention
\n' ||
'in 1787 because it didn''t want the United States government \n' ||
'to interfere with already established state affairs?','A')
(3,7,'Who founded Buddhism?','A')
(3,8,'Where is India?','D')
(3,9,'What is the dominant religion in India?','B')
(3,10,'Near which river did archaeologists find India''s \n' ||
'first civilization?','B')
========== answerOption data ============
(3,8,'A','Australia') (3,8,'B','America') (3,8,'C','Africa')
(3,8,'D','Asia') (3,1,'A','World War II') (3,1,'B','The
Revolutionary War') (3,1,'C','The Civil War') (3,1,'D','World War I')
(3,2,'A','123') (3,2,'B','None') (3,2,'C','10') (3,2,'D','51')
(3,3,'A','Franklin D. Roosevelt') (3,3,'B','Dwight D. Eisenhower')
(3,3,'C','Harry Truman') (3,3,'D','Both A and C') (3,4,'A','Are
forced to work as slaves') (3,4,'B','Work for the common good')
(3,4,'C','Work from home computers') (3,4,'D','Don''t work')
(3,5,'A','John F. Kennedy') (3,5,'B','Franklin D. Roosevelt')
(3,5,'C','Abraham Lincoln') (3,5,'D','Ronald Reagan')
(3,5,'E','James A. Garfield') (3,7,'A','Siddharta Gautama')
(3,7,'B','Jesus Christ') (3,7,'C','Mahatma Gandhi')
(3,7,'D','Muhammad') (3,6,'A','Rhode Island') (3,6,'B','New
Hampshire') (3,6,'C','New Jersey') (3,6,'D','New York')
(3,9,'A','Islam') (3,9,'B','Hinduism') (3,9,'C','Christianity')
(3,9,'D','Buddhism') (3,10,'A','The Tiber River') (3,10,'B','The
Indus River') (3,10,'C','The Yellow River') (3,10,'D','The Nile
River') (1,1,'A','(D) & (E)') (1,1,'B',' (A) & (B)') (1,1,'C','(D)')
(1,1,'D','(A)') (1,1,'E','None of these') (1,2,'A','-17')
(1,2,'B','5') (1,2,'C','3') (1,2,'D','-5') (1,2,'E','None of these')
(1,3,'A','1.66') (1,3,'B','4') (1,3,'C','2') (1,3,'D','1')
(1,3,'E','None of these') (1,4,'A','x = 4, y = 1, z = 5') (1,4,'B',' x
= -7, y = 1, z = 5') (1,4,'C','x = 3, y = 2, z = 6') (1,4,'D',' x = 4, y = 2, z = 6') (1,4,'E',' x = 8, y = 2, z = 1') (1,5,'A','(C) & (D) ')
(1,5,'B',' (A), (B) & (C) ') (1,5,'C','(A), (B), (C) & (D)')
(1,5,'D','(A) & (B) ') (1,5,'E','None of the above')
(1,6,'A','\uffff') (1,6,'B','\u0000') (1,6,'C','" " (space)')
(1,6,'D','\u0001') (1,6,'E','None of the above') (1,7,'A','23')
(1,7,'B','Error. Cannot be executed.') (1,7,'C','57') (1,7,'D','11')
(1,7,'E','34') (1,8,'A','(A), (B) & (C)') (1,8,'B','(C)')
(1,8,'C','(A) & (B) ') (1,8,'D','(A)') (1,8,'E','(D)')
(1,9,'A','Boolean') (1,9,'B','Int') (1,9,'C','String')
(1,9,'D','Char') (1,9,'E','Byte') (1,10,'A','(A) & (C)')
(1,10,'B','(A) & (B)') (1,10,'C','(B) ') (1,10,'D','(B) & (D) ')
(1,10,'E','(D)') (1,11,'A','Integer "j" is not initialized.')
(1,11,'B','Nothing.') (1,11,'C','You cannot declare integer i inside
the for-loop declaration.') (1,11,'D','The syntax of the "if"
statement is incorrect.') (1,11,'E','You cannot print integer values
without converting them to strings.') (1,12,'A','Will compile and run
successfully ') (1,12,'B','Error. ClassA does not define a no-argument
constructor ') (1,12,'C','Error. There is no code in the constructor
ClassA(int x) ') (1,12,'D','Error. ClassB does not define a
no-argument constructor ') (1,12,'E','Error. There is no code in the
class ClassB ') (1,13,'A','(C) & (E) ') (1,13,'B','(B),(C) & (D)')
(1,13,'C','(A), (C) & (E)') (1,13,'D','All of these. ')
(1,13,'E','None of these. ') (1,14,'A','private ') (1,14,'B','public')
(1,14,'C','protected') (1,14,'D','private OR protected')
(1,14,'E','All of the above') (1,15,'A','(C), (D) & (E)')
(1,15,'B','(D) ') (1,15,'C','(A), (D) & (E)') (1,15,'D','(D) & (E) ')
(1,15,'E','(A)')
===== users data ====
(1,'1#gmail.com','','George','Ronald','','','Atlanta','GA','30303')
(2,'2#gmail.com','','Mary','Erikson','','','San Diego','CA','91901')
(3,'3#gmail.com','','John','Washington','','','Atlanta','GA','30314')
====enrolls data=====
(1,1,to_date('2012/02/15 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/22 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(2,1,to_date('2012/02/15 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/22 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(3,1,to_date('2012/02/17 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/24 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(1,3,to_date('2012/02/17 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/24 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(2,3,to_date('2012/02/20 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/27 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
(3,3,to_date('2012/02/20 10:00:00', 'yyyy/mm/dd
hh24:mi:ss'),to_date('2012/02/27 10:00:00', 'yyyy/mm/dd hh24:mi:ss')
===userResponse data====
(1,1,1,'A') (1,1,2,'B') (1,1,3,'A') (1,1,4,'B') (1,1,5,'N')
(1,1,6,'C') (1,1,7,'D') (1,1,8,'A') (1,1,9,'B') (1,1,10,'B')
(1,1,11,'A') (1,1,12,'B') (1,1,13,'A') (1,1,14,'A') (1,1,15,'A')
(2,1,1,'B') (2,1,2,'B') (2,1,3,'E') (2,1,4,'C') (2,1,5,'D')
(2,1,6,'A') (2,1,7,'C') (2,1,8,'B') (2,1,9,'B') (2,1,10,'A')
(2,1,11,'A') (2,1,12,'B') (2,1,13,'B') (2,1,14,'A') (2,1,15,'A')
(3,1,1,'C') (3,1,2,'C') (3,1,3,'D') (3,1,4,'D') (3,1,5,'A')
(3,1,6,'A') (3,1,7,'D') (3,1,8,'D') (3,1,9,'A') (3,1,10,'A')
(3,1,11,'B') (3,1,12,'B') (3,1,13,'A') (3,1,14,'A') (3,1,15,'C')
(1,3,1,'N') (1,3,2,'A') (1,3,3,'A') (1,3,4,'B') (1,3,5,'B')
(1,3,6,'C') (1,3,7,'D') (1,3,8,'A') (1,3,9,'A') (1,3,10,'B')
(2,3,1,'B') (2,3,2,'C') (2,3,3,'A') (2,3,4,'C') (2,3,5,'B')
(2,3,6,'D') (2,3,7,'D') (2,3,8,'D') (2,3,9,'A') (2,3,10,'A')
(3,3,1,'B') (3,3,2,'A') (3,3,3,'A') (3,3,4,'B') (3,3,5,'B')
(3,3,6,'C') (3,3,7,'D') (3,3,8,'A') (3,3,9,'A') (3,3,10,'B')
Then it sounds like you want to do an outer join
select ur.uno, count(q.eno)*5
from userresponse ur
left outer join question q
on( ur.eno = q.eno
and q.correctanswer = ur.response
and q.qno = ur.qno
and q.eno = '1')
where ur.uno = '1'
group by ur.uno
This will return a row for every row in userresponse with a uno of '1' with a count(*) of 0 if there are no matching rows in question.
Try out above Query with ISNULL function:
select ur.uno, NVL(count(*)*5,0)
from userresponse ur, question q
where ur.eno = q.eno
and q.correctanswer = ur.response
and q.qno = ur.qno
and ur.uno = '1'
and q.eno = '1'
group by ur.uno
when ever AND Operator are implementing with the different value on the same column with equality operator the overall condition return NO ROW as AND Operator evaluate on a 1 to 1 with direction towards table to the column...............

How to join three tables?

SELECT
PC_SL_ACNO, -- DB ITEM
SLNAME, -- ACCOUNT NAME:
SL_TOTAL_AMOUNT -- TOTAL AMOUNT:
FROM GLAS_PDC_CHEQUES
WHERE PC_COMP_CODE=:parameter.COMP_CODE
AND pc_bank_from = :block02.pb_bank_code
AND pc_due_date between :block01.date_from
AND :block01.date_to
AND nvl(pc_discd,'X') IN(‘X’, 'R')
GROUP BY
pc_comp_code, pc_sl_ldgr_code, pc_sl_acno
ORDER BY pc_sl_acno
ACCOUNT NAME:
BEGIN
SELECT COAD_PTY_FULL_NAME INTO :BLOCK03.SLNAME
FROM GLAS_PTY_ADDRESS,GLAS_SBLGR_MASTERS
WHERE SLMA_COMP_CODE = :PARAMETER.COMP_CODE
AND SLMA_ADDR_ID = COAD_ADDR_ID
AND SLMA_ADDR_TYPE = COAD_ADDR_TYPE
AND SLMA_ACNO = :BLOCK03.PC_SL_ACNO
AND SLMA_COMP_CODE = COAD_COMP_CODE;
EXCEPTION WHEN OTHERS THEN NULL;
END;
TOTAL AMOUNT:
BEGIN
SELECT SUM(PC_AMOUNT) INTO :SL_TOTAL_AMOUNT
FROM GLAS_PDC_CHEQUES
WHERE PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
AND PC_BANK_FROM = :block02.PB_BANK_CODE
AND PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND NVL(PC_DISCD,'X') = 'R'
AND PC_COMP_CODE = :PARAMETER.COMP_CODE;
EXCEPTION WHEN OTHERS THEN :block03.SL_TOTAL_AMOUNT := 0;
END;
How can I join the three tables?
You'll have to adjust depending on precisely what criteria and required fields you have for your query or queries.
SELECT
c.PC_SL_ACNO,
a.COAD_PTY_FULL_NAME,
SUM(c.PC_AMOUNT)
FROM
GLAS_PDC_CHEQUES c
LEFT JOIN
GLAS_SBLGR_MASTERS m
ON ( c.PC_SL_ACNO = m.SLMA_ACNO
AND c.PC_COMP_CODE = m.SLMA_COMP_CODE
)
LEFT JOIN
GLAS_PTY_ADDRESS a
ON ( m.SLMA_ADDR_ID = a.COAD_ADDR_ID
AND m.SLMA_COMP_CODE = a.COAD_COMP_CODE
AND m.SLMA_ADDR_TYPE = a.COAD_ADDR_TYPE
)
WHERE
c.PC_COMP_CODE = :PARAMETER.COMP_CODE
AND c.PC_SL_ACNO = :BLOCK03.PC_SL_ACNO
AND c.PC_BANK_FROM = :BLOCK02.PB_BANK_CODE
AND NVL(c.PC_DISCD,'X') IN (‘X’, 'R')
AND c.PC_DUE_DATE BETWEEN :BLOCK01.DATE_FROM AND :BLOCK01.DATE_TO
GROUP BY
c.PC_SL_ACNO, -- not sure which grouping exactly you need.
a.COAD_PTY_FULL_NAME
ORDER BY
c.PC_SL_ACNO
I notice that in the first query you have pc_comp_code as a search criterion, and on the leading edge of your grouping - is that what you need?
This is a bit of an 'estimate' due to the enigmatic nature of your question!