Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have table called product like this:
source_item_id source_rev_id
----------------------------
111 a_01_tz
111 b_01_tz
111 c_01_tz
222 e_02_tz
222 f_02_tz
222 g_01_tz
333 h_03_tz
444 g_04_tz
Now I want output in this format:
source_item_id source_rev_id target_rev_id
--------------------------------------------
111 a_01_tz AAA
111 b_01_tz AAB
111 c_01_tz AAC
222 e_02_tz AAA
222 f_02_tz AAB
222 g_01_tz AAC
333 h_03_tz AAA
444 g_04_tz AAA
444 l_04_tz AAB
For one source_item_id, there can be multiple source_rev_id's.
Please help in the query writing. Thanks.
Combine a subquery to create an item counter and an expression to create AAA to ZZZ from the counter:
SELECT
source_item_id, source_rev_id ,
CHAR( (target_rev_num)/676 % 26 + 65)
+ CHAR( (target_rev_num)/26 % 26 + 65)
+ CHAR( target_rev_num % 26 + 65)
AS target_rev_id
FROM (
SELECT source_item_id, source_rev_id ,
ROW_NUMBER() OVER ( PARTITION BY source_item_id
ORDER BY source_rev_id ) -1
AS target_rev_num
FROM product
) P
One way to do this is to create a permanent lookup table of all the target_rev_ids you might have, with an id column, like
1 AAA
2 AAB
3 AAC
4 AAD
5 AAE
etc.
Then you can just join to that table with the ROW_NUMBER function.
Quite a hard thing to do with SQL but there is a next_permutation function used in cpp to get the next permutation of a string for instance, so its something like next_permutation of AAA is AAB you apply it each time you find the same item_id.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
I have 3 tables like
TAB_1
ID
NUMBER
1
101
2
102
3
103
4
104
5
105
6
106
7
107
8
108
9
109
10
110
TAB_2
ID
NUMBER
1
101
2
102
3
105
TAB_3
ID
NUMBER
1
104
2
107
3
110
The output needs to be:
ID
NUMBER
1
103
2
106
3
108
4
109
I think u can use NOT IN with Subqueries
Like;
SELECT
*
FROM
Table_1
WHERE
Number NOT IN (Select number from Table_2) and
Number NOT IN (Select number from Table_3)
I need to select the records in batch wise, like in below example we have 20 records. if I give batch of size of 10 there would be two loops. the problem here is if I do top 10 then 555 value will be split as its position is 10 and 11. hence 555 should also include in that top first batch. how I can achieve this? this is just example, I have 900 million records to process and my batch will be 2 million in real scenario.
ID
-------
111
111
111
222
222
333
333
444
444
555
555
666
666
777
777
888
888
You can use top with ties - this might return more records then stated but will not break similar ids to different batches:
Create and populate sample table (Please save us this step in your future questions):
DECLARE #T AS TABLE
(ID int)
INSERT INTO #T VALUES
(111),(111),(111),
(222),(222),
(333),(333),
(444),(444),
(555),(555),
(666),(666),
(777),(777),
(888),(888)
The select statement:
SELECT TOP 10 WITH TIES ID
FROM #T
ORDER BY ID
Results:
row ID
1 111
2 111
3 111
4 222
5 222
6 333
7 333
8 444
9 444
10 555
11 555
While selecting the records, you can group them by id prior to limiting their number.
I am working on a query and having a difficult time to figure out how to fill the same value based on one column. Let me explain what I am trying to accomplish....
Says, I have a table like this below with too columns: Name & Value. So, "Select Name, Value FROM Table1 Order By Name" will produce the following result.
Table1
Name Value
AAA 111
AAA
BBB 222
BBB
BBB
BBB
CCC 333
CCC
DDD 444
DDD
DDD
Now, What I am trying to accomplish is producing the result below with the "Select .... from Table1" query.
Table1
Name Value
AAA 111
AAA 111
BBB 222
BBB 222
BBB 222
BBB 222
CCC 333
CCC 333
DDD 444
DDD 444
DDD 444
Please help and provide the sample code if possible.
Thanks in advance
You can use MAX() as a window function:
SELECT Name, Value,
MAX(Value) OVER (PARTITION BY Name) as imputed_name
FROM Table1
ORDER BY Name;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
BILL_NO COUTER TRANTYPE BARCODE DES TRANAMT
164 1 V 21021 ALOKOZAY TISSUE 150S (3.50)
164 1 s 21021 210521 CABAGE 5.00
164 1 C CASH CASH 1.50
208 2 V 120110 NATCO ORANGE MARMALA (6.75)
208 2 S 120110 NATCO ORANGE MARMALA 6.75
208 2 C CASH CASH -
164 3 S 5404568 FRESH FISH 18.00
164 3 S 5406464 ARYAA IDLY/DOSA MIX 5.00
164 3 S 654954 DETTOL SENSITIVE 125 7.00
164 3 C CASH CASH 30.00
i want select bill no from my table where trantype='v'
but i need result as metioned below thank you
BILL_NO COUTER TRANTYPE BARCODE DES TRANAMT
164 1 V 21021 ALOKOZAY TISSUE 150S (3.50)
164 1 s 21021 210521 CABAGE 5.00
164 1 C CASH CASH 1.50
208 2 V 120110 NATCO ORANGE MARMALA (6.75)
208 2 S 120110 NATCO ORANGE MARMALA 6.75
It's not entirely clear to me, but I think you want something like this:
select t1.*
from the_table t1
where exists (select 42
from the_table t2
where t2.bill_no = t1.bill_no
and t2.trantype = 'V'
and t2.couter = t1.couter);
That would return bill_no = 208 and trantype = 'C' as well (which is not part of your example output). But as you didn't explain that missing row it's hard to write a proper solution.
Assuming the missing row for Bill_no 208 is just a typo, the following solution might help:
WITH cte AS
(
SELECT BILL_NO, COUNTER
FROM YourTable WHERE TRANTYPE ='V'
)
SELECT yt.*
FROM YourTable yt
INNER JOIN cte ON yt.BILL_NO = cte.BILL_NO AND yt.COUNTER=cte.COUNTER
I have a table having data like
pin id name
3 33 jjj
2 22 bbb
1 111 aaaa
1 112 aa
1 113 aaa
4 44 kkk
I want to print rows of the table where if count(*) group by pin =1 (i.e single entry in table ) print the row
if count(*) group by pin >2 then print first two rows
so my out put should be
pin id name
3 33 jjj
2 22 bbb
1 111 aaaa
1 112 aa
4 44 kkk
Use row_number() OVER(partion by pin order by id) as rownum function . Where rownum <3
. As #Clockwork-Muse said, you need to define an order becase you need to say what do you want to see if there are more than 2 rows for a particular pin.
This will generate you desired output.