sql query for selecting field with condition [closed] - sql

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

Related

SQL JOIN,LEFT JOIN,RIGHT JOIN [closed]

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)

Need query logic in SQL Server [closed]

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.

How to select top 4 records of amount from 2 tables [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have 2 tables one contain document detail as below :
Table1 : contains customer and total document amount.
DocEntry CustID CustName City DocAmount
1 GF002 Raffy N London 120.00
2 GF025 Jhon Liverpool 50.00
3 GF120 Keng London 125.25
4 GF055 Tung L. London 30.00
5 GF020 Lee H. Manchester 60.00
Table2 : contains item and item price of each document.
DocEntry LineNum ItemID ItemName ItemPrice Qty LineAmount
1 0 I0001 Mouse 6.00 5 30.00
1 1 I0002 Key Broad 6.00 5 30.00
1 2 I0200 Monitor 60.00 1 60.00
2 0 I0501 Ext.HDD1 50.00 1 50.00
3 0 I0665 Printer 125.00 1 125.00
4 0 I0002 Key Broad 6.00 4 24.00
4 1 I0001 Mouse 6.00 1 6.00
5 0 I0050 ODD 12.00 1 12.00
5 1 I0001 Mouse 6.00 8 48.00
I would like to select the top 3 of documents from Table1 which have highest DocAmount and in the top 3 selected have to show line detail from Table2
the result should be :
Row DocEntry CustID CustName DocAmount ItemID ItemName ItemPrice Qty LineAmount
1 3 GF120 Keng 125.25 I0665 Printer 125.00 1 125.00
2 1 GF002 Raffy N 120.00 I0001 Mouse 6.00 5 30.00
3 1 GF002 Raffy N 120.00 I0002 Key Broad 6.00 5 30.00
4 1 GF002 Raffy N 120.00 I0200 Monitor 60.00 1 60.00
5 5 GF020 Lee H. 60.00 I0050 ODD 12.00 1 12.00
5 5 GF020 Lee H. 60.00 I0001 Mouse 6.00 8 48.00
select Table2.DocEntry, CustID, CustName, DocAmount, ItemID, ItemName,
ItemPrice, Qty, LineAmount
from (select top 3 * from Table1 order by DocAmount desc) TopDocs
join Table2 on TopDocs.DocEntry=Table2.DocEntry
order by DocAmount desc
SQL Fiddle here.

Creating a new column based on data from an existing column

Consider a system to track repairs. I have a table with customer data and a common id key. I have a second table with a column to show which type of repair part was used on each id key and how many were used. Their definitions are below:
order_information
order_id | cust_last_name
465 Smith
899 Williams
512 Johnson
345 Fenton
122 Bowles
944 Cooper
parts_usage
order_id | part_type | part_quantity
465 Part 1 5
465 Part 2 4
899 Part 1 2
899 Part 2 8
899 Part 3 6
512 Part 3 1
345 Part 2 4
345 Part 3 5
122 Part 2 3
944 Part 1 2
I'd like to run a query for reporting that will return the part's pieces broken out like so:
order_id | Part 1 | Part 2 | Part 3 | Total
465 5 4 9
899 2 8 6 16
512 1 1
345 4 5 9
122 3 3
944 2 2
Is it possible to do this with a query so that my reports can show how many of each part was used on each repair ticket?
As you can see, each order_id can have multiple part types and unique quantities. I want to break the different part types (I have 3 total) into 3 separate columns with their totals listed by order_id.
select order_id, [Part 1], [Part 2], [Part 3], Total
from
(
select oi.order_id
, part_type
, part_quantity
, Total = sum(part_quantity) over (partition by oi.order_id)
from order_information oi
inner join parts_usage pu on oi.order_id = pu.order_id
) as parts
pivot
(
sum(part_quantity) for part_type in ([Part 1], [Part 2], [Part 3])
) as pvt
order by order_id
This works for me.
I have ordered the resultset by order_id as well; there doesn't appear to be a specific order in your example results but it is mentioned in the question details.
You can see the key is to combine the PIVOT with a SUM aggregate window function.
SQL Fiddle with demo.

MS Access, Excel, SQL, and New Tables

I'm just starting out with MS Access 2010 and have the following setup. 3 excel files: masterlist.x (which contains every product that I sell), vender1.x (which contains all products from vender1, I only sell some of these products), and vender2.x (again, contains all products from vender2, I only sell some of these products). Here's an example data collection:
masterlist.x
ID NAME PRICE
23 bananas .50
33 apples .75
35 nuts .87
38 raisins .25
vender1.x
ID NAME PRICE
23 bananas .50
25 pears .88
vender2.x
ID NAME PRICE
33 apples .75
35 nuts .87
38 raisins .25
49 kiwis .88
The vender lists get periodically updated with new items for sell and new prices. For example, vender1 raises the price on bananas to $.75, my masterlist.x would need to be updated to reflect this.
Where I'm at now: I know how to import the 3 excel charts into Access. From there, I've been researching if I need to setup relationships, create a macro, or a SQL query to accomplish my goals. Not necessarily looking for a solution, but to be pointed in the right direction would be great!
Also, once the masterlist.x table is updated, what feature would I use to see which line items were affected?
Update: discovered SQL /JOIN/ and have the following:
SELECT * FROM master
LEFT JOIN vender1
ON master.ID = vender1.ID
where master.PRICE <> vender1.PRICE;
This gives me the output (for the above scenario)
ID NAME PRICE ID NAME PRICE
23 bananas .50 23 bananas .75
What feature would instead give me:
masterlist.x
ID NAME PRICE
23 bananas .75
33 apples .75
35 nuts .87
38 raisins .25
Here is a heads up since you were asking for ideas to design. I don't really fancy your current table schema. The following queries are built in SQL Server 2008, the nearest syntax that I could get in sqlfiddle to MS Access SQL.
Please take a look:
SQLFIDDLE DEMO
Proposed table design:
vendor table:
VID VNAME
1 smp farms
2 coles
3 cold str
4 Anvil NSW
product table:
PID VID PNAME PPRICE
203 2 bananas 0.5
205 2 pears 0.88
301 3 bananas 0.78
303 3 apples 0.75
305 3 nuts 0.87
308 3 raisins 0.25
409 4 kiwis 0.88
masterlist:
ID PID MPRICE
1 203 0.5
2 303 0.75
3 305 0.87
4 308 0.25
Join queries can easily update your masterlist now. for e.g.:
When the vendor updates their prices for the fruits they provide you. Or when they stop supply on that product. You may use where clauses to add the conditions to the query as you desire.
Query:
SELECT m.id, p.vid, p.pname, p.pprice
FROM masterlist m
LEFT JOIN product p ON p.pid = m.pid
;
Results:
ID VID PNAME PPRICE
1 2 bananas 0.5
2 3 apples 0.75
3 3 nuts 0.87
4 3 raisins 0.25
Please comment. Happy to help you if have any doubts.