I want to query the following table in access
Productid LowEnd MidRange HighEnd
1 10% 15% 25%
2 13% 18% 28%
3 14% 19% 30%
based on both the Productid and the CLass (Lowend,midrange,Highend) is there anyway to do it without manually creating a newtable?, I can do it easily by writing an excel macro to make a new table. I have used the crosstab to make tables like these.. can we undo them?
EDIT:
lets say the table name above is 'matrix'
The query I want to be able to do is SELECT * FROM matrix WHERE ((matrix.Productid = 1) and (column = 'LowEnd')) . I know column = lowend is not valid in sql so... I guess I'll have to use the Union of multiple queries which have the where caluse to identify the correct coulmn.
I guess I'll have to use the Union of
multiple queries which have the where
cause to identify the correct column.
I think you provided your own answer. The restriction is all the rows must be of the same data type or at least data types implicitly convertibles each others.
SELECT Productid, LowEnd AS Value
UNION
SELECT Productid, MidRange AS Value
UNION
SELECT Productid, HighEnd AS Value
Related
I have an ssrs report that has 2 fields from 2 different datasets. In the pic below I have a count from a query for Total Bound Policies and another query for Total Bound Policies With At-Fault Endorsements and I'm trying to calculate a % value in another column and after googling for solutions I cannot find a solution using lookup or just a division calculation. Any help/direction would be appreciated. Thanks.
Here is my design layout:
Here is my last expression that is getting an error:
If I'm understanding the issue correctly, Total Bound Policies and Total Bound Policies With At-Fault Endorsements each contain a single result which is a count of each. I can think of a couple of solutions for this problem, the easiest one of which being adjusting the query to bring all of the data into a single query to return your counts AND a value for the percentage. It's almost always better to do as much of the calculations in SQL as possible.
DECLARE #a INT = (SELECT COUNT(*) FROM Table1)
DECLARE #b INT = (SELECT COUNT(*) FROM Table2)
SELECT #a AS Count1, #b AS Count2, CAST(#a AS DECIMAL) / CAST(#b AS DECIMAL) AS aPercent
As you can see, in SQL, I'm casting each count as a decimal, as division with INT values will return a whole number, meaning you'll have either 0% or 100%. Using some random tables from my database brings back these results when I run this query:
If this solution isn't a viable option or if I misunderstood the question, another option that could help is to declare each field with it's corresponding dataset. For example, (Fields!TotalBoundPolicies.Value, "TotalPoliciesBound") would get the field from dataset TotalPoliciesBound.
I'm using SSRS and have made a "pivot table". The pivot table has two calculated columns, and I want to use those values to get a third value, the difference between both values.
This is an example of what my table looks like:
*Edit: the percentages are averages of about 40+ values with those row/column attributes *
Category A B Difference
1 47% 57% B-A
2 45% 49% B-A
3 13% 73% B-A
4 27% 23% B-A
Total 23% 34% B-A
Each percent is an average of the category grouped by A and B. I need a function that will gather the average for A and subtract it from the Average of B for that row.
I've tried the following so far and have resulted in error:
=IIf(Fields!Log1.Value = "End", Avg(Fields!Log1.Value), Nothing) - IIf(Fields!Log1.Value = "Begin", Avg(Fields!Log1.Value), Nothing)
Any help is appreciated. Thanks
I believe the issue here has to do with how you are referencing the Log1 column within the Category group. Think about how the data looks behind the scenes. Within each category you have multiple rows that you want to summarize. Some rows might have a different value for Log1. The intent is to get the average of the rows that have a certain Log1 value.
What's actually happening is that it's arbitrarily picking the first Log1 value within each group and using that in the expression. In other words, you are getting the average of all the rows or none of the rows for that group based on what Log1 happened to be in the first row of the category group.
As a side note, in your expression, you refer to Log1 as a text field and in the Avg function, I'm assuming those should actually be two different columns and you're trying to average numbers in the A and B columns.
What we actually want it to do is check the value of Log1 for every row and then average those values. So, here's what I suggest:
Create two calculated fields on your dataset. Use expressions like this:
=IIf(Fields!Log1.Value = "Begin", Fields!ColumnA.Value, Nothing)
=IIf(Fields!Log1.Value = "End", Fields!ColumnB.Value, Nothing)
In column A of your table refer to this new calculated field. Repeat for column B.
=Avg(Fields!NewColA.Value)
To get the difference:
=Avg(Fields!NewColB.Value) - Avg(Fields!NewColA.Value)
In other words, we are forcing it to filter values row-by-row and then aggregating those filtered values.
could you please explain why mysql count function without providing any table name gives 1 as value?
SELECT COUNT(*);
Result: 1
Because in mysql select constant_value command is valid (such as select 2 will return 2) and will return 1 row. Count() function without group by will collapse the resultset and count the number of items in the resultset. In this case 1 row would be returned and count(*) counts that.
Normally all selects are of the form SELECT [columns, scalar computations on columns, grouped computations on columns, or scalar computations] FROM [table or joins of tables, etc]
Because this allows plain scalar computations we can do something like SELECT 1 + 1 FROM SomeTable and it will return a recordset with the value 2 for every row in the table SomeTable.
Now, if we didn't care about any table, but just wanted to do our scalar computed we might want to do something like SELECT 1 + 1. This isn't allowed by the standard, but it is useful and most databases allow it (Oracle doesn't unless it's changed recently, at least it used to not).
Hence such bare SELECTs are treated as if they had a from clause which specified a table with one row and no column (impossible of course, but it does the trick). Hence SELECT 1 + 1 becomes SELECT 1 + 1 FROM ImaginaryTableWithOneRow which returns a single row with a single column with the value 2.
Mostly we don't think about this, we just get used to the fact that bare SELECTs give results and don't even think about the fact that there must be some one-row thing selected to return one row.
In doing SELECT COUNT() you did the equivalent of SELECT COUNT() FROM ImaginaryTableWithOneRow which of course returns 1.
Reference
I am looking to write a query that selects a percentage of records using something like
select top 30 percent * from people
with a random order.
But I want to select more people if certain criteria is met. So I may want to select 35% where the nationality is Brazilian and a few other conditions.
What's the easiest way to do this? Thought I could use a union to a second query, but there may be several of these exceptions so is there a better way?
You can use variables to do it. Try something like this:
DECLARE #RecordsToReturn AS Decimal(16,2);
SET #RecordsToReturn AS (SELECT COUNT(*) FROM YourTable WHERE "Your Column" = 'Some Criteria') * .3
SELECT TOP (#RecordsToReturn) *
FROM YourTable
I dont have SQL server in front of me, but that should get you the first 30% of records based on the where criteria
I'm doing some statistics based on a database of states. I would like to output the rank of a state and it's percentage as compared to the other states (i.e. state X's value is higher then 55% of the other states' value).
I'm trying something like this:
SELECT
count(*) AS TotalStates,
(SELECT COUNT(*) FROM states) AS NumberStates,
(TotalStates/NumStates) AS percentage
FROM states
WHERE CRITERIA > 7.5
I'm getting an SQL error, TotalStates (my derived value) is not found. How can I get all three of these values returned with one query?
You can put the main calculations in a subselect, then reference the aliased columns in the outer query, both to pull the already calculated values and to obtain another one from them:
SELECT
TotalStates,
NumberStates,
TotalStates / NumberStates AS percentage
FROM (
SELECT
COUNT(*) AS TotalStates,
(SELECT COUNT(*) FROM states) AS NumberStates
FROM states
WHERE CRITERIA > 7.5
) s
The error you are getting comes from the fact that you are trying to use the derived value in the same select clause that you are creating it in. You will need to maybe do something along these lines:
SELECT count(*) as TotalStates,
(SELECT count(*) from states) as NumberStates,
(count(*)/(SELECT count(*) from states)) as percentage
FROM states
WHERE criteria = x
However, this is not very efficient or desirable for readability or maintainability. Is there a design reason that you cannot perform this in two queries, or better yet, get the two data items in separate queries and calculate the percentage in the consuming code?