Issue with creating oracle query - sql

I am having problem coming up with following query.
id| prop_id| obj_id| value|
1 7 2 1
2 8 2 1
4 7 5 7
5 8 5 12
Input parameters to the query are:
Collection of (prop_id, value) pairs
We must select records whose prop_id and values match input parameters: but there is also one constrain, the obj_id of resulting records must be same. If it is not same, result must be empty. Otherwise it must return obj_id.
Example:
Let's say input parameters are: (7,1) and (8,12).
Now there are two records with such values: first and last.
However, obj_id of first record is 2 and obj_id of second record is 5. Hence, the result must be empty because 2!=5.
Another example:
Let's say input parameters are: (7,7) and (8,12).
Now there are two records with such values: last two records.
obj_id of first is 5 and obj_id of second record is also 5. Hence, the result must be 5.

Try this
select obj_id from table
where (prop_id=7 and value=7) or (prop_id=8 and value=12)
group by obj_id having count(*)=2 and count(distinct object_id)=1

Related

Generate random records from the table tblFruit based on the field Type

I will need your help to generate random records from the table tblFruit based on the field Type (without no duplication)
As per the above table.
There are 4 type of fruit number 1,2,3,4
I want to generate x records dynamically from the table tblFruit (e.g 7 records).
Let say I need to get 7 random record of fruit .
My result should contains fruit of the different types. However, we need to ensure that the result contains only 7 records.
i.e
2 records of type 1,
2 records of type 2,
2 records of type 3,
1 records of type 4
e.g
Note: If i want to generate 10 records (without no duplication),
then i will get 2 records of each type and the two remaining records randomly of any type.
Much grateful for your help.
I might suggest:
select top (7) f.*
from tblfruit f
order by row_number() over (partition by type order by newid());
This will actually produce a result with approximately the same number of rows of each type (well, off by 1), but that meets your needs.

google bigquery selecting rows with multiple values in repeating field

Lets say I have a schema:
key STRING NULLABLE
values INTEGER REPEATED
Now, please note that second column is a repeated field of integers,
Lets say the data is something like:
key:'abc'
values: 1 2 3 (3 separate values, same for below values)
key:'def'
values: 1 2 5
key:'ghi'
values: 1 6 9
And here I wish to find out keys which has values 1 and 2 ? Expecting 'abc' and 'def' as result set.
Looking for a query for this. I want an 'and' ('in' does not work here). I need those both values to be present for any key to return as result.
SELECT
key,
SUM(values = 1 or values = 2) WITHIN RECORD AS check
FROM yourtable
HAVING check = 2

Searching for non unique values between two columns

I have a single table with three columns, 'id', 'number' and 'transaction.' Each id should only be tied to one number however may exist many times in the table under different values of transaction. I've been unable to develop a query that will return cases of a single id sharing multiple numbers (and show the id and number in the report). I don't wish to delete these values via the query, I just need to see the values. See example below:
Here's a screenshot example: http://i591.photobucket.com/albums/ss355/riggins_83/table2_zps5509f3cf.jpg I appreciate the assistance, I've tried all the code posted here and it hasn't given me the output I'm looking for. As seen in the screenshot it's possible for the same ID number and Number to appear in the table multiple times with a different transaction number, what shouldn't occur is what's on rows 1 and 2 (two different numbers with same ID number). The ID number is a value that should always be tied to the same Number which the transaction is only linked to that line. I'm trying to generate output of each number that's sharing an ID number (and the shared ID Number if possible).
Test IDNumber Number Transaction
1 31 1551 5
2 31 1553 7
3 32 1701 8
4 33 1701 9
5 33 1701 10
6 33 1701 11
7 39 1885 12
The result of output I would need:
IDNumber Number
31 1551
31 1553
This output is showing me the Number (and ID number) in cases where an ID number is being shared between two (or possibly more) numbers. I know there are cases in the table where an ID number is being shared among many numbers.
Any assistance is greatly appreciated!
SELECT *
FROM thetable t0
WHERE EXISTS (
SELECT *
FROM thetable t1
WHERE t1.id = t0.id
-- Not clear from the question if the OP wants the records
-- to differ by number
-- AND t1.number <> t1.number
-- ... or by "transaction"
AND t1."transaction" <> t0."transaction"
-- ... or by either ???
-- AND (t1.number <> t1.number OR t1."transaction" <> t0."transaction")
);
SELECT IDNumber, Number
FROM YourTable
WHERE IDNumber IN (
SELECT IDNumber
FROM YourTable
GROUP BY IDNumber
HAVING COUNT(DISTINCT number) > 1
)
The subquery returns all the IDNumbers with more than 1 Number. Then the main query returns all the numbers for each of those IDNumbers.
DEMO

SQL Server Stored Procedure SELECT DISTINCT

I'm working on deciphering some stored procedures and have minimal vocabulary on the subject. Can someone please explain to me what role this '1' serves in the below statement? I can not find any DISTINCT syntax tutorials to explain this. I'm referring to the actual "1" one in the statement.
USE TEST
GO
SET ANSI_NULLS, QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].sp_F_SQL
(#Id int)
WITH ENCRYPTION AS
SELECT DISTINCT
dbo.MAP_SQL.rID,
dbo.MAP_SQL.lID,
dbo.MAP_SQL.cID,
**1** as RESPFACT,
dbo.MAP_SQL.Longitude,
dbo.MAP_SQL.Latitude,
dbo.MAP_SQL.Altitude,
...
The 1 has nothing to do with DISTINCT. It just adds an output column titled RESPFACT that has a value of 1 for all rows. I suspect whatever is consuming the output need that column.
SELECT DISTINCT only returns the "distinct" rows from the output - meaning rows where ALL column values are equal.
e.g. if your output without distinct was
1 2 ABC DEF
2 3 GHI JLK
2 1 ABC DEF
1 2 ABC DEF
Then rows 1 and 4 would be seen as "equal" and ony one would be returned:
1 2 ABC DEF
2 3 GHI JLK
2 1 ABC DEF
Note that rows 1 and 3 are NOT equal even though 3 of the 4 column values match.
The 1 generates a column called RESPFACT. This always has the value of 1.
I cannot say why this is important for the sp_F_SQL procedure.
The distinct returns unique rows. If there are duplicate values for the columns in the select then only one row is returned. Clearly, the RESPFACT column is the same in all rows, so it does not affect the rows being returned.

Will multiple columns concatenate in the same order if using STUFF and For Xml Path

Please see http://www.sqlfiddle.com/#!3/fb107/3 for an example schema and query I want to run.
I want to use the STUFF and FOR XML PATH('') solution to concatenate columns having grouped by another column.
If I use this method to concatenate multiple columnns into a csv list, am I guaranteed that the order will be the same in each concatenated string? So if the table was:
ID Col1 Col2 Col3
1 1 1 1
1 2 2 2
1 3 3 3
2 4 4 4
2 5 5 5
2 5 5 5
Am I certain that if Col1 is concatenated such that the result is:
ID Col1Concatenated
1 1,2,3
2 4,5,6
That Col2Concatenated will also be in the same order ("1,2,3", "4,5,6") as opposed to ("2,3,1", "5,6,4") for example?
This solution will only work for me if the index of each row's value is the same in each of the concatenated values. i.e. first row is first in each csv list, second row is second in each csv list etc.
You can add an ORDER BY clause in the query within your STUFF function