update table from other table whithout join - sql

Here is the deal. I have a table T with many columns but two of interest: gen_ID, ordernumber.
Records in this table are always by groups of 5 with the gen_ID being the same and the ordernumber being blank.
So in essence, it looks like this:
Gen_ID ordernumber
233
233
233
233
233
234
234
234
234
234
Now I have a query Q that, when executed, randomizes the numbers 1, 2, 3, 4, and 5.
I want to update ordernumber with the random numbers of Q so it looks like this:
Gen_ID ordernumber
233 3
233 4
233 1
233 2
233 5
234 4
234 5
234 3
234 2
234 1
Etc...
Any idea on how to do this using MS Access 2010 SQL?
Udate query would be fine but I cannot join the two since I don't have a common ID.
Any suggestions? Note that I can run this magic query once a set of 5 records are created in the table (I don't need to have that done once I have more than one set).

I don't think this can be achieved by SQL alone and will need some VB running alongside. My approach would be to get your 1 - 5 numbers in a random order stored in an "Array", you can then open up a recordset to "T" and step through one by one assigning a number from your array. You could also loop this process to begin again whenever it detects a new Gen_ID in "T" and thus populate the whole table in one pass.

Related

How to Count unique values across two colomns with certain types in postgresql

I've got data feeding into a database like below.
Now I can use a single query many times to achieve this but i'm looking for a way to query once and return all results
Time, ID ( The totally unique field), ReqType (can be 1 value out of 30), ReqName (can be 1 out of 20).
time
ID
ReQtype
ReqName
1345
12
1
test
1346
13
2
test
1352
14
1
hello world
1352
15
3
fith
1354
16
1
hello world
1357
17
4
apple
Without constructing a query for every variable of ReqType and ReqName i would like to return in X time(last 30min for example above) ((this will be two queries actually i think)
count
ReqName
2
test
2
hello world
1
fith
1
apple
Count
ReqType
3
1
1
2
1
3
1
4

How to foreach sql result

i have the following problem. I have a table like that:
RollID
ItemCode
Quantity
DocNum
CountRollIDs
1234
abcde
5
111
5
at the moment we get 1 line like that above, but we want to have the line repeated CountRollIDs times. So in this case 5 times and want to add a new RollID every time, like 1235 in the second row 1236 in the third row and so on. Is there any way to do that with normal sql?

Using sequences for every row in a sql-server table

We have a table Property in our database containing a counter in every row stored in the NextVoucherNumber integer column. There are about 2000 rows there.
ID ... {other columns} ... NextVoucherNumber
-----------------------------------------------
1 112
2 34
3 29
4 9456
.... ....
2000 233
We have an issue with a concurrent access to the table.
To improve the performance we would like to extract those columns to a separate table PropertyVoucherNumbers with a 1:1 relation between the rows.
ID NextVoucherNumber
------------------------
1 112
2 34
3 29
4 9456
.... ....
2000 233
Alternatively, we could maintain sequences for every row.
Seq_VoucherNumber_1, Seq_VoucherNumber_2, ... Seq_VoucherNumber_2000.
Looks like the same triggers just a little dynamic SQL there.
Could you please describe what the problems we will face using the second solution?
Can you suggest any better solution?

Microsoft Access 2007 Report with Conditional Columns

I am looking to make a very simple report to condense and show data side by side. All of the examples of reports I find are only row by row.
The query I will use will only have three schema "Company, Model, Total"
The format I am trying to get to is
Company Model Total Company Model Total
A 123 2 B 123 4
A 222 3 B 333 3
A 444 7 B 444 7
The idea is to present the information in a way that multiple companies side by side can compare inventory of the same model and find discrepencies. Ideally the report would eventually group all Model's that span every company at the top, but thats a next generation problem.
I have attempted conditional formating on multiple "Company" boxes, but the conditionals do not seem to be applying properly or for some reason every "Company" box is adopting the same conditionals.
I think you want a crosstab query grouping by model (the rowHeader), company as the column header, and first(total) as the value.
The results should look like
model A total B total
123 2 4
222 3
333 3
444 7 7
then you can create another query based on the crosstab results to calculate the difference between company totals, if you want.
You have to do this in two steps:
Build a query that gives you:
Company Model Total
A 123 2
A 222 3
A 444 7
B 123 4
B 333 3
B 444 7
Let's call q this query.
Build a second query
SELECT q1.Company, q1.Model, q1.Total, q1.Company, q2.Model, q2.Total
FROM q AS q1 INNER JOIN q AS q2 ON q1.Model = q2.Model
WHERE q1.company < q2.company;
This will give you:
A 123 2 B 123 4
A 444 7 B 444 7
(There are no matching data for models 222 and 333)

count the instances of value in sub-query, update table

I am trying to count the number of times a value (mytype) appears within a distinct id value, and update my table with this count (idsubtotal) for each row. The table I have:
id | mytype | idsubtotal
-----+--------+-----------
44 red
101 red
101 red
101 blue
101 yellow
494 red
494 blue
494 blue
494 yellow
494 yellow
I need to calculate/update the idsubtotal column, so it is like:
id | mytype | idsubtotal
-----+--------+-----------
44 red 1
101 red 2
101 red 2
101 blue 1
101 yellow 1
494 red 1
494 blue 2
494 blue 2
494 yellow 2
494 yellow 2
When I try this below, it is counting how many times the mytype value appears in the entire table, but I need to know how many times it appears within that sub-group of id values (e.g. How many times does "red" appear within id 101 rows, answer = 2).
SELECT id, mytype,
COUNT(*) OVER (PARTITION BY mytype) idsubtotal
FROM table_name
I know storing this subtotal in the table itself (versus calculating it live when needed) constitutes a bad data model for the table, but I need to do it this way in my case.
Also, my question is similar to this question but slightly different, and nothing I've tried to tweak using my very primitive understanding of SQL from the previous responses or other posts have worked. TIA for any ideas.
UPDATE table_name a
SET idsubtotal=( SELECT COUNT(1)
FROM table_name b
WHERE a.id=b.id
AND a.mytype=b.mytype
)
When I try this below, it is counting how many times the mytype value appears in the entire table, but I need to know how many times it appears within that sub-group of id values (e.g. How many times does "red" appear within id 101 rows, answer = 2).
SELECT id, mytype, COUNT(*)
FROM table_name
GROUP BY id, mytype