Oracle select query based on multiple conditions - sql

MESSAGE_ID GROUP_ID REV_NO
100 200 1
101 201 1
102 202 1
103 203 1
104 204 1
105 200 2
106 201 2
107 202 2
108 203 2
109 204 2
110 205 2
First I want to select all group ID's and their correpsponding lowest revision number.
Then I want select first X message ID's (Controllable X input) with condition that it should contain all the revisions of of any selected group. For e.g if I select first 5 messages by rownum then all revisions of group_id 200 is not selected.
Hope I made it clear.

Related

Duplex Data Preparation using SQL

I have a table with data and I need to prepare the data rows for duplex printing using defined fields and variables.
I have created set of temp tables as below:
#OriginalData:
Code Department Brand PageNumber SequenceNo Reverse
-----------------------------------------------------
101 201 LG 1 1 0
102 201 Samsung 1 2 0
105 203 Apple 1 3 0
106 203 Nokia 1 4 0
103 202 Sony 2 5 0
104 202 Sony 2 6 0
107 203 TCL 2 7 0
108 203 BenQ 2 8 0
#Required Data:
Code Department Brand PageNumber SequenceNo Reverse
-----------------------------------------------------
101 201 LG 1 1 0
102 201 Samsung 1 2 0
105 203 Apple 1 3 0
106 203 Nokia 1 4 0
101 201 LG 1 5 1 - Required
102 201 Samsung 1 6 1 - Required
105 203 Apple 1 7 1 - Required
106 203 Nokia 1 8 1 - Required
103 202 Sony 2 9 0
104 202 Sony 2 10 0
107 203 TCL 2 11 0
108 203 BenQ 2 12 0
103 202 Sony 2 13 1 - Required
104 202 Sony 2 14 1 - Required
107 203 TCL 2 15 1 - Required
108 203 BenQ 2 16 1 - Required
I need the data as per the second table. I am planning to use the original data for the front of the page and use data from RequireDupexData table for back of the page.
Is there a way to change the SequenceNo order using SQL? so when combined with Original data they can be printed correctly in terms of duplex printing.
Notes:
Example 2 data rows per page
Example 2 columns per page
The data is passed to an application that creates artwork
Same data rows are used for the back of the page hence why the duplicated tables.
Update: Modified my required data to make more sense. In my example I have considered 2 columns and 2 rows but these can change depending on the page/template design. Hence I believe columns or rows or both must be considered in order to reorder the SequenceNo for required data.
I think this will work to reverse your sequence number
--drop table #temp
create table #temp(
seq int,
)
insert into #temp
values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14)
declare #third int;
set #third = ceiling((select count(seq)/3.0 from #temp))
select #third
select seq
, case when seq <= #third
then seq + 2*#third
when seq <= 2*#third
then seq
when seq > 2*#third
then seq - 2*#third
end as rseq
from #temp
order by seq asc

I'm having this error when compiling my code: value with SQL

Team numbers are found by dividing the employee id by four. If 4 divides the employee id evenly, then that employee is part of team 1. For the other cases, a remainder of 1 places that employee on team 2, etc.
I got this result when I run the Code
SELECT EMPLOYEE_ID, MOD(EMPLOYEE_ID, 4) AS "TEAM"
100 0
101 1
102 2
103 3
104 0
105 1
106 2
107 3
but I need to get the following result:
100 1
101 2
102 3
103 4
104 1
105 2
106 3
107 4
Trivially, you may just add one to result of the MOD function:
SELECT
EMPLOYEE_ID,
MOD(EMPLOYEE_ID, 4) + 1 AS "TEAM"
FROM yourTable;

update corresponding value from other table

I have two tables named sales and login.My table structure is given below.Some times my program update the custid instead of userid in sales table column userid, but the logid updated correctly in sales table. I have the another table tbl_log shown below. I want to update the sales table userid based on logid using the tbl_log.
sales table
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 S1003
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 S1006
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 S1009
9 S1010 104 31 d2124
10 S1011 104 60 S1011
Log Table
Fld_log_id Fld_user_id
101 d2121
102 d2122
103 d2123
104 d2124
Exact output
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 d2122
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 d2122
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 d2123
9 S1010 104 31 d2124
10 S1011 104 60 d2124
To update the values in sales based on the values in the log table you do:
UPDATE sales S
SET S.Fld_user_id = (SELECT l.Fld_user_id
FROM logSales l
WHERE l.Fld_log_id = s.Fld_log_id);
sqlfiddle demo

Select from table repeat first value for combination of two keys

I would like to transfer some existing data into new data table.
I have table with substitutions:
- ID
- currentItemId
- formerItemId
- contentId
For the same content there is possibility I have multiple entries for combinations currentItemId and formerItemId.
Let me show how it is now:
ID_T1 currentItemId formerItemId contentId
1 100 200 300
2 100 200 301
3 100 200 302
4 105 201 303
5 105 201 304
6 110 205 320
7 111 206 321
8 120 204 322
9 130 208 323
10 130 208 324
Now, I would like to select TOP ID for each combination formerItemId and currentItemId:
ID ID_T1 contentId
1 1 300
2 1 301
3 1 302
4 4 303
5 4 304
6 6 320
7 7 321
8 8 322
9 9 323
10 9 324
Both tables also contains timestamp and some other data - I haven't included that in order example to be more understandable.
I tried self join (no success), nested select (gives me right value for the original combination, but it doesn't repeat, it gives me NULL on ID for other records), but nothing seems to work. Tried something like:
SELECT di1.ID,
(SELECT TOP(1) di1.ID
FROM TABLE
WHERE
di1.currentItemtId = di2.currentItemtId AND di1.formerItemId = di1.formerItemId
) AS repeat
,di2.deleteItemId
,di1.currentitemtId
,di1.formerItemId
,di1.contentId
FROM Table di1
LEFT JOIN
Table di2 ON di1.ID = di2.ID
But this way ID doesn't repeat - I get same values for ID as in ordinary select.
I am using SQL server 2008.
Any help would be greatly appreciated.
Please try:
SELECT
MIN(ID) OVER (PARTITION BY currentItemId, formerItemId) ID,
currentItemId,
formerItemId,
contentId
FROM YourTable
SELECT
ID,
MIN(ID) OVER (PARTITION BY currentItemId, formerItemId) ID_T1,
contentId
FROM YourTable

How to equally and randomly assign records to user ID's in SQL

Looking for some basic SQL help (SQL Server 2005). I have two tables that look like the following and I need to assign each record with a unique user ID:
Record table:
Record_ID UserID
101 0
102 0
103 0
104 0
105 0
106 0
107 0
User table:
UserID
1
2
3
Update results:
Record_ID UserID
101 1
102 2
103 3
104 1
105 2
106 3
107 1
You trying to have mutually exclusive requirements ... 'randomly' and 'equally'. You can't do both.