Duplex Data Preparation using SQL - 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

Related

How to write the query to make report by month in sql

I have the receiving and sending data for whole year. so i want to built the monthly report base on that data with the rule is Fisrt in first out. It means is the first receiving will be sent out first ...
DECLARE #ReceivingTbl AS TABLE(Id INT,ProId int, RecQty INT,ReceivingDate DateTime)
INSERT INTO #ReceivingTbl
VALUES (1,1001,210,'2019-03-12'),
(2,1001,315,'2019-06-15'),
(3,2001,500,'2019-04-01'),
(4,2001,10,'2019-06-15'),
(5,1001,105,'2019-07-10')
DECLARE #SendTbl AS TABLE(Id INT,ProId int, SentQty INT,SendMonth int)
INSERT INTO #SendTbl
VALUES (1,1001,50,3),
(2,1001,100,4),
(3,1001,80,5),
(4,1001,80,6),
(5,2001,200,6)
SELECT * FROM #ReceivingTbl ORDER BY ProId,ReceivingDate
SELECT * FROM #SendTbl ORDER BY ProId,SendMonth
Id ProId RecQty ReceivingDate
1 1001 210 2019-03-12
2 1001 315 2019-06-15
5 1001 105 2019-07-10
3 2001 500 2019-04-01
4 2001 10 2019-06-15
Id ProId SentQty SendMonth
1 1001 50 3
2 1001 100 4
3 1001 80 5
4 1001 80 6
5 2001 200 6
--- And the below is what i want:
Id ProId RecQty ReceivingDate ... Mar Apr May Jun
1 1001 210 2019-03-12 ... 50 100 60 0
2 1001 315 2019-06-15 ... 0 0 20 80
5 1001 105 2019-07-10 ... 0 0 0 0
3 2001 500 2019-04-01 ... 0 0 0 200
4 2001 10 2019-06-15 ... 0 0 0 0
Thanks!
Your question is not clear to me.
If you want to purely use the FIFO approach, therefore ignore any data the table contains, you necessarely need to order by ID, which in your example you are providing, and looks like it is in order of insert.
The first line inserted should be also the first line appearing in the select (FIFO), in order to do so you have to use:
ORDER BY Id ASC
Which will place the lower value of the ID first (1, 2, 3, ...)
To me though, this doesn't make much sense, so pay attention to the meaning o the data you actually have and leverage dates like ReceivingDate, and order by that, maybe even filtering by month of the date, below an example for January data:
WHERE MONTH(ReceivingDate) = 1

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;

Oracle select query based on multiple conditions

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.

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.