I need to reed aggregated data from SQL into .Net, to do this I use a DataAdapter to Fill a DataTable. The query looks something like this:
SELECT ACTNUMBR AS [SegmentNumber], MAX(DSCRIPTN) AS [Description], COUNT(*) AS [UsageCount]
The SegementNumber and the UsageCount values load into the DataTable as expected, but I cannot get the Description column to load values, it remains blank (running the query in SQL does return values). If I hardcode a value then it loads the values into the DataTable but as soon as I use a MAX() expression it does not load.
I have tried specifying the column detail for the DataTable but it had no effect.
Is this known behavior? How do I fix / work around it?
I believe the problem is in your SQL. You can't include an aggregate function (MAX) without doing a group BY.
SELECT ACTNUMBR AS [SegmentNumber],
MAX(DSCRIPTN) AS [Description],
COUNT(*) AS [UsageCount]
FROM SOMETABLE
GROUP BY ACTNUMBR
Related
I have a Postgres query like this
SELECT * FROM my_table WHERE status IN (2,1);
This is part of a big query, but I am facing an issue with the WHERE IN part here. I am using this query inside a function and the input parameters are in JSON format. Now the status values I am getting in in the form of a JSON array and it will be like status=[2,1]. I need to use this array in the WHERE clause in the query and not sure how to do that. Currently, I am using like
SELECT * FROM my_table WHERE status IN (array([2,1]));
But this is giving me an error. The status column is of smallint data type. I know this is simple, but I am very much new to Postgres and could not figure out any method to use the JSON array in WHERE IN clause. Any help will be appreciated.
I am working on a new sql table. The table has a column [varbinary(8000)], where we are storing hash of a certain text. Now, I am trying to retrieve the same record back by using a where clause against the hashkey, but that yields zero records.
I have added a similar query here: http://sqlfiddle.com/#!18/be996/11
Try without the single quotes, like this
SELECT id, description
FROM ForgeRock
where id = 0x94EE059335E587E501CC4BF90613E0814F00A7B08BC7C648FD865A2AF6A22CC2
and you will get the expected result.
I am trying to move everything from one table to another table. I know how to do that if all of the columns had the same data type, however one column is different. On the new table its varcahar(24) and on the old table its a bigint.
This is what i have so far, Im using the first select statement to set the id, and the rest to add it to the new table. However this always returns the last id, and i need it to be synced up with the second select statement
Any suggestions on how to do this would be awesome. i tried to google it for about an hour but couldnt come up with anything. I am using SQL Server 2012
use DatabaseA
GO
DECLARE #id bigint;
SELECT #id= columnName FROM differentDB.tableB
INSERT INTO tableA
(buyer_id, restOfTheColumns)
Select CAST(#id AS varchar(24)), restOfTheColumns
FROM differentDB.tableB
GO
Variables don't work like that. T-SQL variables hold data values, not object names. Furthermore, that's a scalar variable. It only holds one value.
That said, you don't need the variable at all. You can just do this:
INSERT INTO tableA (buyer_id, restOfTheColumns)
SELECT CAST(columnName AS varchar(24)), restOfTheColumns
FROM differentDB.tableB
I have these queries:
SELECT *
FROM dbo.GRAUD_ProjectsByCostCategory
select right(CostCategoryId,14) as CostBreak
from dbo.GRAUD_ProjectsByCostCategory
They work well in that they give me the correct data, but I would like to know how to combine the new column CostBreak into the table of results rather than as a separate query result.
An example of the results I get are as below:
Where I want them in the same table
The data is coming from the same table so you should be able to just add that value to your initial query. You do not even have to perform a join to get it:
SELECT name,
description,
project,
CostCategoryId,
right(CostCategoryId,14) as CostBreak
FROM dbo.GRAUD_ProjectsByCostCategory
Taking Sql this quarter and not having any luck with the following question:
The height of players in feet (inches/12). Include their name. Hint: Calculation or derived field. Be sure to give the calculated field a column header.
We're learning the basic Select statment and didn't find any reference on how to make custom data at w3schools. I'm using Microsoft SQL server Management Express Here's my statment so far:
select nameLast, nameFirst, height
from Master
where height (I assume its something like 'Player_Height' = height/12)
order by nameLast, nameFirst, height
Thanks for the help
If you need that result a lot, you might want to consider putting that into its own, computed column and persist it into your table. That way, it'll be up to date at all times, and you won't have to compute and re-compute it over and over and over again:
ALTER TABLE dbo.YourTable
ADD HeightInFeet AS ROUND(Height / 12.0, 3) PERSISTED
With this, you can also round your result to e.g. 3 digits after the decimal point (or pick whatever makes sense to you). Each row in your table will now have a new column called HeightInFeet, always computed and always up to date, and you can query on it, you can return it in a SELECT - whatever you like to do! (you just can set a new value to it, since it's being computed by SQL Server itself, based on your Height column)
Try something like
select nameLast, nameFirst, (height/12) as heightInInches
from Master
order by nameLast, nameFirst, height