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
Related
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.
SELECT Boeking.reisnummer, (aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
FROM Boeking
WHERE Boeking.reisnummer = Reis.Reisnummer
AND Reis.Reisnummer = Reis.Bestemmingscode;
Table 1 (Boeking) has aantal_volwassen and aantal_kinderen, and Reisnummer.
Table 2 (Reis) has Reisnummer and Bestemmingscode.
I have to show the total of aantal_volwassenen and aantal_kinderen. But i also have to show Reis.bestemmingscode which comes from a different table. Currently i have to enter a parameter, how can i solve this?
You need to specify all the tables in the FROM part of your query. The tables should then be joined (JOIN) to get the data you need.
SELECT Boeking.reisnummer
,(aantal_volwassenen + aantal_kinderen) AS totaal_reizigers
,Reis.Bestemmingscode
FROM Boeking INNER JOIN Reis
ON Boeking.reisnummer = Reis.Reisnummer
I have 3 tables and I want to know how can I do a triple join with this type of setup if at all?
First Table: LOCATIONS_TABLE
locationID
websiteID
locationCity
locationState
locationCountry
locationURL
Second Table: PREF_TABLE
pref_ID
Pref_LocationID
Pref_WebsiteID
Pref_SavedTitle
Third Table: WEBSITECATEGORY_TABLE
wbcatID
websiteID
WBLinkCategoryParentID
WBLinkTitle
WBLinkURL
the 2nd table is where the user has a store preference with a Pref_SavedTitle for their Locations they have saved for later reference.
The Current SQL Statement I have which only does a double inner join is...
SELECT
LOCATIONS_TABLE.LocationWebsiteID,
LOCATIONS_TABLE.locationCity,
LOCATIONS_TABLE.locationState,
LOCATIONS_TABLE.locationCountry,
LOCATIONS_TABLE.locationURL,
PREF_TABLE.Pref_SavedTitle
FROM PREF_TABLE INNER JOIN LOCATIONS_TABLE
ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID
WHERE PREF_TABLE.Pref_SavedTitle = 'AlabamaPREF'
This returns sample data of...
LocationWebsiteID = 2
locationCity = Mobile
locationState = Alabama
locationCountry = United States
locationURL = alabama.bmv.org
Pref_SavedTitle - AlabamaPREF
The 3rd table has the sample data of...
wbcatID = 1
websiteID = 2
WBLinkCategoryParentID = 0
WBLinkTitle = Alabama Resources
WBLinkURL = /alabama-resources
This does exactly what it needs to do which is returns me all of the LOCATION_TABLE items that equal the LocationID that was stored previously in the PREF_TABLE.
However I have a 3rd table that I need to add to this equation (Which is the Third Table) listed above. I need to be able to link the Third Table on the websiteID somehow so that it knows the WBLinkTitle and WBLinkURL are associated with that specific websiteID
On the final output... I need to be able to pull the results for the columns...
LOCATIONS_TABLE.websiteID,
LOCATIONS_TABLE.locationURL,
WEBSITECATEGORY_TABLE.WBLinkTitle,
WEBSITECATEGORY_TABLE.WBLinkURL
which based off my sample data would be...
websiteID = 2
locationURL = alabama.dmv.org
WBLinkTitle = Alabama Resources
WBLinkURL = /alabama-resources
SQLFIDDLE EXAMPLE
UPDATED v-1
I've updated the answer according to your comment (now the 3rd table is inner-joined to PREF_TABLE and additional condition was added to the first join: AND PREF_TABLE.Pref_WebsiteID = LOCATIONS_TABLE.LocationWebsiteID to prevent all-locations loading):
SELECT
LOCATIONS_TABLE.LocationWebsiteID,
LOCATIONS_TABLE.locationCity,
LOCATIONS_TABLE.locationState,
LOCATIONS_TABLE.locationCountry,
LOCATIONS_TABLE.locationURL,
PREF_TABLE.Pref_SavedTitle,
WEBSITECATEGORY_TABLE.WBLinkTitle,
WEBSITECATEGORY_TABLE.WBLinkURL
FROM
PREF_TABLE
INNER JOIN LOCATIONS_TABLE
ON PREF_TABLE.Pref_LocationID = LOCATIONS_TABLE.LocationID
AND PREF_TABLE.Pref_WebsiteID = LOCATIONS_TABLE.LocationWebsiteID
INNER JOIN WEBSITECATEGORY_TABLE
ON WEBSITECATEGORY_TABLE.websiteID = PREF_TABLE.Pref_WebsiteID
WHERE
PREF_TABLE.Pref_SavedTitle = 'AlabamaPREF'
I am having a bit of trouble, probably from my understanding of SQL. Here is the SQL I am currently using:
CREATE TEMPORARY TABLE Temp
(
sPropertyCode VARCHAR(9),
sDataDate DATE,
PRIMARY KEY (sPropertyCode)
);
INSERT IGNORE Temp (sPropertyCode, sDataDate)
SELECT sPropertyCode, sDataDate
FROM tasks as t, task_data AS d
WHERE t.iTaskId = d.iTaskId
AND iRemoved = 1
AND sDataType = 'sAgencyAgreementDate'
AND iBusinessStreamId = 9;
SELECT t.sPropertyCode, sDataDate, SFirstSeen, sTaskType
FROM tasks AS t, temp AS a
WHERE iRemoved = 1
AND iBusinessStreamId = 9
AND sTaskType IN ('RF', 'IF', 'CM')
AND t.sPropertyCode = a.sPropertyCode
ORDER BY sPropertyCode, sFirstSeen;
DROP TABLE Temp;
So the references 'RF', 'IF' and 'CM' are tasks that we receive. Each propertycode can touch each of these tasks once, and only once. I would like to show the date that each one of these was touched by the propertycode. It is working at the moment but it is showing it in three columns with the tasks types in one column. I would like each task to show in a seperate column with the date it was first seen in its own corresponding column.
So from the picture below is how it is currently laid out with the code above.
And here is how I would like it to look, instead of the tasks showing down the side, I would like them to show accross in columns with their own specific dates
Thank you in advance :)
SELECT t.sPropertyCode, sDataDate, SFirstSeen, a1.sTaskType, a2.sTaskType, a3.sTaskType
FROM tasks AS t
INNER JOIN temp AS a1 on t.sPropertyCode = a1.sPropertyCode and a1.sTaskType = 'RF'
INNER JOIN temp as a2 on t.sPropertyCode = a2.sPropertyCode and a2.sTaskType = 'IF'
INNER JOIN temp as a3 on t.sPropertyCode = a3.sPropertyCode and a3.sTaskType = 'CM'
WHERE iRemoved = 1 AND iBusinessStreamId = 9
ORDER BY sPropertyCode, sFirstSeen;
You can also user outer join if not always have all 3 tast type.
What is the difference in meaning between the CM,IF and RF columns for any row in your 5-col table ? They're always the very same value in the example you listed.
so here's my situation.
I have two tables (keysetdata115) containing vendor information and keysetdata117 that contains either a Remit or Payment address.
Here are the structures with one sample entry:
keysetdata115:
keysetnum ks183 ks178 ks184 ks185 ks187 usagecount
2160826 1 6934 AUDIO DIGEST FOUNDATION 26-1180877 A 0
keysetdata117 (I truncated values for ks192 and ks191 to fit formatting)
keysetnum ks183 ks178 ks188 ks189 ks190 ks192 ks191 usagecount
2160827 1 6934 P001 P EBSCO... TOP OF... A 0
2160828 1 6934 R002 R EBSCO... 123 SE... A 0
There is no 1:1 relationship and the only thing that makes a unique record is the combination or Remit Code,Payment Code, vendor number and vendor group.The codes can only be obtained by referencing the address and / or name.
Ideally what I'd like to do is set this up so that I can pass in the addresses and return all the related values.
I'm dumping this in a table called 'dbo.test' right now (for testing obviously), that has the following entries and what the correspond to in the above tables: vengroup (ks183), vendnum (ks178), remit (ks188), payment (ks188)... ks188 will be a remit or payment based off the value in ks189.
This is what I'm doing so far, using 3 select queries and it works, but there's a lot of redundancy and it's very inefficient.
Any suggestions on how I can streamline it would be MUCH appreciated.
insert into dbo.test (vengroup,vendnum)
select ks183, ks178
from hsi.keysetdata115
where ks184 like 'AUDIO DIGEST%'
update dbo.test
set dbo.test.remit = y.remit
from
dbo.test tst
INNER JOIN
(Select ksd.ks188 as remit, ksd.ks183 as vengroup, ksd.ks178 as vendnum
from hsi.keysetdata117 ksd
inner join dbo.test tst
on tst.vengroup = ksd.ks183 and tst.vendnum = ksd.ks178
where ksd.ks190 like 'EBSCO%' and ks189 = 'R') y
on tst.vengroup = y.vengroup and tst.vendnum = y.vendnum
update dbo.test
set dbo.test.payment = y.payment
from
dbo.test tst
INNER JOIN
(Select ksd.ks188 as payment, ksd.ks183 as vengroup, ksd.ks178 as vendnum
from hsi.keysetdata117 ksd
inner join dbo.test tst
on tst.vengroup = ksd.ks183 and tst.vendnum = ksd.ks178
where ksd.ks190 like 'EBSCO%' and ks189 = 'P') y
on tst.vengroup = y.vengroup and tst.vendnum = y.vendnum
Thanks so much for any suggestions!
You can do what you want in one statement. You just have to do the selection on the run. The way the statement below is written, if Remit gets the value, Payment gets a null and vice versa. If you want the other value to be non-null, just add an else clause to the cases. Like then b.ks188 else 0 end.
INSERT INTO dbo.TEST( vengroup, vendnum, remit, payment )
SELECT a.ks183, a.ks178,
CASE b.ks189 WHEN 'R' THEN b.ks188 END,
CASE b.ks189 WHEN 'P' THEN b.ks188 END
FROM keysetdata115 a
JOIN keysetdata117 b
ON b.ks183 = a.ks183
AND b.ks178 = a.ks178
AND b.ks190 LIKE 'EBSCO%'
WHERE a.ks184 LIKE 'AUDIO DIGEST%';