How to check if a column in a query can produce NULL?

I am scheming how to implement a type generator for arbitrary SQL queries.

I found that for any query, the simplest way to get the types of every column is to create a temporary table and then inspect that table. However, this tells nothing about which columns can be NULL.

I am interested to find a solution to identifying which columns can be NULL for any arbitrary query with the goal of generating types for that query.

What’s the way to go about it?

Group by Multiple columns and then count different value of same column

I want to achieve the result which tells me the number of males and females of each disability types in each district. each district can have multiple disabilities.

So far i have reached the following query :

  SELECT    DistrictId,    fb.DisabilityTypeId,    SUM(     CASE WHEN GenderId = 1 THEN 1 ELSE 0 END   ) AS Male,    SUM(     CASE WHEN GenderId = 2 THEN 1 ELSE 0 END   ) AS Female  FROM    Districts d    LEFT OUTER JOIN FormAddresses a ON d.Id = a.DistrictId    INNER JOIN PeopleForms pf ON a.PeopleFormId = pf.Id    INNER JOIN FormBeneficiaries fb ON pf.Id = fb.PeopleFormId    INNER JOIN FormPersonalInfos fp ON pf.Id = fp.PeopleFormId  where    a.IsDeleted = 0    AND pf.FormTypeId = 2    AND d.CityId = 3  GROUP BY    DistrictId,    fp.GenderId,   fb.DisabilityTypeId 

which gives the following result :

DistrictId  |   DisabilityTypeId    |   Male  | Female     1       |       2               |   1     |     0     3       |       2               |   0     |     3     5       |       16              |   1     |     0     5       |       20              |   2     |     0     5       |       20              |   0     |     1     

But i want to achieve the following result :

DistrictId  |   DisabilityTypeId    |   Male  | Female     1       |       2               |   1     |     3     5       |       16              |   1     |     0     5       |       20              |   2     |     1 

i somehow managed to get the expected result, but that with some complex sub-queries in the select clause of each gender which i didnt like and was not sure about the performance.

how can i write an efficient query for the desired result that i want?

Thanks.

How to adjust table column width with CSS

HELLO.
Site that im working on , contains many products and each product has its own Specification table that are being imported into database. I need to change the column width of the table using CSS . i can not go to each product and change tables properties .. i used
td:nth-child(1) in the css file. using this i can change many properties in the table but width of the column is not changing ? would appreciate any info on this . bellow is the link to a page….

How to adjust table column width with CSS

using a column that references an object_id in your own table

Is it safe to use OBJECT_ID() as a reference in your own tables, with the assumption that the value will always refer to the same database object no matter what?

The msdn states

All metadata functions are nondeterministic. This means these functions do not always return the same results every time they are called, even with the same set of input values.

so that would indicate it’s not safe, but I couldn’t find any (other) hard facts that it shouldn’t be done, and, to be fair, no examples of people actually doing it, which in itself might be evidence not to do it.

But the value OBJECT_ID() is (or seems to be) used in the system tables and views as keys from one object to another, so there must be some form of integrity and reliability.

For the record: I don’t necessarily need it, but it just popped into my mind as an option while designing my system, and got curious.

Column in query should be selected according to value from another column

Assuming I have table1 with following data:

table1

id| coffee_sender | coffee_receiver | received_bag_size | sent_bag_size| price | 21  tux              -1                  m                 -1            45  22  alsa            -1                  -1                 xl            36 23  -1               tux                 l                 -1            51 

And I use the following query:

select t1.* from ( select t.price, t.id from table1 t  where coffee_sender in (tux, alsa)  or coffee_receiver in (tux, alsa) and received_bag_size in (m, xl) or sent_bag_size in (m, xl)) t1 

What i want is to only fetch the rows that have coffee_sender/coffee_receiver AND sent_bag_size/received_bag_size filled with a value (rows that match the following conditions):

coffee_sender = tux, sent_bag_size= xl 

OR

coffee_receiver = tux, received_bag_size= m 

OR

coffee_sender = tux, sent_bag_size = xl AND coffee_receiver = tux, received_bag_size= m 

So id = 21 would be excluded from the example table1.

How can i update the above query for this ? Thank you in advance.

How to convert a Dataset into an indexed dataset / association-of-associations given a column header?

Given a dataset as such

Input

If "letter" is the header that is chosen, how do I convert it into an indexed dataset / association-of-associations?

i.e. How do I define f such that f[dataset_,columnHeader_] produces the following?

enter image description here

Please note GroupBy is close but fails as you are unable to use Part to work with the result to extract column data. eg:

data = {<|"letter" -> "a", "foo" -> 1, "bar" -> 2|>, <|"letter" -> "b", "foo" -> 3, "bar" -> 4|>, <|"letter" -> "c", "foo" -> 5, "bar" -> 6|>}; dataDS = Dataset[data]; dataDSg= GroupBy[dataDS, Key["letter"]]; dataDSg[All, "foo"] (* <- produces an error *) 

Where as data in the format of an association-of-association works fine

data2 = <|"a" -> <|"foo" -> 1, "bar" -> 2|>, "b" -> <|"foo" -> 3, "bar" -> 4|>, "c" -> <|"foo" -> 5, "bar" -> 6|>|>; data2DS = data2 // Dataset; data2DS [All, "foo"] (* <- returns a dataset with 1,3,5 *) 

Evenly select Records on categorical column with Repeating Sequence and pagination in Postgres

Database: Postgres
I have a product(id, title, source, ...) table which contains almost 500K records. An example of data is:

| Id | title    | source   | |:---|---------:|:--------:| | 1  | product1 | source1  | | 2  | product2 | source1  | | 3  | product3 | source1  | | 4  | product4 | source1  | | .  | ........ | source1  | | .  | ........ | source2  | | x  | productx | source2  | |x+n |productX+n| sourceN  | 

There are are 5 distinct source values. And all records have source values random.

I need to get paginated results in such a way that: If I need to select 20 products then the results set should contain results equally distributed based on source and should be in a repeating sequence. 2 products from each source till the last source and again next 2 products from each source. For example:

| #  | title    | source   | |:---|---------:|:--------:| | 1  | product1 | source1  | | 2  | product2 | source1  | | 3  | product3 | source2  | | 4  | product4 | source2  | | 5  | product5 | source3  | | 6  | product6 | source3  | | 7  | product7 | source4  | | 8  | product8 | source4  | | 9  | product9 | source5  | | 10 |product10 | source5  | | 11 | ........ | source1  | | 12 | ........ | source1  | | 13 | ........ | source2  | | 14 | ........ | source2  | | .. | ........ | .......  | | 20 | ........ | source5  | 

What is the optimized PgSql query to achieve the above scenario considering the LIMIT, OFFSET, sources can increase or decrease?

How to get a value-only flat array from $wpdb->get_results when selecting a single column, without foreach()?

My query is:

$  var = $  wpdb->get_results("SELECT field FROM {$  wpdb->prefix}table", ARRAY_A); var_dump($  var); 

it returns someting like array(2) { [0]=> array(1) { ["field"]=> string(5) "test1" } [1]=> array(1) { ["field"]=> string(t) "test2" }. I.e. each item is a row with a single name-value pair.

What I want is array(2) { [0]=> string(5) "test1" [1]=> string(5) "test2" }

Currently I achieve it like this:

$  var = $  wpdb->get_results("SELECT field FROM {$  wpdb->prefix}table"); foreach($  var as $  v_key => $  v_val) $  var[$  v_key] = $  v_val['field']; var_dump($  var); 

Is there a shorter way to do this?