displaying data in dropdownlist using sql with querystring - sql

I have three tables:
tbprd -- prdcod int PK, prdtit v(100), prddefuom int FK to prduomcod (tbprduom)
tbuom -- uomcod int PK, uomnam v(100)
tbprduom -- prduomcod int PK, prduomprdcod int FK prdcod(tbprd), prduomuomcod int FK uomcod(tbuom)
What I need is to display uomnam in dropdownlist display field and uomcod in data value field from prdcod which I have passed through query string but if the chosen prdcod is 4 then the dropdownlist should display all the uomnam matching w.r.t all matching prdtit
Query I am using:
select uomcod, uomnam
from tbuom, tbprd, tbprduom
where prdcod = 4
and prduomuomcod = uomcod
and prddefprduomcod = prduomcod
Look at the images for more clearer idea about what I want and table data
https://drive.google.com/drive/folders/17p3d1WXMppndIq1bXf6EwShLWBrh-IsC?usp=sharing

Based on what I understand, you want to get all products that have the same title as the product with the given prdcod. If that's correct, then here's one way to do it:
Write a query to get the prdtit value for prdcod = 4 (return one column from one row):
SELECT prdtit
FROM ...
Use this value to filter the rest of the rows in the tbprd table:
SELECT *
FROM tbrpd
WHERE prdtit = (<Query in Step 1>)
Add two joins to the tbprduom and tbuom tables to Step #2 to get the remaining info:
SELECT tbuom.uomcod, tbuom.uomnam
FROM tbrpd
WHERE prdtit = (<Query in Step 1>)
LEFT JOIN tbprduom ON ... <PK/FK fields) -- Get uomcod for products
LEFT JOIN tbuom ON ... <PK/FK fields) -- Get "uom" fields to return
Give it a try and see how far you get. If you get stuck on a step, just let me know.

Related

How to replace a value in one field by a value from another field (different column) within the same view table (SQL)?

I'd like to know if it is possible to replace a value from one field by using a value from another column and a different row.
For Example, click this to view the table image.
I'd like the SumRedeemed value 400 in row 15 to be replaced by the value -1*(-395); the value -395 comes from the EarnPointsLeft in row 6 (both have the same CID meaning that they are the same person). Any suggestions?
You need this update statement:
update t
set t.sumredeemed = (-1) * (select earnpointsleft from FifoPtsView where cid = t.cid)
from FifoPtsView t
where t.cid = 5000100008 and t.earnpointsleft = 0
This will work if the select statement will return only 1 row.
you can simply update your table
update t
set t.sumredeemed = ABS(t2.earnpointsleft )
from FifoPtsView t join FifoPtsView t2 on t.cid = t.cid and isnull(t2.earnpointsleft,0)>0
if you want negative values you can remove ABS ,
please give me your feedbacks

Comparing multiple row and columns in sql server

I had two tables first one is the real table second one is the temporary. I had to compare this two table . First table had 'KartelaKod' field should match with temp's 'KartelaKodu' field, First's 'OzellikKod' field should match with temp's 'OzellikKodu',first's 'AltKod' field should match with temp's 'StokKodu'.
Finally if it doesn't match exactly it should return ''.
PS: You should have to group first table according to VrtUrunKod.
In example if MAM|002's (which is VrtUrunKod) OzellikKod field doesn't match with temp table's OzellikKodu field, it should look for another VrtUrunKod record.
Edit1:isnull((Select TOP 1 VrtUrunKod From KonfigOlusmusOzellikler k Where Sirket_Kod = #sp_Sirket_Kod and AnaUrunKod = #spStokKod and
exists(Select * From #TempDegisen t Where t.KartelaKodu = k.KartelaKodu and t.OzellikKodu = k.OzellikKod and t.StokKodu = k.AltKod)),'') this is what i written so far you can ignore Sirket_Kod field and parameter.
This should produce a list of rows where there is a k with no matching t, and at with no matching k. If no rows result, all rows are matched:
SELECT *
FROM
KonfigOlusmusOzellikler k
FULL OUTER JOIN
#TempDegisen t
ON
t.KartelaKodu = k.KartelaKodu and
t.OzellikKodu = k.OzellikKod and
t.StokKodu = k.AltKod
WHERE t.KartelaKodu IS NULL OR k.KartelaKodu IS NULL

Retrieve multiple values from table, bit value and ID

I would like to retrieve all data related to a spesific ID and also all of those data that have a bit value of 1 within the same table.
This is the table data:
Description
---------------------
INT DID
INT DescID
VARCH DescTitle
VARCH DescText
BIT ForAll
Description_Join
---------------------
INT DJID
INT DescID
INT ProductID
The tables are joined by the DescID. So now I would like to show all data with a specific ProductID (from request.querystring) and i will also like to display ALL data from the table "description" where the field "ForAll" has the BIT value of 1.
I just cannot get this to work.
This is the Sql I have tried
SELECT D.DescID, DJ.DescID, DJ.ProductID, D.ForAll, D.DescTitle, D.DescText
FROM Brand_Variant_Description D
INNER JOIN Description_Join DJ ON (D.DescID = DJ.DescID)
WHERE DJ.ProductID=222 AND D.BrandDescAllSel=1
I assume you need all data that matches specific productid plus data with bit value 1 even if it has different productid. if this is what you need, then in your query change Inner join as Left join and replace final AND as OR like this
SELECT D.DescID, DJ.DescID, DJ.ProductID, D.ForAll, D.DescTitle, D.DescText
FROM Brand_Variant_Description D
LEFT JOIN Description_Join DJ ON (D.DescID = DJ.DescID)
WHERE DJ.ProductID=222 OR D.BrandDescAllSel=1
Try this Query
SELECT D.DescID
,DJ.DescID
,DJ.ProductID
,D.ForAll
,D.DescTitle
,D.DescText
FROM Brand_Variant_Description D
LEFT JOIN Description_Join DJ ON (D.DescID = DJ.DescID)
AND (D.ForAll = 1)
WHERE DJ.ProductID = 222
AND D.BrandDescAllSel = 1

SQL, return a row multiple times from single table based on variable

I'm creating some box labels using iReport and need to multiply out the data for each box label.
I have 2 parameters, #id for the record id and #typ for the package type required
SELECT
dr_id,
dr_to_customer,
dr_company_name,
dr_address_0,
dr_address_1,
dr_address_2,
dr_address_3,
dr_postcode,
dr_contact,
dr_per_packs,
dr_per_boxes,
dr_per_pallets
FROM delreq
WHERE dr_id = #id AND ??timesTOrepeat?? = #typ (a string)
I need to return each row x times based on the qty's in either packs, boxes or pallets.
So, I need to first select the qty based on #typ then work out how to multiply out the rows.
Help.
You can try to achieve this using outer join. Try this:
SELECT
dr_id,
dr_to_customer,
dr_company_name,
dr_address_0,
dr_address_1,
dr_address_2,
dr_address_3,
dr_postcode,
dr_contact,
dr_per_packs,
dr_per_boxes,
dr_per_pallets
FROM delreq a, (select -1 id from all_objects where rownum < #typ) b
WHERE dr_id = #id AND a.dr_id(+) = b.id;
I am assuming here that dr_id will not have value -1 also assuming db is oracle [not sure abut other DB.

outer query to list only if its rowcount equates to inner subquery

Need help on a query using sql server 2005
I am having two tables
code
chargecode
chargeid
orgid
entry
chargeid
itemNo
rate
I need to list all the chargeids in entry table if it contains multiple entries having different chargeids
which got listed in code table having the same charge code.
data :
code
100,1,100
100,2,100
100,3,100
101,11,100
101,12,100
entry
1,x1,1
1,x2,2
2,x3,2
11,x4,1
11,x5,1
using the above data , it query should list chargeids 1 and 2 and not 11.
I got the way to know how many rows in entry satisfies the criteria, but m failing to get the chargeids
select count (distinct chargeId)
from entry where chargeid in (select chargeid from code where chargecode = (SELECT A.chargecode
from code as A join code as B
ON A.chargecode = B.chargeCode and A.chargetype = B.chargetype and A.orgId = B.orgId AND A.CHARGEID = b.CHARGEid
group by A.chargecode,A.orgid
having count(A.chargecode) > 1)
)
First off: I apologise for my completely inaccurate original answer.
The solution to your problem is a self-join. Self-joins are used when you want to select more than one row from the same table. In our case we want to select two charge IDs that have the same charge code:
SELECT DISTINCT c1.chargeid, c2.chargeid FROM code c1
JOIN code c2 ON c1.chargeid != c2.chargeid AND c1.chargecode = c2.chargecode
JOIN entry e1 ON e1.chargeid = c1.chargeid
JOIN entry e2 ON e2.chargeid = c2.chargeid
WHERE c1.chargeid < c2.chargeid
Explanation of this:
First we pick any two charge IDs from 'code'. The DISTINCT avoids duplicates. We make sure they're two different IDs and that they map to the same chargecode.
Then we join on 'entry' (twice) to make sure they both appear in the entry table.
This approach gives (for your example) the pairs (1,2) and (2,1). So we also insist on an ordering; this cuts to result set down to just (1,2), as you described.