Perform Query Using Columns of Table as Arguments - sql-server

I need to perform different queries with different arguments. These arguments are arranged in the table T2 below. Everyday these arguments change so the table T2 change and the result of the query I want to do will change. The query is simple but I dont know how to perform using the columns of T2 as arguments...
-----Table T2----
ID Country FilterExpression
----------- ------- ------ ----- -----
1 Argentina 'Filter01'
2 Brazil 'Filter02'
3 USA 'Filter03'
4 UK 'Filter04'
5 France 'Filter05'
6 Mexico 'Filter06'
...
100 Canada 'Filter100'
The query I need to perform:
SELECT Element,Value
FROM ArchTot
WHERE Country = [Column Country of T2]
AND FilterExpr = [Column FilterExpression of T2]
Once I have a Table of arguments of 100 rows, the result of my query must have the same 100 rows.
Somebody can help me to build this query?

Try to be more specific so I can understand what you need to do.. Why you need to retrieve 100 for 100? And when The table data changes what happens to The old data... You need to query new data or The data before it changes?

Everyday the table of arguments change so I have to perform the query to update the Result. Once I Have 100 arguments I'll have 100 results:
Result of query from Tables Archtot or Archtot2
Country FilterExpr Value
----------- ------- ------ ----- -----
Argentina 'Filter01' 100.82
Brazil 'Filter02' 102.87
USA 'Filter03' 82.7
UK 'Filter04' 106.8
France 'Filter05' 110.7
Mexico 'Filter06' 79.9
...
Canada 'Filter100' 102.04
I Tried the following, It works...
SELECT A.Element,A.Value
FROM ArchTot A, T2 B
WHERE A.Country = B.Country
AND A.FilterExpr = B.FilterExpression
The problem now is that I Cant perform a INNER JOIN Statement once I have inserted the Table T2 in the FROM clause, it gives timeout. For example:
SELECT * FROM
(
(SELECT A.Element As Element,A.Value As Value, A.Country As Country
FROM ArchTot A, T2 B
WHERE A.Country = B.Country
AND A.FilterExpr = B.FilterExpression)T1
INNER JOIN
(SELECT C.Element As Element,C.Value As Value, C.Country As Country
FROM ArchTot2 C, T2 D
WHERE C.Country = D.Country
AND C.FilterExpr = D.FilterExpression)T2
ON T1.Country=T2.Country
)
Is the query I Built OK? Any Ideas why I Cant Perform the INNER JOIN?

Related

do i need a certain join statement for my code

I'm stuck on writing this piece of code.
the output that i need to get is
firstName lastname Name Price Date
--------------- --------------- ------------------------- -------- ----------
Bateman Michael Furniture DR 222.80 2013-05-01
Tara Roswell Clothes Ladies 24.25 2013-05-04
LeMay Mike Toys Child 12.00 2013-05-12
Create a query that will show who picked up unsold items during the month of May 2013. Include in your output the first and last name of the owner, along with the name of the item, max price and pick up date. Order your output by Pick up date. My output looked like the following:
and my output is
FirstName LastName Name Price Date
--------------- --------------- ------------------------- ------- ----------
my code is
SELECT P.FirstName
, P.LastName
, IT.Name
, I.MaxPrice AS Price
, IP.Date
FROM People P
JOIN CHARITY C ON P.PeopleID = C.ContactID
JOIN Donation D ON C.CHARITYID = D.CHARITYID
JOIN Item_Donation ID ON D.DonationID = ID.DonationID
JOIN IteM I ON ID.ItemID = I.ItemID
JOIN Item_Type IT ON I.ItemTypeID = IT.ItemTypeID
JOIN Item_PickUp IP ON I.ItemID = IP.ItemID
ORDER BY IP.Date
I see nothing wrong with your query, so I suspect a data issue. Here is how I would investigate this. Start with querying the "root" table:
SELECT * FROM People p
If the results are not what you expect, then you have found the issue in your data.
If the results are ok, then add the first join:
SELECT *
FROM People P
JOIN CHARITY C ON P.PeopleID = C.ContactID
Same thing, if the results are not what you expect, then you have a data issue in the CHARITY table. Query the CHARITY table by itself to see why the rows from that table are not joining to People:
SELECT * FROM CHARITY
If the results are what you expect, then add the next JOIN, and continue, one JOIN at a time, until you find the one that causes no rows to be returned. Examine the data in that table to see why it doesn't join to your query before the join.

SSRS2008 - "GroupBy" in .rdl causes losing such data groupby filter repeating others not

I have a stored procedure in Sql Server 2008. The logic of the SP is looking like:
select Name, Surname, sum(Age)
From (
(Select t1.Name,t1.Surname,t1.sum(Age) as Age
From Table1 t1
group by t1.Name, t1.Surname) mt1
full outer join (Select t2.Name as N,t2.Surname as S,t2.sum(Age) as A
From Table2 t2
group by t2.Name, t2.Surname) mt2
on mt1.Name = mt2.N and mt1.Surname = mt2.S
) t
where sum(Age) <= 100
So this is returning something like:
Name Surname Age
A B 12
X B 13
C D 15
I have an SSRS matrix like:
Description Age
<<Expr>> sum([Age])
where Expr is: IIF(Parameters!check.Value=1,Fields!Name.Value,Fields!Surname.Value)
Without making any groupby under the related row group in SSRS2008, i am able to see it like tkat. However, when I add a groupby filter depending on "Surname", it somehow removes any Surname that occurring with at least 2 different name. So, I see only D 15 row instead of:
B 25
D 15
What is my mistake here? How can I show B 25 at this point as well? (I am supposed to not to change SP if possible)
Any help would be appreciated.

sql server duplicated result while getting data from multiple tables

Hi i have a small problem with SQL SERVER .
an building a Purchase and stokes system i want to retrieve data from 3 Tables:-
Tables Structure
Query:
SELECT Tbl_Products.Product_Name, Tbl_PurchaseDetails.BuyPrice, Tbl_PurchaseDetails.AllPieceBoxes, Tbl_PurchaseDetails.TotalPrice, Tbl_PurchaseHeader.purchaseOrder
FROM Tbl_Products
INNER JOIN Tbl_PurchaseHeader ON Tbl_Products.Product_ID = Tbl_PurchaseHeader.ProductId
INNER JOIN Tbl_PurchaseDetails ON Tbl_PurchaseHeader.purchaseOrder = Tbl_PurchaseDetails.PurchaseOrder
Query Result
Product_Name BuyPrice AllPieceBoxes TotalPrice purchaseOrder
-------------------- -------- ------------- ---------- -------------
Dell Alien-Ware 2500.000 5 12500.000 1
KINGSTON-Desktop 8GB 2500.000 5 12500.000 1
Dell Alien-Ware 95.00 10 950.000 1
KINGSTON-Desktop 8GB 95.00 10 950.000 1
Given the join below I think you have multiple rows for a given Product_ID in Tbl_PurchaseHeader which you then join with tbl_PurchaseDetails. For example if there are two rows in Tbl_PurchaseHeader with Product_ID of 1 then you will join with Tbl_PurchaseDetails twice so 1 Tbl_Products gives 2 Tbl_PurchaseHeader's which if there are multiple (say 3) rows in Tbl_PurchaseDetails then you'll get an additional 2 x 3 so 6 rows.
FROM Tbl_Products
INNER JOIN Tbl_PurchaseHeader ON Tbl_Products.Product_ID = Tbl_PurchaseHeader.ProductId
INNER JOIN Tbl_PurchaseDetails ON Tbl_PurchaseHeader.purchaseOrder = Tbl_PurchaseDetails.PurchaseOrder
Check out your data, do a singleton SELECT against each table and track the data through to see how many rows come back for a) Product_ID and then for PurchaseOrder.

Query in SQL Server with multiple join tables

I have 3 tables:
Table Position:
KodePosition | NamePosition | UserLogin
========================================
0037 Master A winz\alfa
0038 Master B winz\beta
0043 Master C winz\carlie
Table UserBackup (PJS):
KodePosition | UserOrigin | UserChange | StartDate | EndDate
================================================================
0037 winz\alfa winz\carlie 10-10-2014 17-10-2014
Table History:
IdHistory | KodePosition | StartDate | EndDate | User | Comment
===============================================================================
19F5FCFC 0038 14-10-2014 14-10-2014 winz\beta i not agree...
19F5FCFC 0043 15-10-2014 15-10-2014 winz\carlie i agree...
I want to display data like this :
Name | Date | Position | Comment
===================================================
winz\beta 14-10-2014 Master B i not agree...
winz\carlie 15-10-2014 Master A i agree...
Description :
please note the data in Table UserBackup(PJS).
if StartDate in Table History between StartDate and EndDate in Table UserBackup(PJS) and also the same UserChange with user, and then get NamePosition from Table Position by KodePosition of Table UserBackup(PJS).
For now, I have a stored procedure like this, but doesn't display the data I need.
select
A.IdHistory, A.StartDate, B.NamePosition, B.UserLogin, A.Comment
from
History as A
left join
Position as B on A.KodePosition = B.KodePosition
Where
A.IdHistory = '19F5FCFC'
order by
A.StartDate asc
Please help me guys... Thanks...
If you want to ask three tables, you can do it like this (sorry for not using your data, but I dont understand the connections of them(or your explanations)).
SELECT a.Column1, a.Column2, b.Column1
FROM table1 AS a
LEFT JOIN table2 AS b
ON a.Column1=b.Column1
WHERE (
SELECT c.Column1
FROM table3 AS c
INNER JOIN table2 as b
ON b.Column1=c.Column1
WHERE c.Column2 LIKE 'MyTarget%');
You got to think in two SELECTs, dont try to put everything in one. Btw. most DBs dont support multi Joins.
Without more information about what you are trying to do, this is a little bit of a stab in the dark...but based on your description, I believe you are trying to first filter the rows in the UserBackup(PJS) table based on whether the corresponding History.KodePosition record falls within the StartDate and EndDate of the corresponding UserBackup(PJS) record. Then, based on the returned KodePositions, you want to retrieve the related records from the Position table. I'm not sure this is the complete picture of what you are looking for, but hopefully this gets you further along:
;WITH cteData
AS (
SELECT u.KodePosition, h.IDHistory, h.StartDate, h.Comment
FROM UserBackup(PJS) u
INNER JOIN History h ON h.User = u.UserChange AND (h.StartDate >= u.Startdate and h.StartDate <= u.EndDate)
)
SELECT c.IDHistory, c.StartDate, p.NamePostition, p.UserLogin, c.Comment
FROM Position p
INNER JOIN cteData c ON c.KodePosition = p.KodePosition

Get union of two table and taking data with a condition

I have two tables
table-a
id name
100 asd
101 ass
102 gdd
103 hgf
104 cvd
105 erf
table-b
id filter
100 red
101 blue
100 green
100 yellow
102 black
102 red
103 dark
Table-a is the master table and that have all the id's.but Table two is the one which has 'filter' data.
from these two table I want to find out all those 'id's which does not have minimum 2 filters.
note that table-b does not have all the itemnumbers in table-a, and i want all that itemnumber irrespective of if that is in table-a or table-b.I have tried inner joining these two tables and getting data out but nothing worked.
Select A.ID, A.Name, count(*)
from tableA A
LEFT JOIN tableB B on A.ID = B.ID
Group By A.ID, A.name
having count(*) <= 1
LEFT JOIN gives all records from A and only those in B which match.
The group by ID and name let us count the number of filters found in
each
The having says give me any items with a count less than or
equal to 1. (or less than the minimum 2)
Thus results would be.
101 ass 1
103 hgf 1
104 cvd 0
105 erf 0
select
*
from
table-a a
left join (
select id, count(id) as c from table-b group by id
) v on a.id = v.id
where isnull(v.id, 0) < 2
I think this would work in SQL Server (tested in SQLite and usually the two are fairly compatible when it comes to inline view syntax). But syntax issues aside, inline views can make working with sets easier to visualize.
select TA.id, name
from TA
inner join
(
select id from TA
where not exists (select id from TB where TA.id = TB.id)
UNION
select id from TB
group by id having count(filter) < 2
) as FOO
on TA.id = FOO.id
The default behavior of UNION is to remove duplicates.
The first UNIONed set consists of the ids from table A that have no filter (no counterpart in the filters table B).
The second UNIONed set consists of the ids from the filters table, table B, that have only 1 filter.
We inner join those unioned sets back to Table A to get the entity Name.

Resources