I have the table with values as shown in below table:
And I would like to get results as in below table:
I'm trying to query sum of ans1 like this for all rows
`SELECT sub_id, SUM(ans1) FROM booktable WHERE book_id =1 AND sub_id = 4 OR sub_id = 6`
But I'm not getting the expected result as shown in above table, instead I'm getting result for only one value in sub_id column. I'm a beginner in sql, please help. Thanks.
SELECT sub_id, SUM(ans1) FROM booktable
WHERE book_id = 1 AND sub_id IN (4, 6)
Group by sub_id
Related
I'm have a table with following structure and data. I would like to get the conversation_id of a row that having given user_id(s).
For example. I would like to get the conversation_id between user_id 1 and user 2, so the result should be 1. If I would like to get the conversation_id of user_id 1 to 4, the result should be 4.
How could I write in sql query?
You can GROUP BY conversation_id and set the condition in the HAVING clause:
SELECT conversation_id
FROM tablename
WHERE user_id IN (1, 2) -- the ids of the users
GROUP BY conversation_id
HAVING COUNT(DISTINCT user_id) = 2 -- the number of the users
Below is my table, how can i get the item_id with selecting multiple item_subcategory_id.
For example i need to get the item_id with an item_subcategory_id of 86,51 and 1. I should only get the item_id 1 even though item_id 2 has an item_subcategory_id of 51
Please note that the number of item_subcategory_id is dynamic.
I tried the query
SELECT item_id
FROM item_specs
WHERE item_subcategory_id = 86 AND
item_subcategory_id=51 AND
item_subcategory_id = 1
but it displays an empty result
You can use the count and compare the result of count to the no of distinct item_subcategory_ids you provide in in() clause like in your case distinct item_subcategory_ids is 3 ,so this will result only those item_ids which has all of these provided item_subcategory_ids
select * from table
where item_subcategory_id in(1,51,86)
group by item_id
having count(distinct item_subcategory_id) =3
Use AND operator to combine your conditions
select item_category_id
from mytable
where item_id = 1 and item_subcategory_id in (86, 51, 1);
My table is :
I want to select records who all are fail(result=0) but except who has same course_id result is 1.
For example look my table there are two rows have result=0 but student_id=1 has another row with result=1 so i want skip this record.
Sorry for confusing you.I hope my expecting output explains clearly what I want.
Expecting output is :
2 | 1 | 0
Try this
SELECT student_id, course_id, result FROM tbl WHERE result = 0
AND student_id NOT IN (SELECT student_id FROM table WHERE result =1
and course_id = tbl.course_id )
Above we are selecting all records that have a result =0 only if the student_id and course_id is not found in the sub query with a result=1
let's say I have the following Table:
ID, Name
1, John
2, Jim
3, Steve
4, Tom
I run the following query
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill');
I want to get something like:
ID
1
2
NULL or 0
Is it possible?
How about this?
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill')
UNION
SELECT null;
Start by creating a subquery of names you're looking for, then left join the subquery to your table:
SELECT myTable.ID
FROM (
SELECT 'John' AS Name
UNION SELECT 'Jim'
UNION SELECT 'Bill'
) NameList
LEFT JOIN myTable ON NameList.Name = myTable.Name
This will return null for each name that isn't found. To return a zero instead, just start the query with SELECT COALESCE(myTable.ID, 0) instead of SELECT myTable.ID.
There's a SQL Fiddle here.
The question is a bit confusing. "IN" is a valid operator in SQL and it means a match with any of the values (see here ):
SELECT Id FROM Table WHERE NAME IN ('John', 'Jim', 'Bill');
Is the same as:
SELECT Id FROM Table WHERE NAME = 'John' OR NAME = 'Jim' OR NAME = 'Bill';
In your answer you seem to want the replies for each of the values, in order. This is accomplished by joining the results with UNION ALL (only UNION eliminates duplicates and can change the order):
SELECT max(Id) FROM Table WHERE NAME = 'John' UNION ALL
SELECT max(Id) FROM Table WHERE NAME = 'Jim' UNION ALL
SELECT max(Id) FROM Table WHERE NAME = 'Bill';
The above will return 1 Id (the max) if there are matches and NULL if there are none (e.g. for Bill). Note that in general you can have more than one row matching some of the names in your list, I used "max" to select one, you may be better of in keeping the loop on the values outside the query or in using the (ID, Name) table in a join with other tables in your database, instead of making the list of ID and then using it.
My table content:
id aid
----------
1 1
2 2
3 2
4 4
5 4
my sql: select count(*) from table where aid in (1,2,3,4) group by aid
result:
count(*)
----------
1
2
2
I would like result:
count(*)
----------
1
2
0
2
I want a count result when aid is 1,2,3,4. What a sql should I use? Thanks for help.
(I‘m use MySQL)
-- your data
DECLARE #Data TABLE (Id int, Aid int)
INSERT INTO #Data VALUES
(1,1),
(2,2),
(3,2),
(4,4),
(5,4);
DECLARE #Relevant TABLE (Id int )
INSERT INTO #Relevant VALUES (1),(2),(3),(4);
-- the query
SELECT Relevant.Id, COUNT(Data.Aid)
FROM #Data Data
RIGHT JOIN #Relevant Relevant ON Data.Aid = Relevant.Id
GROUP BY Data.Aid, Relevant.Id
Time for an obvious statement:
- Data can't "magically appear", it needs to exist somewhere for it to appear in the output.
This is relevant for your desire to have a count of 0 for aid = 3. Because there isn't a record with aid = 3, it can't be counted. It's data that doesn't exist so will never appear in the output. So we need to force it to exist.
If you have a table that lists all of the aid entities, that's easy to do with a LEFT JOIN...
SELECT
aid.id,
COUNT(yourDataTable.aid) AS data_count
FROM
aid
LEFT JOIN
yourDataTable
ON aid.id = yourDataTable.aid
WHERE
aid.id IN (1,2,3,4)
GROUP BY
aid.id
(COUNT(yourDataTable.aid) does not count records where the field is NULL. So the 3 now exists, and the COUNT() can return 0.)
Hope this helps
SELECT COLUMN_VALUE, COUNT(BT.AID)
FROM SO_BUFFER_TABLE_15 BT, TABLE(SYS.DBMS_DEBUG_VC2COLl(1, 2, 3, 4))
WHERE BT.AID(+) = COLUMN_VALUE
GROUP BY COLUMN_VALUE
ORDER BY COLUMN_VALUE