I'm currently trying to get a value from two tables. I have table A and Table B
Table A holds the main data of which i will be using to display my results.
Table B hold data which holds data if a certain condition in table A is met.
Table A
UserID |Date |useExternalInfo |Address |Hours
1 |12/12/2014 |0 |myaddress 1 | 4
2 |13/12/2014 |0 |myaddress 2 | 9
3 | 14/12/2014 |1 | myaddress 3| 12
Table B
ID | Date |Hours
1 | 12/12/2014 | 10
2 | 13/12/2014 | 6
3 | 14/12/2014 | 7
3 | 15/12/2014 | 8
the query i'm trying to get is
select UserID, Date, (hours from TableA if UseExternalinfo is 0, and from hours TableB if useextername)
from TableA
where
TableA.Date = '14/12/2014' if (ExternalUse = 0) or TableB.Date = '14/12/2014' if (ExternalUse = 1)
and user = 3
for example if date is '14/12/2014' and user = 3 i need to find the hours of 7 not 12 because use external is 1.
Not sure if that makes sense i'm just basically trying to get a 'where if' statement if such a thing exists. Could someone point me in the right direction how i could achieve this please.
thanks
select UserID,
Date,
case when UseExternalInfo = 1 then tableA.hours else tableB.hours end
from TableA left join tableB on TableA.UserId = tableB.Id
where TableA.Date = '14/12/2014' and
TableB.Date = '14/12/2014' and
user = 3
Related
So I have a table that binds ProductId and GroupId. The product can be assigned to all of 5 groups (1-5).
If the product doesn't exist in the table, it's not assigned to any of the group
ProductId | GroupId
-------------------
100 | 1
100 | 2
200 | 1
200 | 2
200 | 3
200 | 4
200 | 5
Taking a look at this table, we know that Product that goes by id 100 is assigned to 2 groups (1,2) and the product of id 200 is assigned to 5 groups (1-5).
I'm trying to write a query that will display each product in separate row, together with columns for all of the 5 groups and a bit value that contains information if the product belongs to the group or not (0,1). A visualization of the result I need:
ProductId | IsGroup1 | IsGroup2 | IsGroup3 | IsGroup4 | IsGroup5
-----------------------------------------------------------------
100 | 1 | 1 | 0 | 0 | 0 -- this belongs to groups 1, 2
200 | 1 | 1 | 1 | 1 | 1 -- this belongs to all of the groups
I know I could probably solve it using a self join 5 times on each distinct product, but I'm wondering if there's a more elegant way of solving it?
Any tips will be strongly appreciated
You could use a pivot. Since you only have 5 groups you don't need a dynamic pivot.
DB FIDDLE
select
ProductId
,IsGroup1 = iif([1] is null,0,1)
,IsGroup2 = iif([2] is null,0,1)
,IsGroup3 = iif([3] is null,0,1)
,IsGroup4 = iif([4] is null,0,1)
,IsGroup5 = iif([5] is null,0,1)
from
(select ProductID, GroupId from mytable) x
pivot
(max(GroupId) for GroupId in ([1],[2],[3],[4],[5])) p
I have two tables in SQL Server, Say in table1 I have two columns Key1Display and Key2Display, they are of datatype bit and used to control whether to display the values in table2, and table 2 will have 2 columns Key1 and Key2.
What I am trying to achieve is a sort of cross join, say if table 1 has 3 rows:
| Key1Display | Key2Display |
+---------------------+------------------+
| 0 | 1 |
| 1 | 0 |
| 1 | 1 |
Say in table 2 there are 2 rows
| Key1 | Key2 |
+---------------------+------------------+
| Row1Key1value | Row1Key2value |
| Row2Key1value | Row2Key2value |
Then based on these two tables, I want to have a query to display 6 (2*3) rows and 1 column of results like this:
null:Row1Key2value
Row1Key1Value:null
Row1Key1Value:Row1Key2value
null:Row2Key2value
Row1Key2Value:null
Row1Key2Value:Row2Key2value
So something like:
select
case when t1.Key1Display = 1 then coalesce(t2.Key1,'??') else 'null' end
+ ':' + case when t1.Key2Display = 1 then coalesce(t2.Key2,'??') else 'null' end
-- And so on for as many keys as you have
from table1 t1
cross join table2 t2
I'm having some difficulty trying to figure how to adjust my query. I'm not very good at SQL queries as it's not my forte. Anyway, I'm not sure what I'm doing wrong. Here's my table setup.
ID | Customer
---+-------------
1 | John
2 | Jane
3 | Steve
ID | Assets
---+-------------
1 | RealEstate
2 | Currency
3 | Stocks
CustomerID | AssetConfigurationId | Status
-----------+----------------------+-------
1 | 1 | E
1 | 2 | F
1 | 3 | X
2 | 3 | X
And if I query customer = 3, I want to get the following
AssetConfigurationId | Status
---------------------+------------
1 | null
2 | null
3 | X
Currently have this. I'm trying to understand how I can use left join to show all the assets and just have the values of the statuses to null for a specific customer. Right now it only shows the 3rd row. Trying to do this in a SQL Server stored procedure so that my .net application can get a list of the assets already and I'll just modify the statuses when it comes to converting them to objects.
select
ac.Id,
r.Status
from
assets ac
left join
assets_ref r on r.AssetConfigurationId = ac.Id
where
r.CustomerID = 3
Move your WHERE condition in the inner query.
select
ac.Id,
r.Status
from assets ac
left join
(select * from assets_ref where CustomerID = 3) r
on r.AssetConfigurationId = ac.Id;
You can use multiple conditions in JOINs:
select
ac.Id,
r.Status
from assets ac
left join assets_ref r
on r.AssetConfigurationId = ac.Id
and CustomerID = 3;
I have 2 tables, one with the ID and its count and the other with the names belonging to the respective IDs. They need to be joined so that the end result is a table with count of 1 in each row and the respective names next to them. Note that the number of names in table 2 is less than the count in table 1 for the same ID in some cases.
Table 1
ID | Count
-----------
100 | 3
101 | 2
102 | 4
Table 2
ID | Name
----------
100 | abc
100 | def
101 | ghi
101 | jkl
102 | mno
102 | pqr
102 | stu
Result
ID | Count | Name
------------------
100 | 1 | abc
100 | 1 | def
100 | 1 |
101 | 1 | ghi
101 | 1 | jkl
102 | 1 | mno
102 | 1 | pqr
102 | 1 | stu
102 | 1 |
I'm using TSQL for this and my current query converts table 1 into multiple rows in the result table; then it inserts individual names from table 2 into the result table through a loop. I'm hoping there must be a simpler or more efficient way to do this as the current method takes considerable amount of time. If there is, please let me know.
The first thing that comes to mind for me involves using a Number table, which you could create (as a one-time task) like this:
CREATE TABLE numbers (
ID INT
)
DECLARE #CurrentNumber INT, #MaxNumber INT
SET #MaxNumber = 100 -- Choose a value here which you feel will always be greater than MAX(table1.Count)
SET #CurrentNumber = 1
WHILE #CurrentNumber <= #MaxNumber
BEGIN
INSERT INTO numbers VALUES (#CurrentNumber)
SET #CurrentNumber = #CurrentNumber + 1
END
Once you have a numbers table, you can solve this problem like this:
SELECT one.ID,
1 AS [Count],
ISNULL(two.Name,'') AS Name
FROM table1 one
JOIN numbers n ON n.ID <= CASE WHEN one.[Count] >= (SELECT COUNT(1) FROM table2 two WHERE one.ID = two.ID)
THEN one.[Count]
ELSE (SELECT COUNT(1) FROM table2 two WHERE one.ID = two.ID)
END
LEFT JOIN (SELECT ID,
Name,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS RecordNo
FROM table2) two ON one.ID = two.ID
AND two.RecordNo = n.ID
I have following Product table and ProductTag tables -
ID | Product
--------------
1 | Product_A
2 | Product_B
3 | Product_C
TagID | ProductID
----------------------
1 | 2
1 | 3
2 | 1
2 | 2
2 | 3
3 | 1
3 | 2
Now I need a SQL query that return all products list which are having both Tag 1 and 2. Result should be as given below -
ProductID | Product
------------------------
2 | Product_B
3 | Product_C
Please suggest how can i write a MS SQL query for this.
SELECT p.ID, p.Product
FROM Product p
INNER JOIN ProductTag pt
ON p.ID = pt.ProductID
WHERE pt.TagID IN (1, 2) -- <== Tags you want to find
GROUP BY p.ID, o.Product
HAVING COUNT(*) = 2 -- <== tag count on WHERE clause
however, if TagID is not unique on every Product, you need to count only the distinct product.
HAVING COUNT(DISTINCT pt.TagID) = 2
More on: SQL of Relational Division