Mysql query to fetch data - sql

i have a table "request" with 4 columns namely:
1.recId :long primary key
2.interactionId:long
3.requestedBy:boolean
4.requestedType:boolean
and data is as follows:
VALUES
(185,455699,0,5),
(186,455746,0,1),
(187,455746,1,1),
(188,455752,0,1),
(189,455753,0,1),
(190,455753,1,1),
(191,455754,1,1)
i want a query to fetch all the rows where interactionId is same and having requestedBy both 1 and 0 values and requestType=1;
regards,
Nihar

Your question was a little difficult to understand. I'm assuming you meant this:
I want a query to fetch all pairs of rows from the request table where:
the interactionId is same in both rows
requestType = 1 for both rows
requestedBy is 1 in one row and 0 in the other row
If so, then try this:
SELECT *
FROM request T1
JOIN request T2
ON T1.interactionId = T2.interactionId
AND T1.requestType = 1
AND T2.requestType = 1
AND T1.requestedBy = 0
AND T2.requestedBy = 1

Related

In operator takes only one Id(if Id is repeating) from the list in sql server

I have a query I am using IN operator and I want all the rows from in given list as shown in the picture that I want 3 rows for id 1 and one for id 2, but I only get one row for Id = 1 is there any other solution for this.
IN can't do what you want. JOIN instead:
select * from logs
JOIN (values (1),(2),(1),(1)) x (id)
ON logs.id = x.id

Update rows using data within the same table SQL

I have a table which looks like this
messageId | conversationId | campusA | campusB
1 1 campusA
2 1
3 1
4 1
5 2
6 2
7 2 campusB
As we can see multiple messages are linked to the same conversationId and we have campus information for some of the records. Using this information I want to update remaining empty rows that are linked to the same conversationId. For example, I can see campusA is linked to conversationId 1. Using this information, I want to update the remaining 3 rows that have the same conversationId as the first record.
I was having trouble figuring this out and was wondering what would be the most efficient way to go around this? Thank you in advance!
You can use the update and correlated sub query as follows:
Update your_table t
Set t.campusa = (select max(tt.campusa) from your_table tt
Where t.conversarion = tt.conversation)
Where t.campusa is null
And exists (select 1 from your_table tt
Where t.conversarion = tt.conversation and tt.campusa is not null)

SELECT - too many rows

let me show you a simple presentation on database structure:
Database structure
Query's parameters:
-id_category
-id_language
-id_account
I need to select all rows from wordconfigs table where id_wordconfig is diffrent from id_wordconfig stored in hiddenwords table.
For example wordconfigs table cointains:
id_wordconfig = 1 (this row is related to successively: id_account=1
(imporatant that 1), id_language=1, id_category=1)
id_wordconfig = 2 (this row is related to successively: id_account=1
(imporatant that 1), id_language=1, id_category=1)
I pass parameters to query:
-id_category = 1
-id_language = 1
-id_account = 2 (other than assigned to wordconfigs in example)
Example 1
hiddenwords table is empty -> query returns wordconfid with id 1 and 2
Example 2
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=1
query returns also wordconfig with id 1 and 2 (because id_account is diffrent from passed to query)
Example 3
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
query returns just wordconfig with id 2 (because hiddenwords contains id_wordconfig=1 and id_account=2 like passed to query)
Example 4
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
id_hiddenwords = 1; id_wordconfig=2; id_account=1 (different that passed to query)
query returns just wordconfig with id = 2
Example 5
hiddenwords table contains:
id_hiddenwords = 1; id_wordconfig=1; id_account=2
id_hiddenwords = 1; id_wordconfig=2; id_account=2
query should nothing returns
I've created a simple query for this task:
SELECT wc.*
FROM categorywordconfig cwc, categories c, languages l, accounts a, wordconfigs wc
LEFT JOIN hiddenwords hw ON hw.id_wordconfig<>wc.id_wordconfig
WHERE wc.id_wordconfig=cwc.id_wordconfig
AND cwc.id_category=c.id_category
AND c.id_language=l.id_language
AND l.id_account=a.id_account
AND c.id_category=1
AND l.id_language=1
AND hw.id_account=2
The given query works for examples from 1 to 4. The Example 5 should nothing returns but with this query it's return all rows (so wordconfig with id 1 and 2) Why is that? What's wrong with it?
Edit:
I've just simple changed query to:
SELECT wc.* FROM categorywordconfig cwc, categories c, languages l, accounts a, wordconfigs wc
WHERE wc.id_wordconfig=cwc.id_wordconfig
AND cwc.id_category=c.id_category AND c.id_language=l.id_language
AND l.id_account=a.id_account AND c.id_category=1 AND l.id_language=1
AND wc.id_wordconfig NOT IN (SELECT id_wordconfig FROM hiddenwords WHERE id_account=2)
And it's working :)

How to prioritize in OR condition in SQL Server

I have a simple query where I want to search for a value in a column, and if no match is found then I want to discard that where condition and select any random value from that column.
I have tried to achieve this using OR condition, using case, but to no avail
select top 10 *
from tblRFCWorkload
where (f1 ='mbb' or f1 = f1)
In the above query I want the result table to have all rows with 'mbb', if 'mbb' is not found then give any value instead of mbb. But instead it returns only two rows with mbb even though there are 10 matching rows
select top 10 *
from tblRFCWorkload
where 1 = case when f1 = 'mbb' then 1 else 1 end
This query also returns the same result as the 1st query
If I change it to
select top 10 *
from tblRFCWorkload
where 1 = case when f1 = 'mbb' then 1 else 0 end
then it only rows having the mbb value.
Need similar thing with between clause... search for one range if no results found then search for second range... how do I do that???
I think you want something like this:
select top 10 *
from tblRFCWorkload
order by (case when f1 = 'mbb' then 1 else 2 end);
This prioritizes the rows that you want, fetching the top 10 which will be 'mbb', if they are available.
You want only the results where f1 = 'mbb' if there are any, otherwise nothing? In that case you need to be testing whether there are any matching rows. Something like this
SELECT TOP 10 *
FROM tblRFCWorkload
WHERE f1 = 'mbb' --the match you need
OR NOT EXISTS --or there are no matches
(SELECT * FROM tblRFCWorkload WHERE f1 = 'mbb')

The MIN() Function Ms Access

this is a sample sql query that i created ms access query. i am trying to get only one row the min(DATE). how ever when i run my query i get multiple lines. any hits? thanks
SELECT tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
Min(tblWarehouseItem.whiDateIn) AS MinOfwhiDateIn,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID
FROM tblWarehouseItem
GROUP BY tblWarehouseItem.whiDateIn,
tblWarehouseItem.whiItemName,
tblWarehouseItem.whiQty,
tblWarehouseItem.whiPrice,
tblWarehouseItem.whiExpiryDate,
tblWarehouseItem.whiwrhID;
If i have my sql code like that is working as it should:
SELECT MIN(tblWarehouseItem.whiDateIn) FROM tblWarehouseItem;
In the first query, you group by a number of columns. That means the minimum value will be calculated for each group, which in turn means you may have multiple rows. On the other hand, the second query will only get the minimum value for the specified column from all rows, so that there is only one row in the result set.
A simple example is shown below to illustrate the above.
Table:
Key Value
1 1
1 2
2 3
2 4
On Group By Key:
GroupKey MinValue
1 = min(1,2) = 1 -> Row 1
2 = min(3,4) = 3 -> Row 2
On Min (Value)
MinValue
=min(1,2,3,4) = 1 -> Row 1
For a table like above, if you want to select all rows and also show the minimum value from whole table rather than per group, you can do something like this:
select key, (select min(value) from table)
from table
SELECT WI.*
FROM tblWarehouseItem AS WI INNER JOIN (SELECT whiimtID, MIN(tblWarehouseItem.whiDateIn) AS whiDateIn
FROM tblWarehouseItem
GROUP BY whiimtID) AS MinWI ON (WI.whiDateIn = MinWI.whiDateIn) AND (WI.whiimtID = MinWI.whiimtID);