I have the following table1:
ITEM1|ITEM2
COOKIE|CHAIR
PIE|TABLE
PIE APPLE|PENCIL
BANANA CAKE|PEN
expected output results:
ITEM_ALL
COOKIE
PIE
PIE APPLE
BANANA CAKE
CHAIR
TABLE
PENCIL
PEN
Can someone help with the query, for transpose column to rows? Thank you before.
You can use union all:
select item1 as item_all from table1
union all
select item2 as item from table1;
If you want to remove duplicates, then use union rather than union all.
I have three databases connected remotedly via DBLink and I want to create a record in the three of them. The dynamic in this problem is that there is a company which has 3 retail stores in which they can sell items found in the others. The item1 is only found in retailstore1, item2 is only found in retailstore2 and so on. However, one store can sell items not currently found in it so it needs to request to the other stores in order to check if there's enough in stock. Everytime one store sells, it creates a record in LOG, but only if it was successful.
These are the tables used in the example.
Retail Store #1: Items(a int), LOG(a int, b int, c int)
Retail Store #2: Items(b int), LOG(a int, b int, c int)
Retail Store #3: Items(c int), LOG(a int, b int, c int)
Now, I want to check if there's enough items in stock before making the record in LOG in the three stores, but this has to be done in a single query or store procedure or the necessary steps to make this possible. The restriction is that the databases need to be open once, execute, close and no more.
It sounds like what you want is a query that shows which store stocks the item, so when the clerk does a lookup, it shows the item, store, and qty left. One way might use unions, such that if a procedure where you passed in the item number (without knowing where it exists), you would query like this:
(p_item IN NUMBER) -- this is the parameter passed in with the item number desired
select item1, store1, qty1, 0 item2, 0 store2, 0 qty2, 0 item3, 0 store3, 0 qty3
from store1
where item1 = p_item
union all
select 0, 0, 0, item2, store2, qty2, 0, 0, 0
from store2
where item2 = p_item
union all
select 0, 0, 0, 0, 0, 0, item3, store3, qty3
from store3
where item3 = p_item
this should only return 1 row with values in it other than 0s, and will tell you where it's at.
The update could then be using an if statement or case statement to update the appropriate store based on which item/store combination it was found in.
How can I specify different FilteringSelect Option contents per row based on the data or condition in different column of the grid? The Filtering Select is created in Dgrid ColumnSets.
Example:
I have 2 sets of stores, store1 and store2
Filtering select is placed on Col2
If Col1 = ConditionA, then set filtering select store to Store1
If Col1 = ConditionB, then set filtering select store to Store2
In my database I have table with multiple "yes/no" rows on which I would like to run some selection check if they are selected or not.
ITEM ORDERED ORDER_ID
item1 true 1
item2 false 1
item3 true 2
item4 true 3
item5 true 4
item6 true 4
In datagridview in my code I will like to achieve this.
ORDER STATUS
1 not ordered
2 ordered
3 ordered
4 ordered
In my code I will like to go through all orders and check if status of ordered items on that order is true. If all items on some order have status true, then i will like to set another flag that I have in ORDER table to true(ordered). Which is the most elegant way to solve this problem? If you have some example of this like problem I will really appreciate it. Thanks in advance.
It appears STATUS means whether all rows for that ORDER_ID have ORDERED = True. If that is correct, I think you can get what you need with an Access aggregate (GROUP BY) query.
True is stored as -1 and False is stored as 0. So if the maximum ORDERED value for any ORDER_ID is 0, you know there is at least one item for that order which has not yet been ordered. If the maximum is -1, then all items have been ordered.
Start with a query to determine Max(ORDERED) for each ORDER_ID.
SELECT y.ORDER_ID, Max(y.ORDERED) AS MaxOfORDERED
FROM YourTable AS y
GROUP BY y.ORDER_ID;
Once you have that working, use it as a subquery in another with an IIf expression to transform MaxOfORDERED to the STATUS text you want.
SELECT
sub.ORDER_ID,
IIf(sub.MaxOfORDERED = 0, 'not ordered', 'ordered') AS STATUS
FROM
(
SELECT y.ORDER_ID, Max(y.ORDERED) AS MaxOfORDERED
FROM YourTable21030926a AS y
GROUP BY y.ORDER_ID
) AS sub
ORDER BY sub.ORDER_ID;
I have a string comma delimited. I need to select with t-sql just first part of it.
Example
table
id items values
1 item, item2, item3, item4 5
I need to get result like
ID Items Values
1 item 5
Try this one
select id,
substring(items,0,charindex(',',items)),
values
from mytable